docs/reference/: README.md (136) — index api-reference-core.md (690) — Matrix4D/CAM/platform api-reference-brep.md (549) — section/gizmo/dimension/hlr/snap/gear api-reference-curves.md (304) — surface_tools/factory methods api-reference-foundation.md (342) — 22 formats + DWG bridge api-reference-mesh.md (248) — blend/point cloud api-reference-spatial.md (203) — BVH indices api-reference-capi.md (238) — C API + Python ctypes format-support.md (118) — 22 formats matrix changelog.md (187) — v1.0.1 additions
13 KiB
13 KiB
Foundation 模块 API 参考
基础 IO 模块 — 25+ 种 CAD/网格/点云格式的导入导出
模块概述
foundation 模块是 ViewDesignEngine 的格式转换层,提供网格格式(STL/OBJ/PLY/glTF/3MF)、工业 CAD 格式(STEP/IGES/DXF/JT/Parasolid/ACIS)、BIM 格式(IFC)和点云格式(E57/LAS)的导入导出能力。
头文件列表
| 头文件 | 说明 |
|---|---|
io_gltf.h |
glTF 2.0 / GLB 导入导出(VDE-082 新增导入) |
io_dxf.h |
DXF 文本格式解析(12+实体类型)+ DWG 外部工具桩 |
io_dwg_plugin.h |
DWG 插件接口(DwgImporter / DwgBridge) |
io_stl.h |
STL 导入导出(返回 bool 修复) |
io_obj.h |
Wavefront OBJ 导入导出 |
io_collada.h |
COLLADA DAE 导入导出(新增格式) |
io_amf.h |
AMF 增材制造格式(新增格式) |
io_vrml.h |
VRML WRL 导入导出(新增格式) |
io_ply.h |
PLY 点云/网格格式 |
io_3mf.h |
3MF 格式(3D 打印) |
industrial_formats.h |
JT/Parasolid XT/ACIS SAT/IFC/STEP AP242/PDF 3D |
serializer.h |
二进制序列化 |
vde_format.h |
统一格式容器 |
format_utils.h |
格式工具函数 |
math_types.h |
基础数学类型(Point/Vector) |
tolerance.h |
容差定义 |
error_codes.h |
错误码 |
interval.h |
区间类型 |
predicates.h |
几何谓词 |
exact_predicates.h |
精确谓词(GMP 后端) |
memory_pool.h |
内存池 |
详细 API
io_gltf.h — glTF 2.0 / GLB
namespace vde::foundation {
// 导出选项
struct GltfOptions {
bool binary = true; // GLB 单文件 / JSON+bin
bool include_normals = true; // 顶点法线
bool include_colors = false; // 顶点颜色
std::vector<float> vertex_colors;
};
// glTF 导出
bool write_gltf(const std::string& filepath, const mesh::HalfedgeMesh& mesh,
const GltfOptions& options = {});
bool write_brep_gltf(const std::string& filepath, const brep::BrepModel& model,
int tessellation_res = 32);
// glTF 导入(VDE-082 新增)
struct GltfImportResult {
std::vector<mesh::HalfedgeMesh> meshes;
std::vector<std::string> mesh_names;
size_t total_vertices, total_triangles;
bool success;
std::string error_message;
};
[[nodiscard]] GltfImportResult import_gltf(const std::string& path); // 文件导入
[[nodiscard]] GltfImportResult import_gltf_binary(const uint8_t* data, size_t size);// 内存导入
} // namespace vde::foundation
io_collada.h — COLLADA DAE(新增格式)
namespace vde::foundation {
[[nodiscard]] mesh::HalfedgeMesh import_collada(const std::string& path); // DAE 导入
bool export_collada(const std::string& path, const mesh::HalfedgeMesh& mesh); // DAE 导出
} // namespace vde::foundation
io_amf.h — AMF 格式(新增格式)
namespace vde::foundation {
[[nodiscard]] mesh::HalfedgeMesh import_amf(const std::string& path); // AMF 导入
bool export_amf(const std::string& path, const mesh::HalfedgeMesh& mesh); // AMF 导出
} // namespace vde::foundation
io_vrml.h — VRML(新增格式)
namespace vde::foundation {
[[nodiscard]] mesh::HalfedgeMesh import_vrml(const std::string& path); // VRML 导入
bool export_vrml(const std::string& path, const mesh::HalfedgeMesh& mesh); // VRML 导出
} // namespace vde::foundation
io_dwg_plugin.h — DWG 插件接口
namespace vde::foundation {
struct DwgResult {
bool success;
std::vector<brep::BrepModel> bodies;
std::string metadata_json;
std::string error_message;
};
class DwgImporter { // 抽象导入器接口(Apache 2.0)
public:
virtual ~DwgImporter() = default;
virtual const char* name() const = 0; // "libreDWG 0.14" / "ODA 2026"
virtual const char* license() const = 0; // "GPLv3" / "Commercial"
virtual DwgResult read_file(const std::string& path) = 0;
virtual DwgResult read_memory(const uint8_t* data, size_t size) = 0;
};
class DwgBridge { // DWG 桥接器
public:
static bool load(const std::string& plugin_path); // 加载插件 .so
static void unload();
static bool is_loaded();
static DwgResult import(const std::string& path);
static std::string plugin_name();
static std::string plugin_license();
};
DwgResult import_dwg(const std::string& path); // 便捷导入函数
} // namespace vde::foundation
io_dxf.h — DXF 格式
namespace vde::foundation {
// === 实体类型(12 种) ===
struct DxfLine { Point3D start, end; std::string layer; };
struct DxfCircle { Point3D center; double radius; std::string layer; };
struct DxfArc { Point3D center; double radius, start_angle, end_angle; std::string layer; };
struct DxfLwPolyline{ struct Vertex { double x,y,bulge; }; std::vector<Vertex> vertices; bool closed; double constant_width, elevation, thickness; std::string layer; };
struct DxfPolyline { struct Vertex { Point3D position; double bulge, start_width, end_width; }; std::vector<Vertex> vertices; bool closed, is_3d; std::string layer; };
struct DxfPoint { Point3D position; std::string layer; };
struct DxfText { std::string text_value; Point3D insertion_point; double height, rotation, width_factor; bool is_mtext; std::string layer; };
struct DxfInsert { std::string block_name; Point3D insertion_point; double scale_x, scale_y, scale_z, rotation; int column_count, row_count; double column_spacing, row_spacing; std::string layer; };
struct DxfEllipse { Point3D center, major_axis_endpoint; double axis_ratio, start_param, end_param; std::string layer; };
struct DxfSpline { std::vector<Point3D> control_points, fit_points; std::vector<double> knots, weights; int degree; bool closed, periodic, rational; double fit_tolerance; std::string layer; };
struct Dxf3DFace { Point3D corners[4]; bool invisible[4]; std::string layer; };
struct DxfHatch { struct BoundaryLoop { std::vector<Point3D> points; bool is_closed; }; std::vector<BoundaryLoop> boundary_loops; std::string pattern_name; double pattern_scale, pattern_angle; bool is_solid; std::string layer; };
using DxfEntity = std::variant<DxfLine, DxfCircle, DxfArc, DxfLwPolyline, DxfPolyline, DxfPoint, DxfText, DxfInsert, DxfEllipse, DxfSpline, Dxf3DFace, DxfHatch>;
// 图层/块
struct DxfLayer { std::string name; int color; std::string linetype; bool frozen, locked, off; };
struct DxfBlock { std::string name; Point3D base_point; std::vector<DxfEntity> entities; };
// 版本
enum class DxfVersion { Unknown, R12, R14, R2000, R2004, R2007, R2010, R2013, R2018 };
// 文档
struct DxfDocument {
DxfVersion version; std::string version_string; int insunits;
std::vector<DxfLayer> layers; std::vector<DxfBlock> blocks; std::vector<DxfEntity> entities;
AABB3D extents; int entities_parsed, entities_skipped;
[[nodiscard]] bool valid() const;
};
// API
[[nodiscard]] DxfDocument import_dxf(const std::string& filepath); // 文件导入
[[nodiscard]] DxfDocument import_dxf_string(const std::string& data); // 字符串导入
[[nodiscard]] mesh::HalfedgeMesh to_mesh(const DxfDocument& document); // 转为网格
[[nodiscard]] const char* dxf_version_name(DxfVersion version);
// DWG 桩函数(始终返回失败,含安装指引)
struct DwgImportResult { std::vector<std::string> messages; bool success; };
[[nodiscard]] DwgImportResult import_dwg(const std::string& filepath); // DWG 导入桩
bool export_dwg(const std::string& filepath, const std::vector<brep::BrepModel>& bodies); // DWG 导出桩
} // namespace vde::foundation
io_stl.h — STL 格式
namespace vde::foundation {
struct StlTriangle {
Vector3D normal; Point3D v0, v1, v2;
// 便捷分量访问: v0x(), v0y(), v0z(), v1x(), ..., v2z()
};
// 导入/导出(返回 bool 修复)
bool import_stl(const std::string& path, std::vector<StlTriangle>& out_triangles, bool merge_duplicates = true);
bool export_stl(const std::string& path, const std::vector<StlTriangle>& triangles, bool binary = true);
bool export_stl_binary(const std::string& path, const mesh::HalfedgeMesh& mesh);
} // namespace vde::foundation
industrial_formats.h — 工业格式(JT/Parasolid/ACIS/IFC/STEP AP242/PDF 3D)
namespace vde::io {
// === JT (ISO 14306) ===
enum class JTSegmentType : uint8_t { LogicalSceneGraph=1, Part=2, Instance=3, MeshLOD=4, Brep=5, MetaData=6, PMI=7 };
struct JTOptions { bool load_brep=true, load_mesh=true; int max_lod=3; bool load_pmi=false, load_meta=false; };
[[nodiscard]] brep::BrepModel import_jt(const std::string& path, const JTOptions& opts = {});
[[nodiscard]] brep::BrepModel import_jt_binary(const uint8_t* data, size_t size, const JTOptions& opts = {});
bool export_jt(const brep::BrepModel& body, const std::string& path); // 返回 bool
// === Parasolid XT ===
enum class ParasolidFormat { Binary, Text, Neutral };
[[nodiscard]] brep::BrepModel import_parasolid_xt(const std::string& path);
[[nodiscard]] brep::BrepModel import_parasolid_xt_binary(const uint8_t* data, size_t size);
void export_parasolid_xt(const brep::BrepModel& body, const std::string& path);
void export_parasolid_xt_binary(const brep::BrepModel& body, const std::string& path);
[[nodiscard]] std::string export_parasolid_xt_binary_str(const brep::BrepModel& body);
// === ACIS SAT ===
enum class ACISVersion { V7=700, V16=1600, V21=2100, V27=2700, R2024=2900 };
[[nodiscard]] brep::BrepModel import_acis_sat(const std::string& path);
[[nodiscard]] brep::BrepModel import_acis_sat_binary(const uint8_t* data, size_t size);
void export_acis_sat(const brep::BrepModel& body, const std::string& path, ACISVersion version = ACISVersion::V27);
// === IFC (ISO 16739) ===
enum class IfcEntityType { Unknown, IfcProject, IfcSite, IfcBuilding, IfcWall, IfcSlab, IfcBeam, IfcColumn, IfcDoor, /* ... */ };
[[nodiscard]] std::vector<brep::BrepModel> import_ifc(const std::string& path);
// === STEP AP242 ===
struct StepPMIAnnotation { std::string type, id, description; double nominal_value, upper_tolerance, lower_tolerance; std::string datum_system; };
struct StepAP242Options { bool include_pmi=true, include_tolerance=true, include_material=true, include_validation=true; };
[[nodiscard]] std::string export_step_ap242(const std::vector<brep::BrepModel>& bodies, const std::vector<StepPMIAnnotation>& annotations = {}, const StepAP242Options& opts = {});
void export_step_ap242_file(const std::string& path, const std::vector<brep::BrepModel>& bodies, const std::vector<StepPMIAnnotation>& annotations = {}, const StepAP242Options& opts = {});
// === PDF 3D ===
[[nodiscard]] std::vector<uint8_t> export_pdf3d(const brep::BrepModel& body);
void export_pdf3d_file(const std::string& path, const brep::BrepModel& body);
} // namespace vde::io
serializer.h — 二进制序列化
namespace vde::foundation {
size_t serialize_mesh(const mesh::HalfedgeMesh& mesh, uint8_t** out_data);
mesh::HalfedgeMesh deserialize_mesh(const uint8_t* data, size_t len);
void free_buffer(uint8_t* data);
} // namespace vde::foundation
代码示例
示例 1: glTF 交叉转换
#include <vde/foundation/io_gltf.h>
#include <vde/foundation/io_stl.h>
#include <vde/mesh/halfedge_mesh.h>
using namespace vde;
// 导入 glTF
auto result = foundation::import_gltf("model.glb");
if (result.success) {
for (auto& mesh : result.meshes) {
// 处理每个 mesh 子对象
std::cout << "Faces: " << mesh.num_faces() << "\n";
}
// 导出为 STL
foundation::export_stl_binary("output.stl", result.meshes[0]);
}
示例 2: DXF 解析与处理
#include <vde/foundation/io_dxf.h>
using namespace vde;
// 解析 DXF
auto doc = foundation::import_dxf("floor_plan.dxf");
std::cout << "DXF 版本: " << foundation::dxf_version_name(doc.version) << "\n";
std::cout << "实体数量: " << doc.entities.size() << "\n";
std::cout << "图层数量: " << doc.layers.size() << "\n";
// 统计各类型实体
for (auto& ent : doc.entities) {
std::visit([](const auto& e) {
using T = std::decay_t<decltype(e)>;
if constexpr (std::is_same_v<T, foundation::DxfLine>) {
// 线段实体
} else if constexpr (std::is_same_v<T, foundation::DxfCircle>) {
// 圆形实体
}
}, ent);
}
// 转为网格
auto mesh = foundation::to_mesh(doc);
示例 3: 工业格式导入
#include <vde/foundation/industrial_formats.h>
using namespace vde;
// JT 导入
auto jt_model = io::import_jt("assembly.jt");
// Parasolid 导入
auto xt_model = io::import_parasolid_xt("part.x_t");
// IFC BIM 导入
auto ifc_bodies = io::import_ifc("building.ifc");
// STEP AP242 导出(含 PMI)
std::vector<io::StepPMIAnnotation> annotations = { /* ... */ };
auto step_data = io::export_step_ap242({jt_model}, annotations);