name: build123d-cad description: “通过 build123d 进行参数化 3D CAD。可从 Python 脚本生成 STEP、STL、SVG 文件。当用户要求设计、建模、创建或导出 3D 零件、外壳、安装件、支架或机械部件时使用。” version: 1.0.0 metadata: {“openclaw”:{“requires”:{“bins”:[“python3”]},“emoji”:“🔧”}}
build123d CAD
通过 build123d 进行参数化 3D CAD。本技能提供用于生成和测量 3D 实体的 Python 脚本。使用 exec 工具运行它们。
安装(仅首次)
cd {baseDir}
uv venv --python 3.12
uv pip install build123d
命令
所有脚本均使用虚拟环境 Python {baseDir}/.venv/bin/python。所有输出均以 JSON 形式输出到 stdout。
生成 — 导出 STEP/STL/SVG
{baseDir}/.venv/bin/python {baseDir}/scripts/cad_generate.py \
--script 'from build123d import *
with BuildPart() as result:
Box(100, 60, 40)' \
--format step \
--filename my_box
输出:{ "success": true, "artifact_path": "...", "format": "step", "file_size_bytes": N, "bounding_box_mm": {...} }
测量 — 尺寸、体积、质量
{baseDir}/.venv/bin/python {baseDir}/scripts/cad_measure.py \
--script 'from build123d import *
with BuildPart() as result:
Cylinder(10, 50)'
输出:{ "success": true, "bounding_box_mm": {...}, "volume_mm3": N, "surface_area_mm2": N, "center_of_mass_mm": {...}, "face_count": N, "edge_count": N }
截面 — 2D 横截面 SVG
{baseDir}/.venv/bin/python {baseDir}/scripts/cad_section.py \
--script '...' \
--plane XY \
--offset 5.0
输出:{ "success": true, "artifact_path": "...", "plane": "XY", "offset_mm": 5.0 }
API 参考 — build123d 速查表
{baseDir}/.venv/bin/python {baseDir}/scripts/cad_api.py
输出:包含基本体、操作、选择器、导出函数的 JSON。如果需要了解可用内容,请先调用此命令。
验证 — 干涉、间隙、扫掠体碰撞
检查多部件装配体的干涉、最小间隙和运动部件碰撞。
{baseDir}/.venv/bin/python {baseDir}/scripts/cad_validate.py \
--script '...' \
--mode full \
--min-clearance 1.0
模式:static(所有对之间的布尔交集)、clearance(包围盒间隙)、sweep(将运动部件旋转角度范围,检查碰撞)、full(全部三种,默认)。
脚本必须定义 parts = {"name": solid, ...} 字典。可选定义运动部件的 sweeps 列表:
from build123d import *
with BuildPart() as enclosure:
Box(100, 80, 50)
offset(amount=-3, openings=enclosure.faces().sort_by(Axis.Z)[-1:])
with BuildPart() as pcb:
with Locations((0, 0, 5)):
Box(60, 40, 2)
with BuildPart() as servo_arm:
with Locations((30, 0, 25)):
Box(5, 20, 3)
parts = {
"enclosure": enclosure.part,
"pcb": pcb.part,
"servo_arm": servo_arm.part,
}
sweeps = [
{
"name": "servo_arm",
"axis_origin": (30, 0, 25),
"axis_direction": (0, 1, 0),
"angle_start": -45,
"angle_end": 45,
"angle_step": 5,
},
]
输出:{ "verdict": "PASS|WARN|FAIL", "static_interference": {...}, "clearance": {...}, "swept_volume": {...} }
- PASS — 无干涉,间隙正常
- WARN — 间隙低于阈值但无硬碰撞
- FAIL — 部件相交或运动部件在扫掠过程中碰撞
扫掠体导出到 ~/.openclaw/workspace/cad-output/swept_<name>.step 用于可视化。
脚本格式
所有单部件脚本必须通过 BuildPart 上下文将最终实体赋给 result:
from build123d import *
with BuildPart() as result:
Box(100, 60, 40)
fillet(result.edges().filter_by(Axis.Z), radius=5)
with Locations((0, 0, 40)):
CounterBoreHole(radius=5, counter_bore_radius=8, counter_bore_depth=3, depth=40)
对于验证脚本,请定义 parts 字典(以及可选的 sweeps 列表),而不是 result。
所有尺寸单位均为毫米。导出文件保存在 ~/.openclaw/workspace/cad-output/。
工作流程
- 如果不确定 build123d API,请先运行
cad_api.py获取速查表。 - 编写参数化脚本(不要使用魔法数字)。
- 在导出之前运行
cad_measure.py验证尺寸。 - 对于多部件装配体:在导出之前运行
cad_validate.py --mode full。 在继续之前修复任何 FAIL/WARN。 - 使用所需格式运行
cad_generate.py(STEP 用于 CAD,STL 用于 3D 打印)。 - 如需间隙可视化,请在相关平面上运行
cad_section.py。 - 报告文件路径和验证结论。
设计规则
- 将所有尺寸参数化以便重复使用。
- 对应力集中部位添加圆角(塑料最小 1mm,金属最小 0.5mm)。
- 在适用处包含安装特征(凸台、支座、螺丝柱)。
- 在注释中指定材料和加工假设。
- 同时输出脚本和导出文件。