Model
class Model(session: NativeSession | None = None)
Model 是交互式原生几何的推荐入口。默认情况下它持有一个 NativeSession,并把返回的原生 Handle 包装为 Shape。
生命周期
import cadflow as cad
with cad.Model() as model:
body = model.box(40.0, 25.0, 6.0)
result = body.describe(detail="summary")
# 此处不能继续查询 body
| 成员 | 契约 |
|---|---|
Model() | 创建并持有一个新 NativeSession |
Model(session) | 借用现有 Session;最终关闭仍由调用方负责 |
close() | 仅关闭由当前 Model 创建的 Session |
| 上下文管理器 | 退出时调用 close(),不吞掉原始异常 |
session | 当前底层 Session;除适配器实现外优先使用 Model 方法 |
所有涉及两个 Shape 的调用都要求 Session 完全相同。前端能够提前判断时,来自其他 Model 的 Shape 会在原生调用前抛出 ValueError。
构造方法组
| 分组 | 方法 |
|---|---|
| 基本体 | box、cylinder、sphere、cone |
| 导入 | import_step、import_brep、import_stl |
| 曲线与 Wire | polyline、circle_profile、arc、interpolate、helix、bspline |
| Face 与曲面 | face、bezier_surface、fit_surface、ruled_surface、filling_surface、gordon_surface |
| 特征 | extrude、revolve、loft、sweep、twisted_sweep、fillet、chamfer、shell |
| 布尔与缝合 | cut、union、intersect、sew、shell_to_solid |
| 变换 | translate、rotate、mirror、scale |
| 查询 | distance、faces、subshapes、free_boundaries |
| 局部构造 | workplane、sketch |
创建与导入、特征与变换以及拓扑与交换解释参数与工作流边界;自动生成的 Model 页面提供精确成员清单。
动态派发
capabilities() 描述已知操作与能力组,不暴露私有 Handle。由其他系统动态选择操作时可先读取它。
caps = model.capabilities()
report = model.preflight("box", 40.0, 25.0, 6.0)
if report.ok:
created = model.apply("box", 40.0, 25.0, 6.0)
body = created.value
preflight() 对常见调用问题进行归一化并返回 OperationReport,但不创建几何。apply() 预检后调用具名公共 Model 操作。操作在代码中静态已知时,直接方法更简单,也保留更好的类型检查。
Workplane 与 Sketch
model.workplane(...) 返回附着于当前 Model 的局部坐标系,其曲线辅助方法会先把局部坐标转换为世界坐标。model.sketch(...) 在可选 Workplane 上创建声明式草图。
with model.workplane(origin=(0, 0, 10), normal=(0, 1, 0)) as plane:
profile = plane.circle_profile(4.0)
face = model.face(profile)
pin = plane.extrude(face, (0, 0, 12.0))
失败与副作用
基本体尺寸、公差、向量和路径由对应原生或领域调用验证。导入与导出方法会执行文件 I/O。几何操作返回新 Shape,不修改输入 Handle。所有路径都应关闭 Model,优先使用 with。