cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

VB.Net for setvalue and getvalue with ComException error!

musa1988
6-Contributor

VB.Net for setvalue and getvalue with ComException error!

Hello guys till now i found some examples for accesing Mathcad sheets and extracting outputs and it works. 

 

The Problem is now i read here a lot about sth like that it is not possible to recalculate some sort of functions. Well I rewrited a code i found here and there is a small problem (maybe big not sure). Tried to get customer services and I have a ticket but ....have not that much of time to wait for them to call me (already one week passed). 

 

Ok attached you can find my Mathcad sheet and everything works fine with it when i set the values. If i do it now with visual basic i get an error for getvalue:

 

ComException errorComException error

 

 

What Im doing is to set some values in my Mathcad Sheet and in the end i have a function with minimize. I defined that function as "optimal" look here: 

 

1.png2.png3.png4.png

 

Well if i set the values in Mathcad everything works well and i have a solution. BUT if i do it with visul basic COMexception occurs. What is wrong here ? I mean if i set the values than i see in Mathcad its calculating around 5 sec. and bäm: comexception ....

 

Alright maybe someone here who knows or had this problem with the help in mathcad i couldnt find a solution sitting here since couple days and tried nearly everything ^^

 

 

7 REPLIES 7
MJG
18-Opal
18-Opal
(To:musa1988)

I've never used the setvalue, but based on my understanding of this function I see three things wrong with your implementation:

  1. My understanding is that setvalue is like defining a variable at the very top of your worksheet.  Therefore, if you are going to setvalue for a variable "k1", then you should NOT have a blank definition within the worksheet.
  2. I don't know if setvalue works with units.  You may need to push setvalue("k1",123), then within your worksheet redefine k1 as k1*Pa to get 123Pa.
  3. Your variabe "p0" has the 0 subscript.  I think you need to use setvalue("p.0",...) to get that to work.

I would suggest trying a more simplified setup.  For example:

setvalue("a",1)

getvalue("b")

with your worksheet only having one equation: b:=a+1

Once you get that working, try including dimensions.

After that's working, try including subscripts.

Slowly add more complexity until you get your desired output.

RichardJ
19-Tanzanite
(To:musa1988)

Mark is correct. SetValue binds the variable prior to any variable definition in the worksheet, including global assignments. Any subsequent definition of the same variable overrides the binding from SetValue. SetValue does not support units.

 

You can do what you want, but to do so you must manipulate the XML of the math region. I've attached a worksheet that has some scripted components that show you how to do that in VBscript. VBscript is not VB.NET, but you should be able to port the code over to your application.

musa1988
6-Contributor
(To:RichardJ)

Hi many thanks for your answers so far but i dont reall understand it. 

 

I tried it with a more simple one and it works : 

a4.png

a2.png

 

a3.png

 

As you can see it works and gives me just the value. On my worksheet not i think it has to do sth with the minimize function. My values are all "multiplied" to the value -.-' im so lost 

MJG
18-Opal
18-Opal
(To:musa1988)

Your simplified example works, but it does not use the units like you probably think it does.

 

First: unless you've defined PA above your screenshot, Mathcad is treating it as an undefined variable.  I assume you intended the unit Pa.

 

In your Mathcad sheet, A:+_*PA does nothing.  Same for B:=_*PA.  You can delete those equations and get the same result.  Setvalue("A",2) defines the variable "A" above the worksheet.  When Mathcad gets to A:=_*PA, it ignores it because it's an incomplete equation.

 

Try changing your equation to C:= A+B*2.5+1*Pa

I expect that you'll get a unit incompatibility error because Mathcad sees A & B as unitless so it can't add 7+1*Pa.

 

If you want A to have units Pa, you need to setvalue("A",2), then in Mathcad define A:=A*Pa.  Then, if you want to return C in Pa, you must define C:=C/Pa at the end of the worksheet (actually, this is unnecessary for Pa since that's the default unit of pressure, but you'd need to divide by your units for any non-standard units).

musa1988
6-Contributor
(To:MJG)

Alright im confused now....why is my First Sheet not Working then?
MJG
18-Opal
18-Opal
(To:musa1988)

In Mathcad, change these two equations:

p.0:=_*Pa

k1:=_*Pa

to these:

p.0:=p.0*Pa

k1:=k1*Pa

 

Delete the equation n1:=_ because it is unitless (or change the equation to n1:=n1)

 

 

In VB, chnage SetValue("p0",myA) to SetValue("p.0",myA)   -- (with a period between p & 0).

 

You use SetValue for two other variables that I don't see in Mathcad, so you can determine what to do with them.

 

RichardJ
19-Tanzanite
(To:musa1988)

Please post your worksheet (you will have to zip it first).

Top Tags