Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape, CAD, maker project and design.

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.

Setting an array of variables

Dylan_StewartDylan_Stewart Member, Developers Posts: 97 PRO
I am working on a project that would allow the user to set any number of variables they choose. 

I am trying to follow the small example in the 'Feature UI' section in the FeatureScript guide and need some help understanding what is going on. 

I'm thinking maybe something like this 

annotation { "Name" : "Variables",
           "Variable Name" : ".....", }
         definition.array is array;
for (var variables in definition.array)
{
     annotation { "Name" : "My String" }
            definition.myString is string;

     annotation { "Name" : definition.myString }
            isLength(definition.myLength, LENGTH_BOUNDS);
}
I'm not sure if something like this is possible. 

As always, any and all help is appreciated. 
Also, a working sample document of the example given in the FsDoc would really help me understand what is going on.



Thank you.
Digital Engineering

Comments

  • Jake_RosenfeldJake_Rosenfeld Onshape Employees, Developers Posts: 319
    edited September 8
    Hi Dylan!

    This is totally possible.  Information about array parameters can be found in the following doc under 'arrays':
    https://cad.onshape.com/FsDoc/uispec.html

    Your code looks good! You need to change the annotation on the array parameter itself, and define the values in the for loop as 'variable.something' instead of 'definition.something'

    Here is an example:
    https://cad.onshape.com/documents/85ead7e8198b48c695b39b58/w/4fb966357258b36a88ac0430/e/4eb384334534b70122d7481c

    Also, if you press the 'parameter' dropdown (the one with the 'p') and click "array parameter" it will autofill a skeleton for you.
    Jake Rosenfeld - Modeling Team
  • Dylan_StewartDylan_Stewart Member, Developers Posts: 97 PRO
    AH!!!! Thank you! I figured that this would be possible and come to find out I was almost there! Thank you so much @Jake_Rosenfeld

    Digital Engineering
  • Jake_RosenfeldJake_Rosenfeld Onshape Employees, Developers Posts: 319
    No Problem! This functionality actually just came out about a month ago, so if theres any way you think we can improve the discoverability or quality of the documentation, we would love to hear your suggestions.
    Jake Rosenfeld - Modeling Team
  • Dylan_StewartDylan_Stewart Member, Developers Posts: 97 PRO
    So I just tried to add an array value to one of the variables and it didn't like that too much...



    Digital Engineering
  • Jake_RosenfeldJake_Rosenfeld Onshape Employees, Developers Posts: 319
    The value of any variable that is being used as _input_ to a feature dialog will depend on the execution state before the feature in question has run.  This is important because otherwise you could create infinitely-recursive or non-deterministic features.  #options won't exist until after the feature is executed, so it cannot be used as input to the feature.

    What you're trying to do is still possible though.  Making a sample now.
    Jake Rosenfeld - Modeling Team
  • Jake_RosenfeldJake_Rosenfeld Onshape Employees, Developers Posts: 319
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    Watching this thread with interest, thanks chaps.

    Owen S.
    Production Engineer
    HWM-Water Ltd
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    edited September 11
    I wanna-play-too! :p

    @Jake_RosenfeldI I just tried to add your feature to my toolbar but OnS reports there are no features to add.  If the document is copied first then it works fine.  Do you have a sharing permission set that blocks linking directly to your feature?

    Cheers, Owen S.
    Production Engineer
    HWM-Water Ltd
  • MichaelThiesmeyerMichaelThiesmeyer Onshape Employees Posts: 22
    @owen_sparks It looks like Jake did not publish a version in that doc, which is why you can't link to his Featurescript. The copy operation puts the Featurescript into the `Start` version of the new doc, which is why that works.

    Best,

    Mike
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    @owen_sparks It looks like Jake did not publish a version in that doc, which is why you can't link to his Featurescript. The copy operation puts the Featurescript into the `Start` version of the new doc, which is why that works.

    Best,

    Mike
    Good information, thanks for looking into it.

    Cheers, Owen S,
    Production Engineer
    HWM-Water Ltd
  • Dylan_StewartDylan_Stewart Member, Developers Posts: 97 PRO
    Ah this is brilliant!! Yes, this is exactly what I was looking to do. Thank you @Jake_Rosenfeld

    Digital Engineering
  • Jake_RosenfeldJake_Rosenfeld Onshape Employees, Developers Posts: 319
    Thanks @MichaelThiesmeyer.  I've published a version now.
    Jake Rosenfeld - Modeling Team
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    Hi @Jake_Rosenfeld

    I think there is a small bug in your FS.  It seems to work for all values of an array except the last one.

    Examples

    (a) I made a feature with 2 options.  Option 1 worked, option 2 failed
    (b) Straight copy of your doc.  The example has 3 options, 1 and 2 work, 3 fails.



    Could you give it a poke?

    Cheers,

    Owen S.
    Production Engineer
    HWM-Water Ltd
  • Dylan_StewartDylan_Stewart Member, Developers Posts: 97 PRO
    @owen_sparks Same... I am trying something like this and it is breaking. I think I know what the problem is but not %100 sure.

    Have you taken a different approach? 
    FeatureScript 675;
    import(path : "onshape/std/geometry.fs", version : "675.0");
    
    const OPTION_COUNT =
    {
                (unitless) : [0, 0, 50]
            } as IntegerBoundSpec;
    
    annotation { "Feature Type Name" : "Configurator" }
    export const configurator = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Option" }
            isInteger(definition.option, OPTION_COUNT);
    
            annotation { "Name" : "Second Option" }
            definition.secondOption is boolean;
    
            if (definition.secondOption)
            {
                annotation { "Name" : "Option2" }
                isInteger(definition.option2, OPTION_COUNT);
    
                annotation { "Name" : "Variable arrays", "Item name" : "Variables", "Item label template" : "###name" }
                definition.mySecondVars is array;
                for (var variable in definition.mySecondVars)
                {
                    annotation { "Name" : "Name" }
                    variable.secondName is string;
    
                    annotation { "Name" : "Array", "Default" : 5 }
                    isAnything(variable.arra);
                }
    
            }
            annotation { "Name" : "Variable arrays", "Item name" : "Variables", "Item label template" : "###name" }
            definition.myVars is array;
            for (var variable in definition.myVars)
            {
                annotation { "Name" : "Name" }
                variable.name is string;
    
                annotation { "Name" : "Second Array", "Default" : 5 }
                isAnything(variable.arr);
            }
        }
        {
            for (var variable in definition.myVars)
            {
                setVariable(context, variable.name, variable.arr[definition.option]);
            }
    
            if (definition.secondOption)
            {
                for (var variable in definition.mySecondVars)
                {
                    setVariable(context, variable.secondName, variable.arra[definition.secondOption]);
                }
            }
        });




    Digital Engineering
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    @Dylan_Stewart,

    Sorry my approach was to add @Jake_Rosenfeld's feature to my toolbar :)

    Wish I could be of more help.

    Owen S.
    Production Engineer
    HWM-Water Ltd
  • Dylan_StewartDylan_Stewart Member, Developers Posts: 97 PRO
    Oh, my misunderstanding.  I see what you are trying to accomplish now. I was thinking the need for 2 or more driving options. 
    Digital Engineering
  • Jake_RosenfeldJake_Rosenfeld Onshape Employees, Developers Posts: 319
    Hi @owen_sparks ,

    The options are 0-indexed.  So the first option is option 0, the second option is option 1 etc.  Just a byproduct of working on code all day :sweat_smile:

    Would it be better for you guys if it was one-indexed?
    Jake Rosenfeld - Modeling Team
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    Doh, should have thought of that, sorry.  My bad.  In my opinion one-indexed would be more intuitive for most users, but you're the Boss, it's your feature. 

    Write it in Hex if it makes you happy!

    Cheers,

    Owen S.
    Production Engineer
    HWM-Water Ltd
  • leon_pootleon_poot Member, Developers Posts: 78 ✭✭
    Brilliant, very useful!
    "A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools." - Douglas Adams, Mostly Harmless
  • Jake_RosenfeldJake_Rosenfeld Onshape Employees, Developers Posts: 319
    Hi All!

    I've added a V2 to the feature that has better error handling and a choice for 0 and 1 indexing.
    Jake Rosenfeld - Modeling Team
  • Dylan_StewartDylan_Stewart Member, Developers Posts: 97 PRO
    @Jake_Rosenfeld this is great! Thank you!

    Digital Engineering
  • owen_sparksowen_sparks Member, Developers Posts: 725 PRO
    Very nice @Jake_Rosenfeld thank you.

    Also love the update feature notification and process. it's clear a lot of thought has gone into this  area.

    Cheers, Owen S.
    Production Engineer
    HWM-Water Ltd
Sign In or Register to comment.