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.

Global Variables?

sloan_thrashersloan_thrasher Member Posts: 3 ✭✭
Looks like a lot of people are asking for global (at least within a document) variables.
Are there any plans to add this feature?

Best Answers

  • rune_thorsen229rune_thorsen229 Member Posts: 182 EDU
    Answer ✓
    @sloan_thrasher
    Its strange this hasn't been solved despite numerous calls and the obvious necessity.  https://forum.onshape.com/discussion/comment/60779#Comment_60779

    However so far I do it by a featurescript: 
    1. next to the tabs press plus - 'create feature studio'
    2, insert the code below where "thickness" etc. will be your global values
    3. press [commit]
    4. on each part studio - SIGH - (where you want to use the globals) you press the {[]} symbol 'Custom features...' and insert '"Global Dimensions"' (name defined in the  featurescript below). Put it early in your features list.
    5 Now you have these centrally defined variables at your disposal.
    e.g. : #thickness, #connhole etc.



    ----------START ------
    FeatureScript 1024;
    import(path : "onshape/std/geometry.fs", version : "1024.0");

    const VALUES = { // Below we have the global variables driving the design:
        "thickness" : 1.5 * millimeter,   //Thickness of walls
        "connhole" : 1.2 * millimeter,   //Connector hole
        "bandwidth" : 20 * millimeter,  //The width of the wrist band
        "padding"   : 5 * millimeter,     //Padding from skin to band
        "margin"    : 0.5 *millimeter ,   //Distance between objects
        "gap"       : 0.8  *millimeter    //Distance between objects
        
    };
    annotation { "Feature Type Name" : "Global Dimensions" }
    export const setGlobals = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
        }
        {
            for (var value in VALUES)
                setVariable(context, value.key, value.value);
        });


    ---------END ---------
  • Jonatan_SchumacherJonatan_Schumacher Member Posts: 2
    Answer ✓
    You can now use the new "Variable Studio" to define shared variables: https://cad.onshape.com/help/Content/variable_studio.htm

Answers

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,688
    FYI “Contact Support” is only available for paid users. Free users should cast their votes on the forum. Thanks. 
    Senior Director, Technical Services, EMEAI
  • rune_thorsen229rune_thorsen229 Member Posts: 182 EDU
    Answer ✓
    @sloan_thrasher
    Its strange this hasn't been solved despite numerous calls and the obvious necessity.  https://forum.onshape.com/discussion/comment/60779#Comment_60779

    However so far I do it by a featurescript: 
    1. next to the tabs press plus - 'create feature studio'
    2, insert the code below where "thickness" etc. will be your global values
    3. press [commit]
    4. on each part studio - SIGH - (where you want to use the globals) you press the {[]} symbol 'Custom features...' and insert '"Global Dimensions"' (name defined in the  featurescript below). Put it early in your features list.
    5 Now you have these centrally defined variables at your disposal.
    e.g. : #thickness, #connhole etc.



    ----------START ------
    FeatureScript 1024;
    import(path : "onshape/std/geometry.fs", version : "1024.0");

    const VALUES = { // Below we have the global variables driving the design:
        "thickness" : 1.5 * millimeter,   //Thickness of walls
        "connhole" : 1.2 * millimeter,   //Connector hole
        "bandwidth" : 20 * millimeter,  //The width of the wrist band
        "padding"   : 5 * millimeter,     //Padding from skin to band
        "margin"    : 0.5 *millimeter ,   //Distance between objects
        "gap"       : 0.8  *millimeter    //Distance between objects
        
    };
    annotation { "Feature Type Name" : "Global Dimensions" }
    export const setGlobals = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
        }
        {
            for (var value in VALUES)
                setVariable(context, value.key, value.value);
        });


    ---------END ---------
  • daniel_svanedaniel_svane Member Posts: 2 ✭✭
    Cant believe this isnt implemented, I need this basically every project
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,936 PRO
    Agreed, i use it in almost every project, but still isn't enough. There needs to be a way to use variables to drive patterns and such in assemblies too
  • sloan_thrashersloan_thrasher Member Posts: 3 ✭✭
    I've used the feature script solution, but it doesn't integrate with the auto-replace of variable names, and you have to print it out or copy to a text file to remember the variable names, especially when you have a lot of vars. I'd love to see an "include" feature where you have the vars in a file and just have to include/reference that file to make either sketch or document level vars. Also, when making as repeating pattern, you can't use a variable to specify the number of repeats. Aaarg! IMHO, you should be able to use variables anyplace the needs a value.
  • Jonatan_SchumacherJonatan_Schumacher Member Posts: 2
    Answer ✓
    You can now use the new "Variable Studio" to define shared variables: https://cad.onshape.com/help/Content/variable_studio.htm
  • jmsaltzmanjmsaltzman Member Posts: 16 ✭✭
    ...use the new "Variable Studio" to define shared variables...
    I was excited to see this but changing a variable name in Variable Studio broke things: it didn't rename the references in the parts studio.
  • luke_petersluke_peters Member Posts: 3
    ...use the new "Variable Studio" to define shared variables...
    I was excited to see this but changing a variable name in Variable Studio broke things: it didn't rename the references in the parts studio.
    This is still an issue as of June 3, 2023. I was similarly extremely excited about this, but changing "Height" to "Depth" broke the reference in every part studio it was being used in instead of changing #Height to #Depth in the feature parameters. Still better than nothing, I guess, as long as you pick the forever-name of the variable from the start...
Sign In or Register to comment.