// P2-3 官方 STEP/IGES/DXF 格式插件集成测试。 // // 覆盖: // 1) 插件发现/加载/能力注册(format:step.read / format:iges.read / format:dxf.read); // 2) STEP:内核导出真实 STEP(平面多面体)→ 直接经 PluginManager 调插件与内置 // import_cad 对比体积/bbox/面数;PREFER=1 时 import_cad 走插件分派; // 3) IGES:同上(VDE IGES 拓扑实体子集;内置 IGES 往返可能为未缝合面; // 因此体积以插件网格体(解析真值)为准,bbox/面数与内置对比); // 4) DXF:合成 3DFACE 立方体夹具 → 内置 drawing 路径不回归 + 插件产出网格体; // 5) 内置优先:默认 import_cad 不出现“插件分派”消息。 #include #include #include #include #include #include #include #include #include #include #include #include "vde/brep/assembly.h" #include "vde/brep/brep.h" #include "vde/brep/format_io.h" #include "vde/brep/iges_export.h" #include "vde/brep/measure.h" #include "vde/brep/modeling.h" #include "vde/brep/step_export.h" #include "vde/mesh/halfedge_mesh.h" #include "vde/plugin/geometry_host_services.h" #include "vde/plugin/plugin_manager.h" #ifndef VDE_PLUGIN_OFFICIAL_DIR #define VDE_PLUGIN_OFFICIAL_DIR "" #endif namespace { using vde::brep::BrepModel; using vde::brep::CadImportResult; using vde::plugin::PluginManager; constexpr const char* kStepId = "com.vde.official.step"; constexpr const char* kIgesId = "com.vde.official.iges"; constexpr const char* kDxfId = "com.vde.official.dxf"; std::string temp_path(const char* name) { return std::string(VDE_TEST_TEMP_DIR) + name; } bool write_text_file(const std::string& path, const std::string& text) { std::ofstream out(path, std::ios::binary | std::ios::trunc); if (!out.good()) return false; out.write(text.data(), static_cast(text.size())); out.close(); return out.good(); } bool set_process_env(const char* name, const char* value) { #ifdef _WIN32 // _putenv_s(name, nullptr) 非法;传空串表示删除变量 return _putenv_s(name, value ? value : "") == 0; #else return value ? (setenv(name, value, 1) == 0) : (unsetenv(name) == 0); #endif } struct Metrics { double volume = 0.0; double area = 0.0; int faces = 0; double bmin[3] = {0.0, 0.0, 0.0}; double bmax[3] = {0.0, 0.0, 0.0}; bool bbox_valid = false; }; Metrics measure_models(const std::vector& bodies) { Metrics metrics; for (const auto& body : bodies) { metrics.volume += vde::brep::volume(body); metrics.area += vde::brep::surface_area(body); metrics.faces += static_cast(body.num_faces()); const auto box = body.bounds(); const double lo[3] = {box.min().x(), box.min().y(), box.min().z()}; const double hi[3] = {box.max().x(), box.max().y(), box.max().z()}; for (int i = 0; i < 3; ++i) { if (!metrics.bbox_valid || lo[i] < metrics.bmin[i]) metrics.bmin[i] = lo[i]; if (!metrics.bbox_valid || hi[i] > metrics.bmax[i]) metrics.bmax[i] = hi[i]; } metrics.bbox_valid = true; } return metrics; } Metrics measure_handles(const VdePluginHost& host, const std::vector& bodies) { Metrics metrics; for (VdeBody body : bodies) { VdeBodyInfo info{}; info.struct_size = sizeof(VdeBodyInfo); if (host.body_query(body, &info) != VDE_PLUGIN_OK) continue; metrics.volume += info.volume; metrics.area += info.surface_area; metrics.faces += static_cast(info.num_faces); for (int i = 0; i < 3; ++i) { if (!metrics.bbox_valid || info.bbox_min[i] < metrics.bmin[i]) metrics.bmin[i] = info.bbox_min[i]; if (!metrics.bbox_valid || info.bbox_max[i] > metrics.bmax[i]) metrics.bmax[i] = info.bbox_max[i]; } metrics.bbox_valid = true; } return metrics; } void expect_bbox_close(const Metrics& expected, const Metrics& actual, double tolerance) { ASSERT_TRUE(expected.bbox_valid); ASSERT_TRUE(actual.bbox_valid); for (int i = 0; i < 3; ++i) { EXPECT_NEAR(expected.bmin[i], actual.bmin[i], tolerance) << "bbox_min[" << i << "]"; EXPECT_NEAR(expected.bmax[i], actual.bmax[i], tolerance) << "bbox_max[" << i << "]"; } } /// 立方体 12 三角面的 HalfedgeMesh → 全三角面 B-Rep(用于 STEP 精确面对比) BrepModel make_triangular_cube(double w, double h, double d) { const double x = w * 0.5, y = h * 0.5, z = d * 0.5; std::vector verts = { {-x, -y, -z}, {x, -y, -z}, {x, y, -z}, {-x, y, -z}, {-x, -y, z}, {x, -y, z}, {x, y, z}, {-x, y, z}}; const std::vector> tris = { {0, 2, 1}, {0, 3, 2}, {4, 5, 6}, {4, 6, 7}, {0, 1, 5}, {0, 5, 4}, {1, 2, 6}, {1, 6, 5}, {2, 3, 7}, {2, 7, 6}, {3, 0, 4}, {3, 4, 7}}; vde::mesh::HalfedgeMesh mesh; mesh.build_from_triangles(verts, tris); return vde::brep::mesh_to_brep_model(mesh); } std::string dxf_pair(int code, const std::string& value) { return std::to_string(code) + "\n" + value + "\n"; } std::string dxf_cube_fixture() { const double x = 10.0, y = 15.0, z = 20.0; const std::vector> points = { {-x, -y, -z}, {x, -y, -z}, {x, y, -z}, {-x, y, -z}, {-x, -y, z}, {x, -y, z}, {x, y, z}, {-x, y, z}}; const std::vector> tris = { {0, 2, 1}, {0, 3, 2}, {4, 5, 6}, {4, 6, 7}, {0, 1, 5}, {0, 5, 4}, {1, 2, 6}, {1, 6, 5}, {2, 3, 7}, {2, 7, 6}, {3, 0, 4}, {3, 4, 7}}; std::string text; text += dxf_pair(0, "SECTION") + dxf_pair(2, "HEADER") + dxf_pair(0, "ENDSEC"); text += dxf_pair(0, "SECTION") + dxf_pair(2, "ENTITIES"); for (const auto& tri : tris) { const auto& p0 = points[static_cast(tri[0])]; const auto& p1 = points[static_cast(tri[1])]; const auto& p2 = points[static_cast(tri[2])]; text += dxf_pair(0, "3DFACE"); text += dxf_pair(8, "0"); text += dxf_pair(10, std::to_string(p0[0])); text += dxf_pair(20, std::to_string(p0[1])); text += dxf_pair(30, std::to_string(p0[2])); text += dxf_pair(11, std::to_string(p1[0])); text += dxf_pair(21, std::to_string(p1[1])); text += dxf_pair(31, std::to_string(p1[2])); text += dxf_pair(12, std::to_string(p2[0])); text += dxf_pair(22, std::to_string(p2[1])); text += dxf_pair(32, std::to_string(p2[2])); // 三角面:第 4 点与第 3 点相同 text += dxf_pair(13, std::to_string(p2[0])); text += dxf_pair(23, std::to_string(p2[1])); text += dxf_pair(33, std::to_string(p2[2])); } text += dxf_pair(0, "ENDSEC") + dxf_pair(0, "EOF"); return text; } class OfficialCadFormatsFixture : public ::testing::Test { protected: void SetUp() override { host_ = vde::plugin::make_geometry_host_services(); PluginManager& manager = PluginManager::instance(); manager.clear(); manager.set_host_services(host_); manager.clear_search_paths(); if (VDE_PLUGIN_OFFICIAL_DIR[0] != '\0') manager.add_search_path(VDE_PLUGIN_OFFICIAL_DIR); manager.scan_search_paths(); } void TearDown() override { set_process_env("VDE_IMPORT_CAD_PREFER_PLUGIN", nullptr); PluginManager::instance().clear(); vde::plugin::release_geometry_host_services(&host_); for (const auto& path : cleanup_paths_) std::remove(path.c_str()); cleanup_paths_.clear(); } bool load_plugins(std::string* error) { PluginManager& manager = PluginManager::instance(); for (const char* id : {kStepId, kIgesId, kDxfId}) { if (manager.is_loaded(id)) continue; if (!manager.load(id, error)) return false; } return true; } /// 直接经 PluginManager 调用官方插件(诊断用;成功返回体句柄,调用方销毁) bool plugin_import(const std::string& path, std::vector* bodies, std::string* error) { vde::plugin::FormatImportResult result; if (!PluginManager::instance().import_file(path, &result)) { if (error) *error = PluginManager::instance().last_error(); return false; } *bodies = result.bodies; return true; } void destroy_bodies(std::vector& bodies) { for (VdeBody body : bodies) host_.body_destroy(body); bodies.clear(); } std::string save_fixture(const char* name, const std::string& text) { const std::string path = temp_path(name); EXPECT_TRUE(write_text_file(path, text)) << path; cleanup_paths_.push_back(path); return path; } VdePluginHost host_{}; std::vector cleanup_paths_; }; } // namespace TEST_F(OfficialCadFormatsFixture, PluginsDiscoveredAndLoaded) { ASSERT_NE(VDE_PLUGIN_OFFICIAL_DIR[0], '\0') << "缺少 VDE_PLUGIN_OFFICIAL_DIR"; std::string error; ASSERT_TRUE(load_plugins(&error)) << error; PluginManager& manager = PluginManager::instance(); const auto formats = manager.formats(); auto has_format = [&](const std::string& id) { for (const auto& format : formats) if (format.format_id == id) return true; return false; }; EXPECT_TRUE(has_format("step")); EXPECT_TRUE(has_format("iges")); EXPECT_TRUE(has_format("dxf")); EXPECT_TRUE(manager.capabilities().has("format:step.read")); EXPECT_TRUE(manager.capabilities().has("format:iges.read")); EXPECT_TRUE(manager.capabilities().has("format:dxf.read")); } TEST_F(OfficialCadFormatsFixture, StepPluginMatchesBuiltinAndKeepsNativePath) { std::string error; ASSERT_TRUE(load_plugins(&error)) << error; // 真实 B-Rep(盒体 6 个四边形面)经内核 STEP 导出器写出 const BrepModel body = vde::brep::make_box(20.0, 30.0, 40.0); const std::string path = temp_path("p23_plugin_box.step"); cleanup_paths_.push_back(path); ASSERT_TRUE(vde::brep::export_step_file(path, {body}, nullptr, nullptr)); // 内置优先:默认 import_cad 走内置解析(无“插件分派”消息) CadImportResult builtin = vde::brep::import_cad(path); ASSERT_TRUE(builtin.ok) << (builtin.messages.empty() ? "" : builtin.messages[0]); ASSERT_EQ(builtin.bodies.size(), 1u); const Metrics expected = measure_models(builtin.bodies); EXPECT_NEAR(std::abs(expected.volume), 24000.0, 1e-3); EXPECT_EQ(expected.faces, 6); for (const auto& message : builtin.messages) EXPECT_EQ(message.find("插件分派"), std::string::npos) << message; // 直接调官方 STEP 插件(宿主服务建体)→ 与内置对比 std::vector plugin_bodies; ASSERT_TRUE(plugin_import(path, &plugin_bodies, &error)) << error << " / host: " << vde::plugin::geometry_host_last_error(); ASSERT_EQ(plugin_bodies.size(), 1u); const Metrics plugin_metrics = measure_handles(host_, plugin_bodies); destroy_bodies(plugin_bodies); EXPECT_NEAR(std::abs(plugin_metrics.volume), 24000.0, 1e-6); expect_bbox_close(expected, plugin_metrics, 1e-6); // 插件按平面多边形三角化:6 个四边形面 → 12 个三角面(映射固定) EXPECT_EQ(plugin_metrics.faces, 2 * expected.faces); // 显式选择插件:PREFER=1 → import_cad 走官方插件分派 ASSERT_TRUE(set_process_env("VDE_IMPORT_CAD_PREFER_PLUGIN", "1")); CadImportResult dispatched = vde::brep::import_cad(path); set_process_env("VDE_IMPORT_CAD_PREFER_PLUGIN", nullptr); ASSERT_TRUE(dispatched.ok) << (dispatched.messages.empty() ? "" : dispatched.messages[0]); ASSERT_EQ(dispatched.bodies.size(), 1u); const Metrics dispatched_metrics = measure_models(dispatched.bodies); EXPECT_NEAR(std::abs(dispatched_metrics.volume), 24000.0, 1e-6); expect_bbox_close(plugin_metrics, dispatched_metrics, 1e-6); bool reported = false; for (const auto& message : dispatched.messages) if (message.find("插件分派") != std::string::npos) reported = true; EXPECT_TRUE(reported); } TEST_F(OfficialCadFormatsFixture, IgesPluginMatchesBuiltinBboxAndFaces) { std::string error; ASSERT_TRUE(load_plugins(&error)) << error; const BrepModel body = vde::brep::make_box(20.0, 30.0, 40.0); const std::string path = temp_path("p23_plugin_box.iges"); cleanup_paths_.push_back(path); ASSERT_TRUE(vde::brep::export_iges_file(path, {body})); CadImportResult builtin = vde::brep::import_cad(path); ASSERT_TRUE(builtin.ok) << (builtin.messages.empty() ? "" : builtin.messages[0]); ASSERT_FALSE(builtin.bodies.empty()); const Metrics builtin_metrics = measure_models(builtin.bodies); std::vector plugin_bodies; ASSERT_TRUE(plugin_import(path, &plugin_bodies, &error)) << error << " / host: " << vde::plugin::geometry_host_last_error(); ASSERT_EQ(plugin_bodies.size(), 1u); const Metrics plugin_metrics = measure_handles(host_, plugin_bodies); destroy_bodies(plugin_bodies); // 插件解析得到闭合多面体网格(体积≈解析真值);内置 IGES 往返可能是未缝合面片, // 因此体积以插件结果为准,bbox/面数与内置对比。 EXPECT_NEAR(std::abs(plugin_metrics.volume), 24000.0, 1e-3); expect_bbox_close(builtin_metrics, plugin_metrics, 1e-6); EXPECT_GE(plugin_metrics.faces, builtin_metrics.faces); } TEST_F(OfficialCadFormatsFixture, StepTriangularBodyMatchesBuiltinFaceCount) { std::string error; ASSERT_TRUE(load_plugins(&error)) << error; const BrepModel body = make_triangular_cube(20.0, 30.0, 40.0); const std::string path = temp_path("p23_plugin_tri.step"); cleanup_paths_.push_back(path); ASSERT_TRUE(vde::brep::export_step_file(path, {body}, nullptr, nullptr)); CadImportResult builtin = vde::brep::import_cad(path); ASSERT_TRUE(builtin.ok) << (builtin.messages.empty() ? "" : builtin.messages[0]); const Metrics expected = measure_models(builtin.bodies); std::vector plugin_bodies; ASSERT_TRUE(plugin_import(path, &plugin_bodies, &error)) << error << " / host: " << vde::plugin::geometry_host_last_error(); ASSERT_EQ(plugin_bodies.size(), 1u); const Metrics plugin_metrics = measure_handles(host_, plugin_bodies); destroy_bodies(plugin_bodies); EXPECT_NEAR(std::abs(plugin_metrics.volume), std::abs(expected.volume), 1e-6); expect_bbox_close(expected, plugin_metrics, 1e-6); EXPECT_EQ(plugin_metrics.faces, expected.faces); } TEST_F(OfficialCadFormatsFixture, DxfPluginReads3dFaceMeshAndNativePathKeepsDrawing) { std::string error; ASSERT_TRUE(load_plugins(&error)) << error; const std::string path = save_fixture("p23_plugin_cube.dxf", dxf_cube_fixture()); // 内置路径:DXF → drawing(3DFACE 图元),保持既有行为(不出现插件分派) CadImportResult builtin = vde::brep::import_cad(path); EXPECT_TRUE(builtin.has_drawing); EXPECT_EQ(builtin.drawing.entities.size(), 12u); for (const auto& message : builtin.messages) EXPECT_EQ(message.find("插件分派"), std::string::npos) << message; // 直接调官方 DXF 插件:3DFACE 立方体 → 网格实体(bbox 20×30×40,体积≈24000) std::vector plugin_bodies; ASSERT_TRUE(plugin_import(path, &plugin_bodies, &error)) << error << " / host: " << vde::plugin::geometry_host_last_error(); ASSERT_EQ(plugin_bodies.size(), 1u); const Metrics plugin_metrics = measure_handles(host_, plugin_bodies); destroy_bodies(plugin_bodies); EXPECT_NEAR(std::abs(plugin_metrics.volume), 24000.0, 1e-3); EXPECT_NEAR(plugin_metrics.bmin[0], -10.0, 1e-9); EXPECT_NEAR(plugin_metrics.bmax[0], 10.0, 1e-9); EXPECT_NEAR(plugin_metrics.bmin[1], -15.0, 1e-9); EXPECT_NEAR(plugin_metrics.bmax[1], 15.0, 1e-9); EXPECT_NEAR(plugin_metrics.bmin[2], -20.0, 1e-9); EXPECT_NEAR(plugin_metrics.bmax[2], 20.0, 1e-9); }