Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

Options

Simple Math with Variables

romeograhamromeograham Member, csevp Posts: 657 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. 
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

Comments

  • Options
    _anton_anton Member, Onshape Employees Posts: 273
    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);
  • Options
    romeograhamromeograham Member, csevp Posts: 657 PRO
    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 !
  • Options
    _anton_anton Member, Onshape Employees Posts: 273
    So the getVariable call just returns the value (or throws, if it's not defined - see here). You could also write
    setVariable(context, "Hrange", getVariable(context, "Hmax") - getVariable(context, "Hmin"));
  • Options
    romeograhamromeograham Member, csevp Posts: 657 PRO
    I think I like that better - I can keep the entire definition of the setVariable "Hrange" on one line. 
  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,370
    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, EMEAI
  • Options
    romeograhamromeograham Member, csevp Posts: 657 PRO
    @NeilCooke
    I thought that's how I started out, but wasn't successful. 
    Can you paste a little example here?
  • Options
    ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,175
    edited September 2020
    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 Inc
  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,370
    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);
    

    That's what I was going to put (honestly)  :p
    Senior Director, Technical Services, EMEAI
  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,370
    And save yourself some typing by putting at the top
    const mm = millimeter;

    Senior Director, Technical Services, EMEAI
  • Options
    romeograhamromeograham Member, csevp Posts: 657 PRO
    Thanks @NeilCooke and @ilya_baran - that mapping seems to be a tidy way to do it.


  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,064 PRO
    Seems like I've been doing this the wordy way. Thanks, @ilya_baran for the for loop solution instead.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
Sign In or Register to comment.