// 官方 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 #include #include #include #include #include #include #include #include #include #include #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; using vde_plugin_common::validate_write_host; using vde_plugin_common::write_file_bytes; constexpr std::size_t kMaxEntities = 5000000u; constexpr std::size_t kMaxTriangles = 50000000u; // 写出上限(P4-50):AP203 faceted_brep 每条边/面均为文本实体,防御性上限。 constexpr std::size_t kMaxWriteTriangles = 20000000u; constexpr std::size_t kMaxWritePoints = 5000000u; constexpr std::size_t kMaxWriteBodies = 10000u; 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* 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(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(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(); } /** 提取字符串中所有 "#" 引用(保持出现顺序)。 */ std::vector refs_of(const std::string& args) { std::vector 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(id)); i = static_cast(end - args.c_str()) - 1; } return refs; } /** 提取所有数字(跳过 #id 引用)。 */ std::vector numbers_of(const std::string& args) { std::vector 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(args[i]))) ++i; continue; } if (std::isdigit(static_cast(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(end - args.c_str()); continue; } ++i; } return values; } bool get_point(const std::map& 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 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& 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 refs = refs_of(it->second.args); if (refs.empty()) return false; *point_id = refs[0]; return true; } bool get_edge_curve(const std::map& 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 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& 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 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& entities, int id, std::vector* 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& 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 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& entities, int id, std::vector* 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& entities, int id, std::vector* 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& 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 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& entities, const std::vector& oriented_edges, std::vector* vertices) { vertices->clear(); std::vector> 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 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 entities; if (!parse_entities(text, &entities)) return fail(host, VDE_PLUGIN_ERR_PARSE, "step: DATA 段无实体"); // 顶点 id → 网格顶点下标(共享顶点去重) std::map 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 bound_ids; if (!get_face_bounds(entities, face_id, &bound_ids)) { ++skipped_faces; return; } std::vector 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 polygon; if (!build_polygon(entities, outer_loop_edges, &polygon) || polygon.size() < 3) { ++skipped_faces; return; } std::vector 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 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 最小实现)"); } // ── 写出(P4-50 / P4-63):host body_tessellate → ISO-10303-21 AP203 FACETED_BREP ── // // 输出契约不变(每 body 一个 FACETED_BREP,CARTESIAN_POINT 全局去重,确定性 #id, // 最小单位/上下文 + SHAPE_REPRESENTATION)。P4-63 起统一走 StepWriteSink: // - write_file:fopen 后按 1 MiB 块缓冲直接 fwrite,不再先构造完整大 string; // - write_memory:sink 到 std::string(按三角数 reserve 预估),两路径字节一致; // - 热点:点去重改 unordered_map(自定义 double 位哈希)、实体行用 to_chars/snprintf // 栈缓冲直接 append,避免 std::to_string 与临时字符串级联分配。 class StepWriteSink { public: static constexpr std::size_t kBlockSize = 1u << 20; // 1 MiB explicit StepWriteSink(std::string* memory) : memory_(memory) {} explicit StepWriteSink(std::FILE* file) : file_(file) { block_.reserve(kBlockSize); } bool write(const char* data, std::size_t size) { if (failed_) return false; if (memory_ != nullptr) { memory_->append(data, size); return true; } block_.append(data, size); if (block_.size() >= kBlockSize) return flush(); return true; } bool write(const std::string& text) { return write(text.data(), text.size()); } bool flush() { if (failed_) return false; if (block_.empty()) return true; if (file_ == nullptr || std::fwrite(block_.data(), 1, block_.size(), file_) != block_.size()) { failed_ = true; return false; } block_.clear(); return true; } bool finish() { return flush() && !failed_; } bool failed() const { return failed_; } /// 内存模式预留额外容量(file 模式 no-op)。 void reserve(std::size_t extra) { if (memory_ != nullptr && extra > 0) memory_->reserve(memory_->size() + extra); } private: std::string* memory_ = nullptr; std::FILE* file_ = nullptr; std::string block_; bool failed_ = false; }; struct StepPointKey { double x = 0.0; double y = 0.0; double z = 0.0; bool operator==(const StepPointKey& other) const { return x == other.x && y == other.y && z == other.z; } }; struct StepPointKeyHash { std::size_t operator()(const StepPointKey& key) const { std::uint64_t bits[3]; std::memcpy(&bits[0], &key.x, sizeof(double)); std::memcpy(&bits[1], &key.y, sizeof(double)); std::memcpy(&bits[2], &key.z, sizeof(double)); std::uint64_t hash = bits[0] ^ (bits[1] << 1) ^ (bits[2] << 7); hash ^= hash >> 33; hash *= 0xff51afd7ed558ccdULL; hash ^= hash >> 33; return static_cast(hash); } }; struct StepWriteContext { StepWriteSink* sink = nullptr; int next_id = 1; std::unordered_map points; // 全局共享点去重 std::size_t triangle_count = 0; }; int step_alloc_id(StepWriteContext* context) { return context->next_id++; } void step_append_int(std::string* out, int value) { char buffer[16]; const auto result = std::to_chars(buffer, buffer + sizeof(buffer), value); out->append(buffer, static_cast(result.ptr - buffer)); } /// STEP 实数:%.9g + 大写 E + 无小数点/指数时补 '.'(EXPRESS REAL 语法)。 void step_append_real(std::string* out, double value) { char buffer[48]; std::snprintf(buffer, sizeof(buffer), "%.9g", value); std::size_t length = std::strlen(buffer); bool has_marker = false; for (std::size_t i = 0; i < length; ++i) { if (buffer[i] == 'e') { buffer[i] = 'E'; has_marker = true; } else if (buffer[i] == 'E' || buffer[i] == '.') { has_marker = true; } } out->append(buffer, length); if (!has_marker) out->push_back('.'); } void step_append_entity(StepWriteContext* context, int id, const std::string& body) { char header[24]; header[0] = '#'; const auto result = std::to_chars(header + 1, header + sizeof(header), id); *result.ptr = '='; const std::size_t length = static_cast(result.ptr - header) + 1; context->sink->write(header, length); context->sink->write(body); context->sink->write(";\n", 2); } int step_intern_point(StepWriteContext* context, double x, double y, double z, std::string* error) { const StepPointKey key{x, y, z}; auto found = context->points.find(key); if (found != context->points.end()) return found->second; if (context->points.size() >= kMaxWritePoints) { if (error) *error = "step: 点数超过写出上限(5000000)"; return -1; } const int id = step_alloc_id(context); std::string body; body.reserve(96); body.append("CARTESIAN_POINT('',("); step_append_real(&body, x); body.push_back(','); step_append_real(&body, y); body.push_back(','); step_append_real(&body, z); body.append("))"); step_append_entity(context, id, body); context->points.emplace(key, id); return id; } /// 写一个 body 的 FACETED_BREP;成功返回 brep 实体 id(>0),失败返回 -1 并置 error。 int step_write_body(const VdePluginHost* host, VdeBody body, std::size_t body_index, StepWriteContext* context, std::string* error) { VdeMeshData mesh{}; mesh.struct_size = static_cast(sizeof(VdeMeshData)); struct MeshReleaser { const VdePluginHost* host; VdeMeshData* mesh; ~MeshReleaser() { if (!host || !mesh || !host->free_buffer) return; if (mesh->positions) host->free_buffer(mesh->positions); if (mesh->indices) host->free_buffer(mesh->indices); if (mesh->normals) host->free_buffer(mesh->normals); } } releaser{host, &mesh}; if (host->body_tessellate(body, 0.01, &mesh) != VDE_PLUGIN_OK) { if (error) *error = "step: body_tessellate 失败"; return -1; } if (!mesh.positions || mesh.num_vertices == 0 || !mesh.indices || mesh.num_triangles == 0) { if (error) *error = "step: body_tessellate 返回空网格(无三角面)"; return -1; } if (context->triangle_count > kMaxWriteTriangles - mesh.num_triangles) { if (error) *error = "step: 三角面数超过写出上限(20000000)"; return -1; } context->sink->reserve(mesh.num_triangles * 480); context->points.reserve(context->points.size() + mesh.num_vertices); // 1) 顶点(全局去重)→ CARTESIAN_POINT std::vector point_ids(mesh.num_vertices, -1); for (std::size_t vertex = 0; vertex < mesh.num_vertices; ++vertex) { const int id = step_intern_point(context, mesh.positions[vertex * 3 + 0], mesh.positions[vertex * 3 + 1], mesh.positions[vertex * 3 + 2], error); if (id < 0) return -1; point_ids[vertex] = id; } // 2) 每三角 → POLY_LOOP / FACE_OUTER_BOUND / FACE std::vector face_ids; face_ids.reserve(mesh.num_triangles); std::string body_text; body_text.reserve(160); for (std::size_t tri = 0; tri < mesh.num_triangles; ++tri) { const std::uint32_t a = mesh.indices[tri * 3 + 0]; const std::uint32_t b = mesh.indices[tri * 3 + 1]; const std::uint32_t c = mesh.indices[tri * 3 + 2]; if (static_cast(a) >= mesh.num_vertices || static_cast(b) >= mesh.num_vertices || static_cast(c) >= mesh.num_vertices) { if (error) *error = "step: body_tessellate 返回越界三角面索引"; return -1; } if (a == b || b == c || a == c) continue; // 退化三角面跳过 const int loop_id = step_alloc_id(context); body_text.assign("POLY_LOOP('',(#"); step_append_int(&body_text, point_ids[a]); body_text.append(",#"); step_append_int(&body_text, point_ids[b]); body_text.append(",#"); step_append_int(&body_text, point_ids[c]); body_text.append("))"); step_append_entity(context, loop_id, body_text); const int bound_id = step_alloc_id(context); body_text.assign("FACE_OUTER_BOUND('',#"); step_append_int(&body_text, loop_id); body_text.append(",.T.)"); step_append_entity(context, bound_id, body_text); const int face_id = step_alloc_id(context); body_text.assign("FACE('',(#"); step_append_int(&body_text, bound_id); body_text.append("),$,.T.)"); step_append_entity(context, face_id, body_text); face_ids.push_back(face_id); } if (face_ids.empty()) { if (error) *error = "step: body 无有效三角面可写"; return -1; } context->triangle_count += face_ids.size(); // 3) CLOSED_SHELL → FACETED_BREP std::string face_list; face_list.reserve(face_ids.size() * 8 + 8); for (std::size_t i = 0; i < face_ids.size(); ++i) { if (i != 0) face_list.push_back(','); face_list.push_back('#'); step_append_int(&face_list, face_ids[i]); } const int shell_id = step_alloc_id(context); body_text.assign("CLOSED_SHELL('',("); body_text.append(face_list); body_text.append("))"); step_append_entity(context, shell_id, body_text); const int brep_id = step_alloc_id(context); body_text.assign("FACETED_BREP('VDE body "); step_append_int(&body_text, static_cast(body_index + 1)); body_text.append("',#"); step_append_int(&body_text, shell_id); body_text.push_back(')'); step_append_entity(context, brep_id, body_text); return brep_id; } /// 共享实现:sink 决定写文件或写内存,输出字节序列完全一致。 int step_write_impl(const VdePluginHost* host, const VdeBody* bodies, std::size_t count, StepWriteSink* sink) { const int host_rc = validate_write_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (!sink) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: 输出 sink 为空"); if (!bodies || count == 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: 无实体可写"); if (count > kMaxWriteBodies) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: body 数超过上限(10000)"); StepWriteContext context; context.sink = sink; if (!sink->write("ISO-10303-21;\n")) return fail(host, VDE_PLUGIN_ERR_IO, "step: 写文件失败"); sink->write("HEADER;\n"); sink->write("FILE_DESCRIPTION(('VDE faceted BRep export'),'2;1');\n"); sink->write( "FILE_NAME('vde_faceted.step','1970-01-01T00:00:00',('VDE'),('VDE')," "'VDE step plugin','VDE','');\n"); sink->write("FILE_SCHEMA(('CONFIG_CONTROL_DESIGN'));\n"); sink->write("ENDSEC;\nDATA;\n"); // 最小单位/精度/坐标上下文(确定性 #1..#5) const int length_unit_id = step_alloc_id(&context); step_append_entity(&context, length_unit_id, "(LENGTH_UNIT() NAMED_UNIT(*) SI_UNIT(.MILLI.,.METRE.))"); const int angle_unit_id = step_alloc_id(&context); step_append_entity(&context, angle_unit_id, "(NAMED_UNIT(*) PLANE_ANGLE_UNIT() SI_UNIT($,.RADIAN.))"); const int solid_angle_unit_id = step_alloc_id(&context); step_append_entity(&context, solid_angle_unit_id, "(NAMED_UNIT(*) SI_UNIT($,.STERADIAN.) SOLID_ANGLE_UNIT())"); const int uncertainty_id = step_alloc_id(&context); std::string unit_text; unit_text.reserve(160); unit_text.assign("UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.E-07),#"); step_append_int(&unit_text, length_unit_id); unit_text.append(",'distance_accuracy_value','confusion accuracy')"); step_append_entity(&context, uncertainty_id, unit_text); const int context_id = step_alloc_id(&context); unit_text.assign( "(GEOMETRIC_REPRESENTATION_CONTEXT(3) GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#"); step_append_int(&unit_text, uncertainty_id); unit_text.append(")) GLOBAL_UNIT_ASSIGNED_CONTEXT((#"); step_append_int(&unit_text, length_unit_id); unit_text.append(",#"); step_append_int(&unit_text, angle_unit_id); unit_text.append(",#"); step_append_int(&unit_text, solid_angle_unit_id); unit_text.append(")) REPRESENTATION_CONTEXT('',''))"); step_append_entity(&context, context_id, unit_text); std::vector brep_ids; brep_ids.reserve(count); for (std::size_t i = 0; i < count; ++i) { std::string error; const int brep_id = step_write_body(host, bodies[i], i, &context, &error); if (brep_id < 0) { return fail(host, VDE_PLUGIN_ERR_INTERNAL, error.empty() ? "step: 写出失败" : error.c_str()); } brep_ids.push_back(brep_id); } std::string brep_list; brep_list.reserve(brep_ids.size() * 8 + 8); for (std::size_t i = 0; i < brep_ids.size(); ++i) { if (i != 0) brep_list.push_back(','); brep_list.push_back('#'); step_append_int(&brep_list, brep_ids[i]); } const int shape_id = step_alloc_id(&context); std::string shape_text; shape_text.reserve(brep_list.size() + 64); shape_text.assign("SHAPE_REPRESENTATION('VDE',("); shape_text.append(brep_list); shape_text.append("),#"); step_append_int(&shape_text, context_id); shape_text.push_back(')'); step_append_entity(&context, shape_id, shape_text); sink->write("ENDSEC;\n"); sink->write("END-ISO-10303-21;\n"); return sink->failed() ? fail(host, VDE_PLUGIN_ERR_IO, "step: 写文件失败") : VDE_PLUGIN_OK; } int step_write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies, std::size_t count) { if (!path) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: path 为空"); std::FILE* file = std::fopen(path, "wb"); if (!file) return fail(host, VDE_PLUGIN_ERR_IO, "step: 写文件失败"); StepWriteSink sink(file); const int rc = step_write_impl(host, bodies, count, &sink); const bool ok = (rc == VDE_PLUGIN_OK) && sink.finish(); std::fclose(file); if (!ok) { std::remove(path); return rc != VDE_PLUGIN_OK ? rc : fail(host, VDE_PLUGIN_ERR_IO, "step: 写文件失败"); } return VDE_PLUGIN_OK; } int step_write_memory(const VdePluginHost* host, const VdeBody* bodies, std::size_t count, VdeBuffer* out) { if (out) { out->data = nullptr; out->size = 0; } if (!out) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "step: 输出缓冲为空"); std::string text; text.reserve(64 * 1024); StepWriteSink sink(&text); const int rc = step_write_impl(host, bodies, count, &sink); if (rc != VDE_PLUGIN_OK || !sink.finish()) return rc != VDE_PLUGIN_OK ? rc : fail(host, VDE_PLUGIN_ERR_IO, "step: 写出失败"); void* buffer = host->alloc(text.size()); if (!buffer) return fail(host, VDE_PLUGIN_ERR_BUFFER, "step: 输出内存分配失败"); std::memcpy(buffer, text.data(), text.size()); out->data = buffer; out->size = text.size(); return VDE_PLUGIN_OK; } int step_probe(const void* head, std::size_t head_size) { if (!head || head_size < 12) return 0; const char* data = static_cast(head); if (std::strncmp(data, "ISO-10303-21", 12) == 0) return 100; return 0; } const char* step_describe() { return "VDE official STEP format (read planar B-rep subset; write AP203 faceted_brep from tessellation)"; } const VdeFormatPlugin kFormat = [] { VdeFormatPlugin format{}; format.struct_size = static_cast(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 = &step_write_file; format.write_memory = &step_write_memory; 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;format:step.write"; 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"