Files
ViewDesignEngine/tests/plugin/test_plugin_official_cad_formats.cpp
hm 5a2d82ff2f feat: P2-3 STEP/IGES/DXF 插件 + P2-4 vde-service + P3-1 安全 + P3-2 shared/plugin 模式(1.0.48)
### P2-3 STEP/IGES/DXF 官方格式插件
- plugins/official/{step,iges,dxf}(纯 C ABI,宿主 body_from_mesh 建体);import_cad 沿用内置优先/显式/回退策略;
  STEP 盒体内置/插件体积 bbox 一致(全三角面时面数一致)、IGES 闭合体积 24000/bbox 一致、DXF 合成 12 tri/24000;
- test_plugin_official_cad_formats 5/5;插件 ctest 65/65。

### P2-4 vde-service 进程外服务接入 + 沙箱/超时
- PluginManager:vde-service.json 发现/扫描、invoke_external_service(request/response、env/cwd 白名单、
  超时 kill、输出/响应上限、protocol 校验、outputs 越界拒绝、崩溃隔离);官方 STL→OBJ 端到端;
- test_plugin_external_service 12/12;docs/design/vde-service-protocol.md + plugin-development §12。

### P3-1 插件签名/许可/权限(分层正确)
- plugin_security.h/cpp(vde_plugin,仅 foundation;宿主注入 SignatureVerifier,不依赖 vde_license);
  canonical manifest payload、ed25519 签名、Policy(require_signature/allow_unsigned/publisher 白名单/权限默认拒绝)、
  manager 钩子(默认关闭,行为不变)、vde_plugin_tool verify;test_plugin_security 28/28 + tool 真实验签 7 类;
  docs/design/plugin-security.md。

### P3-2 VDE_MODULES_MODE shared/plugin 真实实现
- 四值真实生效:shared = C ABI 共享内核 + 官方/示例插件共享库;plugin = shared + 插件部署/加载;
  缺 CAPI/PLUGIN FATAL、非法值 FATAL;plugins/shared_mode smoke + plugins/abi_matrix(1.0/1.1/1.2 接受,1.3/2.0 拒绝);
- shared ctest 97/97、plugin ctest 98/98、ABI_MATRIX accepted=3 rejected=2 failures=0;SDK 插件 install 布局。

### 工程
- .gitignore 修复后本批新增 CMakeLists 全部纳入版本库;版本 1.0.48(CMake/version.h/test_version/README/docs index/module-manual);
- 全量构建 0 error(357 targets);全量 ctest 7441 注册、7346/7346 PASS(0 failed,24 skipped,153.25s)。
2026-10-28 17:34:48 +08:00

376 lines
16 KiB
C++
Raw Permalink 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.
// 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 <gtest/gtest.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#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<std::streamsize>(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<BrepModel>& 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<int>(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<VdeBody>& 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<int>(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<vde::core::Point3D> 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<std::array<int, 3>> 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<std::array<double, 3>> 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<std::array<int, 3>> 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<std::size_t>(tri[0])];
const auto& p1 = points[static_cast<std::size_t>(tri[1])];
const auto& p2 = points[static_cast<std::size_t>(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<VdeBody>* 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<VdeBody>& 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<std::string> 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<VdeBody> 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<VdeBody> 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<VdeBody> 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<VdeBody> 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);
}