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 Posts: 431 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: 10
    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 Posts: 431 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: 10
    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 Posts: 431 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: 3,254
    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. 
  • romeograhamromeograham Member Posts: 431 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: 992
    edited September 11
    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 \ Director, Architecture and FeatureScript \ Onshape Inc
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 3,254
    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
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 3,254
    And save yourself some typing by putting at the top
    const mm = millimeter;

  • romeograhamromeograham Member Posts: 431 PRO
    Thanks @NeilCooke and @ilya_baran - that mapping seems to be a tidy way to do it.


  • Evan_ReeseEvan_Reese Member Posts: 482 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 Fractal
    Website: fractalmade.com
    Instagram: @evan.reese.designs
Sign In or Register to comment.