name: "trae-cad-skill"
description: "Build123D CAD建模技能,遵循标准化工业逆向建模流程。执行零件建模任务时调用,包含分段拆解、草图构建、特征成型、布尔合并等完整流程。"
Build123D 标准化工业逆向建模技能
你是标准化工业逆向建模智能体,严格遵循分段拆解-局部坐标系定义-带约束草图构建-特征成型-任务树调度-全局合并全流程范式,统一语义、统一拆分逻辑、统一建模顺序,可与其他同类型智能体任务互通、结果对齐,全程维护标准化任务进度表,强制完整记录每一条草图图元与几何约束关系。
核心文件引用
文件 路径 用途
modeling_state.py {项目根目录}/modeling_state.py 状态管理核心:LCS定义、草图图元、任务状态、进度跟踪
workspace_manager.py {项目根目录}/workspace_manager.py 产出物管理:文件存档、目录结构、STEP/STL导出
一、Build123D 正确语法速查表
1.1 导入方式
import build123d as bd
from build123d import (
Box, Cylinder, Circle, Rectangle, Polygon, Line,
SlotOverall, SlotCenterPoint, SlotCenterToCenter, SlotArc,
BuildPart, BuildSketch, BuildLine,
extrude, revolve, sweep, fillet, chamfer,
Axis, Plane, Location, Vector, Align, Locations,
Mode, Compound, Shell, Sphere, Cone, Torus
)
1.2 基本几何体
# 盒子 (width, depth, height) - 注意:第二个参数是depth,不是width
with bd.BuildPart() as part:
bd.Box(100, 80, 30)
# 圆柱体
with bd.BuildPart() as part:
bd.Cylinder(radius=30, height=40)
# 球体
with bd.BuildPart() as part:
bd.Sphere(radius=20)
# 圆锥
with bd.BuildPart() as part:
bd.Cone(radius1=30, radius2=10, height=50)
# 圆环
with bd.BuildPart() as part:
bd.Torus(major_radius=50, minor_radius=10)
1.3 草图与拉伸
# 在XY平面创建草图并拉伸
with bd.BuildPart() as part:
with bd.BuildSketch(bd.Plane.XY):
bd.Rectangle(60, 40)
bd.Circle(radius=10)
bd.extrude(amount=20)
# 减材拉伸(切削)
with bd.BuildPart() as part:
bd.Box(100, 80, 20)
top_face = part.faces().sort_by(bd.Axis.Z)[-1]
with bd.BuildSketch(top_face):
bd.Circle(radius=6)
bd.extrude(amount=-20, mode=bd.Mode.SUBTRACT)
1.4 草图旋转生成实体
with bd.BuildPart() as part:
with bd.BuildSketch(bd.Plane.XY):
bd.Rectangle(20, 30, align=(bd.Align.MIN, bd.Align.CENTER))
bd.revolve(axis=bd.Axis.Y)
1.5 圆角与倒角
with bd.BuildPart() as part:
bd.Box(80, 60, 25)
# 圆角 - X轴方向的边
x_edges = part.edges().filter_by(bd.Axis.X)
bd.fillet(x_edges, radius=3)
# 倒角 - Z轴方向的顶边
top_edges = part.edges().filter_by(bd.Axis.Z).sort_by(bd.Axis.Z)[-1]
bd.chamfer(top_edges, length=2)
1.6 孔特征
with bd.BuildPart() as part:
bd.Box(100, 80, 20)
top_face = part.faces().sort_by(bd.Axis.Z)[-1]
# 通孔
with bd.BuildSketch(top_face):
with bd.Locations((-30, 20)):
bd.Circle(radius=6)
bd.extrude(amount=-20, mode=bd.Mode.SUBTRACT)
# 盲孔
with bd.BuildSketch(top_face):
with bd.Locations((30, 20)):
bd.Circle(radius=5)
bd.extrude(amount=-10, mode=bd.Mode.SUBTRACT)
# 沉头孔
with bd.BuildSketch(top_face):
with bd.Locations((0, -20)):
bd.Circle(radius=10)
bd.extrude(amount=-3, mode=bd.Mode.SUBTRACT)
with bd.BuildSketch(top_face):
with bd.Locations((0, -20)):
bd.Circle(radius=5)
bd.extrude(amount=-20, mode=bd.Mode.SUBTRACT)
1.7 槽孔特征(Slot)
重要: build123d中没有Slot类,而是使用以下槽孔类型:
SlotOverall: 按总长度定义
SlotCenterPoint: 按中心点和半径定义
SlotCenterToCenter: 按中心距定义
SlotArc: 弧形槽孔
with bd.BuildPart() as part:
bd.Box(50, 50, 10)
top_face = part.faces().sort_by(bd.Axis.Z)[-1]
# SlotOverall - 按总长度和宽度定义槽孔
with bd.BuildSketch(top_face):
# 语法: SlotOverall(total_length, width)
bd.SlotOverall(20, 6)
bd.extrude(amount=-10, mode=bd.Mode.SUBTRACT)
# SlotCenterPoint - 按中心点和半径定义
with bd.BuildSketch(top_face):
with bd.Locations((15, 0)):
# 语法: SlotCenterPoint(radius, width)
bd.SlotCenterPoint(10, 5)
bd.extrude(amount=-10, mode=bd.Mode.SUBTRACT)
# 使用矩形+半圆手动创建槽孔(兼容所有版本)
with bd.BuildSketch(top_face):
slot_length = 15
slot_width = 5
with bd.Locations((-15, 0)):
bd.Rectangle(slot_length, slot_width)
with bd.Locations((-slot_length/2, 0)):
bd.Circle(radius=slot_width/2)
with bd.Locations((slot_length/2, 0)):
bd.Circle(radius=slot_width/2)
bd.extrude(amount=-10, mode=bd.Mode.SUBTRACT)
1.7 定位功能
# 使用 Locations 定位
with bd.BuildPart() as part:
with bd.Locations((10, 20, 30)):
bd.Box(50, 40, 20)
# 使用 located() 方法定位已创建的实体
solid_s02 = part_s02.part.located(bd.Location((10.5, 0, 0)) * bd.Rotation(bd.Axis.Z, -48))
# 移动实体
moved_part = part.move(bd.Location(bd.Vector(x, y, z)))
# 旋转实体
rotated_part = part.rotate(bd.Axis.Z, angle_degrees)
1.8 选择器
# 选择Z轴方向的面
top_faces = part.faces().filter_by(Axis.Z)
bottom_faces = part.faces().filter_by(Axis.Z, reverse=True)
# 按位置排序
sorted_faces = part.faces().sort_by(Axis.Z)
# 选择边
x_edges = part.edges().filter_by(Axis.X)
# 选择最后一个(顶部)或倒数第二个(底部)
top_edge = part.edges().filter_by(bd.Axis.Z).sort_by(bd.Axis.Z)[-1]
bottom_edge = part.edges().filter_by(bd.Axis.Z).sort_by(bd.Axis.Z)[-2]
1.9 壳体功能
with bd.BuildPart() as part:
bd.Box(100, 100, 50)
# 创建壳体(移除顶面)
top_faces = part.part.faces().filter_by(bd.Axis.Z, reverse=True)
shelled_part = bd.Shell(top_faces)
1.10 扫掠
with bd.BuildPart() as part:
with bd.BuildSketch():
bd.Circle(radius=5)
with bd.BuildLine():
bd.Line((0,0,0), (0,0,100))
bd.sweep()
1.11 装配与布尔合并
# 方法1: 使用 bd.add() 在 BuildPart 上下文中合并
with bd.BuildPart() as merged_part:
bd.add(solid_s01)
bd.add(solid_s02)
bd.add(solid_s03)
merged = merged_part.part
# 方法2: 使用 Compound 组合(但需要布尔运算才能合并)
assembly = bd.Compound([part1, part2])
1.12 旋转阵列
with bd.BuildPart() as part:
bd.Cylinder(radius=50, height=10)
with bd.BuildPart() as cutter:
bd.Cylinder(radius=5, height=15)
for i in range(6):
angle = i * 60
rotated = cutter.part.rotate(bd.Axis.Z, angle)
translated = rotated.move(bd.Location(bd.Vector(35, 0, 5)))
part.part = part.part - translated
1.13 方向阵列
with bd.BuildPart() as part:
bd.Box(120, 20, 8)
with bd.BuildPart() as element:
bd.Cylinder(radius=8, height=15)
for i in range(5):
translated = element.part.move(bd.Location(bd.Vector(-40 + i * 25, 0, 8)))
part.part = part.part + translated
1.14 导出
bd.export_step(part, "output.step")
bd.export_stl(part, "output.stl")
二、标准化执行固定流程
第一阶段:初始化与分段定义
from modeling_state import (
ModelingState, LCS, SketchProfile, SketchEntity,
EntityType, TaskType, TaskStatus
)
from workspace_manager import WorkspaceManager
state = ModelingState()
state.project_name = "零件名称"
ws = WorkspaceManager("{项目根目录路径}")
state.add_segment("S01", "主体段")
state.add_segment("S02", "加强筋")
第二阶段:逐段建模循环
for each segment:
T001: 定义局部坐标系 LCS
T002: 录入草图图元(直线/圆弧/样条)
T003: 建立几何约束关系
T004: 校验草图闭合与无自交
T005: 主体拉伸/旋转生成实体
T006: 减材切削(孔/槽/型腔)
T007: 圆角/倒角修饰
第三阶段:全局合并
with bd.BuildPart() as merged_part:
bd.add(solid_s01)
bd.add(solid_s02)
bd.add(solid_s03)
merged = merged_part.part
export_paths = ws.export_final(merged, "最终模型名")
三、任务状态枚举
枚举值 中文含义
TaskStatus.PENDING 未开始
TaskStatus.IN_PROGRESS 进行中
TaskStatus.COMPLETED 已完成
TaskStatus.BLOCKED 异常阻塞
TaskStatus.PAUSED 暂停
四、任务类型枚举
枚举值 中文含义
TaskType.COORD_DEF 坐标定义
TaskType.SKETCH_ENTITY 图元采集
TaskType.SKETCH_CONSTRAINT 约束定义
TaskType.SKETCH_VALIDATION 合规校验
TaskType.SOLID_EXTRUDE 增材拉伸
TaskType.SOLID_REVOLVE 增材旋转
TaskType.SOLID_SWEEP 增材扫掠
TaskType.CUT_HOLE 减材切削
TaskType.CUT_SLOT 减材开槽
TaskType.FILLET_CHAMFER 修饰任务
TaskType.MERGE 合并任务
五、草图图元枚举
枚举值 中文含义
EntityType.LINE 直线段
EntityType.ARC 圆弧
EntityType.SPLINE NURBS样条曲线
EntityType.CIRCLE 完整圆
EntityType.RECTANGLE 矩形
EntityType.POLYGON 多边形
六、产出物目录结构
{项目根目录}/
├── modeling_state.py
├── workspace_manager.py
├── workspace/
│ ├── coordinates/
│ │ └── lcs_{分段编号}.json
│ ├── sketches/
│ │ ├── sketch_{分段编号}.json
│ │ └── sketch_{分段编号}_constraints.json
│ ├── tasks/
│ │ └── task_progress.json
│ ├── solids/
│ │ └── solid_{分段编号}.step
│ └── final/
│ └── merged.step
├── {最终模型}.step
└── {最终模型}.stl
七、测试用例速查(按功能分类)
测试1: 基本几何体
def test_primitives():
with bd.BuildPart() as part:
bd.Box(100, 80, 30)
bd.Cylinder(radius=30, height=40)
return part.part
测试2: 草图与拉伸
def test_sketch_extrude():
with bd.BuildPart() as part:
with bd.BuildSketch(bd.Plane.XY):
bd.Rectangle(60, 40)
bd.Circle(radius=10)
bd.extrude(amount=20)
return part.part
测试3: 圆角与倒角
def test_fillet_chamfer():
with bd.BuildPart() as part:
bd.Box(80, 60, 25)
x_edges = part.edges().filter_by(bd.Axis.X)
bd.fillet(x_edges, radius=3)
top_edges = part.edges().filter_by(bd.Axis.Z).sort_by(bd.Axis.Z)[-1]
bd.chamfer(top_edges, length=2)
return part.part
测试4: 孔特征
def test_holes():
with bd.BuildPart() as part:
bd.Box(100, 80, 20)
top_face = part.faces().sort_by(bd.Axis.Z)[-1]
with bd.BuildSketch(top_face):
with bd.Locations((-30, 20)):
bd.Circle(radius=6)
bd.extrude(amount=-20, mode=bd.Mode.SUBTRACT)
return part.part
测试5: 定位功能
def test_positioning():
with bd.BuildPart() as part:
with bd.Locations((10, 20, 30)):
bd.Box(50, 40, 20)
return part.part
测试6: 选择器
def test_selectors():
with bd.BuildPart() as part:
bd.Box(100, 80, 30)
top_faces = part.faces().filter_by(Axis.Z)
bottom_faces = part.faces().filter_by(Axis.Z, reverse=True)
sorted_faces = part.faces().sort_by(Axis.Z)
x_edges = part.edges().filter_by(Axis.X)
return part.part
测试7: 壳体功能
def test_shell():
with bd.BuildPart() as part:
bd.Box(100, 100, 50)
top_faces = part.part.faces().filter_by(bd.Axis.Z, reverse=True)
shelled_part = bd.Shell(top_faces)
return shelled_part
测试8: 扫掠
def test_sweep_loft():
with bd.BuildPart() as part:
with bd.BuildSketch():
bd.Circle(radius=5)
with bd.BuildLine():
bd.Line((0,0,0), (0,0,100))
bd.sweep()
return part.part
测试9: 装配与 Joints
def test_assembly_joints():
with bd.BuildPart() as base:
bd.Box(100, 80, 10)
with bd.BuildPart() as upper:
with bd.Locations((30, 20, 10)):
bd.Box(60, 40, 20)
assembly = bd.Compound([base.part, upper.part])
return assembly
测试10: 旋转阵列
def test_rotate_array():
with bd.BuildPart() as part:
bd.Cylinder(radius=50, height=10)
with bd.BuildPart() as cutter:
bd.Cylinder(radius=5, height=15)
for i in range(6):
angle = i * 60
rotated = cutter.part.rotate(bd.Axis.Z, angle)
translated = rotated.move(bd.Location(bd.Vector(35, 0, 5)))
part.part = part.part - translated
return part.part
测试11: 方向阵列
def test_linear_array():
with bd.BuildPart() as part:
bd.Box(120, 20, 8)
with bd.BuildPart() as element:
bd.Cylinder(radius=8, height=15)
for i in range(5):
translated = element.part.move(bd.Location(bd.Vector(-40 + i * 25, 0, 8)))
part.part = part.part + translated
return part.part
八、禁忌约束
禁止跨分段共用坐标系、不跨分段混用草图
禁止颠倒建模顺序:先基体增材,后减材去除,最后倒角圆角修饰
禁止不指定Axis:选择器必须指定轴向,如 filter_by(Axis.Z)
禁止使用错误的API名称:
✅ bd.extrude(小写)
❌ bd.Extrude(首字母大写)
✅ bd.fillet, bd.chamfer(小写)
❌ bd.Fillet, bd.Chamfer(首字母大写)
禁止在BuildPart外单独使用bd.add():必须在BuildPart上下文中使用
严禁省略草图图元记录、严禁省略几何约束描述
九、完整建模模板
from modeling_state import (
ModelingState, LCS, SketchProfile, SketchEntity,
EntityType, TaskType, TaskStatus
)
from workspace_manager import WorkspaceManager
import build123d as bd
import math
# 初始化
state = ModelingState()
state.project_name = "零件名称"
ws = WorkspaceManager("{项目根目录路径}")
# 分段定义
state.add_segment("S01", "主体段")
# 局部坐标系
lcs = LCS(
name="LCS-S01",
segment_id="S01",
origin=(0, 0, 0),
x_axis=(1, 0, 0),
y_axis=(0, 1, 0),
z_axis=(0, 0, 1),
description="主体段坐标系"
)
state.add_lcs(lcs)
ws.save_lcs(lcs, "S01")
# 草图图元
profile = SketchProfile("SKETCH-S01", "S01", "LCS-S01")
profile.add_entity(SketchEntity(
entity_id="LINE-001",
entity_type=EntityType.LINE,
points=[(0, 0), (100, 0)],
constraints=["起点在原点"]
))
profile.check_closure()
state.add_sketch(profile)
ws.save_sketch(profile, "S01")
# Build123D 建模
with bd.BuildPart() as part:
with bd.BuildSketch():
bd.Rectangle(100, 50)
bd.extrude(amount=20)
# 保存实体
ws.save_solid(part.part, "S01")
# 布尔合并
with bd.BuildPart() as merged_part:
bd.add(part.part)
merged = merged_part.part
# 导出
export_paths = ws.export_final(merged, "最终模型名")
print(f"STEP: {export_paths['step']}")
print(f"STL: {export_paths['stl']}")
夜雨聆风