fix(brep): AP214 外部引用装配解析——同目录加载 EXTERNAL_SOURCE 兄弟 STEP,XR1 装配族 3 件空开清零(VDE-1193 §9.9)
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s

- import_step 传递源目录;0 body 时收集 EXTERNAL_SOURCE/DOCUMENT_FILE 引用名,
  精确匹配失败退同目录 .stp/.step 后缀模糊匹配(语料 occ__ 包装前缀)
- 递归导入 depth≤4 + visited 防环 + 64 文件上限;未解析到引用输出明确警告
  (geometry absent, file opens empty)
- 效果:occ__trj4_xr1-db/tc/tu-214 0→5 体(53 面 diag_err=0,与被引文件面数和
  逐位一致);ec/id/pe 与 _0/_1/_2/_6、tr9-db 引用的几何文件不在语料中,
  维持合理空开+明确报告;test_measure 33 + test_brep_heal 41 全过
This commit is contained in:
hm
2026-09-13 11:14:03 +08:00
parent 5581721a4e
commit 2331861c33
2 changed files with 136 additions and 4 deletions
+5 -1
View File
@@ -26,7 +26,9 @@
* @ingroup brep
*/
#include "vde/brep/brep.h"
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
#include <functional>
#include <atomic>
@@ -139,7 +141,9 @@ struct StepImportOptions {
* @see import_step 从文件导入
*/
[[nodiscard]] std::vector<BrepModel> 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<std::unordered_set<std::string>>& visited = nullptr);
/**
* @brief 获取最近一次 STEP 导入的错误码
+131 -3
View File
@@ -29,6 +29,7 @@
#include <algorithm>
#include <array>
#include <memory>
#include <io.h> // _findfirst/_findnext(外部引用同目录枚举,VDE-1193 §9.9)
#include <functional>
#include <iostream>
#include <chrono>
@@ -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<std::string>& 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<std::string> ext_collect_step_names(
const std::unordered_map<int, StepEntity>& ents) {
std::vector<std::string> 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<std::string> 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<BrepModel> import_step_from_string(const std::string& step_data,
const StepImportOptions& opts) {
std::vector<BrepModel> import_step_from_string(
const std::string& step_data, const StepImportOptions& opts,
const std::string& source_dir, int depth,
const std::shared_ptr<std::unordered_set<std::string>>& visited) {
clear_error();
g_import_diag = StepImportDiagnostic{}; // reset diagnostics
@@ -5764,6 +5853,43 @@ std::vector<BrepModel> 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<std::unordered_set<std::string>>();
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<BrepModel> 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);
}
// ─────────────────────────────────────────────────────────────