Files
ViewDesignEngine/python/README.md
T

152 lines
8.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# VDE Python 绑定(ctypes 路径)
本目录提供两条 Python 绑定路径:
1. **原生 pybind11 模块 `_vde`**(`-DVDE_BUILD_PYTHON=ON`):完整类型化 API,
需要与 CPython 解释器 ABI 兼容的工具链(Windows 上为 MSVC)编译。
2. **ctypes 路径 `vde._ctypes`(默认推荐,离线可用)**:不依赖编译器/ABI,
通过 `vde_capi_shared` 目标生成的 `vde_capi.dll`(Linux/macOS 为 `.so/.dylib`)
直接调用 C API,覆盖 `include/vde/capi/vde_capi.h` 的**全部 55 个函数**(48 基础 + 7 Phase6 显示/B-Rep 查询)。
`vde.native` 指示实际加载的是哪条路径;两条路径的常用对象均可用:
```python
from vde import _ctypes as vde # ctypes 路径(无需 pybind11)
# from vde import Body, Mesh, Sdf ... # 原生路径可用时也可直接导入
box = vde.Body.create_box(10, 10, 10)
mesh = box.to_mesh(0.5)
print(mesh.num_vertices(), mesh.num_faces())
mesh.free()
box.free()
```
## 环境与构建
```powershell
# 1) 构建共享 C API(必需)
cmake --build build --target vde_capi_shared -j 8
# 2) 运行全量离线 smoke(25 项检查,覆盖全部 C API 族 + 显示/B-Rep 查询)
python python/smoke_test_ctypes.py
# 或显式指定 DLL:
python python/smoke_test_ctypes.py --library build/src/libvde_capi.dll
```
加载顺序(`vde/vde/_ctypes.py`):
1. 环境变量 `VDE_LIBRARY`(显式 DLL 路径);
2. 环境变量 `VDE_BUILD_DIR` 指定的构建目录,然后仓库 `build/`、`build-*`;
3. Windows 下自动查找 MinGW runtime(`libstdc++-6/libgomp-1/libwinpthread-1` 等),
支持 `VDE_DLL_DIR` 追加目录。
## API 覆盖矩阵(55/55)
| # | C API | Python 包装 | 说明 |
|---:|---|---|---|
| 1 | `vde_create` | `Context()` / `create_context()` | 显式引擎上下文 |
| 2 | `vde_destroy` | `Context.close()` | 上下文释放 |
| 3 | `vde_version` | `version()` | 版本字符串 |
| 4 | `vde_last_error` | `last_error(ctx=None)` | 最近错误文本 |
| 5 | `vde_load_mesh` | `Mesh.load()` / `load_mesh()` | OBJ/STL/PLY/3MF/glTF/OFF |
| 6 | `vde_save_mesh` | `Mesh.save(path, fmt)` | 网格写出 |
| 7 | `vde_mesh_num_vertices` | `Mesh.num_vertices()` | |
| 8 | `vde_mesh_num_faces` | `Mesh.num_faces()` | |
| 9 | `vde_mesh_get_vertex` | `Mesh.get_vertex(i)` | 返回 `(x,y,z)` |
| 10 | `vde_mesh_get_bounds` | `Mesh.get_bounds()` | 返回 `(min, max)` |
| 11 | `vde_mesh_free` | `Mesh.free()` / `with Mesh` | |
| 12 | `vde_mesh_simplify` | `Mesh.simplify(ratio)` | 返回新网格 |
| 13 | `vde_mesh_smooth` | `Mesh.smooth(iterations)` | 返回新网格 |
| 14 | `vde_mesh_boolean` | `Mesh.boolean(other, op)` | op: `union`/`intersection`/`difference` 或 0/1/2 |
| 15 | `vde_curve_create_bezier` | `Curve.create_bezier(points)` | 3D Bezier |
| 16 | `vde_curve_evaluate` | `Curve.evaluate(t)` | 返回 `(x,y,z)` |
| 17 | `vde_curve_degree` | `Curve.degree()` | |
| 18 | `vde_curve_free` | `Curve.free()` | |
| 19 | `vde_collision_gjk_spheres` | `gjk_spheres(c1,r1,c2,r2)` | GJK 球-球相交 |
| 20 | `vde_collision_ray_mesh` | `ray_mesh(mesh, origin, dir)` | 返回 `(hit,t,point)` |
| 21 | `vde_body_create_box` | `Body.create_box(w,h,d)` | |
| 22 | `vde_body_create_cylinder` | `Body.create_cylinder(r,h,segs)` | |
| 23 | `vde_body_create_sphere` | `Body.create_sphere(r,su,sv)` | |
| 24 | `vde_body_to_mesh` | `Body.to_mesh(deflection)` | |
| 25 | `vde_body_free` | `Body.free()` | |
| 26 | `vde_body_clone` | `Body.clone()` | |
| 27 | `vde_body_load_step` | `Body.load_step(path)` | 返回 `list[Body]`(多实体) |
| 28 | `vde_body_load_iges` | `Body.load_iges(path)` | 返回 `list[Body]`(多实体) |
| 29 | `vde_body_save_step` | `Body.save_step(bodies, path)` | |
| 30 | `vde_body_save_iges` | `Body.save_iges(bodies, path)` | |
| 31 | `vde_body_boolean` | `Body.boolean(other, op)` | op 同上 |
| 32 | `vde_mesh_get_face` | `Mesh.get_face(face_idx)` | 返回顶点索引列表;内部按契约 `vde_free_buffer` |
| 33 | `vde_sdf_sphere` | `Sdf.sphere(r)` | |
| 34 | `vde_sdf_box` | `Sdf.box(hx,hy,hz)` | |
| 35 | `vde_sdf_cylinder` | `Sdf.cylinder(r,h)` | |
| 36 | `vde_sdf_union` | `Sdf.union(other)` | |
| 37 | `vde_sdf_intersection` | `Sdf.intersection(other)` | |
| 38 | `vde_sdf_difference` | `Sdf.difference(other)` | |
| 39 | `vde_sdf_to_mesh` | `Sdf.to_mesh(resolution)` | 返回 `Mesh` |
| 40 | `vde_sdf_free` | `Sdf.free()` | |
| 41 | `vde_assembly_create` | `Assembly.create(name)` | |
| 42 | `vde_assembly_add_part` | `Assembly.add_part(name, body)` | 装配持有副本 |
| 43 | `vde_assembly_part_count` | `Assembly.part_count()` | |
| 44 | `vde_assembly_free` | `Assembly.free()` | 先释放装配,再释放 body |
| 45 | `vde_body_free_array` | `body_free_array(ptr,count)` | 低层;STEP/IGES 多实体加载内部已按契约使用 |
| 46 | `vde_serialize_mesh` | `Mesh.serialize()` | 返回 `bytes`;内部 `vde_free_buffer` |
| 47 | `vde_deserialize_mesh` | `Mesh.deserialize(data)` | |
| 48 | `vde_free_buffer` | `free_buffer(ptr)` | 低层 C 缓冲区释放 |
| 49 | `vde_body_to_display_mesh` | `Body.to_display_mesh(...)` | Phase6:平滑法线 + 面 ID + 曲面类型显示网格;`DisplayMesh` 拷贝后自动释放 C 缓冲 |
| 50 | `vde_body_to_wireframe` | `Body.to_wireframe(...)` | Phase6:精确 B-Rep 边线/圆/样条线框;`Wireframe` |
| 51 | `vde_body_get_face_count` | `Body.face_count()` | Phase6:B-Rep 面数 |
| 52 | `vde_body_get_face_info` | `Body.face_info(i)` | Phase6:`FaceInfo`(surface_kind/reversed/visible/bbox/params) |
| 53 | `vde_body_get_edge_count` | `Body.edge_count()` | Phase6:B-Rep 边数 |
| 54 | `vde_body_get_edge_info` | `Body.edge_info(i)` | Phase6:`EdgeInfo`(kind/radius/center/axis/angles);圆边半径精确 |
| 55 | `vde_body_get_edge_polyline` | `Body.edge_polyline(i, tol)` | Phase6:单边按弦高采样;内部 `vde_free_buffer` |
> 覆盖核对:55 个 `vde_*` 函数全部有 Python 入口(含低层 `free_buffer` /
> `body_free_array`;高层便捷用法优先使用类方法)。
## 内存释放契约
- 每个 `Body`/`Mesh`/`Curve`/`Sdf`/`Assembly`/`Context` 均支持 `.free()`
和上下文管理器(`with ... as h:` 自动释放);释放后句柄不可再用。
- `Body.load_step/load_iges` 返回独立 `Body` 列表:C API 返回的句柄数组按
`vde_body_free_array` 语义整体释放,包装层为每个实体 `vde_body_clone`
出独立句柄供 Python 持有,避免悬垂/双重释放。
- `Mesh.get_face` / `Mesh.serialize` 返回的缓冲区由库分配,包装层在拷贝数据后
立即用 `vde_free_buffer` 释放,符合 C API 文档。
- `Body.save_step/save_iges` 不转移 body 所有权;`Assembly` 保存 part 副本,
释放顺序建议先 `Assembly.free()` 再 `Body.free()`(见 C API 文档)。
## Smoke 覆盖(`python/smoke_test_ctypes.py`,无需 pybind11)
25 项检查全部 PASS(build-curves2 + 独立 C API 测试实测):
- 诊断/上下文:`version`、`last_error`、`Context` 创建/销毁;
- B-Rep:box/cylinder/sphere、clone、union/intersection/difference、to_mesh;
- **Phase6 显示/B-Rep 查询**:box display mesh(24V/12T/6 面/法线/面 ID/Plane 类型)、
box wireframe 12 段、cylinder 圆边 kind=Circle/radius=4/折线半径校验、
face/edge info、越界错误拒绝;
- STEP + IGES 导出/导入往返(多实体加载、包围盒/网格数校验);
- Mesh:load/save(OBJ)、vertices/faces/get_vertex/get_bounds/get_face、
simplify/smooth、mesh boolean、serialize/deserialize;
- Curve:Bezier 创建/求值/次数;
- Collision:GJK 球相交 hit/miss、ray-mesh hit/miss(t=45、命中点校验);
- SDF:sphere/box/cylinder 与 union/intersection/difference 的 to_mesh;
- Assembly:create/add_part×2/part_count/free;
- 内存:**100 次 body+mesh+get_face / 100 次 curve / 100 次 context 创建释放循环无崩溃**;STEP 多实体往返 + STEP 连续 10 次 load/free(`vde_body_free_array` 契约);
- 错误契约:缺失网格文件 → `RuntimeError`(不崩溃、不静默)。
运行:
```powershell
python python/smoke_test_ctypes.py --library build/src/libvde_capi.dll
# => SMOKE PASS: 22 passed, 0 failed
```
## 已知边界
- ctypes 路径只覆盖 `vde_capi.h` 声明的 C API(C API 未暴露的模块,如装配导出、
PMI/STEP 完整语义、DWG 等,不在本绑定范围;原生 pybind11 路径可提供更多模块)。
- `vde_mesh_simplify` 的 `target_ratio` 语义由 C API 决定(当前实现返回简化结果,
网格用例见 smoke);`vde_load_mesh` 按扩展名识别格式,`fmt` 形参仅为兼容保留。
- `vde_last_error` 文本由 C API 返回(中文错误串随终端编码显示);包装层按 UTF-8
解码并对非法字节做替换,不抛异常。