Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape, CAD, maker project and design.

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, Administrator, Moderator, Onshape Employees, Developers Posts: 1,196
edited August 23 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: 
+, -, *, /, ^, ceil, floor, round, exp, sqrt, abs, max, min, log, log10
A few examples:
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

  • emagdalenaC2CemagdalenaC2C Member, Developers Posts: 157 ✭✭✭
    Great!!! I did'nt know about the conditional

    Awesome
    Un saludo,                                                                                                                   Perfil en LinkedIn Averigua a quién conocemos en común
    Eduardo Magdalena
    C2i Change 2 improve
    Optimización de procesos
    www.c2i.es

    ☑ ¿Por qué no organizamos una reunión online?
    Selecciona el día y la hora que más te interese
     
    C2C Close 2 Circle™
    Consulting Partner de Onshape
    www.close2circle.com

  • john_mcclaryjohn_mcclary Member, Developers Posts: 405 PRO
    sweet! also didn't know about the inline if statement!
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    Very good indeed thanks for making it clear.

    Cheers, Owen S.
    Production Engineer
    HWM-Water Ltd
  • brian_bradybrian_brady Member, Developers Posts: 201 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.

     #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 Posts: 1,157 PRO
    @jackson_king this could be really handy
    Engineer ı Product Designer ı Onshape Consulting Partner
    Twitter: @onshapetricks  & @babart1977   
  • brian_bradybrian_brady Member, Developers Posts: 201 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
  • Nan_CpNan_Cp Member, Developers Posts: 77 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 Onshape Employees, Developers Posts: 319
    @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: 30 ✭✭
    Red makes me nervous, haha.  :P 

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

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

    Owen S.
    Production Engineer
    HWM-Water Ltd
  • john_mcclaryjohn_mcclary Member, Developers Posts: 405 PRO
    Isn't that the same thing?
  • owen_sparksowen_sparks Member, Developers Posts: 725 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
    Production Engineer
    HWM-Water Ltd
  • john_mcclaryjohn_mcclary Member, Developers Posts: 405 PRO
    Ah i see what you're sayin
  • john_mcclaryjohn_mcclary Member, Developers Posts: 405 PRO
    You mean like when solidworks throws a warning where there is nothing wrong?...



  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    Yes indeed, or when WIN10 states that it can't restart because this (empty) list of applications is preventing it!
    Production Engineer
    HWM-Water Ltd
Sign In or Register to comment.