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.

A few expression tips.

lougallolougallo Member, Moderator, Onshape Employees, Developers, csevp Posts: 2,004
edited August 2017 in Using Onshape
Over the last couple of months we have received a number of questions on how to make expressions in the Onshape dimension and feature dialogs and I wanted to share a few of them with you all.  You can read about most of them in our help documentation here: Numeric fields

As illustrated in our help you can do most math functions: 
</code>+, -, *, /, ^, ceil, floor, round, exp, sqrt, abs, max, min, log, log10</pre>A few examples:<br><pre class="CodeBlock"><code>ceil(5.667) = 6
floor(5.667) = 5
but we also support a array/lookup table like this: (remember the array starts at 0) . (Array example)
[3,5,6,7][2] = 6
[3,5,6,7][3] = 7
and ternary operators which can yield conditional results.  For example let's say I want the #length of my rectangle to be 7in if the width is > 5in, otherwise make it 4in. (Ternary example)
#width>5?7:4
Note: In the example file I am also making the hole fail if the #width is less than 4in.

I thought this might be useful and I will try to get some examples added to our help as well.  ~Lou


Lou Gallo / PD/UX - Support - Community / Onshape, Inc.

Comments

  • emagdalenaC2iemagdalenaC2i Member, Developers, Channel partner Posts: 863 ✭✭✭✭✭
    Great!!! I did'nt know about the conditional

    Awesome
    Un saludo,

    Eduardo Magdalena                         C2i Change 2 improve                         ☑ ¿Por qué no organizamos una reunión online?  
                                                                         Partner de PTC - Onshape                                     Averigua a quién conocemos en común
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,938 PRO
    sweet! also didn't know about the inline if statement!
  • owen_sparksowen_sparks Member, Developers Posts: 2,660 PRO
    Very good indeed thanks for making it clear.

    Cheers, Owen S.
    Business Systems and Configuration Controller
    HWM-Water Ltd
  • brian_bradybrian_brady Member, Developers Posts: 505 EDU
    You can also nest conditionals by placing another complete conditional statement in either the true or false positions. I suggest using parentheses around the second statement (may even be necessary) for readability.

    &nbsp;#width<4?1:(#width>4?3:2)

    This example sets the value to 1 if width is less than 4, 2 if equal to 4, and 3 if greater than 4

  • brucebartlettbrucebartlett Member, OS Professional, Mentor, User Group Leader Posts: 2,141 PRO
    @jackson_king this could be really handy
    Engineer ı Product Designer ı Onshape Consulting Partner
    Twitter: @onshapetricks  & @babart1977   
  • brian_bradybrian_brady Member, Developers Posts: 505 EDU
    @jackson_king this could be really handy
    This is how I used it while trying to use Feature Script. I wanted to use variables to drive a simple woodruff key, then figure out how to do the same thing with Feature Script. I haven't taken much time since to learn more though.

    https://cad.onshape.com/documents/d84339a898f6048107f919e7/w/6bf19a60af5f77fc35b79e73/e/c14681bd9486c363d1cd88a6
  • papawopapawo Member, Developers Posts: 206 PRO
    You said: In the example file I am also making the hole fail if the #width is less than 4in.

    Fail means the browser will turn red and will have error msg that it didnt regenerate properly?
    Is it okay if we just leave it red?Just a question?
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    @Nan_Cp , Yes it's fine if you leave a feature in a failing state.  Because it's red, all of its effects have been rolled back, and it has no affect on the final geometry.
    Jake Rosenfeld - Modeling Team
  • Marc_MillerMarc_Miller Member Posts: 110 ✭✭✭
    Red makes me nervous, haha.  :P 

    Nested conditional statements are cool!  Thanks @lougallo and @brian_brady for the tips.

  • papawopapawo Member, Developers Posts: 206 PRO
    It does make me nervous!   :) 
    Thanks Jake.
  • owen_sparksowen_sparks Member, Developers Posts: 2,660 PRO
    Red makes me nervous, haha.  :P
    Maybe we could have purple for "not wrong, but resolves to nothing"...

    Owen S.
    Business Systems and Configuration Controller
    HWM-Water Ltd
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,938 PRO
    Isn't that the same thing?
  • owen_sparksowen_sparks Member, Developers Posts: 2,660 PRO
    Isn't that the same thing?
    Noo.  Say we want extra holes in our widget if the length is over a certain value, and we use a pattern feature for the extra holes.  If our widget is under the limit the extra holes value will be zero, as is our intent.  Currently the feature will be red.  In ideal-land we could have a soothing purple to indicate our feature is not producing an output, but is not in error. :)  #FirstWorldProblems
    Business Systems and Configuration Controller
    HWM-Water Ltd
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,938 PRO
    Ah i see what you're sayin
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,938 PRO
    You mean like when solidworks throws a warning where there is nothing wrong?...



  • owen_sparksowen_sparks Member, Developers Posts: 2,660 PRO
    Yes indeed, or when WIN10 states that it can't restart because this (empty) list of applications is preventing it!
    Business Systems and Configuration Controller
    HWM-Water Ltd
  • Will_SchleterWill_Schleter Member Posts: 9 EDU
    In one of the examples, a hole_diameter of -1 is used to try and make the hole fail and not be displayed, however, when I copied the file to play with it a hole_diameter variable of -1 used in a dimension on a sketch resulted in a circle of diameter 1 (looks like it used absolute value) The only way I could get it to fail was set the variable to 0.
  • Alex_PittAlex_Pitt Member Posts: 66 PRO
    edited June 2021
    #variableName*2 (multiplication) works for me but #variableName-2 (subtraction) doesn't work (I get the "enter valid expression" error)


    Unless I enter the units...



  • tim_hess427tim_hess427 Member Posts: 648 ✭✭✭✭
    @Alex_Pitt - Those examples are working correctly per the way they're designed. Onshape doesn't "assume" it knows what unit to use, but it always checks units to keep the dimensions accurate. 

    So, 10mm*2 = 20mm. Using dimensional analysis, onshape knows that the result should have a length unit. 

    However, 10mm - 2 does not work because onshape doesn't make assumptions and doesn't know what the units for the result should be. Is it 2 inches? 2 feet? 2mm? 
  • Alex_PittAlex_Pitt Member Posts: 66 PRO
    It's no problem to type in the units. At the time I had incorrectly assumed that when no units were specified, it would use the default document units. (mm in this case). 
  • tim_hess427tim_hess427 Member Posts: 648 ✭✭✭✭
    I think it does make that assumption if you're just typing a single value, but once you start getting into more complex expressions things need to be more explicit.
Sign In or Register to comment.