Variable
Create a variable for use in expressions in a Part Studio, and assign a value. Use the variable in dimensions and expressions. Variables are features in Onshape, so the placement in the Feature list is important. Create a variable before the feature in which you will use it or create it on the fly during an operation that needs the value.
- While in a Part Studio, click to open the Variable dialog box.
- In the dialog:
Enter a name for the variable (and by which to reference it).
- Select a type:
- Length - A numeric value representing a length (decimal, integer, fraction)
- Angle - A numeric value representing an angle (decimal, integer, fraction)
- Number - A numeric value (decimal, integer, fraction)
- Any - Any of the above, a numeric value with different units, or a FeatureScript value such as boolean, map, array, string, or a function. See https://cad.onshape.com/FsDoc/variables.htm#standard-types and examples below.
- Enter a value (and optionally, units for Length, Angle, and Any).
- Enter a custom description for your variable.
Use only English letters and numbers in the name (at least one English letter followed by letters and/or numbers). Variable names are case-sensitive.
- Click .
- While in a Part Studio, click to open the Variable dialog box, then click Measured:
- In the dialog:
- Select a type:
- Distance - A numeric value representing a distance (decimal, integer, fraction)
- Length - A numeric value representing a length (decimal, integer, fraction)
- Diameter - A numeric value representing a diameter (decimal, integer, fraction)
Enter a name for the variable (and by which to reference it).
- Select entities to measure between.
- Fill out the remaining fields according to your preferences.
Use only English letters and numbers in the name (at least one English letter followed by letters and/or numbers). Variable names are case-sensitive.
- Select a type:
- Click .
Create a dimension, in the dimension field enter # and the variable name (and optionally, as part of an expression, as shown below):
and
Save the dimension; the variable is replaced with the value and the expression (if applicable) is solved:
When you double-click the dimension for editing, the variable (and expression) is displayed:
If an expression without a variable is used for a sketch dimension, it is indicated with the Expression without a variable icon () to the left of the dimension value. If an expression with a variable is used for a sketch dimension, it is indicated with the Expression with variable icon () to the left of the dimension value. See Displaying expressions in a sketch.
Use variables anywhere you use expressions in a Part Studio. For example, in an extrude or revolve operation.
Start the operation as usual (in this case, Revolve); in the numeric value field, enter # and the variable name (or optionally, as part of an expression):
Accept the feature.
When you edit the feature, the solution is displayed in the numeric value field:
Click in the field and the variable (and expression, if applicable) is displayed.
Variable autofill feature
When you have defined variables, entering a hashtag (or pound sign, underscore, or any letter) in a numeric dialog field opens the variable autofill feature as shown below:
You can keep typing to filter the list to reasonable options, use the arrow keys to move up and down in the list, and use the Enter key to accept a variable from the list when it is highlighted. You can also use the mouse scroll wheel to scroll the variable list and a mouse click to select a particular variable. Note that FeatureScript functions are also included in the list when the pound sign is not used:
Also note the following:
- Using the Escape key closes the dropdown without selecting anything. Removing focus from the dropdown also closes it without selecting anything.
- Compute values are shown in light gray, and will not be inserted (only the variable or function name is inserted). Likewise with function inputs, because the input placeholder names are not inserted.
- Only variables that exist in the current context are shown; variables further down in the Feature list are not shown
Variable values can contain expressions. You can specify an array with an index, and the index can be a variable. This allows you to change the value of the variable by changing the value of the index variable.
To use arrays in variables, you must first set up a zero-based index.
- Create a variable and set the name to ‘config’. Select Number as the type, and set the value of #config to ‘2’.
- Create a variable and set the name to ‘diameter’. Set the value of #diameter to ‘[0.25, 0.5, 1][#config]’.
-
Create a variable and set the name to ‘length’. Set the value of #length to ‘[2, 4, 10][#config]’.
- Create a circle.
- Create a line.
- Set the diameter of the circle to ‘#diameter’.
Since #config = 2, the diameter of the circle is 1.
- Set the length of the line to ‘#length’.
Since #config = 2, the length of the line is 10.
It is the second set of brackets [#config] that serves as the index pointer in the array.
Change the value of the index variable to change the indices of all array variables.
You can create variables as you need them, when entering a numeric value into a field in a feature dialog box, for example. The variable is saved preceding the current operation in the Feature list.
For example, when creating a sketch, when the dimension field pops up, instead of entering a value for the dimension, you can type the pound sign # and the list of existing variables is displayed, along with an option for a New variable:
Select New variable to open the Variable dialog to create a new variable and use it in the current field. (Otherwise, select one of the existing variables, if available.)
The Create variable dialog gives the option of creating a variable for a Feature (default) or a Configuration.
Enter a name for the new variable, as well as a value. Click the check mark to save the new variable and to use it in the open feature.
You can use FeatureScript functions in a variable, following the FeatureScript syntax.
For example, you might create a variable of type Any, named “Adjust”, to store a function that doubles a given length and adds 2.5mm, as follows:
function(len) { return len * 2 + 2.5 mm; }
and then reference that variable in an expression, such as:
#Adjust(20mm)
- When you change the value of a variable (edit it as you would any feature), all operations that use the variable are automatically updated.
- When you change the name of a variable, you have the choice to propagate the change everywhere the variable is used:
Check the box next to Update all references.
- While in a Part Studio, select the Variable tool to open the Variable tool dialog box:
- In the dialog box:
- Select a type:
- Length - A numeric value representing a length (decimal, integer, fraction)
- Angle - A numeric value representing an angle (decimal, integer, fraction)
- Number - A numeric value (decimal, integer, fraction)
- Any - Any of the above, a numeric value with different units, or a FeatureScript value such as boolean, map, array, string, or a function. See https://cad.onshape.com/FsDoc/variables.htm#standard-types and examples below.
- Enter a name for the variable (and by which to reference it).
- Enter a value (and optionally, units for Length, Angle, and Any).
Use only English letters and numbers in the name (at least one English letter followed by letters and/or numbers). Variable names are case-sensitive.
- Tap the checkmark.
The variable you created is listed in the Feature List and displays the name and value of the variable.
- While in a Part Studio, select the Variable tool to open the Variable tool dialog box, then click Measured:
- In the dialog:
- Select a type:
- Distance - A numeric value representing a distance (decimal, integer, fraction)
- Length - A numeric value representing a length (decimal, integer, fraction)
- Diameter - A numeric value representing an angle (decimal, integer, fraction)
-
Enter a name for the variable (and by which to reference it).
- Select entities to measure between.
- Fill out the remaining fields according to your preferences.
Use only English letters and numbers in the name (at least one English letter followed by letters and/or numbers). Variable names are case-sensitive.
- Tap the checkmark.
The variable you created is listed in the Feature List and displays the name and value of the variable.
Create a dimension, in the dimension field enter # and the variable name (and optionally, as part of an expression, as shown below):
and
Save the dimension; the variable is replaced with the value and the expression (if applicable) is solved:
When you double-click the dimension for editing, the variable (and expression) is displayed:
Use variables anywhere you use expressions in a Part Studio. For example, in an extrude or revolve operation:
Start the operation as usual (in this case, Revolve); in the numeric value field, enter # and the variable name (or optionally, as part of an expression). For example: (#x) deg.
Accept the feature.
When you edit the feature, the solution is displayed in the numeric value field. For example: 4 deg.
Click in the field and the variable (and expression, if applicable) is displayed.
Variable values can contain expressions but must evaluate to a scalar value. You can specify an array with an index, and the index can be a variable. This allows you to change the value of the variable by changing the value of the index variable.
To use arrays in variables you must first set up a zero-based index.
- Tap the Variable tool and set the name to "config." Set the value of #config to 2.
- Tap the Variable tool and set the name to "diameter." Set the value of #diameter to [0.25, 0.5, 1] [#config].
- Tap the Variable tool and set the name to "length." Set the value of #length to [2, 4, 10] [#config].
- Tap the sketch tool and sketch a circle.
- Tap the sketch tool and sketch a line.
- Set the diameter of the circle to "#diameter".
- Set the length of the line to "#length".
The diameter of the circle is set to 1, and the length of the line is set to 10.
- Edit the #config variable, set the value to 0.
It is important that you include the second set of brackets that contain "#config." This is what allows the value of the variable #config to point to the correct place in the array.
It is important that you include the second set of brackets that contain "#config." This is what allows the value of the variable #config to point to the correct place in the array.
The sketch updates and the circle diameter is now set to 0.25 and the line length is set to 2.
You can do this for as many arrays as you like, and your arrays can hold infinite places (they are not limited to only three values as this example shows).
You can use FeatureScript functions in a variable, following the FeatureScript syntax.
For example, you might create a variable of type Any, named “Adjust”, to store a function that doubles a given length and adds 2.5mm, as follows:
function(len) { return len * 2 + 2.5 mm; }
and then reference that variable in an expression, such as:
#Adjust(20mm)
- When you change the value of a variable (edit it as you would any feature), all operations that use the variable are automatically updated.
- Variable names are case-sensitive.
- You can input a variable as a stand-alone value or as part of an equation.
- You can use a variable in a dimension, expression, or feature.
-
In cases where you specify a variable without a unit, expressions will also be unitless or assume the unit of the workspace. But if you explicitly add units to a variable value, then any expression should also match that unit when being written in order to be valid.
- When you change the name of a variable, you have the choice to propagate the change everywhere the variable is used:
Select Update all references.
- While in a Part Studio, select the Variable tool to open the Variable tool dialog box:
- In the dialog box:
- Select a type:
- Length - A numeric value representing a length (decimal, integer, fraction)
- Angle - A numeric value representing an angle (decimal, integer, fraction)
- Number - A numeric value (decimal, integer, fraction)
- Any - Any of the above, a numeric value with different units, or a FeatureScript value such as boolean, map, array, string, or a function. See https://cad.onshape.com/FsDoc/variables.htm#standard-types and examples below.
- Enter a name for the variable (and by which to reference it).
- Enter a value (and optionally, units for Length, Angle, and Any).
Use only English letters and numbers in the name (at least one English letter followed by letters and/or numbers). Variable names are case-sensitive.
- Tap the checkmark.
The variable you created is listed in the Feature List and displays the name and value of the variable.
- While in a Part Studio, select the Variable tool to open the Variable tool dialog box, then click Measured:
- In the dialog:
- Select a type:
- Distance - A numeric value representing a distance (decimal, integer, fraction)
- Length - A numeric value representing a length (decimal, integer, fraction)
- Diameter - A numeric value representing an angle (decimal, integer, fraction)
-
Enter a name for the variable (and by which to reference it).
- Select entities to measure between.
- Fill out the remaining fields according to your preferences.
Use only English letters and numbers in the name (at least one English letter followed by letters and/or numbers). Variable names are case-sensitive.
- Tap the checkmark.
The variable you created is listed in the Feature List and displays the name and value of the variable.
Create a dimension, in the dimension field enter # and the variable name (and optionally, as part of an expression, as shown below):
and
Save the dimension; the variable is replaced with the value and the expression (if applicable) is solved:
When you double-click the dimension for editing, the variable (and expression) is displayed:
Use variables anywhere you use expressions in a Part Studio. For example, in an extrude or revolve operation:
Start the operation as usual (in this case, Revolve); in the numeric value field, enter # and the variable name (or optionally, as part of an expression). For example: (#x) deg.
Accept the feature.
When you edit the feature, the solution is displayed in the numeric value field. For example: 4 deg.
Click in the field and the variable (and expression, if applicable) is displayed.
Variable values can contain expressions but must evaluate to a scalar value. You can specify an array with an index, and the index may be a variable. This allows you to change the value of the variable by changing the value of the index variable.
To use arrays in variables you must first set up a zero-based index.
- Tap the Variable tool and set the name to "config." Set the value of #config to 2.
- Tap the Variable tool and set the name to "diameter." Set the value of #diameter to [0.25, 0.5, 1] [#config].
- Tap the Variable tool and set the name to "length." Set the value of #length to [2, 4, 10] [#config].
- Tap the sketch tool and sketch a circle.
- Tap the sketch tool and sketch a line.
- Set the diameter of the circle to "#diameter".
- Set the length of the line to "#length".
The diameter of the circle is set to 1, and the length of the line is set to 10.
- Edit the #config variable, set the value to 0.
It is important that you include the second set of brackets that contain "#config." This is what allows the value of the variable #config to point to the correct place in the array.
It is important that you include the second set of brackets that contain "#config." This is what allows the value of the variable #config to point to the correct place in the array.
The sketch updates and the circle diameter is now set to 0.25 and the line length is set to 2.
You can do this for as many arrays as you like, and your arrays are able to hold infinite places (they are not limited to only three values as this example shows).
You can use FeatureScript functions in a variable, following the FeatureScript syntax.
For example, you might create a variable of type Any, named “Adjust”, to store a function that doubles a given length and adds 2.5mm, as follows:
function(len) { return len * 2 + 2.5 mm; }
and then reference that variable in an expression, such as:
#Adjust(20mm)
- When you change the value of a variable (edit it as you would any feature), all operations that use the variable are automatically updated.
- Variable names are case-sensitive.
- You can input a variable as a stand-alone value or as part of an equation.
- You can use a variable in a dimension, expression, or feature.
For additional Learning center resource, follow the technical briefing article here: Parametric Variables vs Configuration Variables (Onshape account required). You can also register for the following course: Variables and Expressions (Onshape account required).