Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Simple Math with Variables
romeograham
Member, csevp Posts: 777 PRO
I am trying to use a Feature Studio to set up project variables - to be used in a single Document.
I have, say, two specifications (Hmax & Hmin). I can set up Variables (setVariable) no problem.
Any help would be great!
Thanks
Romeo
I have, say, two specifications (Hmax & Hmin). I can set up Variables (setVariable) no problem.
setVariable(context, "Hmax", 30 * millimeter); setVariable(context, "Hmin", 28 * millimeter); setVariable(context, "Hrange" = (Hmax - Hmin)); /*how do I do math in this variable?*/Now, I'd like to make a new "Hrange" Variable, that does a little simple math with those two variables:
setVariable(context, "Hrange" = (Hmax - Hmin)); /*how do I do math in this variable?*/However, this expression doesn't work. I tried a few different ways to do it, but even this simple thing is beyond my understanding. I'm not sure what search terms will help in the Documentation - haven't had luck with the searches that made sense to me.
Any help would be great!
Thanks
Romeo
0
Comments
-
The setVariable calls store a variable value in the context. You can use getVariable to retrieve the values. E.g.,
const Hmax = getVariable(context, "Hmax"); const Hmin = getVariable(context, "Hmin"); setVariable(context, "Hrange", Hmax - Hmin);
1 -
Ah - so I need to tell FS that my previously-declared Variable is a Constant (const) that can be used in downstream calculations...something like that?
Thanks for the quick help @_anton !
0 -
I think I like that better - I can keep the entire definition of the setVariable "Hrange" on one line.0
-
Or just define them all as variables at the top of the code, then you can do all the calculations you like before calling setVariable at the end. More code, but easier to read.Senior Director, Technical Services, EMEA0
-
@NeilCooke
I thought that's how I started out, but wasn't successful.
Can you paste a little example here?0 -
I would actually do them in a map, something like:
var v = {}; v.Hmax = 30 * millimeter; v.Hmin = 28 * millimeter; v.Hrange = v.Hmax - v.Hmin; ... for (var entry in v) setVariable(context, entry.key, entry.value);
Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc4 -
That's what I was going to put (honestly)ilya_baran said:I would actually do them in a map, something like:var v = {}; v.Hmax = 30 * millimeter; v.Hmin = 28 * millimeter; v.Hrange = v.Hmax - v.Hmin; ... for (var entry in v) setVariable(context, entry.key, entry.value);
Senior Director, Technical Services, EMEA1 -
And save yourself some typing by putting at the top
const mm = millimeter;
Senior Director, Technical Services, EMEA1 -
0
-
Seems like I've been doing this the wordy way. Thanks, @ilya_baran for the for loop solution instead.
0




