OpenSCAD:程序化 3D CAD 指南

June 24, 2026 · View on GitHub

目录

  1. OpenSCAD 简介
  2. 安装
  3. 基本语法
  4. 2D 形状
  5. 3D 形状
  6. 变换
  7. 模块与函数
  8. 导出模型

简介

OpenSCAD 是一个基于脚本的 3D CAD 建模器。与传统 CAD 工具不同,OpenSCAD 通过代码描述对象,非常适合精确的参数化设计。

OpenSCAD 核心功能

功能描述好处
基于脚本代码驱动建模精确控制
参数化基于变量的设计易于修改
跨平台Windows, macOS, Linux广泛兼容
免费开源无费用
程序员友好代码语法开发者熟悉

OpenSCAD vs 传统 CAD

功能OpenSCADFusion 360FreeCAD
界面代码编辑器可视化可视化
学习曲线中等中等高
精确度精确良好良好
参数化原生是是
成本免费免费增值免费

系统要求

组件最低推荐
CPU任何现代 CPU多核
内存1GB4GB
GPUOpenGL 2.0OpenGL 3.3+
显示1024x7681920x1080

安装

在你的操作系统上安装 OpenSCAD。

下载选项

平台来源文件类型
Windowsopenscad.org.exe 安装程序
macOSopenscad.org.dmg 文件
Linux包管理器apt, dnf, flatpak

安装步骤

步骤操作备注
1下载官方网站
2运行安装程序接受提示
3选择位置默认路径
4完成启动 OpenSCAD

Linux 安装

# Ubuntu/Debian
sudo apt install openscad

# Fedora
sudo dnf install openscad

# Flatpak
flatpak install flathub org.openscad.OpenSCAD

界面概述

区域位置用途
编辑器左侧代码输入
预览右侧3D 视图
控制台底部错误消息
工具栏顶部常用操作

基本语法

理解 OpenSCAD 脚本语言基础。

代码结构

// Comments
/* Block comments */

// Variables
radius = 10;
height = 20;

// Shapes
sphere(r = radius);

// Transformations
translate([0, 0, height])
    cylinder(r = radius, h = height);

数据类型

类型描述示例
Number数值10, 3.14
String文本"hello"
Boolean真/假true, false
Vector数组[1, 2, 3]
Range序列[0:10]

运算符

运算符描述示例
+加法5 + 3
-减法5 - 3
*乘法5 * 3
/除法6 / 2
%取模7 % 3

注释

类型语法示例
单行//// This is a comment
多行/* *//* Block comment */

变量

// Simple variable
size = 10;

// Computed variable
area = size * size;

// Using variables
cube(size);

2D 形状

创建用于拉伸或投影的二维形状。

基本 2D 形状

形状函数参数
Circlecircle()r (半径), $fn (分辨率)
Squaresquare()size, center
Polygonpolygon()points, paths
Texttext()text, size, font

Circle

// Simple circle
circle(r = 10);

// Circle with resolution
circle(r = 10, $fn = 100);

// Ellipse
scale([2, 1])
    circle(r = 10);

Square

// Simple square
square(20);

// Centered square
square(20, center = true);

// Rectangle
square([20, 10]);

// Centered rectangle
square([20, 10], center = true);

Polygon

// Triangle
polygon(points = [[0,0], [10,0], [5,10]]);

// Square
polygon(points = [[0,0], [10,0], [10,10], [0,10]]);

// Complex shape
polygon(points = [
    [0, 0],
    [10, 0],
    [10, 10],
    [5, 15],
    [0, 10]
]);

Text

// Simple text
text("Hello");

// Sized text
text("Hello", size = 20);

// Custom font
text("Hello", font = "Arial:style=Bold");

3D 形状

创建三维实体对象。

基本 3D 形状

形状函数参数
Cubecube()size, center
Spheresphere()r, $fn
Cylindercylinder()r, h, center
Polyhedronpolyhedron()points, faces

Cube

// Simple cube
cube(20);

// Centered cube
cube(20, center = true);

// Box (different dimensions)
cube([20, 10, 5]);

// Centered box
cube([20, 10, 5], center = true);

Sphere

// Simple sphere
sphere(r = 10);

// High resolution sphere
sphere(r = 10, $fn = 100);

// Low resolution (faceted)
sphere(r = 10, $fn = 8);

Cylinder

// Simple cylinder
cylinder(r = 10, h = 20);

// Centered cylinder
cylinder(r = 10, h = 20, center = true);

// Cone
cylinder(r1 = 10, r2 = 0, h = 20);

// Truncated cone
cylinder(r1 = 10, r2 = 5, h = 20);

Polyhedron

// Simple tetrahedron
polyhedron(
    points = [
        [0, 0, 0],
        [10, 0, 0],
        [5, 10, 0],
        [5, 5, 10]
    ],
    faces = [
        [0, 1, 2],
        [0, 1, 3],
        [1, 2, 3],
        [0, 2, 3]
    ]
);

变换

使用几何变换修改形状。

平移

// Move in X
translate([10, 0, 0])
    cube(10);

// Move in multiple axes
translate([10, 20, 30])
    cube(10);

旋转

// Rotate around Z axis
rotate([0, 0, 45])
    cube(10);

// Rotate around multiple axes
rotate([45, 45, 0])
    cube(10);

缩放

// Scale uniformly
scale([2, 2, 2])
    cube(10);

// Scale non-uniformly
scale([2, 1, 0.5])
    cube(10);

镜像

// Mirror across XZ plane
mirror([0, 1, 0])
    cube(10);

// Mirror across XY plane
mirror([0, 0, 1])
    cube(10);

颜色

// Set color
color("red")
    cube(10);

// RGB color
color([1, 0, 0])
    cube(10);

// RGBA color
color([1, 0, 0, 0.5])
    cube(10);

Hull

// Convex hull of two shapes
hull() {
    sphere(5);
    translate([20, 0, 0])
        sphere(5);
}

模块与函数

创建可复用的代码组件。

模块

// Simple module
module box(size) {
    cube(size, center = true);
}

// Using the module
box(20);

// Module with default parameters
module cylinder_custom(r = 10, h = 20) {
    cylinder(r = r, h = h, center = true);
}

// Using with custom values
cylinder_custom(r = 15, h = 30);

函数

// Simple function
function area(r) = PI * r * r;

// Using the function
a = area(10);

// Function with multiple parameters
function volume(r, h) = PI * r * r * h;

v = volume(10, 20);

条件语句

// If statement
if (size > 10) {
    cube(size);
} else {
    sphere(size / 2);
}

// Ternary operator
shape = (type == "cube") ? cube(10) : sphere(5);

循环

// For loop
for (i = [0:10]) {
    translate([i * 15, 0, 0])
        cube(10);
}

// For loop with list
for (i = [10, 20, 30]) {
    translate([i, 0, 0])
        cube(10);
}

Include 和 Use

// Include file (executes code)
include <library.scad>;

// Use file (imports modules/functions only)
use <library.scad>;

导出模型

导出你的 3D 模型用于打印或分享。

导出格式

格式扩展名用途
STL.stl3D 打印
OFF.off学术用途
AMF.amf高级打印
3MF.3mf现代打印
DXF.dxf2D 切割
SVG.svg2D 图形
PNG.png图片

导出流程

步骤操作备注
1设计模型编写代码
2预览F5 键
3渲染F6 键
4导出File → Export
5选择格式STL 用于打印

渲染质量

设置描述推荐
$fn分辨率曲线用 100
$fa最小角度1(默认)
$fs最小尺寸0.1(默认)

打印准备

步骤操作工具
1导出 STLOpenSCAD
2检查网格MeshLab 或 Netfabb
3如需修复网格修复工具
4切片Cura, PrusaSlicer
5打印3D 打印机

常见问题

问题原因解决方案
非流形开放边缘检查几何
孔洞缺失面检查 polyhedron
交叉重叠形状使用 union()
太大单位错误适当缩放

总结

主题核心要点
界面代码编辑器 + 3D 预览
语法类 C 脚本语言
2D 形状圆、方、多边形
3D 形状立方体、球、圆柱
变换平移、旋转、缩放
模块可复用代码组件
导出STL 用于 3D 打印