feat(dxf): 二进制 DXF 写出(WS-9 X4)
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

- 新增 DxfExportOptions::binary;文本/二进制共用 DxfEmitter 组码布局,
  二进制实现 22 B 签名 + 1/3 字节组码 + NUL 字符串 + LE f64 + int16/int32
- 四档版本(R12/R2000/R2010/R2018)均可写二进制,二进制读取器原样读回
- 测试:test_dxf 37/37(新增 ExportBinaryRoundtrip 四档坐标/图层断言);
  test_brep_drawing 42/42 回归
- docs:WS-9 X4 完成,能力矩阵/差距总览/审计/VDE-122 同步
This commit is contained in:
hm
2026-10-18 20:11:06 +08:00
parent 0fc3bcb6c1
commit 003aefe8a6
8 changed files with 191 additions and 35 deletions
+3
View File
@@ -177,3 +177,6 @@ DWY 二进制格式非常复杂,推荐路径:
`export_step_file(..., options)`,可选 AP203(`CONFIG_CONTROL_DESIGN`)/
AP214(`AUTOMOTIVE_DESIGN`)/ AP242(`AP242_…MIM_LF`),FILE_NAME 可指定;
三族几何子集通用,导出后可识别并原样导回。`test_step_corpus` 129/129。
- **DXF X4**:`DxfExportOptions::binary` 写出 AutoCAD Binary DXF(22 B 签名 +
组码值),与 ASCII 共用同一份组码布局;四档版本均支持。`test_dxf` 37/37
(新增 `ExportBinaryRoundtrip`)。
@@ -152,7 +152,7 @@ M3.6 落地时需外部生成夹具验收。
| 能力 | 状态 |
|------|------|
| DXF 读:AC1000–AC1032 全版本 + 二进制 DXF(10,311 件语料 100%) | ✅ 已实现(2026-10-18,未知版本如实 `Unknown`) |
| DXF 写 | ✅ R12/R2000/R2010/R2018(`DxfExportOptions`;二进制写出见 WS-9 X4) |
| DXF 写 | ✅ R12/R2000/R2010/R2018 × ASCII/二进制(`DxfExportOptions`) |
| DWG 原生容器解析(R13–R2018,版本/头/节表/第二份头/预览) | ✅ 已实现(M3.1,582 件全解析 + file_size 全一致) |
| DWG 原生容器解析(R12 / AC1009) | ⏳ 未实现(M3.6;当前返回专门错误,不假实现) |
| DWG 实体解码 | ⚠️ 部分(M3.2:AC1015/R2000 对象位流解码,437 件 / 200558 对象残差 0;R2004+ 待 M3.3) |
+1 -1
View File
@@ -146,7 +146,7 @@ AC15 无容器级文件大小字段,原实现只取节定位器最大末尾,
| **X1 读侧全版本** | `$ACADVER` AC1000–AC1032 精确映射(PreR10/R10/R12/R13/R14/R2000/R2004/R2007/R2010/R2013/R2018);未知版本如实返回 `Unknown`(不再猜 R2000);二进制 DXF(22 字节签名 + 组码流)完整解码;UTF-8 BOM 剥离;`DxfDocument::binary` 标志 | ✅ 2026-10-18 |
| **X2 语料验收** | `test_dxf`:10,311 件语料 100% 识别,版本分布与独立 Python 盘点逐项一致;二进制 DXF 夹具(内存 + 落盘)往返 | ✅ 2026-10-18 |
| **X3 导出全版本** | `export_dxf(path, views, DxfExportOptions)`:R12/R2000/R2010/R2018 四档,写对应 `$ACADVER`(AC1009/AC1015/AC1024/AC1032)与 R2000+ 的 `$HANDSEED`;`LINE`/`TABLES` 结构为全系通用子集。旧三参重载保持 R12 默认 | ✅ 2026-10-18 |
| **X4 二进制 DXF 写出** | 按 X3 版本写二进制 DXF(与文本共用序列化层),往返逐实体一致 | 待办(语料无二进制样本,以自造夹具验收) |
| **X4 二进制 DXF 写出** | `DxfExportOptions::binary`:22 字节签名 + 组码值(字符串 NUL 结尾 / f64 / int16 / int32),与 ASCII 形态共用同一份 `DxfEmitter` 布局,四档版本均可写二进制;二进制读取器原样读回 | ✅ 2026-10-18 |
| **X5 版本相关实体补全** | R12 无 LWPOLYLINE/SPLINE/ELLIPSE/HATCH → 读写按版本降级;DIMENSION/INSERT 属性块、AC1024+ `$ACADVER` 外的 2004+ 段(CLASSES/OBJECTS/ACDS)容错 | 待办 |
### 9.2 STEP 全 AP 族(本轮已完成识别与诊断)
+1
View File
@@ -594,3 +594,4 @@ IN/OUT/ON 判定;已改为 `(source, face_ids, reference)`(新增参考实
| R1 路线图 | `34-追赶计划.md` 新增 WS-9(DXF X1–X5 / STEP S1–S6 / DWG M3.6 / .vde V1–V6);`format-support.md`、`33-内核剩余差距总览.md` 同步 | 见对应文档 |
| X3 DXF 导出全版本 | `DxfExportOptions{version}` + `export_dxf(path, views, options)`:R12/R2000/R2010/R2018 → `$ACADVER` AC1009/AC1015/AC1024/AC1032,R2000+ 写 `$HANDSEED`;旧重载保持 R12 | `test_dxf` 36/36(新增 `ExportAllVersions`:四档导出→回读版本/实体数一致) |
| S3 STEP 导出 schema 选择 | `StepExportOptions{schema, filename}` + `export_step(..., options)` / `export_step_file(..., options)`;AP203=`CONFIG_CONTROL_DESIGN`、AP214=`AUTOMOTIVE_DESIGN`、AP242=`AP242_…MIM_LF`;FILE_NAME 可指定;三族几何子集通用 | `test_step_corpus` 129/129(新增 `ExportSchemaSelection` 三族导出→识别→导回、`ExportFileCarriesSchema` 落盘识别) |
| X4 二进制 DXF 写出 | `DxfExportOptions::binary`;文本/二进制共用 `DxfEmitter` 布局,二进制实现 22 B 签名 + 组码(1/3 字节)+ NUL 字符串 + LE f64 + int16/int32;四档版本均可写 | `test_dxf` 37/37(新增 `ExportBinaryRoundtrip`:四档二进制导出→二进制读取器坐标/图层逐项一致);`test_brep_drawing` 42/42 回归 |
+1 -1
View File
@@ -31,7 +31,7 @@
| **Parasolid XT** | `.x_t` / `.x_b` | Siemens Parasolid | ✅ | ✅ | 二进制 (.x_b) + 文本 (.x_t) |
| **ACIS SAT/SAB** | `.sat` / `.sab` | ACIS R7–R2024 | ✅ | ✅ | 文本 (.sat) + 二进制 (.sab) |
| **JT** | `.jt` | ISO 14306 (9.x) | ✅ | ✅ | 导入返回 `BrepModel`,导出返回 `bool`;支持 B-Rep + Mesh LOD |
| **DXF** | `.dxf` | AC1000–AC1032(AutoCAD 1.x → R2018),ASCII + 二进制 | ✅ | ✅ | 导入:`$ACADVER` 全版本精确识别(PreR10/R10/R12/R13/R14/R2000/R2004/R2007/R2010/R2013/R2018),未知版本如实 `Unknown` 不猜测;AutoCAD Binary DXF 完整解码(22 B 签名 + 组码值类型);UTF-8 BOM 容错。导出:R12/R2000/R2010/R2018 可选(`DxfExportOptions`)。12 种实体类型:LINE/CIRCLE/ARC/LWPOLYLINE/POLYLINE/POINT/TEXT/INSERT/ELLIPSE/SPLINE/3DFACE/HATCH |
| **DXF** | `.dxf` | AC1000–AC1032(AutoCAD 1.x → R2018),ASCII + 二进制 | ✅ | ✅ | 导入:`$ACADVER` 全版本精确识别(PreR10/R10/R12/R13/R14/R2000/R2004/R2007/R2010/R2013/R2018),未知版本如实 `Unknown` 不猜测;AutoCAD Binary DXF 完整解码(22 B 签名 + 组码值类型);UTF-8 BOM 容错。导出:R12/R2000/R2010/R2018 × ASCII/二进制(`DxfExportOptions`)。12 种实体类型:LINE/CIRCLE/ARC/LWPOLYLINE/POLYLINE/POINT/TEXT/INSERT/ELLIPSE/SPLINE/3DFACE/HATCH |
| **AMF** | `.amf` | AMF 1.1 | ✅ (新增) | ✅ (新增) | 添加制造格式(新增) |
---
+1
View File
@@ -72,6 +72,7 @@ enum class DxfExportVersion {
/// DXF 导出选项
struct DxfExportOptions {
DxfExportVersion version = DxfExportVersion::R12;
bool binary = false; ///< true = AutoCAD Binary DXF;false = ASCII DXF
};
/// Export views to DXF format
+139 -32
View File
@@ -7,8 +7,11 @@
#include "vde/collision/ray_intersect.h"
#include "vde/core/triangle.h"
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <memory>
using vde::core::AABB3D;
using vde::core::Point2D;
@@ -211,15 +214,106 @@ ProjectionView section_view(const BrepModel& body,
}
// ═══════════════════════════════════════════════════════════
// export_dxf (AutoCAD R12 format)
// export_dxf (AutoCAD R12–R2018 / ASCII + Binary)
//
// 兼容性要点(CAXA/中望等国产 CAD 依赖):
// 1. HEADER 必须含 $ACADVER(AC1009)——缺失会导致读取方无法
// 1. HEADER 必须含 $ACADVER——缺失会导致读取方无法
// 识别版本而拒绝打开(此前导出 HEADER 为空段)
// 2. 提供 BLOCKS 段(可为空)与层 "0",TABLES 中定义
// CONTINUOUS + DASHED 线型
// 文本与二进制形态共用同一份组码布局(DxfEmitter 两实现),
// 避免两套序列化逻辑漂移。
// ═══════════════════════════════════════════════════════════
namespace {
/// DXF 组码输出抽象(值类型由调用点的重载决定)
class DxfEmitter {
public:
virtual ~DxfEmitter() = default;
virtual void value(int code, const std::string& text) = 0;
virtual void value(int code, int number) = 0;
virtual void value(int code, double number) = 0;
};
/// ASCII DXF:`组码\n值\n`
class TextDxfEmitter final : public DxfEmitter {
public:
explicit TextDxfEmitter(std::ostream& out) : out_(out) {
out_.precision(6);
out_ << std::fixed;
}
void value(int code, const std::string& text) override {
out_ << code << '\n' << text << '\n';
}
void value(int code, int number) override {
out_ << code << '\n' << number << '\n';
}
void value(int code, double number) override {
out_ << code << '\n' << number << '\n';
}
private:
std::ostream& out_;
};
/// AutoCAD Binary DXF:22 字节签名 + 逐组码值
/// 组码 1 字节(0xFF 时追加 LE u16);字符串 NUL 结尾;double 8 字节 LE;
/// 整数按组码段选 int16/int32(与读取端 tokenize_dxf_binary 一致)
class BinaryDxfEmitter final : public DxfEmitter {
public:
explicit BinaryDxfEmitter(std::ostream& out) : out_(out) {
static const char kSignature[] = "AutoCAD Binary DXF\r\n\x1a";
out_.write(kSignature, sizeof(kSignature)); // 21 字符 + NUL = 22 字节
}
void value(int code, const std::string& text) override {
write_code(code);
out_.write(text.c_str(), static_cast<std::streamsize>(text.size()) + 1);
}
void value(int code, int number) override {
write_code(code);
if (is_int32_code(code)) {
put_u32(static_cast<std::uint32_t>(number));
} else {
put_u16(static_cast<std::uint16_t>(static_cast<std::int16_t>(number)));
}
}
void value(int code, double number) override {
write_code(code);
std::uint64_t bits = 0;
std::memcpy(&bits, &number, sizeof(bits));
for (int i = 0; i < 8; ++i)
put_u8(static_cast<std::uint8_t>((bits >> (8 * i)) & 0xFF));
}
private:
static bool is_int32_code(int code) {
return (code >= 90 && code <= 99) || (code >= 420 && code <= 429) ||
(code >= 440 && code <= 459) || code == 1071;
}
void put_u8(std::uint8_t v) { out_.put(static_cast<char>(v)); }
void put_u16(std::uint16_t v) {
put_u8(static_cast<std::uint8_t>(v & 0xFF));
put_u8(static_cast<std::uint8_t>((v >> 8) & 0xFF));
}
void put_u32(std::uint32_t v) {
for (int i = 0; i < 4; ++i)
put_u8(static_cast<std::uint8_t>((v >> (8 * i)) & 0xFF));
}
void write_code(int code) {
if (code >= 0 && code <= 254) {
put_u8(static_cast<std::uint8_t>(code));
} else {
put_u8(0xFF);
put_u16(static_cast<std::uint16_t>(code));
}
}
std::ostream& out_;
};
} // namespace
bool export_dxf(const std::string& filepath,
const std::vector<ProjectionView>& views) {
return export_dxf(filepath, views, DxfExportOptions{});
@@ -228,11 +322,16 @@ bool export_dxf(const std::string& filepath,
bool export_dxf(const std::string& filepath,
const std::vector<ProjectionView>& views,
const DxfExportOptions& options) {
std::ofstream out(filepath);
const std::ios::openmode mode =
std::ios::out | (options.binary ? std::ios::openmode(std::ios::binary)
: std::ios::openmode(0));
std::ofstream out(filepath, mode);
if (!out.is_open()) return false;
out.precision(6);
out << std::fixed;
std::unique_ptr<DxfEmitter> emitter;
if (options.binary) emitter = std::make_unique<BinaryDxfEmitter>(out);
else emitter = std::make_unique<TextDxfEmitter>(out);
DxfEmitter& e = *emitter;
// 第一遍:统计平移后全部线段的范围 → $EXTMIN/$EXTMAX
double ex_min = 0, ex_max = 1, ey_min = 0, ey_max = 1;
@@ -278,36 +377,42 @@ bool export_dxf(const std::string& filepath,
case DxfExportVersion::R12:
default: break;
}
out << "0\nSECTION\n2\nHEADER\n";
out << "9\n$ACADVER\n1\n" << acadver << "\n";
e.value(0, "SECTION"); e.value(2, "HEADER");
e.value(9, "$ACADVER"); e.value(1, acadver);
if (options.version != DxfExportVersion::R12) {
// R2000+ 句柄体系:$HANDSEED 为下一个可用句柄
out << "9\n$HANDSEED\n5\nFF\n";
e.value(9, "$HANDSEED"); e.value(5, "FF");
}
out << "9\n$INSBASE\n10\n0.0\n20\n0.0\n30\n0.0\n";
out << "9\n$EXTMIN\n10\n" << ex_min << "\n20\n" << ey_min << "\n30\n0.0\n";
out << "9\n$EXTMAX\n10\n" << ex_max << "\n20\n" << ey_max << "\n30\n0.0\n";
out << "0\nENDSEC\n";
e.value(9, "$INSBASE");
e.value(10, 0.0); e.value(20, 0.0); e.value(30, 0.0);
e.value(9, "$EXTMIN");
e.value(10, ex_min); e.value(20, ey_min); e.value(30, 0.0);
e.value(9, "$EXTMAX");
e.value(10, ex_max); e.value(20, ey_max); e.value(30, 0.0);
e.value(0, "ENDSEC");
// TABLES: linetypes (CONTINUOUS + DASHED) + layers (0/visible/hidden)
out << "0\nSECTION\n2\nTABLES\n";
out << "0\nTABLE\n2\nLTYPE\n70\n2\n";
out << "0\nLTYPE\n2\nCONTINUOUS\n70\n0\n3\nSolid line\n72\n65\n73\n0\n40\n0.0\n";
out << "0\nLTYPE\n2\nDASHED\n70\n0\n3\nDashed __ __ __ __\n";
out << "72\n65\n73\n2\n40\n4.0\n49\n2.5\n49\n-1.5\n";
out << "0\nENDTAB\n";
out << "0\nTABLE\n2\nLAYER\n70\n3\n";
out << "0\nLAYER\n2\n0\n70\n0\n62\n7\n6\nCONTINUOUS\n";
out << "0\nLAYER\n2\nvisible\n70\n0\n62\n7\n6\nCONTINUOUS\n";
out << "0\nLAYER\n2\nhidden\n70\n0\n62\n8\n6\nDASHED\n";
out << "0\nENDTAB\n";
out << "0\nENDSEC\n";
e.value(0, "SECTION"); e.value(2, "TABLES");
e.value(0, "TABLE"); e.value(2, "LTYPE"); e.value(70, 2);
e.value(0, "LTYPE"); e.value(2, "CONTINUOUS"); e.value(70, 0);
e.value(3, "Solid line"); e.value(72, 65); e.value(73, 0); e.value(40, 0.0);
e.value(0, "LTYPE"); e.value(2, "DASHED"); e.value(70, 0);
e.value(3, "Dashed __ __ __ __");
e.value(72, 65); e.value(73, 2); e.value(40, 4.0);
e.value(49, 2.5); e.value(49, -1.5);
e.value(0, "ENDTAB");
e.value(0, "TABLE"); e.value(2, "LAYER"); e.value(70, 3);
e.value(0, "LAYER"); e.value(2, "0"); e.value(70, 0); e.value(62, 7); e.value(6, "CONTINUOUS");
e.value(0, "LAYER"); e.value(2, "visible"); e.value(70, 0); e.value(62, 7); e.value(6, "CONTINUOUS");
e.value(0, "LAYER"); e.value(2, "hidden"); e.value(70, 0); e.value(62, 8); e.value(6, "DASHED");
e.value(0, "ENDTAB");
e.value(0, "ENDSEC");
// BLOCKS — 提供空段(R12 常见结构,避免部分读取方报缺段)
out << "0\nSECTION\n2\nBLOCKS\n0\nENDSEC\n";
e.value(0, "SECTION"); e.value(2, "BLOCKS"); e.value(0, "ENDSEC");
// ENTITIES
out << "0\nSECTION\n2\nENTITIES\n";
e.value(0, "SECTION"); e.value(2, "ENTITIES");
double offset_x = 0.0;
const double spacing = 50.0;
@@ -330,11 +435,12 @@ bool export_dxf(const std::string& filepath,
double shift_x = offset_x - (vx_min + vx_max) * 0.5;
auto emit = [&](const DrawSegment& seg, const char* layer) {
out << "0\nLINE\n8\n" << layer << "\n";
out << "10\n" << (seg.x1 + shift_x) << "\n";
out << "20\n" << seg.y1 << "\n";
out << "11\n" << (seg.x2 + shift_x) << "\n";
out << "21\n" << seg.y2 << "\n";
e.value(0, "LINE");
e.value(8, layer);
e.value(10, seg.x1 + shift_x);
e.value(20, seg.y1);
e.value(11, seg.x2 + shift_x);
e.value(21, seg.y2);
};
for (const auto& seg : view.segments) emit(seg, "visible");
@@ -343,7 +449,8 @@ bool export_dxf(const std::string& filepath,
offset_x += (vx_max - vx_min) + spacing;
}
out << "0\nENDSEC\n0\nEOF\n";
e.value(0, "ENDSEC");
e.value(0, "EOF");
out.close();
return true;
}
+44
View File
@@ -440,6 +440,50 @@ TEST(DxfTest, AsciiDxfNotMarkedBinary) {
EXPECT_FALSE(doc.binary);
}
/// 二进制 DXF 写出:四档版本导出后由二进制读取器原样读回
TEST(DxfTest, ExportBinaryRoundtrip) {
vde::brep::ProjectionView view;
view.name = "front";
view.segments.push_back({1.0, 2.0, 3.0, 4.0, false});
view.hidden_lines.push_back({5.0, 6.0, 7.0, 8.0, true});
struct Case {
vde::brep::DxfExportVersion out;
DxfVersion expect;
const char* tag;
};
const Case cases[] = {
{vde::brep::DxfExportVersion::R12, DxfVersion::R12, "R12"},
{vde::brep::DxfExportVersion::R2000, DxfVersion::R2000, "R2000"},
{vde::brep::DxfExportVersion::R2010, DxfVersion::R2010, "R2010"},
{vde::brep::DxfExportVersion::R2018, DxfVersion::R2018, "R2018"},
};
for (const auto& c : cases) {
const std::string path = std::string("vde_dxf_binary_") + c.tag + ".dxf";
vde::brep::DxfExportOptions opts;
opts.version = c.out;
opts.binary = true;
ASSERT_TRUE(vde::brep::export_dxf(path, {view}, opts)) << c.tag;
auto doc = import_dxf(path);
std::remove(path.c_str());
EXPECT_TRUE(doc.binary) << c.tag;
EXPECT_EQ(doc.version, c.expect) << c.tag;
ASSERT_EQ(doc.entities.size(), 2u) << c.tag;
// x 平移:全部线段 x∈[1,7] → shift = -(1+7)/2 = -4
const auto& visible = std::get<DxfLine>(doc.entities[0]);
EXPECT_DOUBLE_EQ(visible.start.x(), -3.0) << c.tag;
EXPECT_DOUBLE_EQ(visible.start.y(), 2.0) << c.tag;
EXPECT_DOUBLE_EQ(visible.end.x(), -1.0) << c.tag;
EXPECT_DOUBLE_EQ(visible.end.y(), 4.0) << c.tag;
const auto& hidden = std::get<DxfLine>(doc.entities[1]);
EXPECT_EQ(hidden.layer, "hidden") << c.tag;
EXPECT_DOUBLE_EQ(hidden.start.x(), 1.0) << c.tag;
EXPECT_DOUBLE_EQ(hidden.end.x(), 3.0) << c.tag;
}
}
/// 导出侧全版本:$ACADVER 随目标版本写入,且回读识别一致
TEST(DxfTest, ExportAllVersions) {
vde::brep::ProjectionView view;