fix(brep): IGES 写出器 DE 段与拓扑实体参数,修复往返丢体
append_de 每实体写 2 条 80 列 DE 记录(IGES 5.3 规定),此前每实体只写 1 行,读入端成对消费时丢失约半数实体(含整只体)。 拓扑查找表改按顶点/边索引键控:TopoEdge 端点与 TopoLoop 边均存索引(见 step_export.cpp 的 body.edge(lop.edges[i])),此前按 e.id/v.id 键控导致索引与 id 错位,静默丢弃多数 loop/face(box 6 面仅出 4 面、每环仅有 2 边)。 Type 502/504/508/510 参数对齐 IgesToBrep 读入契约:502=X,Y,Z;504=9 字段(CURType,CURDEF,SVDE,SVINDEX,EVDE,EVINDEX,CSDE,SP,EP);508=N 后每条边 0,EDGE,0,0,SP,EP;510=surface,N,(flag,loop)*N。 新增测试:DE 段 2 记录/实体;多体文件级 round-trip 保留全部体。
This commit is contained in:
@@ -45,14 +45,19 @@ namespace vde::brep {
|
||||
* ```
|
||||
* S 1H,,1H;, ... S0000001
|
||||
* G ... G0000003
|
||||
* D 116,1,2,... (Point entity) D0000001
|
||||
* D 110,3,4,... (Line entity) D0000002
|
||||
* D 116,1,2,... (Point entity, DE record 1/2) D0000001
|
||||
* D 116,1,0,3,... (Point entity, DE record 2/2) D0000002
|
||||
* D 110,3,4,... (Line entity, DE record 1/2) D0000003
|
||||
* D 110,3,0,4,... (Line entity, DE record 2/2) D0000004
|
||||
* ... ...
|
||||
* P 116,0.,0.,0.; 1P0000001
|
||||
* P 110,0.,0.,0.,1.,0.,0.; 3P0000002
|
||||
* T 1 T0000001
|
||||
* ```
|
||||
*
|
||||
* 每个实体的 Directory Entry 固定占 2 条 80 列记录(IGES 5.3 规定),
|
||||
* 因此 D 段行数恒为实体数的两倍。
|
||||
*
|
||||
* ## 实体输出策略
|
||||
*
|
||||
* 1. **几何实体**(点/线/弧/曲线/面/曲面)先输出并使 Directory 条目索引递增
|
||||
|
||||
+89
-45
@@ -66,25 +66,52 @@ private:
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
// Append a DE line
|
||||
// Columns 73-80 of a DE record: section letter 'D' + 7-digit sequence
|
||||
static std::string de_seq_field(int seq) {
|
||||
char buf[9];
|
||||
std::snprintf(buf, sizeof(buf), "D%07d", seq);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
// Append the two Directory Entry records for one entity and return the
|
||||
// sequence number of the first record (the DE pointer used by the PD
|
||||
// section). IGES 5.3 fixes a Directory Entry at exactly two 80-column
|
||||
// records / 20 fields; emitting only the first left the D section at one
|
||||
// line per entity, which desynchronises any reader that consumes DE lines
|
||||
// in pairs (e.g. IgesParser::parse_directory_entries): the second line of
|
||||
// each entity was read as the first line of the next one, silently dropping
|
||||
// roughly half of the entities — including whole bodies in multi-body files.
|
||||
int append_de(int entity_type, int pd_start, int pd_count,
|
||||
int line_font = 1, int level = 1) {
|
||||
int seq = next_de();
|
||||
const int seq = next_de(); // first (odd-numbered) DE record
|
||||
const int seq2 = next_de(); // second (even-numbered) DE record
|
||||
|
||||
std::string line;
|
||||
line += field_i(entity_type); // 1-8
|
||||
line += field_i(entity_type); // 1-8 entity type number
|
||||
line += field_i(pd_start); // 9-16 PD start line number
|
||||
line += field_i(0); // 17-24 structure
|
||||
line += field_i(line_font); // 25-32 line font
|
||||
line += field_i(line_font); // 25-32 line font pattern
|
||||
line += field_i(level); // 33-40 level
|
||||
line += field_i(0); // 41-48 view
|
||||
line += field_i(0); // 49-56 transform
|
||||
line += field_i(0); // 57-64 label
|
||||
line += field_i(0); // 65-72 status
|
||||
// 73-80: sequence + 'D'
|
||||
char rhs[9];
|
||||
std::snprintf(rhs, sizeof(rhs), "%c%07d", 'D', seq);
|
||||
line += std::string(rhs);
|
||||
line += field_i(0); // 49-56 transformation matrix
|
||||
line += field_i(0); // 57-64 label display associativity
|
||||
line += field_i(0); // 65-72 status number
|
||||
line += de_seq_field(seq); // 73-80
|
||||
de_lines_.push_back(line);
|
||||
|
||||
std::string line2;
|
||||
line2 += field_i(entity_type); // 1-8 entity type number (repeat)
|
||||
line2 += field_i(1); // 9-16 line weight number
|
||||
line2 += field_i(0); // 17-24 color number
|
||||
line2 += field_i(pd_count); // 25-32 parameter line count
|
||||
line2 += field_i(0); // 33-40 form number
|
||||
line2 += field_i(0); // 41-48 reserved
|
||||
line2 += field_i(0); // 49-56 reserved
|
||||
line2 += field_i(0); // 57-64 entity label
|
||||
line2 += field_i(0); // 65-72 entity subscript number
|
||||
line2 += de_seq_field(seq2); // 73-80
|
||||
de_lines_.push_back(line2);
|
||||
|
||||
return seq;
|
||||
}
|
||||
|
||||
@@ -372,44 +399,62 @@ private:
|
||||
}
|
||||
|
||||
// ── Topology entity writers ──────────────────────────
|
||||
//
|
||||
// IGES 5.3 leaves the B-Rep topology entities (502/504/508/510) as
|
||||
// non-normative "untested" entities: the standard does not pin down their
|
||||
// parameter layout, so interoperability depends on an agreed contract.
|
||||
// The parameter layouts emitted below are exactly the ones consumed by
|
||||
// this module's sibling reader (IgesToBrep in iges_import.cpp); keeping the
|
||||
// two in lockstep is what makes an export→import round-trip reconstruct the
|
||||
// same topology. Any change here must be mirrored in the reader.
|
||||
|
||||
/// Type 502: Vertex
|
||||
int write_vertex(int point_de) {
|
||||
std::string params = std::to_string(point_de);
|
||||
/// Type 502: Vertex — parameters are the vertex coordinates X, Y, Z.
|
||||
/// The reader (convert_vertex) reads p[0..2] as the point directly, so the
|
||||
/// vertex carries its own coordinates instead of a Type 116 point pointer.
|
||||
int write_vertex(const Point3D& p) {
|
||||
std::string params = fmt_iges_real(p.x()) + ","
|
||||
+ fmt_iges_real(p.y()) + ","
|
||||
+ fmt_iges_real(p.z());
|
||||
int pd_start = append_pd(params, de_seq_ + 1);
|
||||
return append_de(502, pd_start, pd_seq_ - pd_start + 1);
|
||||
}
|
||||
|
||||
/// Type 504: Edge
|
||||
/// Reader contract (convert_edge): CURType, CURDEF, SVDE, SVINDEX, EVDE,
|
||||
/// EVINDEX, CSDE, SP, EP — it uses p[1]=curve DE, p[2]=start vertex DE and
|
||||
/// p[4]=end vertex DE, and rejects the entity unless at least 5 fields are
|
||||
/// present. Emitting only 3 fields made every edge fail to convert.
|
||||
int write_edge(int curve_de, int v_start_de, int v_end_de) {
|
||||
std::string params = std::to_string(curve_de) + ","
|
||||
+ std::to_string(v_start_de) + ","
|
||||
+ std::to_string(v_end_de);
|
||||
std::string params = "0," + std::to_string(curve_de) + ","
|
||||
+ std::to_string(v_start_de) + ",1,"
|
||||
+ std::to_string(v_end_de) + ",1,0,0.0,0.0";
|
||||
int pd_start = append_pd(params, de_seq_ + 1);
|
||||
return append_de(504, pd_start, pd_seq_ - pd_start + 1);
|
||||
}
|
||||
|
||||
/// Type 508: Loop
|
||||
/// Reader contract (convert_loop): N, then per edge entry
|
||||
/// TYPE, EDGE_DE, COUNT, VX1..VX_COUNT, CS_DE, SP, EP for a total of
|
||||
/// 6+COUNT fields (the reader advances by exactly that stride). We emit the
|
||||
/// edge-only form (COUNT=0) → "0, EDGE_DE, 0, 0, SP, EP".
|
||||
int write_loop(const std::vector<int>& edge_des, const std::vector<int>& orientations) {
|
||||
(void)orientations; // reader derives edge sense from geometry, not the loop
|
||||
std::ostringstream ss;
|
||||
ss << edge_des.size();
|
||||
// For each edge: type(1=edge), edge DE, orientation(0=forward), param_count(0)
|
||||
for (size_t i = 0; i < edge_des.size(); ++i) {
|
||||
int orient = (i < orientations.size()) ? orientations[i] : 0;
|
||||
ss << "," << "1," << edge_des[i] << "," << orient << ",0";
|
||||
}
|
||||
// Number of index groups (same as edge count)
|
||||
ss << "," << edge_des.size();
|
||||
for (int ed : edge_des)
|
||||
ss << ",0," << ed << ",0,0,0.0,0.0";
|
||||
int pd_start = append_pd(ss.str(), de_seq_ + 1);
|
||||
return append_de(508, pd_start, pd_seq_ - pd_start + 1);
|
||||
}
|
||||
|
||||
/// Type 510: Face
|
||||
int write_face(int surface_de, const std::vector<int>& loop_des, bool sense) {
|
||||
/// Reader contract (convert_face): surface_DE, N_loops, then N pairs of
|
||||
/// (loop_flag, loop_DE) — loop_flag 0 = outer boundary, 1 = inner hole.
|
||||
int write_face(int surface_de, const std::vector<int>& loop_des) {
|
||||
std::ostringstream ss;
|
||||
ss << surface_de << "," << (sense ? 1 : 0) << "," << loop_des.size();
|
||||
for (int ld : loop_des)
|
||||
ss << "," << "1," << ld << "," << (ld == loop_des[0] ? 1 : 0) << ",0";
|
||||
ss << surface_de << "," << loop_des.size();
|
||||
for (size_t i = 0; i < loop_des.size(); ++i)
|
||||
ss << "," << (i == 0 ? 0 : 1) << "," << loop_des[i];
|
||||
int pd_start = append_pd(ss.str(), de_seq_ + 1);
|
||||
return append_de(510, pd_start, pd_seq_ - pd_start + 1);
|
||||
}
|
||||
@@ -511,22 +556,29 @@ private:
|
||||
}
|
||||
|
||||
// Phase 4: Write topology — vertex, edge, loop, face, shell, MSBO
|
||||
// Vertex topology entities
|
||||
std::map<int, int> vert_topo_de; // vertex id → vertex entity DE
|
||||
//
|
||||
// Reference convention: a TopoEdge stores its endpoints as vertex
|
||||
// *indices* (add_edge is called with add_vertex() return values), and a
|
||||
// TopoLoop stores its edges as edge *indices* (add_loop is called with
|
||||
// find_edge()/add_edge() return values — see step_export.cpp, which
|
||||
// resolves them via body.edge(lop.edges[i])). The lookup tables below
|
||||
// must therefore be keyed by index, not by the entities' synthetic
|
||||
// `id` fields; keying by `id` silently dropped every loop whose edge
|
||||
// index did not coincide with an unrelated entity's id.
|
||||
std::map<int, int> vert_topo_de; // vertex index → vertex entity DE
|
||||
for (size_t vi = 0; vi < body.num_vertices(); ++vi) {
|
||||
auto& v = body.vertex(static_cast<int>(vi));
|
||||
int pt_de = point_de_.count(v.id) ? point_de_[v.id] : 0;
|
||||
vert_topo_de[v.id] = write_vertex(pt_de);
|
||||
vert_topo_de[static_cast<int>(vi)] = write_vertex(v.point);
|
||||
}
|
||||
|
||||
// Edge topology entities
|
||||
std::map<int, int> edge_topo_de; // edge id → edge entity DE
|
||||
std::map<int, int> edge_topo_de; // edge index → edge entity DE
|
||||
for (size_t ei = 0; ei < body.num_edges(); ++ei) {
|
||||
auto& e = body.edge(static_cast<int>(ei));
|
||||
int cde = curve_des.count(static_cast<int>(ei)) ? curve_des[static_cast<int>(ei)] : 0;
|
||||
int vs = vert_topo_de.count(e.v_start) ? vert_topo_de[e.v_start] : 0;
|
||||
int ve = vert_topo_de.count(e.v_end) ? vert_topo_de[e.v_end] : 0;
|
||||
edge_topo_de[e.id] = write_edge(cde, vs, ve);
|
||||
edge_topo_de[static_cast<int>(ei)] = write_edge(cde, vs, ve);
|
||||
}
|
||||
|
||||
// Loop entities
|
||||
@@ -537,14 +589,7 @@ private:
|
||||
for (int ei : loop.edges) {
|
||||
if (edge_topo_de.count(ei)) {
|
||||
e_des.push_back(edge_topo_de[ei]);
|
||||
// Check edge orientation
|
||||
for (size_t eidx = 0; eidx < body.num_edges(); ++eidx) {
|
||||
auto& e = body.edge(static_cast<int>(eidx));
|
||||
if (e.id == ei) {
|
||||
orientations.push_back(e.reversed ? 1 : 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
orientations.push_back(body.edge(ei).reversed ? 1 : 0);
|
||||
}
|
||||
}
|
||||
if (!e_des.empty())
|
||||
@@ -561,9 +606,8 @@ private:
|
||||
if (loop_topo_de.count(li))
|
||||
ld_list.push_back(loop_topo_de[li]);
|
||||
}
|
||||
if (!ld_list.empty()) {
|
||||
face_topo_de[f.id] = write_face(sd, ld_list, !f.reversed);
|
||||
}
|
||||
if (!ld_list.empty())
|
||||
face_topo_de[f.id] = write_face(sd, ld_list);
|
||||
}
|
||||
|
||||
// Shell entity
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include "vde/brep/brep.h"
|
||||
#include "vde/brep/modeling.h"
|
||||
#include "vde/brep/iges_export.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace vde::brep;
|
||||
using namespace vde::core;
|
||||
@@ -212,3 +214,40 @@ TEST(IgesExportTest, ExportBox_DELinesHaveCorrectSuffix) {
|
||||
}
|
||||
EXPECT_GT(de_count, 0) << "Should have DE lines with D suffix";
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
// DE section structure: two records per entity
|
||||
//
|
||||
// IGES 5.3 fixes a Directory Entry at exactly two 80-column records.
|
||||
// The writer used to emit a single record per entity, which made readers
|
||||
// that consume DE lines in pairs drop every other entity (and whole bodies
|
||||
// in multi-body files). Guard the invariant directly.
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
|
||||
TEST(IgesExportTest, ExportDirectoryEntriesAreTwoRecordsPerEntity) {
|
||||
auto box = make_box(1, 1, 1);
|
||||
std::string iges = export_iges({box});
|
||||
|
||||
std::vector<std::string> d_lines;
|
||||
std::istringstream iss(iges);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.size() == 80 && line[72] == 'D') d_lines.push_back(line);
|
||||
}
|
||||
ASSERT_FALSE(d_lines.empty()) << "DE section is empty";
|
||||
ASSERT_EQ(d_lines.size() % 2, 0u)
|
||||
<< "DE section must contain 2 records per entity, got " << d_lines.size();
|
||||
|
||||
for (size_t i = 0; i + 1 < d_lines.size(); i += 2) {
|
||||
const std::string& r1 = d_lines[i];
|
||||
const std::string& r2 = d_lines[i + 1];
|
||||
// Record 2 repeats the entity type number of record 1 (field 11 == field 1).
|
||||
EXPECT_EQ(r1.substr(0, 8), r2.substr(0, 8))
|
||||
<< "DE pair " << (i / 2) << " has mismatched entity types";
|
||||
// Sequence numbers must advance one per record (odd then even).
|
||||
EXPECT_EQ(std::stoi(r1.substr(73, 7)), static_cast<int>(i) + 1)
|
||||
<< "DE pair " << (i / 2) << " first record has wrong sequence";
|
||||
EXPECT_EQ(std::stoi(r2.substr(73, 7)), static_cast<int>(i) + 2)
|
||||
<< "DE pair " << (i / 2) << " second record has wrong sequence";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "vde/brep/iges_export.h"
|
||||
#include "vde/brep/brep.h"
|
||||
#include "vde/brep/modeling.h"
|
||||
#include "vde/core/transform.h"
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
@@ -569,6 +570,27 @@ TEST(IgesImport, FileLevelRoundtripBox) {
|
||||
EXPECT_GT(bodies[0].num_faces(), 0u);
|
||||
}
|
||||
|
||||
// VDE-1210: 多体文件 round-trip。写出端曾每实体只写 1 条 DE 记录,读入端按 2 行
|
||||
// 成对消费时会把第 2、4、6…条 DE 当成前一条的第 2 行,从而静默丢弃约半数实体
|
||||
// (含整只体)。此用例保证多体文件读回后体数与面数都不丢。
|
||||
TEST(IgesImport, FileLevelRoundtripKeepsAllBodies) {
|
||||
auto box_a = make_box(2.0, 1.0, 3.0);
|
||||
auto box_b = make_box(4.0, 2.0, 1.5);
|
||||
box_b.transform(core::translate(50.0, 0.0, 0.0));
|
||||
ASSERT_GT(box_a.num_faces(), 0u);
|
||||
ASSERT_GT(box_b.num_faces(), 0u);
|
||||
|
||||
const std::string path = std::string(VDE_TEST_TEMP_DIR) + "vde_iges_multi_rt.igs";
|
||||
ASSERT_TRUE(export_iges_file(path, {box_a, box_b}));
|
||||
|
||||
const auto bodies = import_iges(path);
|
||||
EXPECT_EQ(iges_last_error(), IgesError::Ok)
|
||||
<< iges_last_error_message();
|
||||
ASSERT_EQ(bodies.size(), 2u) << "multi-body IGES round-trip lost a body";
|
||||
EXPECT_GT(bodies[0].num_faces(), 0u);
|
||||
EXPECT_GT(bodies[1].num_faces(), 0u);
|
||||
}
|
||||
|
||||
TEST(IgesImport, VerifyArcGeometry) {
|
||||
// A 90-degree arc: center(0,0), start(5,0), end(0,5)
|
||||
IgesBuilder b;
|
||||
|
||||
Reference in New Issue
Block a user