Is there any workaround of using the IMathCADWorksheet:etValue instead of accessing directly the XML file?
The SetValue method does not access the Mathcad XML file. SetValue talks to an executing Mathcad process with a worksheet loaded, whether it is visible or not is developers choice. If you want the SetValue changes saved to the XML file you will need to execute the SaveAs method as well before existing.
Thank you John.
But the SetValue method of the Worksheet doesnt change anything in the active worksheet when I tried to execute the method from my C++ application. Is there any other method to the change the value in the mathcad worksheet? If there is any sample application it will be helpful for me.
Here is what I suspect is biting you Karthik, everyone makes this mistake at first.
If you have a worksheet which has a variable X and X is set to 3.14159 x:=3.14159,
you can NOT use SetValue to reset MC->SetValue("X",99999).
The API runs first pass, sets X to 99999, then the worksheet executes the x:=3.14159.
It looks like SetValue i sdoing nothing, but it is , just first.
So you need a worksheet where X is left unset ... X:=
Try that Karthik
Another method is to set a non-seen value and use it in your saved worksheet.
And in your worksheet have a region x:=myhiddenregion This will show as an error (in red) when you save the worksheet the first time. Thats OK.
As John says, SetValue binds the variable before any assignments in the worksheet. That means that any assignment will overwrite what you do with SetValue. This is, in my opinion, a very bad piece of design, but it's the way it works. You can either disable the assignment of the variable you are trying to set, or you could change the XML of the assignment by setting the MathInterface.XML property (the easiest way to find the region is to set a tag, and then search for the tag).
If you are willing to try Richard's method here are some tips. I believe this is the best method, but definitely more work programatically.
As he mentioned you pre-process your worksheet by RMB on the math region of interest and then tag it.
a tag is just a text string you will use to filter regions programatically. Can be as simple as "input1", "input2" etc.
In your code , after you get the interface to the worksheet get the collection of regions of the worksheet interface.
Now loop for all recgions (either stop on NULL region or use regions.count) and search for your region of interest' tag. Some VB.NET code to review.
SW2MC is the tag I am looking for on regions. Value comes from other code snippets , pretend it is the value you are trying to set .. 3.14159.
WS = MC.Worksheets.Open(openFileDialog1.FileName)
MyRegions = WS.Regions
For Each myregion In
If(String.Compare(myregion.Tag, "SW2MC") = 0) Then
If (newstr.Compare(newstr, leftstr) = 0) And ((substring_test) Or (pos = 0))Then
tmpstr = tmpstr & "</ml:id><ml:real>"
tmpstr = tmpstr & value
tmpstr = tmpstr & "</ml:real></ml:define>"
myregion.MathInterface.XML = tmpstr
End If ' found the right region
Dont get too hung up on the VB.NET string manipulation, bottom line I am formatting a proper XML string according to Mathcad schema. To learn the format open a plane worksheet, type in the region you would like to see as a result. Save the worksheet and open it in Notepad. Now you see what your region's syntax needs to look like.
In addition to Johns example, here's a worksheet that contains some scripted components that create variable assignments within a worksheet (written by Tom Gutman, not me, but I can't find the original thread, so I'm reposting the worksheet). With this example and John's code I think you should be able to make this work with only a smallish amount of effort.
Note that one advantage of this method over Setvalue is that it can also handle units.