Choosing parameters depending on length parameter
urszula_wachulska
Member Posts: 21 PRO
hello,
how can I read some previously defined parameters (dimensions) depending on other dimension typed by user?
I have tried something like this:
But in part studio I get FeatureScript notices that cannot compare boolean and ValueWithUnits, which I don't really understand, because both var "toCheck" and values to compare are ValuesWithUnits...
Could you please explain me what am I doing wrong and how to solve it?
how can I read some previously defined parameters (dimensions) depending on other dimension typed by user?
I have tried something like this:
<br>export function toCheckSth(toCheck)<br>{<br> <br> const mm = millimeter; <br> return {<br> <br> 0 *mm <= toCheck < 5* mm : { "a" : 16*mm, "b":20*mm,"l":10*mm},<br> 5*mm <= toCheck < 10*mm : { "a" : 20*mm, "b":22*mm, "l":20*mm},<br> 10*mm<= toCheck < 15*mm : { "a" : 22*mm, "b":25*mm, "l":30*mm},<br> <br> };<br>}<br><br><br>annotation { "Feature Type Name" : "My Feature" }<br>export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)<br> precondition<br> {<br> // Define the parameters of the feature type<br> annotation { "Name" : "My Length" }<br> isLength(definition.myLength, LENGTH_BOUNDS);<br> <br> }<br> {<br> var toCheck = definition.myLength;<br> var dim = toCheckSth(toCheck);<br> var myL = dim.l;<br> print(myL);<br> <br> <br> <br> });Could you please explain me what am I doing wrong and how to solve it?
0
Comments
-
</code><code>0 *mm <= toCheck < 5* mm
is not legal syntax, try0 *mm <= toCheck && toCheck < 5* mm
Senior Director, Technical Services, EMEA1 -
Actually (after running your code) to get the result you want, you will have to use long hand:
const mm = millimeter;<br><br> if (0 * mm <= toCheck && toCheck < 5 * mm)<br> return { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm };<br> if (5 * mm <= toCheck && toCheck < 10 * mm)<br> return { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm };<br> if (10 * mm <= toCheck && toCheck < 15 * mm)<br> return { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm };Senior Director, Technical Services, EMEA1 -
Thank you very much!
0 -
Another way would be to use a predefined table of values, but this requires the test to be in increments of 5:
const mm = millimeter;<br><br>const myValues = {<br> 5 * mm : { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm },<br> 10 * mm : { "a" : 20 * mm, "b" : 22 * mm, "l" : 20 * mm },<br> 15 * mm : { "a" : 22 * mm, "b" : 25 * mm, "l" : 30 * mm }<br> };<br><br>annotation { "Feature Type Name" : "My Feature" }<br>export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)<br> precondition<br> {<br> annotation { "Name" : "My Length" }<br> isLength(definition.myLength, LENGTH_BOUNDS);<br> }<br> {<br> var toCheck = definition.myLength;<br> var dim = myValues[ceil(toCheck, 5 * millimeter)];<br> var myL = dim.l;<br> print(myL);<br> });Senior Director, Technical Services, EMEA2 -
Or you could use this:
export function toCheckSth(toCheck) { const mm = millimeter; return { 0 * mm <= toCheck && toCheck < 5 * mm : { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm }, 5 * mm <= toCheck && toCheck < 10 * mm : { "a" : 20 * mm, "b" : 22 * mm, "l" : 20 * mm }, 10 * mm <= toCheck && toCheck < 15 * mm : { "a" : 22 * mm, "b" : 25 * mm, "l" : 30 * mm }, }[true]; }3 -
@mbartlett21 I would never have guessed that in a million years!
Senior Director, Technical Services, EMEA1 -
Also:
https://forum.onshape.com/discussion/comment/44453/#Comment_44453
https://cad.onshape.com/documents/f6780441494046f4439a8219/w/71e7c5c95c521d8716fa75fa/e/631535138fdaba3ef7928ea2
I think that the other solutions in this thread are better than this one, but it is an option.Jake Rosenfeld - Modeling Team1 -
@mbartlett21 I don't know if I'd call that elegant necessarily since it's not as obvious what's going on, but that is a really clever solution!
Arul Suresh
PhD, Mechanical Engineering, Stanford University0 -
Thank you all again!
0




