OpenSCAD CheatSheet

February 21, 2022

)

不可變動的變數

``````my_text = "Hello, OpenSCAD!";
step_angle = 30;
radius = 30;
height = 5;
``````

OpenSCAD is a Functional programming language, as such variables are bound to expressions and keep a single value during their entire lifetime due to the requirements of referential transparency.

``````x = 1;
echo(x); // 顯示 2
x = 2;
echo(x); // 顯示 2
``````

for 迴圈

``````for (a =[3:5]) {
echo(a);
}
``````

``````for (a =[1, 2, 3]) {
echo(a);     // 逐一顯示 1 2 3
}

for (c = "abc") {
echo(c);      // 逐一顯示  a b c
}
``````

``````sum = 0;
for (a =[0:5]) {
sum = sum + 1;
echo(sum);
}
``````

內建 2D 模組

`text` 參數沒有問題，就是你要指定的文字，不好打出來的特殊字元，也可以使用 Unicode 碼指定，例如：

``````text("\u20AC 10 \u263A");
``````

`size` 參數預設是 10，單位是 mm，至於 `font`字型名稱，如果要建立中文，原始碼 .scad 檔案要是 UTF-8 編碼，而 `font` 指定中文字型，例如，來個「標楷體」的「春」字：

``````text("春", font = "標楷體");
``````

擠出（extrude）

``````linear_extrude(10)
text("春", font = "標楷體");
``````

`linear_extrude(10)` 之後不用分號，這類輔助某個模組的操作，後頭都是接上某個完成操作的模組。這會建立以下的 3D 模型：

轉換（transformation）

``````translate([-5, -5, -5])
linear_extrude(10)
text("春", font = "標楷體");
``````

``````rotate([90, 0, 0])
translate([-5, -5, -5])
linear_extrude(10)
text("春", font = "標楷體");
``````