Files
ViewDesignEngine/plugins/official/step/step_plugin.cpp
T
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

441 lines
17 KiB
C++
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.
// 官方 STEP 格式插件(VDE 微内核 P2-3;纯 C ABI,不链接内核)。
//
// 目标:以插件形式提供 STEP(ISO-10303-21)**平面多面体子集**的读取能力,
// 解析后经宿主 body_from_mesh 创建实体(与官方内核导入器同源网格→B-Rep 转换)。
//
// 支持子集(用于 VDE 自身 STEP 导出文件与常见平面多面体):
// - CARTESIAN_POINT / VERTEX_POINT / EDGE_CURVE / ORIENTED_EDGE / EDGE_LOOP
// - FACE_OUTER_BOUND / FACE_BOUND / ADVANCED_FACE / CLOSED_SHELL / OPEN_SHELL
// - MANIFOLD_SOLID_BREP(多体合并为单网格体)
// 不支持(已知限制):NURBS/圆锥/球面等解析曲面(面仍按边界多边形三角化,
// 曲面被"拉平")、带孔面(内环忽略 → 仅外环)、装配/product 结构(忽略)。
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include "../common/format_plugin_common.h"
namespace {
using vde_plugin_common::MeshAccumulator;
using vde_plugin_common::fail;
using vde_plugin_common::read_file_bytes;
using vde_plugin_common::validate_read_host;
constexpr std::size_t kMaxEntities = 5000000u;
constexpr std::size_t kMaxTriangles = 50000000u;
struct StepEntity {
std::string type;
std::string args;
};
bool is_space(char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v';
}
std::string trim(const std::string& text) {
std::size_t begin = 0, end = text.size();
while (begin < end && is_space(text[begin])) ++begin;
while (end > begin && is_space(text[end - 1])) --end;
return text.substr(begin, end - begin);
}
/** 解析 DATA 段实体:每行 "#id = TYPE(args);"(VDE 导出为单行/实体)。 */
bool parse_entities(const std::string& text, std::map<int, StepEntity>* entities) {
if (!entities) return false;
entities->clear();
const std::size_t data_pos = text.find("DATA;");
if (data_pos == std::string::npos) return false;
std::size_t pos = data_pos + 5;
const std::size_t end_pos = text.find("ENDSEC;", pos);
const std::size_t limit = (end_pos == std::string::npos) ? text.size() : end_pos;
while (pos < limit) {
std::size_t line_end = text.find('\n', pos);
if (line_end == std::string::npos || line_end > limit) line_end = limit;
const std::string line = trim(text.substr(pos, line_end - pos));
pos = line_end + 1;
if (line.empty() || line[0] != '#') continue;
const std::size_t eq = line.find('=');
if (eq == std::string::npos) continue;
const int id = static_cast<int>(std::strtol(line.c_str() + 1, nullptr, 10));
if (id <= 0) continue;
std::size_t p = eq + 1;
while (p < line.size() && is_space(line[p])) ++p;
const std::size_t type_begin = p;
while (p < line.size() && (std::isalnum(static_cast<unsigned char>(line[p])) ||
line[p] == '_'))
++p;
if (p == type_begin) continue;
StepEntity entity;
entity.type = line.substr(type_begin, p - type_begin);
const std::size_t open = line.find('(', p);
const std::size_t close = line.rfind(')');
if (open == std::string::npos || close == std::string::npos || close <= open) continue;
entity.args = line.substr(open + 1, close - open - 1);
entities->emplace(id, std::move(entity));
if (entities->size() > kMaxEntities) return false;
}
return !entities->empty();
}
/** 提取字符串中所有 "#<id>" 引用(保持出现顺序)。 */
std::vector<int> refs_of(const std::string& args) {
std::vector<int> refs;
for (std::size_t i = 0; i < args.size(); ++i) {
if (args[i] != '#') continue;
char* end = nullptr;
const long id = std::strtol(args.c_str() + i + 1, &end, 10);
if (end == args.c_str() + i + 1 || id <= 0) continue;
refs.push_back(static_cast<int>(id));
i = static_cast<std::size_t>(end - args.c_str()) - 1;
}
return refs;
}
/** 提取所有数字(跳过 #id 引用)。 */
std::vector<double> numbers_of(const std::string& args) {
std::vector<double> values;
std::size_t i = 0;
while (i < args.size()) {
const char c = args[i];
if (c == '#') {
++i;
while (i < args.size() && std::isdigit(static_cast<unsigned char>(args[i]))) ++i;
continue;
}
if (std::isdigit(static_cast<unsigned char>(c)) || c == '-' || c == '+' || c == '.') {
char* end = nullptr;
const double value = std::strtod(args.c_str() + i, &end);
if (end == args.c_str() + i) {
++i;
continue;
}
values.push_back(value);
i = static_cast<std::size_t>(end - args.c_str());
continue;
}
++i;
}
return values;
}
bool get_point(const std::map<int, StepEntity>& entities, int id, double out[3]) {
auto it = entities.find(id);
if (it == entities.end() || it->second.type != "CARTESIAN_POINT") return false;
const std::vector<double> values = numbers_of(it->second.args);
if (values.size() < 3) return false;
// CARTESIAN_POINT('name',(x,y,z)):取最后 3 个数
out[0] = values[values.size() - 3];
out[1] = values[values.size() - 2];
out[2] = values[values.size() - 1];
return true;
}
bool get_vertex(const std::map<int, StepEntity>& entities, int id, int* point_id) {
auto it = entities.find(id);
if (it == entities.end() || it->second.type != "VERTEX_POINT") return false;
const std::vector<int> refs = refs_of(it->second.args);
if (refs.empty()) return false;
*point_id = refs[0];
return true;
}
bool get_edge_curve(const std::map<int, StepEntity>& entities, int id, int* v_start,
int* v_end) {
auto it = entities.find(id);
if (it == entities.end() || it->second.type != "EDGE_CURVE") return false;
const std::vector<int> refs = refs_of(it->second.args);
if (refs.size() < 3) return false; // v1, v2, curve
*v_start = refs[0];
*v_end = refs[1];
return true;
}
bool get_oriented_edge(const std::map<int, StepEntity>& entities, int id, int* edge_id,
bool* same_sense) {
auto it = entities.find(id);
if (it == entities.end() || it->second.type != "ORIENTED_EDGE") return false;
const std::vector<int> refs = refs_of(it->second.args);
if (refs.empty()) return false;
*edge_id = refs[0];
*same_sense = it->second.args.find(".F.") == std::string::npos;
return true;
}
bool get_loop_edges(const std::map<int, StepEntity>& entities, int id,
std::vector<int>* oriented_edges) {
auto it = entities.find(id);
if (it == entities.end() || it->second.type != "EDGE_LOOP") return false;
*oriented_edges = refs_of(it->second.args);
return !oriented_edges->empty();
}
bool get_bound_loop(const std::map<int, StepEntity>& entities, int id, int* loop_id,
bool* is_outer) {
auto it = entities.find(id);
if (it == entities.end()) return false;
if (it->second.type != "FACE_OUTER_BOUND" && it->second.type != "FACE_BOUND")
return false;
const std::vector<int> refs = refs_of(it->second.args);
if (refs.empty()) return false;
*loop_id = refs[0];
*is_outer = it->second.type == "FACE_OUTER_BOUND";
return true;
}
bool get_face_bounds(const std::map<int, StepEntity>& entities, int id,
std::vector<int>* bound_ids) {
auto it = entities.find(id);
if (it == entities.end() || it->second.type != "ADVANCED_FACE") return false;
// ADVANCED_FACE('',(#b1,#b2,...),#surface,.T.):第二个参数是 bounds 列表
const std::size_t first_comma = it->second.args.find(',');
if (first_comma == std::string::npos) return false;
const std::size_t list_begin = it->second.args.find('(', first_comma);
const std::size_t list_end = it->second.args.find(')', list_begin);
if (list_begin == std::string::npos || list_end == std::string::npos) return false;
*bound_ids = refs_of(it->second.args.substr(list_begin, list_end - list_begin));
return !bound_ids->empty();
}
bool get_shell_faces(const std::map<int, StepEntity>& entities, int id,
std::vector<int>* face_ids, bool* closed) {
auto it = entities.find(id);
if (it == entities.end()) return false;
if (it->second.type != "CLOSED_SHELL" && it->second.type != "OPEN_SHELL") return false;
*face_ids = refs_of(it->second.args);
*closed = it->second.type == "CLOSED_SHELL";
return true;
}
bool get_shell_of_brep(const std::map<int, StepEntity>& entities, int id, int* shell_id) {
auto it = entities.find(id);
if (it == entities.end() || it->second.type != "MANIFOLD_SOLID_BREP") return false;
const std::vector<int> refs = refs_of(it->second.args);
if (refs.empty()) return false;
*shell_id = refs[0];
return true;
}
/** 有向边 → 有序多边形顶点(按 STEP 顶点 id);失败返回 false(开放/自交环跳过)。 */
bool build_polygon(const std::map<int, StepEntity>& entities,
const std::vector<int>& oriented_edges, std::vector<int>* vertices) {
vertices->clear();
std::vector<std::pair<int, int>> segments; // (start_vertex_id, end_vertex_id)
for (int oe_id : oriented_edges) {
int edge_id = 0;
bool same = true;
if (!get_oriented_edge(entities, oe_id, &edge_id, &same)) return false;
int v_start = 0, v_end = 0;
if (!get_edge_curve(entities, edge_id, &v_start, &v_end)) return false;
if (same) segments.emplace_back(v_start, v_end);
else segments.emplace_back(v_end, v_start);
}
if (segments.empty()) return false;
vertices->push_back(segments[0].first);
int current = segments[0].second;
vertices->push_back(current);
std::vector<bool> used(segments.size(), false);
used[0] = true;
std::size_t used_count = 1;
while (used_count < segments.size()) {
bool found = false;
for (std::size_t i = 0; i < segments.size(); ++i) {
if (used[i]) continue;
if (segments[i].first == current) {
current = segments[i].second;
vertices->push_back(current);
used[i] = true;
++used_count;
found = true;
break;
}
}
if (!found) return false; // 边不连续(环不一致)
}
if (current != vertices->front()) return false; // 未闭合
vertices->pop_back(); // 首尾重复
return vertices->size() >= 3;
}
int step_read_file(const VdePluginHost* host, const char* path, VdeBody** bodies,
std::size_t* count) {
if (bodies) *bodies = nullptr;
if (count) *count = 0;
if (!bodies || !count) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: 输出指针为空");
const int host_rc = validate_read_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
std::string text;
if (!path || !read_file_bytes(path, &text))
return fail(host, VDE_PLUGIN_ERR_IO, "step: 无法读取输入文件");
if (text.find("ISO-10303-21") == std::string::npos)
return fail(host, VDE_PLUGIN_ERR_PARSE, "step: 缺少 ISO-10303-21 头");
std::map<int, StepEntity> entities;
if (!parse_entities(text, &entities))
return fail(host, VDE_PLUGIN_ERR_PARSE, "step: DATA 段无实体");
// 顶点 id → 网格顶点下标(共享顶点去重)
std::map<int, uint32_t> vertex_index;
auto ensure_vertex = [&](int vertex_id, MeshAccumulator& mesh, uint32_t* index) -> bool {
auto found = vertex_index.find(vertex_id);
if (found != vertex_index.end()) {
*index = found->second;
return true;
}
int point_id = 0;
double point[3] = {0.0, 0.0, 0.0};
if (!get_vertex(entities, vertex_id, &point_id) ||
!get_point(entities, point_id, point))
return false;
const uint32_t created = mesh.add_vertex(point[0], point[1], point[2]);
vertex_index.emplace(vertex_id, created);
*index = created;
return true;
};
MeshAccumulator mesh;
std::size_t skipped_faces = 0;
std::size_t parsed_faces = 0;
auto triangulate_face = [&](int face_id) {
std::vector<int> bound_ids;
if (!get_face_bounds(entities, face_id, &bound_ids)) {
++skipped_faces;
return;
}
std::vector<int> outer_loop_edges;
bool found_outer = false;
for (int bound_id : bound_ids) {
int loop_id = 0;
bool is_outer = false;
if (!get_bound_loop(entities, bound_id, &loop_id, &is_outer)) continue;
if (!is_outer) continue; // 已知限制:内环(孔)忽略
found_outer = get_loop_edges(entities, loop_id, &outer_loop_edges);
if (found_outer) break;
}
if (!found_outer) {
++skipped_faces;
return;
}
std::vector<int> polygon;
if (!build_polygon(entities, outer_loop_edges, &polygon) || polygon.size() < 3) {
++skipped_faces;
return;
}
std::vector<uint32_t> indices(polygon.size(), 0);
for (std::size_t i = 0; i < polygon.size(); ++i) {
if (!ensure_vertex(polygon[i], mesh, &indices[i])) {
++skipped_faces;
return;
}
}
for (std::size_t i = 2; i < indices.size(); ++i) {
mesh.add_triangle(indices[0], indices[i - 1], indices[i]);
if (mesh.triangle_count() > kMaxTriangles) return;
}
++parsed_faces;
};
// 优先从 B-Rep 实体(MANIFOLD_SOLID_BREP)遍历;否则退回所有 ADVANCED_FACE。
bool any_brep = false;
for (const auto& kv : entities) {
if (kv.second.type != "MANIFOLD_SOLID_BREP") continue;
any_brep = true;
int shell_id = 0;
if (!get_shell_of_brep(entities, kv.first, &shell_id)) continue;
std::vector<int> face_ids;
bool closed = false;
if (!get_shell_faces(entities, shell_id, &face_ids, &closed)) continue;
for (int face_id : face_ids) triangulate_face(face_id);
}
if (!any_brep) {
for (const auto& kv : entities) {
if (kv.second.type == "ADVANCED_FACE") triangulate_face(kv.first);
}
}
if (mesh.empty()) {
return fail(host, VDE_PLUGIN_ERR_PARSE,
"step: 未解析出可三角化的平面面(NURBS/曲面/空文件不支持)");
}
(void)skipped_faces;
(void)parsed_faces;
return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "step");
}
int step_read_memory(const VdePluginHost* host, const void* data, std::size_t size,
VdeBody** bodies, std::size_t* count) {
if (bodies) *bodies = nullptr;
if (count) *count = 0;
if (!bodies || !count) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: 输出指针为空");
const int host_rc = validate_read_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
if (!data || size == 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: 数据为空");
// 复用文件读取路径:内存数据先落临时文件代价高;这里只支持 read_file。
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
"step: 仅支持 read_file(P2-3 最小实现)");
}
int step_probe(const void* head, std::size_t head_size) {
if (!head || head_size < 12) return 0;
const char* data = static_cast<const char*>(head);
if (std::strncmp(data, "ISO-10303-21", 12) == 0) return 100;
return 0;
}
const char* step_describe() {
return "VDE official STEP reader (planar B-rep subset; host mesh conversion)";
}
const VdeFormatPlugin kFormat = [] {
VdeFormatPlugin format{};
format.struct_size = static_cast<uint32_t>(sizeof(VdeFormatPlugin));
format.format_id = "step";
format.extensions = ".step;.stp";
format.probe = &step_probe;
format.read_file = &step_read_file;
format.read_memory = &step_read_memory;
format.write_file = nullptr;
format.write_memory = nullptr;
format.describe = &step_describe;
return format;
}();
} // namespace
extern "C" {
VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) {
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI;
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
info->id = "com.vde.official.step";
info->name = "VDE Official STEP Format";
info->version = "1.0.0";
info->kind = VDE_PLUGIN_KIND_FORMAT;
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
info->capabilities = "format:step.read";
info->license = "Apache-2.0";
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host,
VdePluginRegistration* reg) {
if (!reg || reg->struct_size < sizeof(VdePluginRegistration))
return VDE_PLUGIN_ERR_ABI;
const int host_rc = vde_plugin_common::validate_common_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
reg->kind = VDE_PLUGIN_KIND_FORMAT;
reg->format = &kFormat;
reg->cae_solver = nullptr;
reg->cam_strategy = nullptr;
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
} // extern "C"