前言

你有沒有過這種經驗:在 Illustrator 裡畫好了一個盒子,尺寸 50mm x 30mm x 20mm,卡榫間距算得好好的。結果客戶說:「可以改成 60mm x 35mm 嗎?」然後你就得全部重畫一次。

這就是參數化設計要解決的問題。把設計裡的數值都變成參數,改一個數字,整個模型自動重新計算。 不用重畫、不怕改需求、甚至可以一次產生好幾種尺寸的版本。

OpenSCAD 是我最推薦的參數化設計入門工具。它不是傳統的 3D 建模軟體(沒有滑鼠拖拉,不能用手去「捏」模型),而是用程式碼來描述幾何。聽起來很硬核,但其實語法非常簡單,而且一旦上手,你會發現它的效率高得驚人。


OpenSCAD 基礎

安裝與介面

OpenSCAD 是免費開源軟體,支援 Windows / Mac / Linux。從 openscad.org 下載安裝即可。

介面分為三個主要區域:

  • 左側:程式碼編輯器
  • 右側:3D 預覽視窗
  • 下方:控制台(顯示錯誤訊息)

F5 快速預覽,按 F6 完整渲染。日常設計用 F5 就好,匯出前用 F6。

基本幾何元素

// 3D 基本形狀
cube([30, 20, 10]);          // 長方體:x=30, y=20, z=10
sphere(r=15);                 // 球:半徑 15
cylinder(h=20, r=10);         // 圓柱:高 20, 半徑 10
cylinder(h=20, r1=15, r2=5);  // 圓錐台:底部半徑 15, 頂部半徑 5

// 2D 基本形狀(用於雷切匯出) square([30, 20]); // 矩形 circle(r=15); // 圓 polygon([[0,0], [30,0], [15,20]]); // 多邊形(三角形)

變換操作

// 平移
translate([10, 20, 0])
  cube([5, 5, 5]);

// 旋轉(單位:度) rotate([0, 0, 45]) cube([10, 10, 5]);

// 縮放 scale([2, 1, 1]) sphere(r=10);

布林運算

這是 OpenSCAD 最強大的工具:

// 聯集:合併兩個形狀
union() {
  cube([20, 20, 10]);
  translate([10, 10, 0])
    cylinder(h=15, r=8);
}

// 差集:從第一個形狀挖掉後面的 difference() { cube([30, 30, 10]); translate([15, 15, -1]) cylinder(h=12, r=8); // 挖一個圓孔 }

// 交集:取兩個形狀重疊的部分 intersection() { cube([20, 20, 20]); sphere(r=15); }


參數化的核心:變數

定義參數

// 在檔案開頭定義所有參數
box_width = 60;
box_depth = 40;
box_height = 30;
wall_thickness = 3;
material_thickness = 3;
kerf = 0.15;  // 雷射切割寬度補償

// 用參數來建模 difference() { cube([box_width, box_depth, box_height]); translate([wall_thickness, wall_thickness, wall_thickness]) cube([ box_width - 2 * wall_thickness, box_depth - 2 * wall_thickness, box_height // 頂部開口 ]); }

現在如果要改尺寸,只要改開頭的三個數字就好。所有的牆壁厚度、內部空間都會自動重算。

Customizer 功能

OpenSCAD 有一個內建的 Customizer 面板,可以把參數變成可拖曳的滑桿:

/ [Main Dimensions] /
box_width = 60;   // [30:5:100]
box_depth = 40;   // [20:5:80]
box_height = 30;  // [15:5:60]

/ [Material] / material_thickness = 3; // [2:0.5:6] kerf = 0.15; // [0:0.05:0.3]

註解裡的 [min:step:max] 定義了滑桿的範圍。開啟 Customizer(Window > Customizer)後,就可以用 GUI 調參數,像玩遊戲設定一樣。


實作:參數化方盒

讓我們做一個可以雷切的參數化盒子。這個盒子由六片板材組成,邊緣用 finger joint(指接榫)卡合。

定義參數

// 盒子外部尺寸
width = 80;
depth = 60;
height = 40;

// 材料與加工 t = 3; // 材料厚度 kerf = 0.15; // 雷射切割寬度 finger_w = 8; // 指接榫寬度 tolerance = 0.1; // 組裝容差

指接榫生成函式

module finger_edge(length, finger_width, thickness, is_positive) {
  // 計算能放幾個 finger
  n = floor(length / finger_width);
  actual_fw = length / n;

for (i = [0:n-1]) { if ((i % 2 == 0 && is_positive) || (i % 2 == 1 && !is_positive)) { translate([i * actual_fw, 0, 0]) square([actual_fw + kerf, thickness]); } } }

面板生成

module front_panel() {
  difference() {
    square([width, height]);

// 底邊 finger joint(凹槽) translate([0, 0, 0]) finger_edge(width, finger_w, t, false);

// 左右邊 finger joint(凹槽) translate([0, 0, 0]) finger_edge(height, finger_w, t, false);

translate([width - t, 0, 0]) rotate([0, 0, 90]) finger_edge(height, finger_w, t, false); } }

module bottom_panel() { // 底板帶有四邊的 finger joint difference() { square([width, depth]);

// 四邊的凹槽 // ... 依此類推 } }

2D 投影與排版

OpenSCAD 是 3D 軟體,但雷切需要 2D。用 projection() 可以把 3D 物件投影到 2D:

// 把所有面板排列在同一平面上,準備雷切
projection() {
  front_panel();
}

translate([0, height + 5, 0]) projection() { bottom_panel(); }

// ... 其他面板依序排列

匯出 SVG 或 DXF

  1. 按 F6 完整渲染
  2. File > Export > Export as SVG(2D 物件)或 Export as DXF
  3. 如果是 3D 物件,需要先用 projection() 轉成 2D

實作:參數化齒輪

齒輪是參數化設計的經典案例。手動畫齒輪極其痛苦,但用程式碼可以輕鬆生成任意齒數的齒輪。

使用 MCAD 齒輪模組

OpenSCAD 有一個常用的函式庫叫 MCAD,裡面包含齒輪模組:

use <MCAD/involute_gears.scad>

// 生成一個 20 齒的齒輪 gear( number_of_teeth = 20, circular_pitch = 300, gear_thickness = 3, rim_thickness = 3, hub_thickness = 3, bore_diameter = 5 );

自訂簡易齒輪

如果不想用外部函式庫,可以自己寫一個簡化版:

module simple_gear(teeth, module_val, thickness) {
  pitch_r = teeth * module_val / 2;
  outer_r = pitch_r + module_val;
  inner_r = pitch_r - 1.25 * module_val;
  tooth_angle = 360 / teeth;

linear_extrude(height=thickness) { difference() { circle(r=outer_r, $fn=teeth*6);

for (i = [0:teeth-1]) { rotate([0, 0, i * tooth_angle + tooth_angle/4]) translate([pitch_r, 0, 0]) circle(r=module_val * 0.8, $fn=20); }

circle(r=2.5); // 中心孔 } } }

// 使用 simple_gear(teeth=24, module_val=2, thickness=3);

這個版本的齒形不是精確的漸開線,但對於裝飾用途或簡易機構已經足夠。


自訂函式與模組

函式(Function)

OpenSCAD 的函式用於計算數值:

// 計算正多邊形的內切圓半徑
function apothem(radius, sides) = radius * cos(180/sides);

// 計算 finger joint 數量 function finger_count(length, finger_width) = max(3, floor(length / finger_width));

// 使用 r = 30; a = apothem(r, 6); echo(str("六邊形內切圓半徑: ", a));

模組(Module)

模組是可重複使用的幾何組件:

module rounded_rect(w, h, r) {
  // 圓角矩形
  hull() {
    translate([r, r]) circle(r=r);
    translate([w-r, r]) circle(r=r);
    translate([r, h-r]) circle(r=r);
    translate([w-r, h-r]) circle(r=r);
  }
}

module mounting_hole(x, y, d) { translate([x, y]) circle(d=d + kerf*2, $fn=30); }

// 使用:一塊帶有圓角和螺絲孔的面板 difference() { rounded_rect(80, 50, 5); mounting_hole(10, 10, 3); mounting_hole(70, 10, 3); mounting_hole(10, 40, 3); mounting_hole(70, 40, 3); }

迴圈與條件

// for 迴圈
for (i = [0:5]) {
  translate([i * 15, 0, 0])
    circle(r=5);
}

// 條件判斷 module panel(with_holes = true) { difference() { square([60, 40]); if (with_holes) { translate([10, 10]) circle(d=5); translate([50, 10]) circle(d=5); } } }

panel(with_holes = true); translate([0, 50]) panel(with_holes = false);


STL 與 SVG 匯出

匯出 2D (SVG / DXF) — 用於雷切

確保你的設計是 2D 的(只用了 2D 基本形狀),或者用 projection() 把 3D 物件投影到 2D。

// 方法 1:直接用 2D
square([50, 30]);

// 方法 2:用 projection 把 3D 投影到 2D projection(cut=true) translate([0, 0, -1.5]) // 在 z=0 處切一刀 cube([50, 30, 3]);

匯出步驟:

  1. F6 渲染
  2. File > Export > Export as SVGExport as DXF

匯出 3D (STL) — 用於 3D 列印

如果你的設計是 3D 的,可以匯出 STL 給 3D 列印機:

  1. F6 渲染
  2. File > Export > Export as STL

圓滑度設定

OpenSCAD 的圓形預設是很粗糙的(低多邊形)。匯出前要提高圓滑度:

$fn = 60;  // 全域設定:圓形用 60 個面
// 或者對個別形狀設定
circle(r=10, $fn=100);

數值越高越圓滑,但渲染時間也越長。一般雷切用 60-100 就夠了。


實用技巧

1. 用 echo() 除錯

w = 80;
n = finger_count(w, 8);
echo(str("寬度 ", w, " mm, finger 數量: ", n));

2. 用 color() 視覺化分組

color("red") front_panel();
color("blue") translate([0, 50, 0]) back_panel();
color("green") translate([0, 100, 0]) bottom_panel();

3. 建立個人函式庫

把常用的模組存成 .scad 檔案,用 useinclude 引入:

use <my_library/finger_joint.scad>
use <my_library/rounded_shapes.scad>

// 現在可以直接使用函式庫裡定義的模組 finger_edge(80, 8, 3, true);

4. 命令列批次生成

OpenSCAD 支援命令列渲染,可以批次生成不同參數的版本:

# 生成寬度 60mm 的版本
openscad -o box_60.svg -D "width=60" box.scad

# 生成寬度 80mm 的版本 openscad -o box_80.svg -D "width=80" box.scad

# 用 shell script 批次生成 for w in 40 50 60 70 80; do openscad -o "box_${w}.svg" -D "width=${w}" box.scad done

這在需要量產不同尺寸的產品時特別好用。


小結

OpenSCAD 的哲學是「程式碼即設計」。一開始可能會覺得沒有滑鼠拖拉很不直覺,但當你需要:

  • 精確控制每一個尺寸
  • 快速修改參數生成新版本
  • 重複使用設計元件
  • 批次產生不同規格

這時候 OpenSCAD 的優勢就非常明顯了。

建議的學習路線:

  1. 從基本形狀和布林運算開始,做一些簡單的 2D 圖形
  2. 學會用變數和模組,把設計參數化
  3. 嘗試做一個完整的盒子(含 finger joint)
  4. 建立自己的函式庫,累積可重用的元件

下一篇我們要做一個具體的專案 — 壓克力燈箱。那是結合雷切和光學效果的經典作品,也是送禮的超人氣選擇。