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.

how to make a variable feature with multiple variables

joris_van_tubergenjoris_van_tubergen OS Professional Posts: 3 PRO
edited November 2016 in FeatureScript
Hi,
I would like to have a feature script like the Variable, but then with multiple Variables for the input.
The reason is that when you have a very complex document and the Variables in the beginning I takes quite some time to regenerate the drawing after submitting the values. You have to wait for that before adjusting a new variable.
My solution was to make a feature where you can change multiple variables at once.

Another reason is that you can quickly change a bunch of variables at once by suppressing or changing the order of the MultiVariable feature.


Since I am not a programmer but a product designer I get a bit stuck. I tried to copy/paste and adjust some code, but not succesfull.
Below the changes I made in a copy of the Variable Feature.

Anybody any ideas/solution

cheers / joris



    precondition
    {
        annotation { "Name" : "Name" }
        definition.name is string;

        annotation { "Name" : "Value" }
        isAnything(definition.value);

        annotation { "Name" : "Name2" }
        definition.name2 is string;

        annotation { "Name" : "Value2" }
        isAnything(definition.value2);
    }
    {
        verifyVariableName(definition.name);

        setVariable(context, definition.name, definition.value);

        verifyVariableName(definition.name2);

        setVariable(context, definition.name2, definition.value2);

Best Answers

  • ilya_baranilya_baran Posts: 932
    Accepted Answer
    At the same time, I see no problems with your code at all -- here's a public document I made with it working: https://cad.onshape.com/documents/5f5777809672e49d503f5e8e/v/5b94daf8670296d0b6b20c91/e/30a2b09bfe50448f31a7eab5
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc

Answers

  • john_f_carrjohn_f_carr Onshape Employees Posts: 74
    edited November 2016
    If you move the rollback bar back to the end of the variable section of your feature list you can edit separate variable features without regenerating the entire model.  Then "roll to end" once to regenerate the model with all the new variables.

    We have been looking at ways to make this process faster.
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 932
    Accepted Answer
    At the same time, I see no problems with your code at all -- here's a public document I made with it working: https://cad.onshape.com/documents/5f5777809672e49d503f5e8e/v/5b94daf8670296d0b6b20c91/e/30a2b09bfe50448f31a7eab5
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • joris_van_tubergenjoris_van_tubergen OS Professional Posts: 3 PRO
    @john_f_carr
    That's how i do this now indeed, but it is still very nice if this is not necessary... : )
    also with more than 12 variables, it has a better overview in grouping them in a feature script, maybe even with captions in between


    @ilya_baran
    Thanks...!! seems to work indeed, but.... : )
    it doesn't work when the second variable doesn't have a unit behind. only works with 12 in [and not with just '12']

    btw, my original code was way longer, I also copied everything in front and after, with copying your code in my file it works. Even without the mm suffix in var 2...[?!!]

    thanks...!!

    cheers / joris


    FeatureScript 442; /* Automatically generated version */
    // This module is part of the FeatureScript Standard Library and is distributed under the MIT License.
    // See the LICENSE tab for the license text.
    // Copyright (c) 2013-Present Onshape Inc.

    // Imports used in interface
    export import(path : "onshape/std/query.fs", version : "442.0");

    // Imports used internally
    import(path : "onshape/std/containers.fs", version : "442.0");
    import(path : "onshape/std/evaluate.fs", version : "442.0");
    import(path : "onshape/std/feature.fs", version : "442.0");
    import(path : "onshape/std/string.fs", version : "442.0");
    import(path : "onshape/std/tool.fs", version : "442.0");
    import(path : "onshape/std/valueBounds.fs", version : "442.0");

    /**
     * Feature performing a `setVariable` allowing a user to assign a FeatureScript
     * value to a context variable. This variable may be retrieved within a feature by
     * calling `getVariable`, or in a Part Studio using `#` syntax (e.g. `#foo`)
     * inside any parameter which allows an expression (including the `value`
     * parameter of another variable!)
     *
     * @param definition {{
     *      @field name : Must be an identifier.
     *      @field value : Can be anything, including a length, an array, or a function.
     * }}
     */
    annotation {"Feature Type Name" : "MultiVariable", "Feature Name Template": "Variable ###name = #value", "UIHint" : "NO_PREVIEW_PROVIDED"}
    export const assignVariable = defineFeature(function(context is Context, id is Id, definition is map)

        precondition
        {
            annotation { "Name" : "Name" }
            definition.name is string;

            annotation { "Name" : "Value" }
            isAnything(definition.value);

            annotation { "Name" : "Name2" }
            definition.name2 is string;

            annotation { "Name" : "Value2" }
            isAnything(definition.value2);
        }
        {
            verifyVariableName(definition.name);

            setVariable(context, definition.name, definition.value);

            verifyVariableName(definition.name2);

            setVariable(context, definition.name2, definition.value2);


            if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V424_VARIABLE_WARNINGS))
            {
                if (definition.value == undefined)
                {
                    reportFeatureWarning(context, id, ErrorStringEnum.VARIABLE_CANNOT_EVALUATE);
                }
            }
        });

    /**
     * Throws an error if `name` is not a valid identifier.
     */
    export function verifyVariableName(name is string)
    {
        const replaceNameWithRegExpShouldBeBlank = replace(name, '[a-zA-Z_][a-zA-Z_0-9]*', '');
        if (name == '' || replaceNameWithRegExpShouldBeBlank != '')
            throw regenError(ErrorStringEnum.VARIABLE_NAME_INVALID);
    }

    /**
     * Make a function to look up variables from the given context.  Used in generated part studio code.
     */
    export function makeLookupFunction(context is Context, id is Id) returns function
    {
        return function(name is string)
            {
                return getVariable(context, name);
            };
    }


  • leon_pootleon_poot Member, Developers Posts: 87 ✭✭
    From what I understand, this defines a set number of variables. Is there a way one could have a new set of variable inputs show up, every time one has entered one? I.e. when I have have given the name and value of variable 1 and 2, I might want to add 3, 5, 8, 13 ... n more variables.
    "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
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 449
    edited April 2017
    @leon_poot Not yet, but we agree there's a need in custom features for a resizable array of parameters. Stay tuned!

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 932
    Not currently, but this type of ability is being worked on.  In addition, there are a number of enhancements planned to the overall variable infrastructure (including improved built-in support for the type of workflow that this custom feature is designed to help).
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
Sign In or Register to comment.