跳轉到內容

OpenSCAD 教程/第 6 章

來自華夏公益教科書

OpenSCAD 變數

[編輯 | 編輯原始碼]

在前面的章節中,您已經使用變數來引數化您的設計並使它們易於定製。具體來說,您已在指令碼的某個部分為它們分配數值,然後在指令碼的另一個部分使用它們儲存的值。例如,您可以將 wheel_radius 變數設定為所需的輪子半徑,並在建立汽車輪子的相應語句中使用該變數。這樣,您就可以輕鬆地自定義汽車輪子的半徑,而不必搜尋和更改多個值,只需直接更改 wheel_radius 變數的值即可。

您還了解了 OpenSCAD 變數的一個重要屬性。那就是一個變數只能有一個特定值。如果您為一個變數分配一個值,然後在指令碼的後面部分為它分配另一個值,那麼您的變數在整個設計執行過程中將只有最終值。以下示例說明了這一點。

程式碼

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);

兩個圓柱體的半徑均為 10 個單位,這是分配給 radius 變數的最後一個值。

當變數儲存數值時,它們可用於指定不同物件的尺寸或定義轉換命令。數值不是可以分配給變數的唯一型別的值。變數還可以儲存布林值(真或假)以及字元 (a, b, c, d, …) 。正如您將在以下主題中看到的那樣,透過使用布林或字元變數,您可以進一步引數化您的模型和模組。

條件變數賦值

[編輯 | 編輯原始碼]

到目前為止,您一直使用適當的賦值命令為變數分配特定值。但是,在某些情況下,您可能更希望賦值本身是引數化的,並且取決於設計的某些方面。

建立汽車車身需要定義各種引數。這些引數可以在呼叫車身模組時定義,方法是使用在指令碼中定義的相應變數。以下是一個示例。

程式碼

parameterized_car_body.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);

以上版本的汽車車身將被稱為短版本。透過選擇變數的不同值,也可以建立長版本。

程式碼

long_car_body.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);

如果這兩個版本的汽車車身是您目前感興趣的唯一版本?有沒有一種方法可以快速切換這兩個版本,而不必分別修改每個變數?

您可能認為修改三個變數並不多,但對於更復雜的模型,所需的變數數量很容易變得難以管理。幸運的是,這個問題有一個解決方案,那就是條件變數賦值。條件變數賦值是一種指示 OpenSCAD 根據某個條件是否為真或假,為變數分配不同值的方法。在本例中,該條件是汽車車身是否應該很長。您可以透過定義一個 long_body 變數來表示此條件,如果希望車身很長,則將其設定為 true,如果不想讓車身很長,則將其設定為 false。

長車身的選項由以下語句表示。

程式碼
long_body = true;

相應地,短車身的選項由以下語句表示。

程式碼
long_body = false;

long_body 變數稱為布林變數,因為它被分配了布林值(真或假)。下一步是定義條件賦值,這些賦值將根據 long_body 變數的值,為 base_length、top_length 和 top_offset 變數分配適當的值。這些條件賦值可以用以下方式定義。

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

您應該注意條件賦值定義的以下幾點。首先鍵入變數名稱,然後是等號。然後是一對括號,其中包含將用於條件賦值的條件。在本例中,該條件是一個布林變數。通常,該條件還可以是多個變數之間邏輯和比較運算的組合。在閉合括號之後,是一個問號和兩個相應的變數值,用冒號分隔。如果提供的條件為真,則第一個值將是分配給變數的值。如果提供的條件為假,則第二個值將是分配給變數的值。

透過將以上條件賦值合併到您的指令碼中,您可以僅透過將 long_body 變數從 false 更改為 true,反之亦然,來在短車身和長車身之間切換。

程式碼

car_with_normal_conditional_body.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);

程式碼

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);

練習
在前面的示例中新增一個 large_wheels 變數。該變數應該只接受布林值。新增兩個條件賦值,為 wheel_radius 和 wheel_width 變數分配不同的值。large_wheels 變數應該用作兩個賦值的條件。如果 large_wheels 變數為 false,則 wheel_radius 和 wheel_width 變數應該分別設定為 8 和 4 個單位。如果 large_wheels 變數為 true,則 wheel_radius 和 wheel_width 變數應該分別設定為 10 和 8 個單位。為 long_body 和 large_wheels 變數設定適當的值,以建立以下版本的汽車:短車身 - 大輪子,短車身 - 小輪子,長車身 - 大輪子,長車身 - 小輪子。
  • 短車身 - 大輪子
程式碼

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);

  • 短車身 - 小輪子
程式碼

car_with_short_body_and_small_wheels.scad

…
long_body = false;
large_wheels = false;
…

  • 長車身 - 大輪子
程式碼

car_with_long_body_and_large_wheels.scad

…
long_body = true;
large_wheels = true;
…

  • 長車身 - 小輪子
程式碼

car_with_long_body_and_small_wheels.scad

…
long_body = true;
large_wheels = false;
…

更多條件變數賦值

[編輯 | 編輯原始碼]

條件變數賦值也可以使用適當調整的語法,當您希望在其中進行選擇時,有兩種以上的情況。在前面的示例中,車身只有兩種選擇(短或長),並且使用了一個布林變數 (long_body) 來在這兩種選擇之間進行選擇。

如果您希望能夠在四個版本的車身(短、長、矩形和正常)之間進行選擇怎麼辦?布林變數不能用於表示您的車身版本選擇,因為它只能有兩個值(真或假)。因此,您將使用一個字元來表示您的車身選擇。

短車身的選項將由字元 s 表示。

程式碼
body_version = "s";

長車身的選項將由字元 l 表示。

程式碼
body_version = "l";

矩形車身的選項將由字元 r 表示。

程式碼
body_version = "r";

普通車身的選項將由字元 n 表示。

程式碼
body_version = "n";

當存在兩種以上選項時,條件賦值應採用以下形式。

程式碼
// 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;

您應該注意當存在兩種以上選項時,條件賦值定義的以下幾點。首先鍵入變數名稱,然後是等號。然後是一對括號,其中包含一個條件,然後是一個問號,然後是如果條件為真將分配的值,然後是一個冒號。根據可用車身版本的數量,前面的序列會重複。最後一個序列略有不同,因為它有一個附加的值,該值將在所有條件都不為真的情況下用作預設值。在本例中,預設值對應於車身的普通版本。這就是為什麼對應於車身普通版本的字元 n 不參與任何條件的原因。您應該注意的另一件事是,條件現在是比較運算,特別是相等比較。如果 body_version 變數的值等於雙等號後面的字元,則該條件為真,並且該條件後面的相應值將被分配給該變數。

透過將以上條件賦值合併到您的指令碼中,您可以僅透過將 body_version 分別設定為字元 s、l、r 或 n 來在短車身、長車身、矩形車身和普通車身之間切換。

程式碼

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);

程式碼

car_with_short_body_version.scad

…
body_version = "s";
…

程式碼

car_with_rectangular_body_version.scad

…
body_version = "r";
…

程式碼

car_with_normal_body_version.scad

…
body_version = "n";
…

練習
在前面的例子中新增一個 `wheels_version` 變數。該變數只應接受字元值。新增相應的條件賦值語句,根據 `wheels_version` 變數的值為 `wheel_radius` 和 `wheel_width` 變數分配不同的值。`wheels_version` 變數應作為這兩個賦值語句的條件。如果 `wheels_version` 變數的值為字元 "s"(小),則 `wheel_radius` 和 `wheel_width` 變數應分別設定為 8 和 4 個單位。如果 `wheels_version` 變數的值為字元 "m"(中),則 `wheel_radius` 和 `wheel_width` 變數應分別設定為 9 和 6 個單位。如果 `wheels_version` 變數的值為字元 "l"(大),則 `wheel_radius` 和 `wheel_width` 變數應分別設定為 10 和 8 個單位。小型輪子的情況應作為條件賦值語句的預設情況。將適當的值分配給 `body_version` 和 `wheels_version` 變數,以建立以下版本的汽車:短車身 - 中型輪子、矩形車身 - 大型輪子、普通車身 - 小型輪子。
  • 短車身 - 中型輪子
程式碼

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;
…

  • 矩形車身 - 大型輪子
程式碼

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 
…

  • 普通車身 - 小型輪子
程式碼

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 
…

條件建立物件 - if 語句

[edit | edit source]

條件變數賦值是一個強大的工具,可以輕鬆地在模型的不同但特定的版本之間導航。使用條件賦值,您可以為汽車定義不同的車身和車輪尺寸,並輕鬆地在它們之間進行選擇,而不必每次都手動提供所有相關變數的值。

如果您想要對輪子型別(例如,簡單、圓形、複雜)或車身型別(例如,方形、圓形)進行相同的控制,需要怎麼做?要實現這一點,您需要進行條件物件建立,這可以使用 if 語句來實現。

在開始定製輪子和車身型別之前,您可以透過一些簡短的示例熟悉 if 語句。回想一下您在前面章節中建立的車身模組。該模組具有一些輸入引數,用於建立兩個立方體,一個用於車身底部,一個用於車身頂部。

程式碼

car_body_from_module.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();

使用 if 語句,您將瞭解如何引數化車身頂部的建立。首先,您需要為模組定義一個額外的輸入引數。該引數將被命名為 `top`,並儲存布林值。如果該引數為 `false`,則模組應該只建立車身底部。如果為 `true`,則它還應該建立車身頂部。這可以透過以下方式使用 if 語句來實現。

程式碼
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;

您應該注意 if 語句的定義中的一些要點。首先是 if 關鍵字,然後是一對圓括號。在圓括號內,定義了將決定是否執行 if 語句的條件。最後,有一對大括號,其中包含所有將在條件為 `true` 時執行的語句。在本例中,提供的條件是布林變數 `top`,它代表您選擇建立是否有頂部的車身。放在大括號內的語句是建立汽車車身頂部部分的語句。

這種特定形式的 if 語句稱為簡單 if 語句。這意味著,如果條件為 `true`,則執行相應的命令,否則什麼也不發生。if 語句還有另外兩種形式,將在後面介紹,但首先花點時間調查一下新的車身模組在實踐中是如何工作的。

當輸入引數 `top` 設定為 `false` 時,只建立車身底部。

程式碼

car_body_without_top.scad

…
body(top=false);
…

當它設定為 `true` 時,會建立車身底部和頂部。

程式碼

car_body_with_top.scad

…
body(top=true);
…

練習
檢視以下車身模組。您會注意到該模組已被修改為包含後保險槓的建立。應用於保險槓的 `color` 命令只是在預覽時添加了一種視覺效果。在本例中,`color` 命令用於將您的注意力吸引到新新增的部分;它不應該讓您除此之外有任何困擾。
程式碼

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);

練習
要建立前保險槓,請複製並貼上建立後保險槓的語句,並相應地修改 `translate` 語句。
程式碼

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);

練習
為車身模組定義兩個額外的輸入引數。一個名為 `front_bumper`,另一個名為 `rear_bumper`。請記住,這些引數應該接受布林值,定義兩個 if 語句,它們有條件地建立前後保險槓。只有當輸入引數 `front_bumper` 為 `true` 時,才會建立前保險槓,第二個保險槓也是如此。使用車身模組建立以下車身:僅底部 - 前後保險槓、底部和頂部 - 前保險槓、底部和頂部 - 前後保險槓。
  • 僅底部 - 前後保險槓
程式碼

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);

  • 底部和頂部 - 前保險槓
程式碼

body_with_top_with_front_bumper.scad

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

  • 底部和頂部 - 前後保險槓
程式碼

body_with_top_with_front_and_rear_bumper.scad

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

挑戰

[edit | edit source]

在本節中,您學習了條件變數賦值和簡單的 if 語句。具體來說,您學習瞭如何有條件地修改設計的各個部分的尺寸和變換,以及如何有條件地包含或排除部分。現在,是將這兩者結合到一個汽車模型中的時候了。

練習
如果您一直在按照本教程進行操作,那麼您應該在計算機上有一個來自前面章節的 `vehicle_parts.scad` 指令碼。開啟此指令碼,並根據最後一個示例更新車身模組,使其能夠有條件地建立車身頂部以及前後保險槓。為新新增的輸入引數設定預設值。具體來說,分別將 `true`、`false` 和 `false` 作為 `top`、`front_bumper` 和 `rear_bumper` 變數的預設值。儲存更改並關閉指令碼。
程式碼
…
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);
            }
        }
    }
}
…
練習
給定以下建立汽車的指令碼,請對指令碼進行適當的新增和修改,以便對汽車的設計進行引數化。具體來說,您需要定義一個 `body_version`、一個 `wheels_version`、一個 `top`、一個 `front_bumper` 和一個 `rear_bumper` 變數,這些變數將用於做出有關汽車設計的決策。如有必要,請檢視本章前面的示例和練習,以記住這些變數應該對汽車設計產生什麼影響以及如何實現它們。使用生成的指令碼建立一個您喜歡的汽車版本。
  • 給定指令碼
程式碼

basic_car_script.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();
  • 修改後的指令碼
程式碼

car_from_highly_parameterized_script.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);

華夏公益教科書