Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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.
Can you use "conditionals" with variables?
brian_brady
Member, Developers Posts: 505 EDU
What I mean here is can you do things like; if x>10 then y=0.5*x else y=x. What about logical functions like AND and OR? If conditionals and logicals could be used, then I could easily "program" multiple variables to change off one or two primary parameters. For example, enter a woodruff key number (i.e. 204) into a variable and use conditional logic to set all of the dimensions. Diameter and thickness can be done without conditionals, but the offset cannot, it varies from size to size.
On the other hand, can FS be used to just import existing variables and set values to new variables that can be used in a part model without actually making any geometry?
On the other hand, can FS be used to just import existing variables and set values to new variables that can be used in a part model without actually making any geometry?
1
Best Answer
-
ilya_baran Onshape Employees, Developers, HDM Posts: 1,215Yes and yes. The syntax for your conditional expression is:
#x > 10 ? 0.5 * #x : #x<br>
Another possibility (without using a custom feature) is something like:{ 202 : 0.248 in, 204 : 0.491 in, 305 : 0.612 in}[#keyNumber]<br>
which if #keyNumberis 202 will evaluate to 0.248 in, etc (what this expression does is define a map from key to length and then does a lookup in this map).
Finally, if you need fancier logic, you can get and set variables in a custom feature.Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc7
Answers
Another possibility (without using a custom feature) is something like:
which if #keyNumberis 202 will evaluate to 0.248 in, etc (what this expression does is define a map from key to length and then does a lookup in this map).
Finally, if you need fancier logic, you can get and set variables in a custom feature.
These are just standard javascript constructs
IR for AS/NZS 1100
Note that what you enter into the expression fields is not quite FeatureScript syntax. The main differences are that shorthand units (eg., "in" rather than "inch") are allowed, the multiplication before a unit is optional in some cases (as in "3 inch" rather than "3 * inch"), #foo for accessing context variable foo, and a couple of other minor differences.