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.

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

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,310
    </code><code>0 *mm <= toCheck < 5* mm
    is not legal syntax, try
    0 *mm <= toCheck && toCheck < 5* mm
    Senior Director, Technical Services, EMEAI
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,310
    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, EMEAI
  • urszula_wachulskaurszula_wachulska Member Posts: 21 PRO
    Thank you very much!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,310
    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, EMEAI
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    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
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,310
    @mbartlett21 I would never have guessed that in a million years!
    Senior Director, Technical Services, EMEAI
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    edited December 2018
    Jake Rosenfeld - Modeling Team
  • lemon1324lemon1324 Member, Developers Posts: 223 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_wachulskaurszula_wachulska Member Posts: 21 PRO
    Thank you all again!
Sign In or Register to comment.