跳到主要内容

快速开始

本教程会创建一个带通孔的安装板,检查最终实体,并同时导出工程用 STEP 文件和浏览器可直接加载的 GLB 预览。

构建零件

创建 mounting_plate.py

mounting_plate.py
import json
from pathlib import Path

import cadflow as cad


output = Path("output")
output.mkdir(exist_ok=True)

with cad.Model() as model:
plate = model.box(80, 50, 8)

bore = model.cylinder(radius=6, height=12)
bore = model.translate(bore, 20, 25, -2)
part = model.cut(plate, bore)

report = part.validate()
if not report.ok:
raise RuntimeError(report.to_dict())

print(json.dumps(part.describe(), indent=2))
part.export_step(str(output / "mounting_plate.step"))
part.export_stl(str(output / "mounting_plate.stl"))
part.export_preview_glb(str(output / "mounting_plate.glb"))

在已经安装 CadFlow 的环境内运行:

python mounting_plate.py
ls -lh output/

理解模型生命周期

cad.Model() 管理一个原生 C++ Session。该 Model 返回的每个 Shape 都是指向 Session 内部几何的轻量句柄:

Model 上下文
└─ 原生 Session
├─ plate 句柄
├─ bore 句柄
└─ final part 句柄

不同 Model 创建的 Shape 不能相互组合,Model 关闭后其中的句柄也会失效。因此应在 with cad.Model() 代码块内完成构造、测量和导出。

读取结构化几何证据

Shape.describe() 返回 JSON-safe 值,而不是格式化控制台文本。摘要包含如下字段:

{
"kind": "solid",
"volume": 18904.42,
"bbox": [0.0, 0.0, 0.0, 80.0, 50.0, 8.0],
"topology": {
"solids": 1,
"faces": 7
}
}

确切计数和浮点值取决于最终 OCCT 拓扑。程序应直接使用报告字段,不要解析展示文本。

交付前验证

Shape.validate() 检查 Shape 并返回 OperationReport。交付代码还应叠加工作流自身的不变量:

report = part.validate()
if not report.ok:
raise RuntimeError(report.to_dict())

if part.topology.get("solids") != 1:
raise RuntimeError(f"expected one solid: {part.topology}")

if part.volume <= 0:
raise RuntimeError("part has no positive volume")

这样可以明确区分几何内核有效性和你的工程预期。

选择输出格式

输出方法典型使用方
STEPshape.export_step(path)CAD 软件和下游制造工作流
STLshape.export_stl(path, binary=True)基于网格的制造与检查工具
GLBshape.export_preview_glb(path)浏览器、Web Viewer 和智能体运行记录
网格数组shape.mesh(deflection=0.1)自定义渲染器和数值处理

下一步