算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
openscad【设计与创意】Use when creating 3D models programmatically via CSG (Constructive Solid Geometry) scripting — parametric parts, customizable designs for 3D printing. OpenSCAD: script-based 3D CAD using a declarative language rather than GUI modeling.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: openscad description: "Use when creating 3D models programmatically via CSG (Constructive Solid Geometry) scripting — parametric parts, customizable designs for 3D printing. OpenSCAD: script-based 3D CAD using a declarative language rather than GUI modeling." tags:
项目地址: https://github.com/openscad/openscad
官方文档: https://openscad.org/documentation.html
Cheatsheet: https://openscad.org/cheatsheet/
许可证: GPL-2.0+
最新稳定版: 2021.01(官方 release;截至2026年官网推荐开发快照构建以获得 Manifold 内核性能,见 GitHub tags)
OpenSCAD 与传统 GUI CAD 的区别:
openscad -o out.stl model.scad# Linux
sudo apt install openscad
# macOS
brew install --cask openscad
# Windows: 安装包
# 开发版(更快的 Manifold 内核):<https://openscad.org/downloads.html#snapshots>
// 变量
length = 50;
width = 30;
height = 10;
// 立方体
cube([length, width, height], center = false);
// 球
sphere(r = 5, $fn = 64); // $fn 控制圆滑度
// 圆柱
cylinder(h = 20, r1 = 5, r2 = 10, center = true, $fn = 32);
// 文字
text("Hello", size = 8, font = "DejaVu Sans:style=Bold");
translate([10, 0, 0]) cube(5);
rotate([0, 0, 45]) cube(5);
scale([2, 1, 1]) cube(5);
mirror([1, 0, 0]) cube(5);
链式:
translate([20, 0, 0])
rotate([0, 90, 0])
cylinder(h = 10, r = 2);
union() { cube(10); translate([8,0,0]) cube(10); }
difference() { cube(10); translate([5,5,-1]) cylinder(h=12, r=2); }
intersection() { cube(10); translate([5,5,5]) sphere(7); }
module hex(d = 10, h = 5) {
cylinder(h = h, r = d/2, $fn = 6);
}
// 阵列模块
module hex_grid(rows, cols, sp = 12) {
for (i = [0:rows-1])
for (j = [0:cols-1])
translate([i*sp, j*sp*sin(60), 0])
hex();
}
hex_grid(rows = 5, cols = 5);
// 函数(返回值)
function sq(x) = x * x;
echo(sq(5)); // → 25
// for 循环
for (i = [0 : 30 : 360])
rotate([0, 0, i]) translate([20, 0, 0]) cube(2);
// 列表推导
points = [for (i = [0 : 0.1 : 6.28]) [10*cos(i*180/PI), 10*sin(i*180/PI)]];
polygon(points);
// 2D 多边形拉伸
linear_extrude(height = 5, twist = 30, scale = 0.5)
polygon([[0,0],[10,0],[10,10],[0,10]]);
// 旋转扫掠
rotate_extrude($fn = 100)
translate([20, 0, 0]) circle(r = 3);
// 导入 SVG 后拉伸
linear_extrude(height = 4) import("logo.svg");
hull() {
translate([0,0,0]) sphere(2);
translate([20,0,0]) sphere(2);
}
minkowski() {
cube(10);
sphere(2); // 倒圆角效果
}
/* [Box] */
length = 50; // [10:1:200]
width = 30; // [10:1:200]
/* [Hidden] */
$fn = 64;
注释格式让 GUI 自动生成参数滑块。
# 渲染 STL
openscad -o out.stl part.scad
# 传参覆盖
openscad -D 'length=80' -D 'width=40' -o out.stl part.scad
# 导出 PNG 预览
openscad -o preview.png --camera=0,0,0,55,0,25,150 --imgsize=800,600 part.scad
$fn 不要全局设过高,需要圆滑的对象再设hull() 或 minkowski() 后立即缓存为模块use <lib.scad> 引入库不递归求值| 库 | 用途 |
|---|---|
| BOSL2 | 强大的几何工具集(齿轮/螺纹/金属件) |
| MCAD | 早期通用机械库 |
| NopSCADlib | 大量真实零部件(电机/轴承/PCB) |
| dotSCAD | 工程函数 / 多边形工具 |
| BezierScad | 贝塞尔曲线扫掠 |
| 问题 | 解决 |
|---|---|
| 渲染极慢 | 启用 Manifold 内核;降低 $fn |
| 导出 STL 不闭合 | 检查布尔输入是否流形 |
| 中文文字 | font = "Noto Sans CJK SC" |
import("a.stl") 慢 | 转换为 OFF 或预先 mesh decimation |
| 装配线性挤出失真 | convexity 参数加大 |
length=50; width=30; height=10; hole_r=2;module base() { cube([length, width, height]); }module holes() { translate(...) cylinder(h=height+1, r=hole_r); }difference() { base(); holes(); }/* [Dimensions] */ length = 50; // [10:1:200].scad 文件,用 -D 传递参数openscad -o out_${variant}.stl model.scad -D "length=${L}" -D "width=${W}"openscad -o preview.png --camera=... --imgsize=800,600 model.scadmodule 封装可复用组件。参数化面板用 /* [ParamGroup] */ 注释格式暴露变量给 GUI Customizer。批处理使用 CLI openscad -o out.stl model.scad -D 'param=value'。$fn 不要全局设过高,仅对需要圆滑的对象单独设置;② 开发版启用 Manifold 内核(比 CGAL 快 10×+);③ import("a.stl") 场景避免频繁重导入,应缓存为模块;④ 中文字体使用 font = "Noto Sans CJK SC"。cube([l,w,h]) / cylinder(h, r, $fn=N) / sphere(r, $fn=N)。布尔:difference() { ... }。变换:translate([x,y,z]) rotate([ax,ay,az])。参数暴露:/* [Section] */ variable = default; // [min:step:max]。