diff --git a/include/vde/brep/step_import.h b/include/vde/brep/step_import.h index 69c349156..df3967a54 100644 --- a/include/vde/brep/step_import.h +++ b/include/vde/brep/step_import.h @@ -26,7 +26,9 @@ * @ingroup brep */ #include "vde/brep/brep.h" +#include #include +#include #include #include #include @@ -139,7 +141,9 @@ struct StepImportOptions { * @see import_step 从文件导入 */ [[nodiscard]] std::vector import_step_from_string( - const std::string& step_data, const StepImportOptions& opts = {}); + const std::string& step_data, const StepImportOptions& opts = {}, + const std::string& source_dir = {}, int depth = 0, + const std::shared_ptr>& visited = nullptr); /** * @brief 获取最近一次 STEP 导入的错误码 diff --git a/src/brep/step_import.cpp b/src/brep/step_import.cpp index 9f0689b23..4ceb8698c 100644 --- a/src/brep/step_import.cpp +++ b/src/brep/step_import.cpp @@ -29,6 +29,7 @@ #include #include #include +#include // _findfirst/_findnext(外部引用同目录枚举,VDE-1193 §9.9) #include #include #include @@ -5717,12 +5718,100 @@ private: ResolvedPlacement StepToBrep::default_placement_; +// ───────────────────────────────────────────────────────────── +// 外部引用装配(EXTERNAL_SOURCE / DOCUMENT_FILE,AP214 xr1 系) +// 主装配文件只含装配结构(NEXT_ASSEMBLY_USAGE_OCCURRENCE 等),几何在 +// EXTERNAL_SOURCE 指向的同目录兄弟 STEP 文件里。VDE-1193 §9.9: +// 此前不解析外部引用 → 12 件空开(occ__trj4_xr1-* / occ__tr9_*-db)。 +// ───────────────────────────────────────────────────────────── + +static std::string ext_dir_of(const std::string& path) { + size_t p = path.find_last_of("/\\"); + return p == std::string::npos ? std::string() : path.substr(0, p); +} + +static std::string ext_to_lower(std::string s) { + for (auto& c : s) c = (char)std::tolower((unsigned char)c); + return s; +} + +static bool ext_is_step_name(const std::string& s) { + std::string l = ext_to_lower(s); + size_t n = l.size(); + return (n > 4 && l.compare(n - 4, 4, ".stp") == 0) || + (n > 5 && l.compare(n - 5, 5, ".step") == 0); +} + +/// 从 EXTERNAL_SOURCE / DOCUMENT_FILE 实体收集引用的 STEP 文件名。 +/// 内联类型参数(IDENTIFIER('x.stp'))被解析为 [Omitted, List[String]], +/// 文件名在嵌套 String 里,递归收集即可。 +static void ext_collect_strings(const StepValue& v, std::vector& out) { + if (v.type == StepValue::Type::String) out.push_back(v.sval); + else if (v.type == StepValue::Type::List) + for (const auto& x : v.list) ext_collect_strings(x, out); +} + +static std::vector ext_collect_step_names( + const std::unordered_map& ents) { + std::vector names; + for (const auto& [id, e] : ents) { + if (e.type != "EXTERNAL_SOURCE" && e.type != "DOCUMENT_FILE") continue; + for (const auto& a : e.args) ext_collect_strings(a, names); + } + std::vector out; + for (const auto& n : names) { + if (!ext_is_step_name(n)) continue; + std::string l = ext_to_lower(n); + bool dup = false; + for (const auto& o : out) + if (ext_to_lower(o) == l) { dup = true; break; } + if (!dup) out.push_back(n); + } + return out; +} + +/// 同目录解析被引用文件:先精确名,失败退"后缀匹配"(语料包装前缀 +/// occ__ 不在引用名里,如引用 xr1-db-214_3.stp ↔ 实存 occ__xr1-db-214_3.stp)。 +static bool ext_resolve_sibling(const std::string& dir, const std::string& ref_name, + std::string& out) { + if (dir.empty() || ref_name.empty()) return false; + std::string p = dir + "\\" + ref_name; + std::ifstream probe(p, std::ios::binary); + if (probe.good()) { out = p; return true; } + p = dir + "/" + ref_name; + probe.open(p, std::ios::binary); + if (probe.good()) { out = p; return true; } + // 后缀模糊:枚举同目录 .stp/.step,取以 ref_name(不区分大小写)结尾者 + std::string lr = ext_to_lower(ref_name); + std::string pat = dir + (dir.find('\\') != std::string::npos ? "\\*" : "/*"); + _finddata_t fd; + intptr_t h = _findfirst(pat.c_str(), &fd); + if (h == -1) return false; + bool found = false; + do { + if (fd.attrib & _A_SUBDIR) continue; + std::string n = fd.name; + std::string ln = ext_to_lower(n); + if (!ext_is_step_name(ln)) continue; + if (ln.size() >= lr.size() && + ln.compare(ln.size() - lr.size(), lr.size(), lr) == 0) { + out = dir + (dir.find('\\') != std::string::npos ? "\\" : "/") + n; + found = true; + break; + } + } while (_findnext(h, &fd) == 0); + _findclose(h); + return found; +} + // ───────────────────────────────────────────────────────────── // Public API // ───────────────────────────────────────────────────────────── -std::vector import_step_from_string(const std::string& step_data, - const StepImportOptions& opts) { +std::vector import_step_from_string( + const std::string& step_data, const StepImportOptions& opts, + const std::string& source_dir, int depth, + const std::shared_ptr>& visited) { clear_error(); g_import_diag = StepImportDiagnostic{}; // reset diagnostics @@ -5764,6 +5853,43 @@ std::vector import_step_from_string(const std::string& step_data, set_error(StepError::Cancelled, "STEP import cancelled (B-Rep conversion)"); return {}; } + if (result.empty()) { + // 外部引用装配(§9.9):解析成功但 0 body 且文件声明了 + // EXTERNAL_SOURCE/DOCUMENT_FILE → 同目录解析被引用 STEP 并导入。 + // 深度 ≤4 + visited 防环;未解析到则维持 EmptyResult 分类口径。 + auto visited_local = visited ? visited + : std::make_shared>(); + if (depth < 4) { + for (const auto& ref : ext_collect_step_names(parser.entities())) { + std::string resolved; + if (!ext_resolve_sibling(source_dir, ref, resolved)) { + if (depth == 0) + g_import_diag.add_warning( + "External STEP reference not found: " + ref + + " — geometry absent, file opens empty"); + continue; + } + std::string key = ext_to_lower(resolved); + if (!visited_local->insert(key).second) continue; + if (visited_local->size() > 64) break; // 装配文件数上限 + std::ifstream sub_file(resolved, std::ios::binary); + if (!sub_file.good()) continue; + std::ostringstream sub_buf; + sub_buf << sub_file.rdbuf(); + auto sub = import_step_from_string( + sub_buf.str(), opts, ext_dir_of(resolved), depth + 1, + visited_local); + for (auto& m : sub) result.push_back(std::move(m)); + } + if (!result.empty()) { + g_import_diag.add_warning( + "External assembly references resolved: " + + std::to_string(visited_local->size()) + + " file(s) loaded"); + clear_error(); + } + } + } if (result.empty()) { // 空开分类(c5):解析成功但零 body 时,若文件中含「无导入通道」的 // 表达类别(线框/曲线集/通用 CSG/离散面片等),EmptyResult 明确输出 @@ -5980,7 +6106,9 @@ std::vector import_step(const std::string& filepath, std::ostringstream buf; buf << file.rdbuf(); - return import_step_from_string(buf.str(), opts); + // 外部引用装配:把源目录传下去,EXTERNAL_SOURCE 指向的兄弟文件 + // 才能被解析加载(VDE-1193 §9.9) + return import_step_from_string(buf.str(), opts, ext_dir_of(filepath), 0, nullptr); } // ─────────────────────────────────────────────────────────────