OpenSCAD variables
::开放SCAD变量

In the previous chapters you have made use of variables to parameterize your designs and make them easily customizable. Specifically, you have been assigning them numerical values at some part of your script and then using their stored value in some other part. For example, you can set a wheel_radius variable equal to the desired wheel radius and use that variable in the corresponding statements that create the wheels of your car. This way you can easily customize the radius of your car’s wheels without having to search for and change multiple values, but only by directly changing the value of the wheel_radius variable.
::在前几章中,您使用变量来参数化您的设计,使其易于自定义.具体来说,您在脚本的某个部分赋予它们数值,然后在另一个部分使用它们的存储值.例如,您可以将一个 wheel_radius 变量设置为等于所需的轮半径,并在创建您的汽车轮的相应语句中使用该变量.这样,您可以轻松地自定义您的汽车轮的半径,而不必搜索和更改多个值,只需直接更改 wheel_radius 变量的值.

You also learned about an important property of OpenSCAD variables. This is that a variable can only have one specific value. If you assign one value to a variable and then assign it a different value at a later part of the script, your variable will have only the final value throughout the execution of your design. This is demonstrated on the following example.
::你还了解了 OpenSCAD 变量的一个重要属性. 这就是一个变量只能有一个特定的值. 如果你给一个变量赋予一个值,然后在脚本的后面部分赋予它一个不同的值,你的变量在整个设计执行过程中只会有最终的值. 这在下面的示例中得到了证明.

Code

two_cylinder_with_same_radius.scad
::两个圆柱体的半径相同

$fa=1;
$fs=0.4;
height=10;
radius=5;
cylinder(h=height,r=radius);
radius=10;
translate([30,0,0])
    cylinder(h=height,r=radius);

Both cylinders have a radius of 10 units, which is the last value that is assigned to the radius variable.
::两个圆柱体的半径为10单位,这是半径变量所赋予的最后一个值.

When variables store numerical values, they can be used to specify dimensions of different objects or define transformation commands. Numerical values aren’t the only kind of values that can be assigned to a variable. Variables can also hold boolean values (true or false) as well as characters (a, b, c, d, …). As you are going to see on the following topics, by using boolean or character variables you can further parameterize your models and modules.
::当变量存储数值时,它们可以用来指定不同对象的维度或定义转换命令.数值不是唯一可以赋予变量的值.变量也可以包含布尔值 (true或false),以及字符 (a,b,c,d,......).正如你将在以下主题中看到的,通过使用布尔或字符变量,你可以进一步参数化你的模型和模块.

Conditional variable assignment
::条件变量赋值

So far you have been assigning specific values to variables using appropriate assignment commands. There are cases, though, where you would prefer the assignment itself to be parametric and dependent on some aspect of your design.
::现在,你已经使用适当的赋值命令来赋值变量.然而,有时候你更喜欢赋值本身是参数性的,并且取决于你的设计的某些方面.

The creation of a car’s body requires the definition of various parameters. These parameters can be defined when calling the body module by using corresponding variables that have been defined in your script. One example of this is the following.
::创建汽车车身需要定义各种参数.这些参数可以在调用车身模块时通过使用您脚本中定义的相应变量来定义.以下是其中的一个例子.

Code

parameterized_car_body.scad
::参数化_汽车_车身.scad

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;
base_length = 60;
top_length = 30;
top_offset = 5;
body(base_length=base_length, top_length=top_length, top_offset=top_offset);

The above version of the car’s body will be called the short version. By choosing different values for the variables a long version can also be created.
::上述车身版本将被称为短版本.通过选择不同的变量值,也可以创建长版本.

Code

long_car_body.scad
::长_汽车_车身.scad

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;
base_length = 80;
top_length = 50;
top_offset = 10;
body(base_length=base_length, top_length=top_length, top_offset=top_offset);

What if these two versions of the car’s body are the only versions that you currently interested in? Is there a way to quickly switch between these two versions without having to modify each variable separately?
::如果这两种车身版本是你目前唯一感兴趣的版本呢? 有没有办法快速切换这两种版本,而不必单独修改每个变量?

You may think modifying three variables isn’t much work, but the number of required variables on more complex models can easily get unmanageable. Luckily there is a solution to this problem, which is the conditional assignment of variables. The conditional assignment of variables is a way to instruct OpenSCAD to assign different values to variables depending on whether some condition is true or false. In this case the condition is whether the car’s body should be long or not. You can represent this condition by defining a long_body variable and setting it equal to true if you want the body to be long or equal to false if you don’t want the body to be long.
::你可能认为修改三个变量不是很大的工作,但在更复杂的模型中,所需变量的数量很容易变得无法管理.幸运的是,这个问题有一个解决方案,即变量有条件赋值.变量有条件赋值是指导OpenSCAD根据某种条件是否真或假赋值变量不同的值的一种方式.在这种情况下,条件是车的车身是否长.你可以通过定义long_body变量来表示这个条件,并设置它等于true,如果你希望车身长,或者等于false,如果你不希望车身长.

The choice of a long body is represented by the following statement.
::长身的选择是由以下声明表示的.

Code
long_body = true;

Respectively the choice of a short body is represented by the following statement.
::选择一个短的身体是由以下声明表示.

Code
long_body = false;

The long_body variable is called a boolean variable because boolean values (true or false) are assigned to it. The next step is the definition of the conditional assignments which will assign the appropriate values to base_length, top_length and top_offset variables depending on the value of the long_body variable. These conditional assignments can be defined in the following manner.
::long_body变量被称为布尔变量,因为它被赋予了布尔值 (true或false).下一步是定义条件赋值,根据 long_body变量的值将赋予 base_length, top_length 和 top_offset 变量适当的值.这些条件赋值可以按照以下方式定义.

Code
base_length = (long_body) ? 80:60;
top_length = (long_body) ? 50:30;
top_offset = (long_body) ? 10:5;

You should notice the following points about the definition of a conditional assignment. First the name of variable is typed out followed by the equal sign. Then follows a pair of parentheses that contains the condition which will be used in the conditional assignment. The condition in this case is a boolean variable. In general, the condition can also be a combination of logical and comparison operations between multiple variables. After the closing parenthesis follow a question mark and the two corresponding variable values that are separated by a colon. If the supplied condition is true, the first value will be the one assigned to the variable. If the supplied condition is false, the second values will be the one assigned to the variable.
::关于条件赋值的定义,你应该注意以下几点.首先是输入变量的名称,然后是等号.然后是包含在条件赋值中使用的条件的括号.在这种情况下,条件是布尔变量.一般来说,条件也可以是多个变量之间的逻辑和比较操作的组合.在闭括号之后,一个问号和两个相应的变量值以两位素隔开.如果提供的条件是真,第一个值将是分配给变量的值.如果提供的条件是错误的,第二个值将是分配给变量的值.

By incorporating the above conditional assignments in your script, you can switch between a short and a long car body just by changing the long_body variable from false to true and vice versa.
::通过将上述条件分配纳入脚本, 您可以通过将long_body变量从false改为true, 换个方向, 切换短车身和长车身.

Code

car_with_normal_conditional_body.scad
::车辆_有_正常_条件_车身.scad

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;

// Conditional assignment of body variables
long_body = false;
base_length = (long_body) ? 80:60;
top_length = (long_body) ? 50:30;
top_offset = (long_body) ? 10:5;
// Creation of body
body(base_length=base_length, top_length=top_length, top_offset=top_offset);

// Creation of wheels and axles
track = 30;
wheelbase = 40;
wheel_radius = 8;
wheel_width = 4;
// Front left wheel 
translate([-wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
 // Front right wheel 
translate([-wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear left wheel 
translate([wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear right wheel 
translate([wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Front axle 
translate([-wheelbase/2,0,0])
    axle(track=track); 
// Rear axle 
translate([wheelbase/2,0,0])
    axle(track=track);

Code

car_with_long_conditional_body.scad
::车身长度有条件的车

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;

// Conditional assignment of body variables
long_body = true;
base_length = (long_body) ? 80:60;
top_length = (long_body) ? 50:30;
top_offset = (long_body) ? 10:5;
// Creation of body
body(base_length=base_length, top_length=top_length, top_offset=top_offset);

// Creation of wheels and axles
track = 30;
wheelbase = 40;
wheel_radius = 8;
wheel_width = 4;
// Front left wheel 
translate([-wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
 // Front right wheel 
translate([-wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear left wheel 
translate([wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear right wheel 
translate([wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Front axle 
translate([-wheelbase/2,0,0])
    axle(track=track); 
// Rear axle 
translate([wheelbase/2,0,0])
    axle(track=track);

Exercise
Add a large_wheels variable to the previous example. The variable should only take boolean values. Add two conditional assignments that assign different values to the wheel_radius and wheel_width variables. The large_wheels variable should be used as the condition for both assignments. If the large_wheels variable is false, the wheel_radius and wheel_width variables should be set equal to 8 and 4 units respectively. If the large_wheels variable is true, the wheel_radius and wheel_width variables should be set equal to 10 and 8 units respectively. Set appropriate values to the long_body and large_wheels variables to create the following versions of the car: short body - large wheels, short body - small wheels, long body - large wheels, long body - small wheels.
  • short body - large wheels
    ::车身短,车轮大
Code

car_with_short_body_and_large_wheels.scad
::车_有_短_车身_和_大_轮.

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;

// Conditional assignment of body variables
long_body = false;
base_length = (long_body) ? 80:60;
top_length = (long_body) ? 50:30;
top_offset = (long_body) ? 10:5;
// Creation of body
body(base_length=base_length, top_length=top_length, top_offset=top_offset);

// Creation of wheels and axles
large_wheels = true;
wheel_radius = (large_wheels) ? 10:6;
wheel_width = (large_wheels) ? 8:4;
track = 30;
wheelbase = 40;
// Front left wheel 
translate([-wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
 // Front right wheel 
translate([-wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear left wheel 
translate([wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear right wheel 
translate([wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Front axle 
translate([-wheelbase/2,0,0])
    axle(track=track); 
// Rear axle 
translate([wheelbase/2,0,0])
    axle(track=track);

  • short body - small wheels
    ::车身短 - 小轮
Code

car_with_short_body_and_small_wheels.scad
::车身短,车轮小

…
long_body = false;
large_wheels = false;
…

  • long body - large wheels
    ::长长的车身 - 大的轮子
Code

car_with_long_body_and_large_wheels.scad
::车_长_机身_大_轮.

…
long_body = true;
large_wheels = true;
…

  • long body - small wheels
    ::长长的车身 - 小小的车轮
Code

car_with_long_body_and_small_wheels.scad
::车_长_机身_小_轮.

…
long_body = true;
large_wheels = false;
…

More conditional variable assignments
::更多的条件变量赋值

The conditional assignment of variables can also be used with a properly adjusted syntax when there are more than two cases between which you would like to choose. In the previous example there were only two options for the body (short or long) and a boolean variable (long_body) was used to choose between those two options.
::当你想要选择的情况超过两个时,也可以使用适当调整的语法来分配变量.在上一个例子中,只有两个选择的体 (短或长) 和一个布尔变量 (long_body) 被用来选择这两个选项.

What if you want to be able to choose between four versions of the body (short, long, rectangular and normal)? A boolean variable can’t be used to represent your choice of body version since it can only have two values (true or false). For this reason, you are going to use a character to represent your choice of body.
::如果您想要在四个版本的体 (短,长,矩形和正常) 之间进行选择呢? 布尔变量不能用来表示您选择的体版本,因为它只能有两个值 (true或false). 因此,您将使用一个字符来表示您选择的体.

The choice of a short body will be represented by the character s.
::选择一个短的身体将由字符s表示.

Code
body_version = "s";

The choice of a long body will be represented by the character l.
::长体的选择将以字符l表示.

Code
body_version = "l";

The choice of a rectangular body will be represented by the character r.
::选择一个矩形体将由字符r表示.

Code
body_version = "r";

The choice of a normal body will be represented by the character n.
::选择一个正常的体将由字符n表示.

Code
body_version = "n";

The conditional assignments when there are more than two options should take the following form.
::如果有两个以上的选项,则条件分配应采用以下形式.

Code
// base_length
base_length =
(body_version == "l") ? 80:
(body_version == "s") ? 60:
(body_version == "r") ? 65:70;

// top_length
top_length =
(body_version == "l") ? 50:
(body_version == "s") ? 30:
(body_version == "r") ? 65:40;

// top_offset
top_offset =
(body_version == "l") ? 10:
(body_version == "s") ? 5:
(body_version == "r") ? 0:7.5;

You should notice the following points about the definition of a conditional assignment when there are more than two options. First the name of the variable is typed out followed by the equal sign. Then follows a pair of parentheses that contains a condition, then a question mark, then the value to be assigned if the condition is true and then a colon. The previous sequence is repeated as required depending on the number of different available body versions. The last sequence is slightly different as it has an additional value which will be used as the default value when none of the conditions are true. In this case the default value corresponds to the normal version of the body. This is the reason why the character n that corresponds to the normal version of the body doesn’t participate in any condition. Another thing you should notice is that the conditions are now comparison operations, specifically equality comparisons. If the value of the body_version variable is equal to the character that follows the double equal sign, then the condition is true and the corresponding value that follows the condition will be assigned to the variable.
::当有两个以上的选项时,你应该注意以下关于条件赋值的定义.首先是输入变量的名称,然后是等号.然后是包含条件的括号,然后是问号,然后是条件是真的,然后是值.根据不同的可用体版本的数量,根据需要重复前面的序列.最后的序列略有不同,因为它有一个额外的值,当条件都不真时将被用作默认值.在这种情况下,默认值与体的正常版本相对应.这就是为什么与体的正常版本相对应的n字符不参与任何条件.另一个你应该注意的是,条件现在是比较操作,特别是等式比较.如果体_version变量的值与真字符相等,则相对应的符号是双,并且条件是等,则值将被

By incorporating the above conditional assignments in your script, you can switch between a short, a long, a rectangular and a normal car body just by setting the body_version equal to the character s, l, r or n respectively.
::通过将上述条件赋值纳入您的脚本, 您可以在短,长,矩形和普通车身之间切换, 只需将body_version设置为分别等于s,l,r或n的字符.

Code

car_with_long_body_version.scad
::车型: 长型车型

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;

// Conditional assignment of body variables
body_version = "l";

// base_length
base_length =
(body_version == "l") ? 80:
(body_version == "s") ? 60:
(body_version == "r") ? 65:70;

// top_length
top_length =
(body_version == "l") ? 50:
(body_version == "s") ? 30:
(body_version == "r") ? 65:40;

// top_offset
top_offset =
(body_version == "l") ? 10:
(body_version == "s") ? 5:
(body_version == "r") ? 0:7.5;

// Creation of body
body(base_length=base_length, top_length=top_length, top_offset=top_offset);

// Creation of wheels and axles
large_wheels = false;
wheel_radius = (large_wheels) ? 10:6;
wheel_width = (large_wheels) ? 8:4;
track = 30;
wheelbase = 40;
// Front left wheel 
translate([-wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
 // Front right wheel 
translate([-wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear left wheel 
translate([wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear right wheel 
translate([wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Front axle 
translate([-wheelbase/2,0,0])
    axle(track=track); 
// Rear axle 
translate([wheelbase/2,0,0])
    axle(track=track);

Code

car_with_short_body_version.scad
::车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型:

…
body_version = "s";
…

Code

car_with_rectangular_body_version.scad
::车型为: 车型为: 车型为: 车型为: 车型为: 车型为: 车型为: 车型为: 车型为: 车型为: 车型为: 车型为: 车型为:

…
body_version = "r";
…

Code

car_with_normal_body_version.scad
::车辆_与_正常_车身_版本.scad

…
body_version = "n";
…

Exercise
Add a wheels_version variable to the previous example. The variable should only take character values. Add appropriate conditional assignments that assign different values to the wheel_radius and wheel_width variables. The wheels_version variable should be used as the condition for both assignments. If the value of the wheels_version variable is the character s (small), the wheel_radius and wheel_width variables should be set equal to 8 and 4 units respectively. If the value of the wheels_version variable is the character m (medium), the wheel_radius and wheel_width variables should be set equal to 9 and 6 units respectively. If the value of the wheels_version variable is the character l (large), the wheel_radius and wheel_width variables should be set equal to 10 and 8 units respectively. The case of the small version of the wheels should be used as the default case of the conditional assignments. Set appropriate values to the body_version and wheels_version variables to create the following versions of the car: short body - medium wheels, rectangular body - large wheels, normal body - small wheels.
  • short body - medium wheels
    ::短车身 - 中等轮
Code

car_with_short_body_and_medium_wheels.scad
::车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车型: 车

…
body_version = "s"; //s-short, n-normal, l-large, r-rectangular
…
wheels_version = "m"; //s-small, m-medium, l-large 
wheel_radius =
(wheels_version == "l") ? 10:
(wheels_version == "m") ? 8:6;
wheel_width =
(wheels_version == "l") ? 8:
(wheels_version == "m") ? 6:4;
…

  • rectangular body - large wheels
    ::长方形车身 - 大轮
Code

car_with_rectangular_body_and_large_wheels.scad
::车_有_长方形_车身_和_大_轮.

…
body_version = "r"; //s-short, n-normal, l-large, r-rectangular
…
wheels_version = "l"; //s-small, m-medium, l-large 
…

  • normal body - small wheels
    ::标准车身 - 小轮
Code

car_with_normal_body_and_small_wheels.scad
::车_与_正常_机身_和_小_轮.

…
body_version = "n"; //s-short, n-normal, l-large, r-rectangular
…
wheels_version = "s"; //s-small, m-medium, l-large 
…

Conditional creation of objects - If statement
::条件创建对象 - 如果声明

Conditional assignment of variables is a great tool to easily navigate between different but specific versions of your model. Using conditional assignments, you were able to define different body and wheel sizes for your car and effortlessly choose between them without having to manually provide the values for all involved variables every single time.
::条件赋值变量是很好的工具,可以轻松地在不同但特定版本的模型之间进行导航.使用条件赋值,您可以为您的汽车定义不同的车身和轮子大小,并且轻松地在它们之间进行选择,而不必每次手动提供所有相关变量的值.

What if you wanted to have the same control over the type of wheel (ex. simple, round, complex) or body (ex. square, round)? What would this require? In order to achieve this, you would need to have conditional creation of objects, which can be achieved with the use of the if statement.
::如果想要对轮 (例如简单,圆,复杂) 或体 (例如方形,圆) 的类型有相同的控制,那么需要什么?为了实现这一点,你需要有条件的对象创建,这可以通过使用if语句来实现.

Before you go into customizing the type of wheel and body, you can get familiar with if statements with some shorter examples. Recall the car body module that you created in a previous chapter. The module has some input parameters which are used to create two cubes, one cube for the body’s base and one for the body’s top.
::在开始定制轮子和车身类型之前,你可以通过一些更短的例子熟悉 if 语句.请回想一下你在上一章中创建的汽车车身模块.该模块有一些输入参数,用于创建两个立方体,一个立方体用于车身底部,一个用于车身顶部.

Code

car_body_from_module.scad
::车身来自模块.scad

module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5) {
    // Car body base 
    cube([base_length,width,base_height],center=true); 
    // Car body top 
    translate([top_offset,0,base_height/2+top_height/2])
        cube([top_length,width,top_height],center=true);
}
$fa = 1;
$fs = 0.4;
body();

Using an if statement you are going to see how the creation of the body’s top can be parameterized. First you need to define an additional input parameter for the module. This parameter will be named top and will hold boolean values. If this parameter is false, the module should create only the base of the body. If it’s true, it should also create the top of the body. This can be achieved by using an if statement in the following way.
::使用 if语句,你将看到如何参数化创建 bodys顶部.首先,你需要为模块定义一个额外的输入参数.这个参数将被命名为 top,并将包含布尔值.如果这个参数为 false,模块应该只创建 body 的底部.如果它是 true,它也应该创建 body 的顶部.这可以通过使用 if语句来实现.

Code
module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5, top) {
    // Car body base 
    cube([base_length,width,base_height],center=true); 
    // Car body top
    if (top) {
        translate([top_offset,0,base_height/2+top_height/2])
        cube([top_length,width,top_height],center=true);
    }
}
$fa = 1;
$fs = 0.4;

You should notice the following points regarding the definition of the if statement. First the if keyword is typed out and then follows a pair of parentheses. Inside of the parentheses the condition that will dictate whether the if statement will be executed is defined. Lastly, there is a pair of curly brackets inside of which exist all statements that will be executed if the supplied condition is true. In this case the supplied condition is the boolean variable top, which represents your choice to create a car body that does or doesn’t have a top part. The statement that is placed inside the curly brackets is the statement that creates the top part of the car’s body.
::关于 if 语句的定义,你应该注意以下几点.首先是 if 关键字被输入,然后是 括号.括号内定义了将 if 语句执行的条件.最后,有一个曲括号,如果所提供的条件是真实的,则所有语句都存在其中.在这种情况下,所提供的条件是布尔变量顶部,它代表您选择创建具有或没有顶部部分的汽车车身.在曲括号内放置的语句是创建汽车车身顶部部分的语句.如果该语句是真实的,则该语句将被执行.如果该语句是真实的,则该语句将被执行.如果该语句是真实的,则该语句将被执行.如果该语句是真实的,则该语句将被执行.如果该语句是真

This particular form of if statement is known as a simple if statement. This means that if the condition is true then the corresponding commands are executed, otherwise nothing happens. There are two other forms of the if statement that will be covered later, but first take a moment to investigate how the new body module works in practice.
::这种特殊的 if 语句形式被称为简单的 if 语句.这意味着如果条件是真实的,则相应的命令被执行,否则什么也不会发生.还有另外两种形式的 if 语句,我们将稍后讨论,但首先请花点时间来研究新体模块在实践中如何工作.

When the input parameter top is set to false, only the base of the body is created.
::当输入参数顶部设置为false时, 只有体的底部才会被创建.

Code

car_body_without_top.scad
::没有顶部的车_车身_车身_车顶

…
body(top=false);
…

When it’s set to true, both the base and the top of the body are created.
::它们的基础和顶部都被创建.

Code

car_body_with_top.scad
::车身与顶部的车

…
body(top=true);
…

Exercise
Take a look at the following car body module. You will notice that the module has been modified to include the creation of a rear bumper. The color command that has been applied to the bumper simply adds a visual effect during preview. In this case, the color command is used to draw your attention on the newly added part; it shouldn’t bother you any further than that.
Code

car_body_with_rear_bumper.scad
::车身与后面的保险

module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5, top) {
    // Car body base 
    cube([base_length,width,base_height],center=true); 
    // Car body top
    if (top) {
        translate([top_offset,0,base_height/2+top_height/2])
            cube([top_length,width,top_height],center=true);
    }
    // Rear bumper
    color("blue") {
        translate([base_length/2,0,0])rotate([90,0,0]) {
            cylinder(h=width - base_height,r=base_height/2,center=true);
            translate([0,0,(width - base_height)/2])
                sphere(r=base_height/2);
            translate([0,0,-(width - base_height)/2])
                sphere(r=base_height/2);
        }
    }
}
$fa = 1;
$fs = 0.4;
body(top=true);

Exercise
To create the front bumper, copy and paste the statements that create the rear bumper and modify the translation statement accordingly.
Code

car_body_with_front_and_rear_bumper.scad
::车身与前后冲击器

module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5, top) {
    // Car body base 
    cube([base_length,width,base_height],center=true); 
    // Car body top
    if (top) {
        translate([top_offset,0,base_height/2+top_height/2])
            cube([top_length,width,top_height],center=true);
    }
    // Front bumper
    color("blue") {
        translate([-base_length/2,0,0])rotate([90,0,0]) {
            cylinder(h=width - base_height,r=base_height/2,center=true);
            translate([0,0,(width - base_height)/2])
                sphere(r=base_height/2);
            translate([0,0,-(width - base_height)/2])
                sphere(r=base_height/2);
        }
    }
    // Rear bumper
    color("blue") {
        translate([base_length/2,0,0])rotate([90,0,0]) {
            cylinder(h=width - base_height,r=base_height/2,center=true);
            translate([0,0,(width - base_height)/2])
                sphere(r=base_height/2);
            translate([0,0,-(width - base_height)/2])
                sphere(r=base_height/2);
        }
    }
}
$fa = 1;
$fs = 0.4;
body(top=true);

Exercise
Define two additional input parameters for the body module. One named front_bumper and one named rear_bumper. Keeping in mind that these parameters should take boolean values define two if statements that conditionally create the front and rear bumpers. The front bumper should be created only if the front_bumper input parameter is true, the second bumper accordingly. Use the body module to create the following car bodies: base only - front and rear bumper, base and top - front bumper, base and top - front and rear bumper.
  • base only - front and rear bumper
    ::只有底座 - 前和后保险
Code

body_without_top_with_front_and_rear_bumper.scad
::车身_没有_顶部_带_前_后_防撞器.

module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5, top, front_bumper, rear_bumper) {
    // Car body base 
    cube([base_length,width,base_height],center=true); 
    // Car body top
    if (top) {
        translate([top_offset,0,base_height/2+top_height/2])
        cube([top_length,width,top_height],center=true);
    }
    // Front bumper
    if (front_bumper) {
        color("blue") {
            translate([-base_length/2,0,0])rotate([90,0,0]) {
                cylinder(h=width - base_height,r=base_height/2,center=true);
                translate([0,0,(width - base_height)/2])
                    sphere(r=base_height/2);
                translate([0,0,-(width - base_height)/2])
                    sphere(r=base_height/2);
            }
        }
    }
    // Rear bumper
    if (rear_bumper) {
        color("blue") {
            translate([base_length/2,0,0])rotate([90,0,0]) {
                cylinder(h=width - base_height,r=base_height/2,center=true);
                translate([0,0,(width - base_height)/2])
                    sphere(r=base_height/2);
                translate([0,0,-(width - base_height)/2])
                    sphere(r=base_height/2);
            }
        }
    }
}
$fa = 1;
$fs = 0.4;
body(top=false,front_bumper=true,rear_bumper=true);

  • base and top - front bumper
    ::底部和顶部 - 前保险
Code

body_with_top_with_front_bumper.scad
::车身_与_顶部_与_前面_保险.scad

…
body(top=true,front_bumper=true,rear_bumper=false);
…

  • base and top - front and rear bumper
    ::底部和顶部 - 前部和后部保险
Code

body_with_top_with_front_and_rear_bumper.scad
::车身_与_顶部_与_前_后_防撞器.

…
body(top=true,front_bumper=true,rear_bumper=true);
…

Challenge
::挑战

In this chapter you learned about conditional assignment of variables and simple if statements. Specifically, you learned how to conditionally modify dimensions and transformations of parts of your designs as well as how to conditionally include or exclude parts from them. It’s time to put these two together in a single car model.
::在本章中,你学到了变量和简单的if语句的条件赋值.具体来说,你学到了如何条件修改设计部分的维度和转换以及如何条件包含或排除部分.现在是时候把这两者放在一起,成为一个单一的汽车模型.

Exercise
If you have been following along this tutorial you should have a vehicle_parts.scad script on your machine from a previous chapter. Open this script and update the body module according to the last example so that it has the ability to conditionally create the top of the body as well as a front and a rear bumper. Set default values for the newly added input parameters. Specifically set true, false and false as the default value of the top, front_bumper and rear_bumper variable accordingly. Save the changes and close the script.
Code
…
module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5, top=true, front_bumper=false, rear_bumper=false) {
    // Car body base 
    cube([base_length,width,base_height],center=true); 
    // Car body top
    if (top) {
        translate([top_offset,0,base_height/2+top_height/2])
            cube([top_length,width,top_height],center=true);
    }
    // Front bumper
    if (front_bumper) {
        color("blue") {
            translate([-base_length/2,0,0])rotate([90,0,0]) {
                cylinder(h=width - base_height,r=base_height/2,center=true);
                translate([0,0,(width - base_height)/2])
                    sphere(r=base_height/2);
                translate([0,0,-(width - base_height)/2])
                    sphere(r=base_height/2);
            }
        }
    }
    // Rear bumper
    if (rear_bumper) {
        color("blue") {
            translate([base_length/2,0,0])rotate([90,0,0]) {
                cylinder(h=width - base_height,r=base_height/2,center=true);
                translate([0,0,(width - base_height)/2])
                    sphere(r=base_height/2);
                translate([0,0,-(width - base_height)/2])
                    sphere(r=base_height/2);
            }
        }
    }
}
…
Exercise
Given the following script that creates a car, make appropriate additions and modifications to the script in order to parameterize conditionally the design of the car. Specifically, you will need to define a body_version, a wheels_version, a top, a front_bumper and a rear_bumper variable that will be used for making design choices regarding the car’s design. If necessary, review the previous examples and exercises of this chapter to remember what effect these variables should have on the design of the car and how to implement them. Use the resulting script to create a version of the car that you like.
  • Given script
    ::给定的脚本
Code

basic_car_script.scad
::基本的_汽车_脚本.scad

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;

// Variables
track = 30;
wheelbase=40;

// Body
body();
// Front left wheel 
translate([-wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel();
 // Front right wheel 
translate([-wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel();
// Rear left wheel 
translate([wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel();
// Rear right wheel 
translate([wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel();
// Front axle 
translate([-wheelbase/2,0,0])
    axle(); 
// Rear axle 
translate([wheelbase/2,0,0])
    axle();
  • Modified script
    ::修改的脚本
Code

car_from_highly_parameterized_script.scad
::车_从_高度_参数化_脚本.scad

use <vehicle_parts.scad>
$fa=1;
$fs=0.4;

// Variables
body_version = "l"; //s-short, n-normal, l-large, r-rectangular
wheels_version = "l"; //s-small, m-medium, l-large
top = true;
front_bumper = true;
rear_bumper = true;
track = 30;
wheelbase=40;

// Conditional assignments
// Body: base_length
base_length =
(body_version == "l") ? 80:
(body_version == "s") ? 60:
(body_version == "r") ? 65:70;

// Body: top_length
top_length =
(body_version == "l") ? 50:
(body_version == "s") ? 30:
(body_version == "r") ? 65:40;

// Body: top_offset
top_offset =
(body_version == "l") ? 10:
(body_version == "s") ? 5:
(body_version == "r") ? 0:7.5;

// Wheels: radius
wheel_radius =
(wheels_version == "l") ? 10:
(wheels_version == "m") ? 8:6;

// Wheels: width
wheel_width =
(wheels_version == "l") ? 8:
(wheels_version == "m") ? 6:4;

// Body
body(base_length=base_length, top_length=top_length, top_offset=top_offset, top=top, front_bumper=front_bumper, rear_bumper=rear_bumper);
// Front left wheel 
translate([-wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
 // Front right wheel 
translate([-wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear left wheel 
translate([wheelbase/2,-track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Rear right wheel 
translate([wheelbase/2,track/2,0])
    rotate([0,0,0])
    simple_wheel(wheel_radius=wheel_radius, wheel_width=wheel_width);
// Front axle 
translate([-wheelbase/2,0,0])
    axle(track=track); 
// Rear axle 
translate([wheelbase/2,0,0])
    axle(track=track);


Last modified: Thursday, 20 March 2025, 12:57 PM