变量
创建变量以便用于 Part Studio 中的表达式并赋值。在尺寸标注和表达式中使用变量。变量是 Onshape 中的特征,因此如何在特征列表中放置非常重要。在要在其中使用变量的特征前创建变量,或操作过程中需要变量时进行实时创建。
- 在 Part Studio 中,单击
打开“变量”对话框。
- 在对话框中:
输入变量的名称(参考变量所依据的名称)。
- 选择类型:
- 长度 - 代表长度的数值(小数、整数、分数)
- 角度 - 代表角度的数值(小数、整数、分数)
- 数字 - 数值(小数、整数、分数)
- 任意项目 - 上述任意项目,具有不同单位的数值,或 FeatureScript 值,例如布尔、映射、数组、字符串或函数。请参见 https://cad.onshape.com/FsDoc/variables.html#standard-types 及下面的示例。
- 输入一个值(可选:长度、角度及任意项目的单位)。
- 输入变量的自定义说明。
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- 单击
。
- While in a Part Studio, click
to open the Variable dialog box, then click Measured:
- 在对话框中:
- 选择类型:
- 距离 - 表示距离的数值(小数、整数、分数)
- 长度 - 代表长度的数值(小数、整数、分数)
- 直径 - 表示直径的数值(小数、整数、分数)
输入变量的名称(参考变量所依据的名称)。
- 选择要测量其间距的图元。
- 根据您的喜好填写剩余输入框。
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- 选择类型:
- 单击
。
The From table option in the Variable feature enables users to create a single variable that stores a subset of rows and columns from the CSV table. The Row options filter the rows and determine the Result type (Single value, Array, or Map) of the variable. The Column options then filter each row and determine the type of each entry in the final variable.
- While in a Part Studio, click
to open the Variable dialog, then click From table:
- In the dialog:
- Enter a Name for the variable (and by which to reference it).
- Under CSV file, click the Select CSV file field to open the dialog:

Select the CSV file from either the Current document, Other documents, or click the Import button at the bottom of the dialog to select a CSV file from your device.
Once selected, click the file. It is loaded into the Select CSV file field.
- Fill out the Row and Column fields according to your requirements:
- Access type - Select By index, By index range, By label, or All values.
- Index - If By index is selected, enter a single Index value.
- Min/Max index - When By index range is selected, enter both the minimum and maximum index values.
- Value - If By label is selected, enter the label's string value.
- Regular expression - Check to pattern match a set of target strings.
- Result type - Select Single value, Array, or Map.
- Label column/row index - If the Result type is a Map, enter either a numerical index value or a variable.
- Access type - Select By index, By index range, By label, or All values.
- Check Conversion factor and enter a value with measurement units if you are converting from one measurement to another (for example, if the table uses inches, and you want these values converted to centimeters).

- Optionally, enter a Description to provide some details about the Variable or CSV file being used.
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- Click
.
Examples
All examples below use the following 3x3 CSV table:
-
An array of arrays - Setting Access type: All values and Result type: Array for both Row and Column creates an array of arrays that corresponds to the entire CSV file:
-
Rows indexed by the first column - To index the CSV rows by the values in the first column, select Access type: All values, Result type: Map, and Label column index: 0 for Row. This sets the first column in the table as the map key.
For Column, select Access type: By index range and set the Min/Max index range for the table columns (in the example below, a Min/Max index range of 1/2 is entered, which provides an array for the second and third columns in the table). Finally, select Result type: Array:
-
First column - To access only the first column, set Access type: All values and Result type: Array for Row. Then select Access type: By index (0) and Result type: Single value for Column:
-
First row - To access only the first row, set Access type: By index (0) and Result type: Single value for Row. Then select Access type: All values and Result type: Array for Column:
Notes
-
To view the variable result, hover your mouse over the feature name. A tooltip displays the result from the CSV file:
-
Since a variable is sourced from a table, its Variable type and Value in the Variable table are read-only (the Dimensions variable in the example below):
创建尺寸,在尺寸输入框中输入 # 和变量名称(也可作为表达式的一部分,如下所示):
和
保存尺寸,变量会替换为值,并且会求解表达式(如果适用):
双击尺寸进行编辑时,会显示变量(和表达式):
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.
在 Part Studio 中使用表达式的任何地方均可使用变量。例如,在拉伸或旋转运算中。
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):
接受特征。
编辑特征时,会在数值输入框中显示求解:
在输入框中单击,会显示变量(在适当的情况下,还会显示表达式)。
变量自动填充特征
定义变量后,如果在数值对话框输入框中输入井号标签(或者井号、下划线或任何字母),将打开变量自动填充特征,如下所示:
Left: using a variable in a dialog. Right: using a variable in a dimension field in a sketch.
您可以一直键入内容以筛选列表中的合理选项,使用方向键可在列表中上移和下移,当列表中的变量亮显时,使用 Enter 键可接受该变量。您也可以使用鼠标滚轮来滚动变量列表,并通过单击鼠标来选择特定的变量。请注意,如果未使用井号 (#),FeatrureScript 函数也会包含在列表中:
另请注意以下几点:
- 使用 Esc 键可关闭下拉列表而不选择任何内容。如果从下拉列表中移除焦点,也会将其关闭而不选择任何内容。
- 计算值显示为浅灰色,不会插入这些值(仅插入变量或函数名称)。函数输入也是如此,因为不插入输入占位符名称。
- 仅显示当前使用环境中存在的变量,而不显示特征列表中再往下的变量
变量值可以包含表达式。您可以指定带索引的数组,该索引可以是变量。这样,您可以通过更改索引变量的值来更改变量的值。
要在变量中使用数组,必须先设置基于零的索引。
- 创建一个变量并将名称设置为“config”。选择“数字”作为类型,将 #config 的值设置为“2”。
- 创建一个变量,将名称设置为“diameter”。将 #diameter 的值设置为“[0.25, 0.5, 1][#config]”。
-
创建一个变量,将名称设置为“length”。将 #length 的值设置为“[2, 4, 10][#config]”。
- 创建一个圆。
- 创建一条线。
- 将圆的直径设置为“#diameter”。
因为 #config = 2,所以圆的直径为 1。
- 将线的长度设置为“#length”。
因为 #config = 2,所以线的长度为 10。
第二组方括号 [#config] 充当数组中的索引指针。
更改索引变量的值即可更改所有数组变量的索引。
您可以根据需要创建变量,例如在特征对话框的输入框中输入数字值时。该变量会保存在特征列表中的当前操作之前。
例如,在创建草图的过程中弹出尺寸对话框时,除了输入尺寸的值,您也可以输入 #,系统会显示现有变量的列表,同时显示“新建变量”选项:
选择“新建变量”打开“变量”对话框以创建新变量,然后将其用于当前输入框。(或者如果适用,请从当前变量中选择一个)
通过“创建变量”对话框,您可以为特征(默认)或配置创建变量。
为新变量输入名称和值。单击复选标记以保存新变量并将其用于打开的特征。
根据 FeatureScript 语句,您可以在变量中使用 FeatureScript 函数 。
例如,您可以创建名为“调整”的“任意项目”类型的变量,以保存将指定长度加倍并加上 2.5 mm 的函数,如下所示:
function(len) { return len * 2 + 2.5 mm; }
然后在表达式中参考该变量,例如:
#Adjust(20mm)
- 更改变量的值时(像编辑任何特征一样来编辑变量),使用该变量的所有运算都会自动更新。
- When you change the name of a variable, you have the choice to propagate the change everywhere the variable is used:

选中 “更新所有参考” 旁边的方框。
- 在 Part Studio 中,选择“变量”工具图标 (
) 打开“变量工具”对话框:
- 在对话框中:
- 选择类型:
- 长度 - 代表长度的数值(小数、整数、分数)
- 角度 - 代表角度的数值(小数、整数、分数)
- 数字 - 数值(小数、整数、分数)
- 任意项目 - 上述任意项目,具有不同单位的数值,或 FeatureScript 值,例如布尔、映射、数组、字符串或函数。请参见 https://cad.onshape.com/FsDoc/variables.html#standard-types 及下面的示例。
- 输入变量的名称(参考变量所依据的名称)。
- 输入一个值(可选:长度、角度及任意项目的单位)。
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- 轻按复选标记。
您创建的变量列在特征列表中,显示变量的名称和值。
- While in a Part Studio, select the Variable tool icon (
) to open the Variable tool dialog box, then tap Measured:
- 在对话框中:
- 选择类型:
- 距离 - 表示距离的数值(小数、整数、分数)
- 长度 - 代表长度的数值(小数、整数、分数)
- 直径 - 表示直径的数值(小数、整数、分数)
-
输入变量的名称(参考变量所依据的名称)。
- 选择要测量其间距的图元。
- 根据您的喜好填写剩余输入框。
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- 轻按复选标记。
您创建的变量列在特征列表中,显示变量的名称和值。
The From table option in the Variable feature enables users to create a single variable that stores a subset of rows and columns from the CSV table. The Row options filter the rows and determine the Result type (Single value, Array, or Map) of the variable. The Column options then filter each row and determine the type of each entry in the final variable.
iOS support for creating a Variable from a CSV file (From table) is limited to editing and viewing variables created on the desktop (browser) platform.
- Tap
to open the Variable dialog, then tap From table:
- In the dialog:
- Enter a Name for the variable (and by which to reference it).
- Under CSV file, tap the Select CSV file field to select and open an existing CSV file. It is loaded into the Select CSV file field.
Fill out the Row and Column fields according to your requirements:
- Access type - Select By index, By index range, By label, or All values.
- Index - If By index is selected, enter a single Index value.
- Min/Max index - When By index range is selected, enter both the minimum and maximum index values.
- Value - If By label is selected, enter the label's string value.
- Regular expression - Check to pattern match a set of target strings.
- Result type - Select Single value, Array, or Map.
- Label column/row index - If the Result type is a Map, enter either a numerical index value or a variable.
- Access type - Select By index, By index range, By label, or All values.
- Toggle Conversion factor and enter a value with measurement units if you are converting from one measurement to another (for example, if the table uses inches, and you want these values converted to centimeters).
- Optionally, enter a Description to provide some details about the Variable or CSV file being used.
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- Tap the checkmark.
在 Part Studio 中使用表达式的任何地方均可使用变量。例如,在拉伸或旋转运算中:
像往常一样开始操作(在本例中是旋转),在数值输入框中输入 # 和变量名称(也可作为表达式的一部分)。例如:(#x) deg。
接受特征。
编辑特征时,会在数值输入框中显示求解。例如:4 deg。
在输入框中单击,会显示变量(在适当的情况下,还会显示表达式)。
变量值可以包含表达式,但是必须求得标量值。您可以指定带索引的数组,该索引可以是变量。这样,您可以通过更改索引变量的值来更改变量的值。
要在变量中使用数组,必须先设置基于零的索引。
- 轻按变量工具,将名称设置为“config”。将 #config 的值设置为 2。
- 轻按变量工具,将名称设置为“diameter”。将 #diameter 的值设置为 [0.25, 0.5, 1] [#config]。
- 轻按变量工具,将名称设置为“length”。将 #length 的值设置为 [2, 4, 10] [#config]。
- 轻按草图工具,绘制一个圆的草图。
- 轻按草图工具,绘制一条线的草图。
- 将圆的直径设置为“#diameter”。
- 将线的长度设置为“#length”。
圆的直径设置为 1,线的长度设置为 10。
- 编辑 #config 变量,将值设置为 0。
请务必包含括住“#config”的第二组括号,这样可以使变量 #config 的值可以指向数组中的正确位置。
请务必包含括住“#config”的第二组括号,这样可以使变量 #config 的值可以指向数组中的正确位置。
草图将会更新,圆的直径现在设置为 0.25,线的长度设置为 2。
您可以按照自己的意愿针对任意多个数组执行此操作,数组可以容纳无限个位置(并不是如本例中所示那样仅限于三个值)。
根据 FeatureScript 语句,您可以在变量中使用 FeatureScript 函数 。
例如,您可以创建名为“调整”的“任意项目”类型的变量,以保存将指定长度加倍并加上 2.5 mm 的函数,如下所示:
function(len) { return len * 2 + 2.5 mm; }
然后在表达式中参考该变量,例如:
#Adjust(20mm)
- 更改变量的值时(像编辑任何特征一样来编辑变量),使用该变量的所有运算都会自动更新。
- 变量名称区分大小写。
- 您可以输入变量作为独立的值或作为方程的一部分。
- 您可以在尺寸标注、表达式或特征中使用变量。
-
如果您指定没有单位的变量,表达式也将没有单位或假定工作区单位。但是,如果您明确为变量值添加单位,则任何表达式在编写时也应匹配该单位才能有效。
- When you change the name of a variable, you have the choice to propagate the change everywhere the variable is used:

选择“更新所有参考”。
- While in a Part Studio, select the Variable tool
to open the Variable tool dialog box:
- 在对话框中:
- 选择类型:
- 长度 - 代表长度的数值(小数、整数、分数)
- 角度 - 代表角度的数值(小数、整数、分数)
- 数字 - 数值(小数、整数、分数)
- 任意项目 - 上述任意项目,具有不同单位的数值,或 FeatureScript 值,例如布尔、映射、数组、字符串或函数。请参见 https://cad.onshape.com/FsDoc/variables.html#standard-types 及下面的示例。
- 输入变量的名称(参考变量所依据的名称)。
- 输入一个值(可选:长度、角度及任意项目的单位)。
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- 轻按复选标记。
您创建的变量列在特征列表中,显示变量的名称和值。
- While in a Part Studio, select the Variable tool
to open the Variable tool dialog box, then click Measured:
- 在对话框中:
- 选择类型:
- 距离 - 表示距离的数值(小数、整数、分数)
- 长度 - 代表长度的数值(小数、整数、分数)
- 直径 - 表示直径的数值(小数、整数、分数)
-
输入变量的名称(参考变量所依据的名称)。
- 选择要测量其间距的图元。
- 根据您的喜好填写剩余输入框。
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- 轻按复选标记。
您创建的变量列在特征列表中,显示变量的名称和值。
The From table option in the Variable feature enables users to create a single variable that stores a subset of rows and columns from the CSV table. The Row options filter the rows and determine the Result type (Single value, Array, or Map) of the variable. The Column options then filter each row and determine the type of each entry in the final variable.
Android support for creating a Variable from a CSV file (From table) is limited to editing and viewing variables created on the desktop (browser) platform.
- Tap
to open the Variable dialog, then tap From table:
- In the dialog:
- Enter a Name for the variable (and by which to reference it).
- Under CSV file, tap the Select CSV file field to select and open an existing CSV file. It is loaded into the Select CSV file field.
Fill out the Row and Column fields according to your requirements:
- Access type - Select By index, By index range, By label, or All values.
- Index - If By index is selected, enter a single Index value.
- Min/Max index - When By index range is selected, enter both the minimum and maximum index values.
- Value - If By label is selected, enter the label's string value.
- Regular expression - Check to pattern match a set of target strings.
- Result type - Select Single value, Array, or Map.
- Label column/row index - If the Result type is a Map, enter either a numerical index value or a variable.
- Access type - Select By index, By index range, By label, or All values.
- Toggle Conversion factor and enter a value with measurement units if you are converting from one measurement to another (for example, if the table uses inches, and you want these values converted to centimeters).
- Optionally, enter a Description to provide some details about the Variable or CSV file being used.
Use only English alphanumeric characters and underscores (_) in the Name field. Variable names are case-sensitive and cannot start with a number.
- Tap the checkmark.
在 Part Studio 中使用表达式的任何地方均可使用变量。例如,在拉伸或旋转运算中:
像往常一样开始操作(在本例中是旋转),在数值输入框中输入 # 和变量名称(也可作为表达式的一部分)。例如:(#x) deg。
接受特征。
编辑特征时,会在数值输入框中显示求解。例如:4 deg。
在输入框中单击,会显示变量(在适当的情况下,还会显示表达式)。
变量值可以包含表达式,但是必须求得标量值。您可以指定带索引的数组,该索引可以是变量。这样,您可以通过更改索引变量的值来更改变量的值。
要在变量中使用数组,必须先设置基于零的索引。
- 轻按变量工具,将名称设置为“config”。将 #config 的值设置为 2。
- 轻按变量工具,将名称设置为“diameter”。将 #diameter 的值设置为 [0.25, 0.5, 1] [#config]。
- 轻按变量工具,将名称设置为“length”。将 #length 的值设置为 [2, 4, 10] [#config]。
- 轻按草图工具,绘制一个圆的草图。
- 轻按草图工具,绘制一条线的草图。
- 将圆的直径设置为“#diameter”。
- 将线的长度设置为“#length”。
圆的直径设置为 1,线的长度设置为 10。
- 编辑 #config 变量,将值设置为 0。
请务必包含括住“#config”的第二组括号,这样可以使变量 #config 的值可以指向数组中的正确位置。
请务必包含括住“#config”的第二组括号,这样可以使变量 #config 的值可以指向数组中的正确位置。
草图将会更新,圆的直径现在设置为 0.25,线的长度设置为 2。
您可以针对任意多个数组执行此操作,您的数组可以容纳无限位(并不是如本例中所示那样仅限于三个值)。
根据 FeatureScript 语句,您可以在变量中使用 FeatureScript 函数 。
例如,您可以创建名为“调整”的“任意项目”类型的变量,以保存将指定长度加倍并加上 2.5 mm 的函数,如下所示:
function(len) { return len * 2 + 2.5 mm; }
然后在表达式中参考该变量,例如:
#Adjust(20mm)
- 更改变量的值时(像编辑任何特征一样来编辑变量),使用该变量的所有运算都会自动更新。
- 变量名称区分大小写。
- 您可以输入变量作为独立的值或作为方程的一部分。
- 您可以在尺寸标注、表达式或特征中使用变量。
如需其他学习中心资源,请点击此处的技术简报文章:参数化变量与配置变量(需要 Onshape 帐户)。您还可以注册以下课程:变量和表达式(需要 Onshape 帐户)。
和

