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:


export function toCheckSth(toCheck)
{

const mm = millimeter;
return {

0 *mm <= toCheck < 5* mm : { "a" : 16*mm, "b":20*mm,"l":10*mm},
5*mm <= toCheck < 10*mm : { "a" : 20*mm, "b":22*mm, "l":20*mm},
10*mm<= toCheck < 15*mm : { "a" : 22*mm, "b":25*mm, "l":30*mm},

};
}


annotation { "Feature Type Name" : "My Feature" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
// Define the parameters of the feature type
annotation { "Name" : "My Length" }
isLength(definition.myLength, LENGTH_BOUNDS);

}
{
var toCheck = definition.myLength;
var dim = toCheckSth(toCheck);
var myL = dim.l;
print(myL);



});


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: 2,260
    0 *mm <= toCheck < 5* mm
    is not legal syntax, try
    0 *mm <= toCheck && toCheck < 5* mm
    Neil Cooke, Director of Technical Marketing, Onshape Inc.
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 2,260
    Actually (after running your code) to get the result you want, you will have to use long hand:
        const mm = millimeter;

    if (0 * mm <= toCheck && toCheck < 5 * mm)
    return { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm };
    if (5 * mm <= toCheck && toCheck < 10 * mm)
    return { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm };
    if (10 * mm <= toCheck && toCheck < 15 * mm)
    return { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm };
    Neil Cooke, Director of Technical Marketing, Onshape Inc.
  • urszula_wachulskaurszula_wachulska Member Posts: 20 PRO
    Thank you very much!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 2,260
    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;

    const myValues = {
    5 * mm : { "a" : 16 * mm, "b" : 20 * mm, "l" : 10 * mm },
    10 * mm : { "a" : 20 * mm, "b" : 22 * mm, "l" : 20 * mm },
    15 * mm : { "a" : 22 * mm, "b" : 25 * mm, "l" : 30 * mm }
    };

    annotation { "Feature Type Name" : "My Feature" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
    annotation { "Name" : "My Length" }
    isLength(definition.myLength, LENGTH_BOUNDS);
    }
    {
    var toCheck = definition.myLength;
    var dim = myValues[ceil(toCheck, 5 * millimeter)];
    var myL = dim.l;
    print(myL);
    });
    Neil Cooke, Director of Technical Marketing, Onshape Inc.
  • MBartlett21MBartlett21 Member Posts: 1,572 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 - I make FeatureScripts: view FS (My FS's have "Official" beside them)
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 2,260
    @mbartlett21 I would never have guessed that in a million years!
    Neil Cooke, Director of Technical Marketing, Onshape Inc.
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,295
    edited December 2018
    Jake Rosenfeld - Modeling Team
  • lemon1324lemon1324 Member, Developers Posts: 166 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 Candidate at Stanford University
  • urszula_wachulskaurszula_wachulska Member Posts: 20 PRO
    Thank you all again!
Sign In or Register to comment.