Choosing parameters depending on length parameter

hello,
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>    });


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?


Comments

  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,116 image
    </code><code>0 *mm <= toCheck < 5* mm
    is not legal syntax, try
    0 *mm <= toCheck && toCheck < 5* mm
    Senior Director, Technical Services, EMEA
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,116 image
    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, EMEA
  • urszula_wachulska
    urszula_wachulska Member Posts: 21 PRO
    Thank you very much!
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,116 image
    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, EMEA
  • MBartlett21
    MBartlett21 Member, OS Professional, Developers Posts: 2,064 ✭✭✭✭✭
    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];
    }
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,116 image
    @mbartlett21 I would never have guessed that in a million years!
    Senior Director, Technical Services, EMEA
  • Jake_Rosenfeld
    Jake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646 image
    edited December 2018
    Jake Rosenfeld - Modeling Team
  • lemon1324
    lemon1324 Member, Developers Posts: 230 EDU
    @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 University
  • urszula_wachulska
    urszula_wachulska Member Posts: 21 PRO
    Thank you all again!