Global Variables?

sloan_thrasher
sloan_thrasher Member Posts: 14 ✭✭
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_thorsen229
    rune_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_Schumacher
    Jonatan_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

  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    FYI “Contact Support” is only available for paid users. Free users should cast their votes on the forum. Thanks. 
    Senior Director, Technical Services, EMEA
  • rune_thorsen229
    rune_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_svane
    daniel_svane Member Posts: 2 ✭✭
    Cant believe this isnt implemented, I need this basically every project
  • john_mcclary
    john_mcclary Member, Developers Posts: 4,117 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_thrasher
    sloan_thrasher Member Posts: 14 ✭✭
    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_Schumacher
    Jonatan_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
  • jmsaltzman
    jmsaltzman Member Posts: 20 ✭✭
    ...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_peters
    luke_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...