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.

Simple Math with Variables

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

  • _anton_anton Member, Onshape Employees Posts: 410
    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);
  • romeograhamromeograham Member, csevp Posts: 676 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 !
  • _anton_anton Member, Onshape Employees Posts: 410
    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"));
  • romeograhamromeograham Member, csevp Posts: 676 PRO
    I think I like that better - I can keep the entire definition of the setVariable "Hrange" on one line. 
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    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
  • romeograhamromeograham Member, csevp Posts: 676 PRO
    @NeilCooke
    I thought that's how I started out, but wasn't successful. 
    Can you paste a little example here?
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,212
    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
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    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
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    And save yourself some typing by putting at the top
    const mm = millimeter;

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


  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    Seems like I've been doing this the wordy way. Thanks, @ilya_baran for the for loop solution instead.
    Evan Reese
Sign In or Register to comment.