Files
hm b2245ca0f7 feat(brep/foundation): Wave21 IGES face orientation, zero-copy import caches, UTF-8 leftovers
A52 (IGES face orientation closure, importer/exporter symmetric):
- importer: apply_shell_orientation derives face.reversed = (514 ORIENTATION==0) XOR (186 SHELLFLAG==0);
  token_is_pure_int guards legacy "0HNAME"; convert_face reads 510 OUTERLOOPFLAG, convert_loop reads
  508 ORIENTATION (diagnostics only); empty/void shells use their own SHELLFLAG
- exporter: 514 writes per-face ORIENTATION (0 when reversed), 510 OUTERLOOPFLAG=1, 508 ORIENTATION
  from edge.reversed; 144 counts reversed trimmed faces only (no orientation field); 186 keeps "1,0"
- test_iges_export 34 -> 43 tests (T1-T8, real corpus round-trips): occ__bug25415 vol_rel
  0.9697 -> 8.30e-12, occ__bug22821_test 2.0 -> 0, PWB/31000/28366 stay ~0

A53 (IGES import zero-copy caches):
- curve_cache_/surface_cache_ become unordered_map<int, shared_ptr<const NurbsCurve/NurbsSurface>>;
  hits share pointers, misses make_shared<const T>(std::move(...)); composite curves return pointers
- split_params/params_of return std::string_view (zero-copy token slicing; tokens are substrings)
- import_iges reads the file in one sized binary read (chunked fallback for >16GB or tellg failure)
- V8 Motor_1.igs 3.40 -> 1.346 s, moteur_v8_asm.igs 9.27 -> 3.284 s; topology/model hashes unchanged

A54/A55 (UTF-8 path leftovers):
- A54: 13 foundation files, 17 open sites -> open_ifstream_utf8/open_ofstream_utf8; 9 UTF-8 cases
- A55: 13 infra/plugin/capi/core files, 24+ sites (11 user-path sites in plugin_manager); 12 UTF-8 cases
- Linux-only fixed paths (/sys,/proc,/etc/machine-id) and platform.cpp fopen wrapper intentionally kept

Verification: 23 suites 918 passed / 0 failed; PR gate 1067/1067 (81.44 s); four KPI windows
bit-identical (fixed 196/36, auto 196/36, band 8/0 and 8/0); 35 extra suites green; redline identical.
2026-11-23 00:28:07 +08:00

4310 lines
168 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file test_dwg_writer.cpp
* @brief AC1015(R2000)DWG 原生写入测试(WS-3 M3.4 验收)
*
* 验收口径:写出的字节流必须被本模块自己的 `decode_dwg_objects()` 逐字段
* 读回——容器结构(文件头/节定位器/Handles 页)、对象帧(MS/BS/RL/H/EED)、
* 表(LAYER/STYLE/LTYPE/BLOCK_HEADER/9 类控制对象)与实体几何全部一致,
* 且每个对象的数据流残差为 0。
*
* 覆盖:
* 1. 空文档:必需表与 *Model_Space / *Paper_Space 结构完整;
* 2. 几何往返:LINE(含 z≠0)/ CIRCLE / ARC / POINT / TEXT / MTEXT /
* ELLIPSE / LWPOLYLINE / POLYLINE_2D(含 VERTEX / SEQEND)逐字段相等;
* INSERT + 用户块(BLOCK/ENDBLK、块记录基点/首末实体、四种缩放编码)一致;
* SPLINE(控制点法〔含有理/权重〕与拟合点法)逐字段一致;
* HATCH(实体填充的多段线边界、图案填充的直线/圆弧/椭圆弧/样条边界、
* 派生边界像素尺寸与种子点)逐字段一致;
* ATTDEF(属性定义)与 INSERT 属性流(ATTRIB + SEQEND,含旋转/标志)
* 逐字段一致;
* 3. 图层:多图层写出、表项句柄与实体 layer 引用一致;
* 4. 多页 Handles:400 条实体跨页后仍全部可解码;
* 5. 参数校验:重复/空图层名、引用不存在图层、非 ASCII 文本、
* LWPOLYLINE 顶点/凸度/共面约束、ELLIPSE 长轴/轴比约束、
* POLYLINE_2D 逐顶点宽度数量/非负约束、块名(空/重复/保留名)、
* INSERT 块引用与缩放非零约束、块内实体图层校验、
* SPLINE 次数/节点数/节点序/权重约束;
* HATCH 路径/多段线边界/圆弧半径/椭圆轴比/样条节点/图案名约束;
* INSERT 属性与 ATTDEF 的标签/文本 ASCII 约束;
* 6. 文件往返:`dwg_write_ac1015_file` 与内存写出一致,可重新读回。
*/
#include <gtest/gtest.h>
#include "vde/foundation/dwg_container.h"
#include "vde/foundation/dwg_object.h"
#include "vde/foundation/dwg_writer.h"
#include "vde/foundation/io_dxf.h"
#include "vde/foundation/industrial_formats.h"
#include "vde/brep/modeling.h"
#include "vde/foundation/utf8_path.h"
#include <cmath>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#ifndef VDE_TEST_TEMP_DIR
#define VDE_TEST_TEMP_DIR "."
#endif
using vde::foundation::decode_dwg_objects;
using vde::foundation::decode_dwg_objects_file;
using vde::foundation::DwgArc;
using vde::foundation::DwgCircle;
using vde::foundation::DwgEllipse;
using vde::foundation::DwgLwPolyline;
using vde::foundation::DwgBlock;
using vde::foundation::DwgBlockHeader;
using vde::foundation::DwgContainerInfo;
using vde::foundation::DwgFace3D;
using vde::foundation::DwgHatch;
using vde::foundation::DwgHatchEdge;
using vde::foundation::DwgInsert;
using vde::foundation::DwgLine;
using vde::foundation::DwgMText;
using vde::foundation::DwgObject;
using vde::foundation::DwgObjectDocument;
using vde::foundation::DwgObjectType;
using vde::foundation::DwgPolyline2D;
using vde::foundation::DwgPolyline3D;
using vde::foundation::DwgPolylineMesh;
using vde::foundation::DwgPolylinePFace;
using vde::foundation::DwgSectionInfo;
using vde::foundation::DwgSeqEnd;
using vde::foundation::DwgSpline;
using vde::foundation::DwgText;
using vde::foundation::DwgVertex2D;
using vde::foundation::DwgVertex3D;
using vde::foundation::DwgVertexPFace;
using vde::foundation::DwgVertexPFaceFace;
using vde::foundation::DwgAcis;
using vde::foundation::DwgAttrib;
using vde::foundation::DxfSolid;
using vde::foundation::Point3D;
using vde::foundation::DwgVersion;
using vde::foundation::DwgWriteAttrib;
using vde::foundation::DwgWriteBlock;
using vde::foundation::DwgWriteDocument;
using vde::foundation::DwgWriteEntity;
using vde::foundation::DwgWriteEntityKind;
using vde::foundation::DwgWriteHatch;
using vde::foundation::DwgWriteHatchEdge;
using vde::foundation::DwgWriteHatchEdgeType;
using vde::foundation::DwgWriteHatchPath;
using vde::foundation::DwgWriteHatchVertex;
using vde::foundation::DwgWriteLayer;
using vde::foundation::DwgWritePFaceFace;
using vde::foundation::DwgWritePoint;
using vde::foundation::DwgWriteResult;
using vde::foundation::parse_dwg_container;
namespace {
DwgWritePoint point(double x, double y, double z = 0.0) {
DwgWritePoint value;
value.x = x;
value.y = y;
value.z = z;
return value;
}
DwgWriteEntity line(const DwgWritePoint& start, const DwgWritePoint& end,
const std::string& layer = "0") {
DwgWriteEntity entity;
entity.kind = DwgWriteEntityKind::Line;
entity.layer = layer;
entity.start = start;
entity.end = end;
return entity;
}
/// 写 → 读回;失败时把解码告警一并输出
DwgObjectDocument roundtrip(const DwgWriteDocument& document,
DwgWriteResult* written = nullptr) {
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
if (written != nullptr) *written = result;
if (!result.ok) return {};
DwgObjectDocument doc =
decode_dwg_objects(result.bytes.data(), result.bytes.size());
if (!doc.ok) {
for (const auto& warning : doc.warnings) {
std::cout << " ! " << warning << "\n";
}
std::cout << " header_ok=" << doc.header_ok
<< " consumed=" << doc.header_bits_consumed
<< " declared=" << doc.header_bits_declared << "\n";
}
return doc;
}
/// 写 → 读回(R2018);失败时把解码告警一并输出
DwgObjectDocument roundtrip_ac1032(const DwgWriteDocument& document,
DwgWriteResult* written = nullptr) {
const DwgWriteResult result =
vde::foundation::dwg_write_ac1032(document);
if (written != nullptr) *written = result;
if (!result.ok) return {};
DwgObjectDocument doc =
decode_dwg_objects(result.bytes.data(), result.bytes.size());
if (!doc.ok) {
for (const auto& warning : doc.warnings) {
std::cout << " ! " << warning << "\n";
}
std::cout << " header_ok=" << doc.header_ok
<< " consumed=" << doc.header_bits_consumed
<< " declared=" << doc.header_bits_declared << "\n";
}
return doc;
}
/// 模型空间中指定类型的实体(按句柄升序)
std::vector<const DwgObject*> model_entities(const DwgObjectDocument& doc,
DwgObjectType type) {
std::vector<const DwgObject*> result;
for (const auto& object : doc.objects) {
if (object.type != type) continue;
if (object.common.owner != doc.header.model_space) continue;
result.push_back(&object);
}
return result;
}
/// 全部对象的结构自洽断言(数据流残差 0 / 句柄流不越界)
void expect_all_consistent(const DwgObjectDocument& doc) {
ASSERT_TRUE(doc.ok) << doc.error;
for (const auto& object : doc.objects) {
EXPECT_TRUE(object.header_ok)
<< object.type_name << " #" << object.handle;
EXPECT_TRUE(object.decoded)
<< object.type_name << " #" << object.handle
<< " bits=" << object.bitsize
<< " data_res=" << object.data_residual_bits
<< " hdl_res=" << object.handle_residual_bits;
EXPECT_EQ(object.data_residual_bits, 0)
<< object.type_name << " #" << object.handle;
EXPECT_TRUE(object.consistent)
<< object.type_name << " #" << object.handle;
EXPECT_GE(object.handle_residual_bits, 0)
<< object.type_name << " #" << object.handle;
}
}
void expect_near(double actual, double expected) {
EXPECT_NEAR(actual, expected, 1e-9);
}
} // namespace
TEST(DwgWriter, EmptyDocumentHasCompleteTables) {
DwgWriteDocument document;
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R2000);
EXPECT_TRUE(doc.header_ok);
EXPECT_TRUE(doc.classes_ok);
EXPECT_TRUE(doc.header_end_sentinel_ok);
EXPECT_EQ(doc.objects.size(), written.object_count);
// 9 类控制对象 + 2 个块记录 + 1 图层 + 1 文字样式 + 3 线型
// (ByBlock/ByLayer/Continuous)+ 3 个表记录 + 1 个 VX_CONTROL
// (VPORT/APPID/DIMSTYLE,AutoCAD 打开必需)+ 2×(BLOCK/ENDBLK) = 24,
// 再加 R2000 最小数据库对象 10 个
// (NOD + 5 个子字典 + 1 个占位 + 1 个多线样式 + 2 个 LAYOUT)
EXPECT_EQ(doc.objects.size(), 34u);
EXPECT_EQ(doc.count_type(DwgObjectType::Line), 0u);
EXPECT_EQ(doc.count_type(DwgObjectType::BlockControl), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::Layer), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::Style), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::LType), 3u);
EXPECT_EQ(doc.count_type(DwgObjectType::BlockHeader), 2u);
EXPECT_EQ(doc.count_type(DwgObjectType::Block), 2u);
EXPECT_EQ(doc.count_type(DwgObjectType::EndBlk), 2u);
EXPECT_EQ(doc.count_type(DwgObjectType::Dictionary), 5u);
EXPECT_EQ(doc.count_type(DwgObjectType::DictionaryWDFlt), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::ClassPlaceholder), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::MLineStyle), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::LayoutObject), 2u);
EXPECT_EQ(doc.count_type(DwgObjectType::VPort), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::AppId), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::DimStyle), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::VxControl), 1u);
// HEADER 关键句柄必须指向真实对象
const DwgObject* model_block =
doc.find_object(doc.header.model_space);
ASSERT_NE(model_block, nullptr);
ASSERT_TRUE(std::holds_alternative<vde::foundation::DwgBlockHeader>(
model_block->data));
EXPECT_EQ(std::get<vde::foundation::DwgBlockHeader>(model_block->data).name,
"*Model_Space");
const DwgObject* layer_zero = doc.find_object(doc.header.clayer);
ASSERT_NE(layer_zero, nullptr);
ASSERT_TRUE(std::holds_alternative<vde::foundation::DwgLayer>(
layer_zero->data));
EXPECT_EQ(std::get<vde::foundation::DwgLayer>(layer_zero->data).name, "0");
expect_all_consistent(doc);
}
TEST(DwgWriter, RoundtripAllEntityKinds) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"GEOM", 3});
// LINE:z 全零(不写 z 字段)
document.entities.push_back(line(point(0.0, 0.0), point(10.0, 5.0)));
// LINE:z 非零(写 z 字段)
document.entities.push_back(line(point(1.5, -2.5, 3.5),
point(4.25, 6.75, -8.125)));
DwgWriteEntity circle;
circle.kind = DwgWriteEntityKind::Circle;
circle.layer = "GEOM";
circle.center = point(2.0, 3.0, 4.0);
circle.radius = 7.5;
document.entities.push_back(circle);
DwgWriteEntity arc;
arc.kind = DwgWriteEntityKind::Arc;
arc.layer = "GEOM";
arc.center = point(-1.0, 0.5, 0.0);
arc.radius = 2.25;
arc.start_angle = 0.5;
arc.end_angle = 3.0;
document.entities.push_back(arc);
DwgWriteEntity point_entity;
point_entity.kind = DwgWriteEntityKind::Point;
point_entity.location = point(9.0, -3.0, 1.0);
document.entities.push_back(point_entity);
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.layer = "GEOM";
text.insertion = point(5.0, 6.0, 0.0);
text.height = 2.5;
text.rotation = 0.75; // 弧度
text.text = "VDE DWG";
document.entities.push_back(text);
DwgWriteEntity mtext;
mtext.kind = DwgWriteEntityKind::MText;
mtext.layer = "GEOM";
mtext.insertion = point(1.0, 2.0, 0.5);
mtext.height = 1.75;
mtext.rotation = 0.25; // 弧度 → x_axis=(cos, sin, 0)
mtext.text = "line one\\Pline two";
document.entities.push_back(mtext);
DwgWriteEntity ellipse;
ellipse.kind = DwgWriteEntityKind::Ellipse;
ellipse.layer = "GEOM";
ellipse.center = point(2.0, -1.0, 0.0);
ellipse.major_axis = point(4.0, 0.0, 0.0);
ellipse.axis_ratio = 0.5;
ellipse.start_param = 0.0;
ellipse.end_param = 3.141592653589793;
document.entities.push_back(ellipse);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto lines = model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), 2u);
{
const auto& data = std::get<DwgLine>(lines[0]->data);
expect_near(data.start.x, 0.0);
expect_near(data.start.y, 0.0);
expect_near(data.start.z, 0.0);
expect_near(data.end.x, 10.0);
expect_near(data.end.y, 5.0);
expect_near(data.end.z, 0.0);
}
{
const auto& data = std::get<DwgLine>(lines[1]->data);
expect_near(data.start.x, 1.5);
expect_near(data.start.y, -2.5);
expect_near(data.start.z, 3.5);
expect_near(data.end.x, 4.25);
expect_near(data.end.y, 6.75);
expect_near(data.end.z, -8.125);
}
const auto circles = model_entities(doc, DwgObjectType::Circle);
ASSERT_EQ(circles.size(), 1u);
{
const auto& data =
std::get<vde::foundation::DwgCircle>(circles[0]->data);
expect_near(data.center.x, 2.0);
expect_near(data.center.y, 3.0);
expect_near(data.center.z, 4.0);
expect_near(data.radius, 7.5);
}
const auto arcs = model_entities(doc, DwgObjectType::Arc);
ASSERT_EQ(arcs.size(), 1u);
{
const auto& data = std::get<vde::foundation::DwgArc>(arcs[0]->data);
expect_near(data.center.x, -1.0);
expect_near(data.center.y, 0.5);
expect_near(data.radius, 2.25);
expect_near(data.start_angle, 0.5);
expect_near(data.end_angle, 3.0);
}
const auto points = model_entities(doc, DwgObjectType::Point);
ASSERT_EQ(points.size(), 1u);
{
const auto& data =
std::get<vde::foundation::DwgPoint>(points[0]->data);
expect_near(data.location.x, 9.0);
expect_near(data.location.y, -3.0);
expect_near(data.location.z, 1.0);
}
const auto texts = model_entities(doc, DwgObjectType::Text);
ASSERT_EQ(texts.size(), 1u);
{
const auto& data = std::get<DwgText>(texts[0]->data);
expect_near(data.insertion.x, 5.0);
expect_near(data.insertion.y, 6.0);
expect_near(data.height, 2.5);
expect_near(data.rotation, 0.75);
EXPECT_EQ(data.value, "VDE DWG");
EXPECT_EQ(data.style, doc.header.textstyle);
}
const auto mtexts = model_entities(doc, DwgObjectType::MText);
ASSERT_EQ(mtexts.size(), 1u);
{
const auto& data = std::get<DwgMText>(mtexts[0]->data);
expect_near(data.insertion.x, 1.0);
expect_near(data.insertion.y, 2.0);
expect_near(data.insertion.z, 0.5);
expect_near(data.height, 1.75);
expect_near(data.x_axis.x, std::cos(0.25));
expect_near(data.x_axis.y, std::sin(0.25));
expect_near(data.x_axis.z, 0.0);
EXPECT_EQ(data.value, "line one\\Pline two");
EXPECT_EQ(data.style, doc.header.textstyle);
EXPECT_EQ(data.attachment, 1);
EXPECT_EQ(data.drawing_direction, 1);
expect_near(data.line_spacing_factor, 1.0);
}
const auto ellipses = model_entities(doc, DwgObjectType::Ellipse);
ASSERT_EQ(ellipses.size(), 1u);
{
const auto& data =
std::get<vde::foundation::DwgEllipse>(ellipses[0]->data);
expect_near(data.center.x, 2.0);
expect_near(data.center.y, -1.0);
expect_near(data.major_axis.x, 4.0);
expect_near(data.major_axis.y, 0.0);
expect_near(data.extrusion.z, 1.0);
expect_near(data.ratio, 0.5);
expect_near(data.start_param, 0.0);
expect_near(data.end_param, 3.141592653589793);
}
// 图层引用:实体 layer 句柄必须指向同名表项
const std::string expected_layers[8] = {"0", "0", "GEOM", "GEOM",
"0", "GEOM", "GEOM", "GEOM"};
const std::vector<const DwgObject*> entities = {
lines[0], lines[1], circles[0], arcs[0],
points[0], texts[0], mtexts[0], ellipses[0]};
for (std::size_t i = 0; i < entities.size(); ++i) {
const DwgObject* layer = doc.find_object(entities[i]->common.layer);
ASSERT_NE(layer, nullptr) << "实体 #" << entities[i]->handle;
ASSERT_TRUE(std::holds_alternative<vde::foundation::DwgLayer>(
layer->data));
EXPECT_EQ(std::get<vde::foundation::DwgLayer>(layer->data).name,
expected_layers[i]);
}
}
TEST(DwgWriter, RoundtripLwPolyline) {
DwgWriteDocument document;
// 开放折线:z 全零(不写标高字段)
DwgWriteEntity open;
open.kind = DwgWriteEntityKind::LwPolyline;
open.vertices = {point(0.0, 0.0), point(10.0, 0.0), point(10.0, 5.0)};
document.entities.push_back(open);
// 闭合折线:非零标高 + 逐顶点凸度(圆弧段)
DwgWriteEntity closed;
closed.kind = DwgWriteEntityKind::LwPolyline;
closed.closed = true;
closed.vertices = {point(-2.0, -3.0, 2.5), point(4.0, -3.0, 2.5),
point(4.0, 6.0, 2.5), point(-2.0, 6.0, 2.5)};
closed.bulges = {0.0, 0.5, 0.0, -0.5};
document.entities.push_back(closed);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto polylines = model_entities(doc, DwgObjectType::LwPolyline);
ASSERT_EQ(polylines.size(), 2u);
{
const auto& data =
std::get<vde::foundation::DwgLwPolyline>(polylines[0]->data);
EXPECT_EQ(data.flags & 512, 0); // 未闭合
EXPECT_EQ(data.flags & 8, 0); // 无标高
ASSERT_EQ(data.vertices.size(), 3u);
expect_near(data.vertices[0].x, 0.0);
expect_near(data.vertices[0].y, 0.0);
expect_near(data.vertices[1].x, 10.0);
expect_near(data.vertices[1].y, 0.0);
expect_near(data.vertices[2].x, 10.0);
expect_near(data.vertices[2].y, 5.0);
}
{
const auto& data =
std::get<vde::foundation::DwgLwPolyline>(polylines[1]->data);
EXPECT_NE(data.flags & 512, 0); // 闭合
EXPECT_NE(data.flags & 8, 0); // 标高存在
expect_near(data.elevation, 2.5);
ASSERT_EQ(data.vertices.size(), 4u);
const double expected_x[4] = {-2.0, 4.0, 4.0, -2.0};
const double expected_y[4] = {-3.0, -3.0, 6.0, 6.0};
const double expected_bulge[4] = {0.0, 0.5, 0.0, -0.5};
for (std::size_t i = 0; i < data.vertices.size(); ++i) {
expect_near(data.vertices[i].x, expected_x[i]);
expect_near(data.vertices[i].y, expected_y[i]);
expect_near(data.vertices[i].bulge, expected_bulge[i]);
}
}
}
TEST(DwgWriter, RoundtripPolyline2D) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"GEOM", 5});
// 闭合折线:非零标高 + 逐顶点宽度与凸度
DwgWriteEntity closed;
closed.kind = DwgWriteEntityKind::Polyline2D;
closed.layer = "GEOM";
closed.closed = true;
closed.vertices = {point(0.0, 0.0, 1.5), point(6.0, 0.0, 1.5),
point(6.0, 4.0, 1.5), point(0.0, 4.0, 1.5)};
closed.bulges = {0.0, 0.5, 0.0, -0.5};
closed.start_widths = {0.0, 0.25, 0.5, 0.75};
closed.end_widths = {0.0, 0.5, 0.75, 1.0};
document.entities.push_back(closed);
// 开放折线:z 全零(标高字段为 0)
DwgWriteEntity open;
open.kind = DwgWriteEntityKind::Polyline2D;
open.vertices = {point(-1.0, -2.0), point(3.0, -2.0), point(3.0, 5.0)};
document.entities.push_back(open);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto polylines = model_entities(doc, DwgObjectType::Polyline2D);
ASSERT_EQ(polylines.size(), 2u);
{
const auto& data = std::get<DwgPolyline2D>(polylines[0]->data);
EXPECT_NE(data.flags & 1u, 0u); // 闭合
EXPECT_EQ(data.curve_type, 0u);
expect_near(data.elevation, 1.5);
expect_near(data.extrusion.z, 1.0);
ASSERT_EQ(data.vertex_handles.size(), 0u); // R2000 无顶点向量
ASSERT_NE(data.first_vertex, 0u);
ASSERT_NE(data.last_vertex, 0u);
ASSERT_NE(data.seqend, 0u);
// 顶点按句柄升序(owner 指向多段线)
std::vector<const DwgObject*> vertices;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Vertex2D) continue;
if (object.common.owner != polylines[0]->handle) continue;
vertices.push_back(&object);
}
ASSERT_EQ(vertices.size(), 4u);
EXPECT_EQ(vertices.front()->handle, data.first_vertex);
EXPECT_EQ(vertices.back()->handle, data.last_vertex);
const double expected_x[4] = {0.0, 6.0, 6.0, 0.0};
const double expected_y[4] = {0.0, 0.0, 4.0, 4.0};
const double expected_bulge[4] = {0.0, 0.5, 0.0, -0.5};
for (std::size_t i = 0; i < vertices.size(); ++i) {
const auto& vertex = std::get<DwgVertex2D>(vertices[i]->data);
EXPECT_EQ(vertex.flags, 0u);
expect_near(vertex.location.x, expected_x[i]);
expect_near(vertex.location.y, expected_y[i]);
expect_near(vertex.location.z, 0.0); // z 相对标高
expect_near(vertex.start_width, closed.start_widths[i]);
expect_near(vertex.end_width, closed.end_widths[i]);
expect_near(vertex.bulge, expected_bulge[i]);
expect_near(vertex.tangent, 0.0);
}
// SEQEND:owner=多段线,句柄与多段线记录一致
const DwgObject* seqend = doc.find_object(data.seqend);
ASSERT_NE(seqend, nullptr);
ASSERT_TRUE(std::holds_alternative<DwgSeqEnd>(seqend->data));
EXPECT_EQ(seqend->common.owner, polylines[0]->handle);
}
{
const auto& data = std::get<DwgPolyline2D>(polylines[1]->data);
EXPECT_EQ(data.flags & 1u, 0u); // 未闭合
expect_near(data.elevation, 0.0);
std::size_t vertex_count = 0;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Vertex2D) continue;
if (object.common.owner != polylines[1]->handle) continue;
const auto& vertex = std::get<DwgVertex2D>(object.data);
expect_near(vertex.start_width, 0.0);
expect_near(vertex.end_width, 0.0);
++vertex_count;
}
EXPECT_EQ(vertex_count, 3u);
}
}
TEST(DwgWriter, RoundtripFace3DAndPolyline3D) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"GEOM", 3});
// 四边面:z 非零 + 边 2 不可见
DwgWriteEntity quad;
quad.kind = DwgWriteEntityKind::Face3D;
quad.layer = "GEOM";
quad.corners[0] = point(0.0, 0.0, 1.0);
quad.corners[1] = point(4.0, 0.0, 2.0);
quad.corners[2] = point(4.0, 3.0, 3.0);
quad.corners[3] = point(0.0, 3.0, 4.0);
quad.invis_flags = 4;
document.entities.push_back(quad);
// 三角面:z 全零(第 4 点与第 3 点相同)
DwgWriteEntity triangle;
triangle.kind = DwgWriteEntityKind::Face3D;
triangle.corners[0] = point(10.0, 0.0, 0.0);
triangle.corners[1] = point(13.0, 0.0, 0.0);
triangle.corners[2] = point(10.0, 3.0, 0.0);
triangle.corners[3] = triangle.corners[2];
document.entities.push_back(triangle);
// 闭合三维多段线:顶点为绝对 WCS
DwgWriteEntity polyline;
polyline.kind = DwgWriteEntityKind::Polyline3D;
polyline.layer = "GEOM";
polyline.closed = true;
polyline.vertices = {point(0.0, 0.0, 0.0), point(5.0, 0.0, 1.0),
point(5.0, 5.0, 2.0)};
document.entities.push_back(polyline);
for (int pass = 0; pass < 2; ++pass) {
const bool ac1032 = pass == 1;
DwgWriteResult written;
const DwgObjectDocument doc =
ac1032 ? roundtrip_ac1032(document, &written)
: roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto faces = model_entities(doc, DwgObjectType::Face3D);
ASSERT_EQ(faces.size(), 2u);
{
const auto& data = std::get<DwgFace3D>(faces[0]->data);
expect_near(data.corner[0].x, 0.0);
expect_near(data.corner[0].z, 1.0);
expect_near(data.corner[1].y, 0.0);
expect_near(data.corner[2].x, 4.0);
expect_near(data.corner[2].z, 3.0);
expect_near(data.corner[3].y, 3.0);
expect_near(data.corner[3].z, 4.0);
EXPECT_EQ(data.invis_flags, 4u);
}
{
const auto& data = std::get<DwgFace3D>(faces[1]->data);
expect_near(data.corner[1].x, 13.0);
expect_near(data.corner[2].y, 3.0);
expect_near(data.corner[3].x, data.corner[2].x);
expect_near(data.corner[3].y, data.corner[2].y);
expect_near(data.corner[3].z, 0.0);
EXPECT_EQ(data.invis_flags, 0u);
}
const auto polylines = model_entities(doc, DwgObjectType::Polyline3D);
ASSERT_EQ(polylines.size(), 1u);
const auto& data = std::get<DwgPolyline3D>(polylines[0]->data);
EXPECT_EQ(data.curve_type, 0u);
EXPECT_NE(data.flags & 1u, 0u); // 闭合
ASSERT_NE(data.seqend, 0u);
if (ac1032) {
ASSERT_EQ(data.vertex_handles.size(), 3u); // R2004+ 顶点向量
} else {
ASSERT_EQ(data.vertex_handles.size(), 0u);
ASSERT_NE(data.first_vertex, 0u);
ASSERT_NE(data.last_vertex, 0u);
}
std::vector<const DwgObject*> vertices;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Vertex3D) continue;
if (object.common.owner != polylines[0]->handle) continue;
vertices.push_back(&object);
}
ASSERT_EQ(vertices.size(), 3u);
const double expected[3][3] = {{0.0, 0.0, 0.0},
{5.0, 0.0, 1.0},
{5.0, 5.0, 2.0}};
for (std::size_t i = 0; i < vertices.size(); ++i) {
const auto& vertex = std::get<DwgVertex3D>(vertices[i]->data);
EXPECT_EQ(vertex.flags, 0u);
expect_near(vertex.location.x, expected[i][0]);
expect_near(vertex.location.y, expected[i][1]);
expect_near(vertex.location.z, expected[i][2]);
}
const DwgObject* seqend = doc.find_object(data.seqend);
ASSERT_NE(seqend, nullptr);
EXPECT_EQ(seqend->common.owner, polylines[0]->handle);
}
}
TEST(DwgWriter, RoundtripPolylineMesh) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
// 2×3 多边形网格(M=2 行、N=3 列,行优先),M 方向闭合、N 方向开放
DwgWriteEntity mesh;
mesh.kind = DwgWriteEntityKind::PolylineMesh;
mesh.mesh_m = 2;
mesh.mesh_n = 3;
mesh.mesh_closed_m = true;
mesh.mesh_closed_n = false;
for (int m = 0; m < 2; ++m) {
for (int n = 0; n < 3; ++n) {
mesh.vertices.push_back(
point(2.0 * m, 3.0 * n, 0.5 * (m + n)));
}
}
document.entities.push_back(mesh);
for (int pass = 0; pass < 2; ++pass) {
const bool ac1032 = pass == 1;
DwgWriteResult written;
const DwgObjectDocument doc =
ac1032 ? roundtrip_ac1032(document, &written)
: roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto meshes = model_entities(doc, DwgObjectType::PolylineMesh);
ASSERT_EQ(meshes.size(), 1u);
const auto& data = std::get<DwgPolylineMesh>(meshes[0]->data);
EXPECT_EQ(data.num_m_verts, 2u);
EXPECT_EQ(data.num_n_verts, 3u);
EXPECT_EQ(data.curve_type, 0u);
EXPECT_EQ(data.flags & 1u, 1u); // M 方向闭合
EXPECT_EQ(data.flags & 2u, 0u); // N 方向开放
ASSERT_NE(data.seqend, 0u);
if (ac1032) {
ASSERT_EQ(data.vertex_handles.size(), 6u);
} else {
ASSERT_EQ(data.vertex_handles.size(), 0u);
ASSERT_NE(data.first_vertex, 0u);
ASSERT_NE(data.last_vertex, 0u);
}
std::vector<const DwgObject*> vertices;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::VertexMesh) continue;
if (object.common.owner != meshes[0]->handle) continue;
vertices.push_back(&object);
}
ASSERT_EQ(vertices.size(), 6u);
for (std::size_t i = 0; i < vertices.size(); ++i) {
const auto& vertex = std::get<DwgVertex3D>(vertices[i]->data);
const int m = static_cast<int>(i) / 3;
const int n = static_cast<int>(i) % 3;
expect_near(vertex.location.x, 2.0 * m);
expect_near(vertex.location.y, 3.0 * n);
expect_near(vertex.location.z, 0.5 * (m + n));
}
const DwgObject* seqend = doc.find_object(data.seqend);
ASSERT_NE(seqend, nullptr);
EXPECT_EQ(seqend->common.owner, meshes[0]->handle);
}
}
TEST(DwgWriter, RoundtripPolylinePFace) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
// 四棱锥:底面四边形 + 顶点,2 个面(四边形底面、三角形侧面)
DwgWriteEntity pface;
pface.kind = DwgWriteEntityKind::PolylinePFace;
pface.vertices.push_back(point(0.0, 0.0, 0.0));
pface.vertices.push_back(point(4.0, 0.0, 0.0));
pface.vertices.push_back(point(4.0, 4.0, 0.0));
pface.vertices.push_back(point(0.0, 4.0, 0.0));
pface.vertices.push_back(point(2.0, 2.0, 3.0));
vde::foundation::DwgWritePFaceFace quad;
quad.vertices[0] = 4; // 逆时针:4→3→2→1(DWG 面顶点按逆时针)
quad.vertices[1] = 3;
quad.vertices[2] = 2;
quad.vertices[3] = 1;
pface.faces.push_back(quad);
vde::foundation::DwgWritePFaceFace triangle;
triangle.vertices[0] = 1;
triangle.vertices[1] = 2;
triangle.vertices[2] = 5;
triangle.vertices[3] = 0; // 三角面:第 4 索引为空
pface.faces.push_back(triangle);
document.entities.push_back(pface);
for (int pass = 0; pass < 2; ++pass) {
const bool ac1032 = pass == 1;
DwgWriteResult written;
const DwgObjectDocument doc =
ac1032 ? roundtrip_ac1032(document, &written)
: roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto pfaces = model_entities(doc, DwgObjectType::PolylinePFace);
ASSERT_EQ(pfaces.size(), 1u);
const auto& data = std::get<DwgPolylinePFace>(pfaces[0]->data);
EXPECT_EQ(data.num_verts, 5u);
EXPECT_EQ(data.num_faces, 2u);
ASSERT_NE(data.seqend, 0u);
if (ac1032) {
ASSERT_EQ(data.vertex_handles.size(), 7u); // 5 顶点 + 2 面
} else {
ASSERT_EQ(data.vertex_handles.size(), 0u);
ASSERT_NE(data.first_vertex, 0u);
ASSERT_NE(data.last_vertex, 0u);
}
std::vector<const DwgObject*> vertices;
std::vector<const DwgObject*> faces;
for (const auto& object : doc.objects) {
if (object.common.owner != pfaces[0]->handle) continue;
if (object.type == DwgObjectType::VertexPFace) {
vertices.push_back(&object);
} else if (object.type == DwgObjectType::VertexPFaceFace) {
faces.push_back(&object);
}
}
ASSERT_EQ(vertices.size(), 5u);
ASSERT_EQ(faces.size(), 2u);
const double expected[5][3] = {{0.0, 0.0, 0.0}, {4.0, 0.0, 0.0},
{4.0, 4.0, 0.0}, {0.0, 4.0, 0.0},
{2.0, 2.0, 3.0}};
for (std::size_t i = 0; i < vertices.size(); ++i) {
const auto& vertex = std::get<DwgVertexPFace>(vertices[i]->data);
EXPECT_EQ(vertex.flags, 192u);
expect_near(vertex.location.x, expected[i][0]);
expect_near(vertex.location.y, expected[i][1]);
expect_near(vertex.location.z, expected[i][2]);
}
const std::uint16_t expected_faces[2][4] = {{4, 3, 2, 1}, {1, 2, 5, 0}};
for (std::size_t i = 0; i < faces.size(); ++i) {
const auto& face = std::get<DwgVertexPFaceFace>(faces[i]->data);
for (int k = 0; k < 4; ++k) {
EXPECT_EQ(face.index[k], expected_faces[i][k]);
}
}
const DwgObject* seqend = doc.find_object(data.seqend);
ASSERT_NE(seqend, nullptr);
EXPECT_EQ(seqend->common.owner, pfaces[0]->handle);
}
}
TEST(DwgWriter, RoundtripAcisEntities) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
// 最小 SAT 文本载荷(VDE 不解析/不合成 ACIS,只做原样搬运)
const std::string sat =
"700 0 1 0\n"
"@11 ASM 21700 0 1 0\n"
"#\n"
"# VDE raw ACIS payload\n"
"-0 body $-1 -1 $-1 $1 $-1 $-1 #\n"
"\x0E\x03" "End" "\x0E\x02" "of" "\x0E\x04" "ACIS\r\x04" "data";
const DwgWriteEntityKind kinds[3] = {DwgWriteEntityKind::Solid3D,
DwgWriteEntityKind::Body,
DwgWriteEntityKind::Region};
const DwgObjectType types[3] = {DwgObjectType::Solid3D,
DwgObjectType::Body,
DwgObjectType::Region};
for (const auto kind : kinds) {
DwgWriteEntity entity;
entity.kind = kind;
entity.acis_data = sat;
document.entities.push_back(entity);
}
for (int pass = 0; pass < 2; ++pass) {
const bool ac1032 = pass == 1;
DwgWriteResult written;
const DwgObjectDocument doc =
ac1032 ? roundtrip_ac1032(document, &written)
: roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
for (const DwgObjectType type : types) {
const auto entities = model_entities(doc, type);
ASSERT_EQ(entities.size(), 1u);
const auto& acis = std::get<DwgAcis>(entities[0]->data);
EXPECT_TRUE(acis.wires.empty());
if (!ac1032) {
// AC1015:载荷按 version 1 分块编码后逐字节还原
EXPECT_EQ(acis.data, sat);
EXPECT_FALSE(acis.is_binary);
EXPECT_EQ(acis.acis_version, 1u);
EXPECT_TRUE(written.warnings.empty());
} else {
// AC1032:AcDs 数据流未实现,载荷如实降级丢弃并登记警告
EXPECT_TRUE(acis.data.empty());
EXPECT_TRUE(acis.has_wireframe);
}
}
if (ac1032) {
EXPECT_EQ(written.warnings.size(), 3u);
for (const auto& warning : written.warnings) {
EXPECT_NE(warning.find("AcDs"), std::string::npos);
}
}
}
}
TEST(DwgWriter, RoundtripInsertAndBlock) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"GEOM", 3});
// 用户块:非零基点 + 块内 LINE / CIRCLE
vde::foundation::DwgWriteBlock block;
block.name = "BOLT";
block.base_point = point(1.0, 2.0, 3.0);
block.entities.push_back(line(point(0.0, 0.0, 0.0), point(2.0, 0.0, 0.0)));
DwgWriteEntity block_circle;
block_circle.kind = DwgWriteEntityKind::Circle;
block_circle.center = point(1.0, 0.0, 0.0);
block_circle.radius = 0.5;
block.entities.push_back(block_circle);
document.blocks.push_back(block);
document.entities.push_back(
line(point(-1.0, -1.0, 0.0), point(-2.0, -2.0, 0.0), "GEOM"));
// 4 个 INSERT 覆盖缩放编码全部分支:
// 全 1 → bb(3);各分量相等 → bb(2)+RD;x==1 → bb(1)+DD+DD;
// 其余 → bb(0)+RD+DD+DD
const DwgWritePoint scales[4] = {point(1.0, 1.0, 1.0), point(2.0, 2.0, 2.0),
point(1.0, 3.0, 4.0), point(2.0, 3.0, 4.0)};
const double rotations[4] = {0.0, 0.5, 1.0, 1.5};
for (int i = 0; i < 4; ++i) {
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.layer = "GEOM";
insert.block_name = "BOLT";
insert.insertion = point(10.0 * i, 20.0, 0.5);
insert.scale = scales[i];
insert.rotation = rotations[i];
document.entities.push_back(insert);
}
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
// 块记录:*Model_Space / *Paper_Space / BOLT
EXPECT_EQ(doc.count_type(DwgObjectType::BlockHeader), 3u);
EXPECT_EQ(doc.count_type(DwgObjectType::Block), 3u);
EXPECT_EQ(doc.count_type(DwgObjectType::EndBlk), 3u);
const DwgObject* bolt = nullptr;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::BlockHeader) continue;
if (std::get<DwgBlockHeader>(object.data).name == "BOLT") {
bolt = &object;
}
}
ASSERT_NE(bolt, nullptr);
const auto& bolt_header = std::get<DwgBlockHeader>(bolt->data);
expect_near(bolt_header.base_point.x, 1.0);
expect_near(bolt_header.base_point.y, 2.0);
expect_near(bolt_header.base_point.z, 3.0);
ASSERT_NE(bolt_header.begin_block, 0u);
ASSERT_NE(bolt_header.end_block, 0u);
// R2000 的 first/last_entity 是块内顶层实体链入口,必须写真实句柄
// (写 0 会被 AutoCAD 当作空块,实体虽在对象表中却不可见)
ASSERT_NE(bolt_header.first_entity, 0u);
ASSERT_NE(bolt_header.last_entity, 0u);
// 块内实体 owner = 块记录;首/末实体覆盖块内全部实体
// (BLOCK/ENDBLK 也以块记录为 owner,须排除)
std::vector<std::uint64_t> block_owned_handles;
std::size_t block_lines = 0;
std::size_t block_circles = 0;
for (const auto& object : doc.objects) {
if (object.common.owner != bolt->handle) continue;
if (object.type == DwgObjectType::Line) {
block_owned_handles.push_back(object.handle);
const auto& data = std::get<DwgLine>(object.data);
expect_near(data.start.x, 0.0);
expect_near(data.end.x, 2.0);
++block_lines;
} else if (object.type == DwgObjectType::Circle) {
block_owned_handles.push_back(object.handle);
const auto& data = std::get<vde::foundation::DwgCircle>(object.data);
expect_near(data.center.x, 1.0);
expect_near(data.radius, 0.5);
++block_circles;
}
}
EXPECT_EQ(block_lines, 1u);
EXPECT_EQ(block_circles, 1u);
ASSERT_FALSE(block_owned_handles.empty());
// BLOCK 图元带块名;ENDBLK owner = 块记录
const DwgObject* block_begin = doc.find_object(bolt_header.begin_block);
ASSERT_NE(block_begin, nullptr);
ASSERT_TRUE(std::holds_alternative<DwgBlock>(block_begin->data));
EXPECT_EQ(std::get<DwgBlock>(block_begin->data).name, "BOLT");
EXPECT_EQ(block_begin->common.owner, bolt->handle);
const DwgObject* block_end = doc.find_object(bolt_header.end_block);
ASSERT_NE(block_end, nullptr);
EXPECT_EQ(block_end->type, DwgObjectType::EndBlk);
EXPECT_EQ(block_end->common.owner, bolt->handle);
// INSERT 逐字段(模型空间)
const auto inserts = model_entities(doc, DwgObjectType::Insert);
ASSERT_EQ(inserts.size(), 4u);
for (std::size_t i = 0; i < inserts.size(); ++i) {
const auto& data = std::get<DwgInsert>(inserts[i]->data);
expect_near(data.insertion.x, 10.0 * static_cast<double>(i));
expect_near(data.insertion.y, 20.0);
expect_near(data.insertion.z, 0.5);
expect_near(data.x_scale, scales[i].x);
expect_near(data.y_scale, scales[i].y);
expect_near(data.z_scale, scales[i].z);
expect_near(data.rotation, rotations[i]);
expect_near(data.extrusion.x, 0.0);
expect_near(data.extrusion.y, 0.0);
expect_near(data.extrusion.z, 1.0);
EXPECT_FALSE(data.has_attributes);
EXPECT_EQ(data.block_header, bolt->handle);
EXPECT_EQ(data.first_attribute, 0u);
EXPECT_EQ(data.seqend, 0u);
}
}
TEST(DwgWriter, RoundtripInsertAttributes) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"TAGS", 2});
// 块定义:一条线 + 一个 ATTDEF(属性定义)
vde::foundation::DwgWriteBlock block;
block.name = "TAGBLOCK";
block.base_point = point(0.0, 0.0, 0.0);
block.entities.push_back(
line(point(0.0, 0.0, 0.0), point(1.0, 0.0, 0.0)));
DwgWriteEntity attdef;
attdef.kind = DwgWriteEntityKind::AttDef;
attdef.layer = "TAGS";
attdef.insertion = point(0.5, 0.5, 0.0);
attdef.height = 0.25;
attdef.text = "DEFAULT";
attdef.tag = "PART_NO";
attdef.prompt = "Enter part number";
attdef.attribute_flags = 0x01 | 0x08; // 不可见 + 预设
block.entities.push_back(attdef);
document.blocks.push_back(block);
// INSERT 带两个属性(第二个无旋转、常量标志)
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.layer = "TAGS";
insert.block_name = "TAGBLOCK";
insert.insertion = point(5.0, 6.0, 0.0);
insert.rotation = 0.25;
vde::foundation::DwgWriteAttrib first;
first.insertion = point(5.5, 6.5, 0.0);
first.height = 0.2;
first.rotation = 0.75;
first.text = "A-102";
first.tag = "PART_NO";
first.flags = 0x01;
vde::foundation::DwgWriteAttrib second;
second.insertion = point(5.5, 7.0, 0.0);
second.height = 0.3;
second.text = "REV-A";
second.tag = "REV";
second.flags = 0x02;
insert.insert.attributes = {first, second};
document.entities.push_back(insert);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
// ATTDEF:块内属性定义,owner = 块记录
std::vector<const DwgObject*> attdefs;
for (const auto& object : doc.objects) {
if (object.type == DwgObjectType::AttDef) attdefs.push_back(&object);
}
ASSERT_EQ(attdefs.size(), 1u);
const auto& attdef_data = std::get<vde::foundation::DwgAttrib>(
attdefs[0]->data);
EXPECT_EQ(attdef_data.tag, "PART_NO");
EXPECT_EQ(attdef_data.prompt, "Enter part number");
EXPECT_EQ(attdef_data.text.value, "DEFAULT");
EXPECT_EQ(attdef_data.flags, 0x09u);
expect_near(attdef_data.text.insertion.x, 0.5);
expect_near(attdef_data.text.insertion.y, 0.5);
expect_near(attdef_data.text.height, 0.25);
expect_near(attdef_data.text.rotation, 0.0);
// INSERT:has_attributes 与两端点/SEQEND 句柄
const auto inserts = model_entities(doc, DwgObjectType::Insert);
ASSERT_EQ(inserts.size(), 1u);
const auto& insert_data = std::get<DwgInsert>(inserts[0]->data);
EXPECT_TRUE(insert_data.has_attributes);
ASSERT_NE(insert_data.first_attribute, 0u);
ASSERT_NE(insert_data.last_attribute, 0u);
ASSERT_NE(insert_data.seqend, 0u);
// 属性对象:owner = INSERT,逐字段读回
std::vector<const DwgObject*> attribs;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Attrib) continue;
if (object.common.owner != inserts[0]->handle) continue;
attribs.push_back(&object);
}
ASSERT_EQ(attribs.size(), 2u);
EXPECT_EQ(insert_data.first_attribute, attribs.front()->handle);
EXPECT_EQ(insert_data.last_attribute, attribs.back()->handle);
const auto& back_first =
std::get<vde::foundation::DwgAttrib>(attribs[0]->data);
EXPECT_EQ(back_first.tag, "PART_NO");
EXPECT_EQ(back_first.text.value, "A-102");
EXPECT_EQ(back_first.flags, 0x01u);
expect_near(back_first.text.insertion.x, 5.5);
expect_near(back_first.text.insertion.y, 6.5);
expect_near(back_first.text.height, 0.2);
expect_near(back_first.text.rotation, 0.75);
const auto& back_second =
std::get<vde::foundation::DwgAttrib>(attribs[1]->data);
EXPECT_EQ(back_second.tag, "REV");
EXPECT_EQ(back_second.text.value, "REV-A");
EXPECT_EQ(back_second.flags, 0x02u);
expect_near(back_second.text.rotation, 0.0);
expect_near(back_second.text.height, 0.3);
// SEQEND:owner = INSERT
const DwgObject* seqend = doc.find_object(insert_data.seqend);
ASSERT_NE(seqend, nullptr);
EXPECT_EQ(seqend->type, DwgObjectType::SeqEnd);
EXPECT_EQ(seqend->common.owner, inserts[0]->handle);
}
TEST(DwgWriter, RoundtripSpline) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"GEOM", 3});
// 控制点法:有理、闭合(4 个 3 次控制段)
DwgWriteEntity rational;
rational.kind = DwgWriteEntityKind::Spline;
rational.layer = "GEOM";
rational.spline.degree = 3;
rational.spline.rational = true;
rational.spline.closed = true;
rational.spline.periodic = false;
rational.spline.knot_tolerance = 1e-6;
rational.spline.control_tolerance = 2e-6;
rational.spline.knots = {0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0};
rational.spline.control_points = {point(0.0, 0.0, 0.0),
point(1.0, 2.0, 0.0),
point(3.0, 2.5, 0.0),
point(4.0, 1.0, 0.0),
point(5.0, 0.0, 0.0)};
rational.spline.weights = {1.0, 2.0, 1.0, 2.0, 1.0};
document.entities.push_back(rational);
// 控制点法:非有理、开放(2 次)
DwgWriteEntity open;
open.kind = DwgWriteEntityKind::Spline;
open.spline.degree = 2;
open.spline.knots = {0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0};
open.spline.control_points = {point(-1.0, 0.0, 0.0), point(0.0, 1.0, 0.0),
point(1.0, 1.0, 0.0), point(2.0, 0.0, 0.0)};
document.entities.push_back(open);
// 拟合点法(控制点为空)
DwgWriteEntity fit;
fit.kind = DwgWriteEntityKind::Spline;
fit.spline.degree = 3;
fit.spline.fit_tolerance = 5e-6;
fit.spline.start_tangent = point(1.0, 0.0, 0.0);
fit.spline.end_tangent = point(0.0, 1.0, 0.0);
fit.spline.fit_points = {point(0.0, 0.0, 0.0), point(1.0, 1.0, 0.0),
point(2.0, 1.5, 0.5), point(3.0, 0.5, 1.0)};
document.entities.push_back(fit);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto splines = model_entities(doc, DwgObjectType::Spline);
ASSERT_EQ(splines.size(), 3u);
{
const auto& data = std::get<DwgSpline>(splines[0]->data);
EXPECT_EQ(data.scenario, 1);
EXPECT_EQ(data.degree, 3);
EXPECT_TRUE(data.rational);
EXPECT_TRUE(data.closed);
EXPECT_FALSE(data.periodic);
expect_near(data.knot_tolerance, 1e-6);
expect_near(data.control_tolerance, 2e-6);
ASSERT_EQ(data.knots.size(), 9u);
ASSERT_EQ(data.control_points.size(), 5u);
ASSERT_EQ(data.weights.size(), 5u);
for (std::size_t i = 0; i < data.knots.size(); ++i) {
expect_near(data.knots[i], rational.spline.knots[i]);
}
for (std::size_t i = 0; i < data.control_points.size(); ++i) {
expect_near(data.control_points[i].x,
rational.spline.control_points[i].x);
expect_near(data.control_points[i].y,
rational.spline.control_points[i].y);
expect_near(data.weights[i], rational.spline.weights[i]);
}
}
{
const auto& data = std::get<DwgSpline>(splines[1]->data);
EXPECT_EQ(data.scenario, 1);
EXPECT_EQ(data.degree, 2);
EXPECT_FALSE(data.rational);
EXPECT_FALSE(data.closed);
EXPECT_TRUE(data.weights.empty());
ASSERT_EQ(data.knots.size(), 7u);
ASSERT_EQ(data.control_points.size(), 4u);
expect_near(data.control_points[3].x, 2.0);
expect_near(data.control_points[3].y, 0.0);
}
{
const auto& data = std::get<DwgSpline>(splines[2]->data);
EXPECT_EQ(data.scenario, 2);
EXPECT_EQ(data.degree, 3);
expect_near(data.fit_tolerance, 5e-6);
expect_near(data.start_tangent.x, 1.0);
expect_near(data.end_tangent.y, 1.0);
ASSERT_EQ(data.fit_points.size(), 4u);
expect_near(data.fit_points[2].x, 2.0);
expect_near(data.fit_points[2].z, 0.5);
EXPECT_TRUE(data.knots.empty());
EXPECT_TRUE(data.control_points.empty());
}
}
TEST(DwgWriter, RoundtripHatch) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"FILL", 2});
// 实体填充:多段线边界(含凸度)、非零标高与种子点
DwgWriteEntity solid;
solid.kind = DwgWriteEntityKind::Hatch;
solid.layer = "FILL";
solid.hatch.solid = true;
solid.hatch.elevation = 2.5;
solid.hatch.style = 1;
solid.hatch.pattern_type = 1;
DwgWriteHatchPath solid_path;
solid_path.flags = 1u | 2u; // 外部 + 多段线
DwgWriteHatchEdge solid_edge;
solid_edge.type = DwgWriteHatchEdgeType::Polyline;
solid_edge.vertices = {{0.0, 0.0, 0.0},
{4.0, 0.0, 0.5},
{4.0, 3.0, 0.0},
{0.0, 3.0, -0.5}};
solid_edge.closed = true;
solid_path.edges.push_back(solid_edge);
solid.hatch.paths.push_back(solid_path);
solid.hatch.seeds.push_back(point(1.0, 1.0));
document.entities.push_back(solid);
// 图案填充:直线 + 圆弧 + 椭圆弧 + 有理样条边界,另加一条派生边界路径
DwgWriteEntity pattern;
pattern.kind = DwgWriteEntityKind::Hatch;
pattern.layer = "FILL";
pattern.hatch.solid = false;
pattern.hatch.pattern_name = "ANSI31";
pattern.hatch.associative = true;
pattern.hatch.style = 0;
pattern.hatch.pattern_type = 1;
pattern.hatch.pattern_angle = 0.5;
pattern.hatch.pattern_scale = 2.0;
pattern.hatch.double_hatch = true;
DwgWriteHatchPath loop;
loop.flags = 1; // 外部
DwgWriteHatchEdge line_edge;
line_edge.type = DwgWriteHatchEdgeType::Line;
line_edge.start = point(0.0, 0.0);
line_edge.end = point(4.0, 0.0);
loop.edges.push_back(line_edge);
DwgWriteHatchEdge arc_edge;
arc_edge.type = DwgWriteHatchEdgeType::Arc;
arc_edge.center = point(1.0, 2.0);
arc_edge.radius = 3.0;
arc_edge.start_angle = 0.25;
arc_edge.end_angle = 1.5;
arc_edge.counter_clockwise = false;
loop.edges.push_back(arc_edge);
DwgWriteHatchEdge ellipse_edge;
ellipse_edge.type = DwgWriteHatchEdgeType::Ellipse;
ellipse_edge.center = point(5.0, 6.0);
ellipse_edge.major_axis = point(2.0, 1.0);
ellipse_edge.ratio = 0.5;
ellipse_edge.start_angle = 0.0;
ellipse_edge.end_angle = 3.14159265358979323846;
ellipse_edge.counter_clockwise = true;
loop.edges.push_back(ellipse_edge);
DwgWriteHatchEdge spline_edge;
spline_edge.type = DwgWriteHatchEdgeType::Spline;
spline_edge.degree = 2;
spline_edge.rational = true;
spline_edge.knots = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
spline_edge.control_points = {point(0.0, 0.0), point(1.0, 2.0),
point(2.0, 0.0)};
spline_edge.weights = {1.0, 2.0, 1.0};
loop.edges.push_back(spline_edge);
pattern.hatch.paths.push_back(loop);
DwgWriteHatchPath derived;
derived.flags = 1u | 4u; // 外部 + 派生边界
DwgWriteHatchEdge derived_line;
derived_line.type = DwgWriteHatchEdgeType::Line;
derived_line.start = point(10.0, 10.0);
derived_line.end = point(12.0, 10.0);
derived.edges.push_back(derived_line);
pattern.hatch.paths.push_back(derived);
pattern.hatch.seeds = {point(0.5, 0.5), point(11.0, 11.0)};
document.entities.push_back(pattern);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto hatches = model_entities(doc, DwgObjectType::Hatch);
ASSERT_EQ(hatches.size(), 2u);
{
const auto& data = std::get<DwgHatch>(hatches[0]->data);
EXPECT_TRUE(data.solid);
EXPECT_FALSE(data.associative);
expect_near(data.elevation, 2.5);
expect_near(data.extrusion.z, 1.0);
EXPECT_EQ(data.style, 1);
EXPECT_EQ(data.pattern_type, 1);
ASSERT_EQ(data.paths.size(), 1u);
EXPECT_EQ(data.paths[0].flags, 3u);
ASSERT_EQ(data.paths[0].edges.size(), 1u);
const auto& edge = data.paths[0].edges[0];
EXPECT_EQ(edge.type, DwgHatchEdge::Type::Polyline);
EXPECT_TRUE(edge.closed);
ASSERT_EQ(edge.vertices.size(), 4u);
expect_near(edge.vertices[1].x, 4.0);
expect_near(edge.vertices[1].bulge, 0.5);
expect_near(edge.vertices[3].bulge, -0.5);
}
{
const auto& data = std::get<DwgHatch>(hatches[1]->data);
EXPECT_FALSE(data.solid);
EXPECT_TRUE(data.associative);
EXPECT_EQ(data.pattern_name, "ANSI31");
expect_near(data.pattern_angle, 0.5);
expect_near(data.pattern_scale, 2.0);
EXPECT_TRUE(data.double_hatch);
ASSERT_EQ(data.paths.size(), 2u);
const auto& loop = data.paths[0];
EXPECT_EQ(loop.flags, 1u);
ASSERT_EQ(loop.edges.size(), 4u);
EXPECT_EQ(loop.edges[0].type, DwgHatchEdge::Type::Line);
expect_near(loop.edges[0].start.x, 0.0);
expect_near(loop.edges[0].end.x, 4.0);
EXPECT_EQ(loop.edges[1].type, DwgHatchEdge::Type::Arc);
expect_near(loop.edges[1].center.y, 2.0);
expect_near(loop.edges[1].radius, 3.0);
expect_near(loop.edges[1].start_angle, 0.25);
expect_near(loop.edges[1].end_angle, 1.5);
EXPECT_FALSE(loop.edges[1].counter_clockwise);
EXPECT_EQ(loop.edges[2].type, DwgHatchEdge::Type::Ellipse);
expect_near(loop.edges[2].center.x, 5.0);
expect_near(loop.edges[2].major_axis.y, 1.0);
expect_near(loop.edges[2].ratio, 0.5);
EXPECT_TRUE(loop.edges[2].counter_clockwise);
EXPECT_EQ(loop.edges[3].type, DwgHatchEdge::Type::Spline);
EXPECT_EQ(loop.edges[3].degree, 2);
EXPECT_TRUE(loop.edges[3].rational);
EXPECT_FALSE(loop.edges[3].periodic);
ASSERT_EQ(loop.edges[3].knots.size(), 6u);
ASSERT_EQ(loop.edges[3].control_points.size(), 3u);
ASSERT_EQ(loop.edges[3].weights.size(), 3u);
expect_near(loop.edges[3].control_points[1].y, 2.0);
expect_near(loop.edges[3].weights[1], 2.0);
const auto& derived_path = data.paths[1];
EXPECT_EQ(derived_path.flags, 5u);
ASSERT_EQ(derived_path.edges.size(), 1u);
EXPECT_EQ(derived_path.edges[0].type, DwgHatchEdge::Type::Line);
expect_near(derived_path.edges[0].start.x, 10.0);
expect_near(derived_path.edges[0].end.y, 10.0);
}
}
TEST(DwgWriter, MultipleLayersAndColors) {
DwgWriteDocument document;
document.layers = {DwgWriteLayer{"0", 7},
DwgWriteLayer{"OUTLINE", 1},
DwgWriteLayer{"CENTER", 4}};
document.entities.push_back(line(point(0.0, 0.0), point(1.0, 1.0)));
document.entities.push_back(
line(point(1.0, 1.0), point(2.0, 2.0), "OUTLINE"));
document.entities.push_back(
line(point(2.0, 2.0), point(3.0, 3.0), "CENTER"));
const DwgObjectDocument doc = roundtrip(document);
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
EXPECT_EQ(doc.count_type(DwgObjectType::Layer), 3u);
const DwgObject* outline = nullptr;
const DwgObject* center = nullptr;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Layer) continue;
const auto& layer = std::get<vde::foundation::DwgLayer>(object.data);
if (layer.name == "OUTLINE") outline = &object;
if (layer.name == "CENTER") center = &object;
}
ASSERT_NE(outline, nullptr);
ASSERT_NE(center, nullptr);
EXPECT_EQ(std::get<vde::foundation::DwgLayer>(outline->data).color_index,
1);
EXPECT_EQ(std::get<vde::foundation::DwgLayer>(center->data).color_index,
4);
}
TEST(DwgWriter, ManyEntitiesSpanMultipleHandlePages) {
DwgWriteDocument document;
constexpr int kCount = 1200;
for (int i = 0; i < kCount; ++i) {
document.entities.push_back(
line(point(i, 0.0), point(i, 100.0)));
}
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
// Handles 页目标 2000 字节;每条实体条目约 2 字节,1200 条必然跨页
// (AC15 的页只在 Handles 节内部,容器层无页概念,故直接看节 2 的长度)
const DwgContainerInfo info =
parse_dwg_container(written.bytes.data(), written.bytes.size());
ASSERT_TRUE(info.parsed) << info.error;
EXPECT_EQ(info.declared_file_size, written.bytes.size());
EXPECT_TRUE(info.file_size_matches);
std::uint64_t handles_size = 0;
for (const auto& locator : info.locators) {
if (locator.number == 2) handles_size = locator.size;
}
EXPECT_GT(handles_size, 2000u);
const auto lines = model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), static_cast<std::size_t>(kCount));
for (int i = 0; i < kCount; ++i) {
const auto& data = std::get<DwgLine>(lines[static_cast<std::size_t>(i)]
->data);
expect_near(data.start.x, static_cast<double>(i));
expect_near(data.end.y, 100.0);
}
}
TEST(DwgWriter, RejectsInvalidInput) {
{
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"", 7});
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
EXPECT_FALSE(result.error.empty());
EXPECT_TRUE(result.bytes.empty());
}
{
DwgWriteDocument document;
document.layers = {DwgWriteLayer{"A", 7}, DwgWriteLayer{"A", 1}};
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
DwgWriteDocument document;
document.entities.push_back(
line(point(0.0, 0.0), point(1.0, 1.0), "MISSING"));
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
DwgWriteDocument document;
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.text = "中文";
text.height = 1.0;
document.entities.push_back(text);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// LWPOLYLINE:顶点不足 2
DwgWriteDocument document;
DwgWriteEntity polyline;
polyline.kind = DwgWriteEntityKind::LwPolyline;
polyline.vertices = {point(0.0, 0.0)};
document.entities.push_back(polyline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// LWPOLYLINE:凸度数量与顶点数不一致
DwgWriteDocument document;
DwgWriteEntity polyline;
polyline.kind = DwgWriteEntityKind::LwPolyline;
polyline.vertices = {point(0.0, 0.0), point(1.0, 0.0)};
polyline.bulges = {0.0};
document.entities.push_back(polyline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// LWPOLYLINE:顶点不共面(z 不同)
DwgWriteDocument document;
DwgWriteEntity polyline;
polyline.kind = DwgWriteEntityKind::LwPolyline;
polyline.vertices = {point(0.0, 0.0, 0.0), point(1.0, 0.0, 2.0)};
document.entities.push_back(polyline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// ELLIPSE:长轴向量为零
DwgWriteDocument document;
DwgWriteEntity ellipse;
ellipse.kind = DwgWriteEntityKind::Ellipse;
ellipse.major_axis = point(0.0, 0.0, 0.0);
ellipse.axis_ratio = 0.5;
document.entities.push_back(ellipse);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// ELLIPSE:轴比越界
DwgWriteDocument document;
DwgWriteEntity ellipse;
ellipse.kind = DwgWriteEntityKind::Ellipse;
ellipse.major_axis = point(1.0, 0.0, 0.0);
ellipse.axis_ratio = 0.0;
document.entities.push_back(ellipse);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// POLYLINE_2D:逐顶点宽度数量与顶点数不一致
DwgWriteDocument document;
DwgWriteEntity polyline;
polyline.kind = DwgWriteEntityKind::Polyline2D;
polyline.vertices = {point(0.0, 0.0), point(1.0, 0.0)};
polyline.start_widths = {0.0};
document.entities.push_back(polyline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// POLYLINE_2D:宽度为负
DwgWriteDocument document;
DwgWriteEntity polyline;
polyline.kind = DwgWriteEntityKind::Polyline2D;
polyline.vertices = {point(0.0, 0.0), point(1.0, 0.0)};
polyline.end_widths = {0.0, -0.5};
document.entities.push_back(polyline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// 块名不能为空
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
document.blocks.push_back(block);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// 块名不能重复
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
block.name = "BOLT";
document.blocks.push_back(block);
document.blocks.push_back(block);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// 块名不能使用 *Model_Space / *Paper_Space 保留名
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
block.name = "*Model_Space";
document.blocks.push_back(block);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// INSERT 引用了未定义的块
DwgWriteDocument document;
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "MISSING";
document.entities.push_back(insert);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// INSERT 缩放分量不能为 0
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
block.name = "BOLT";
document.blocks.push_back(block);
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "BOLT";
insert.scale = point(1.0, 0.0, 1.0);
document.entities.push_back(insert);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// 块内实体引用了不存在的图层(块内实体同样校验)
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
block.name = "BOLT";
block.entities.push_back(
line(point(0.0, 0.0), point(1.0, 1.0), "MISSING"));
document.blocks.push_back(block);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:次数越界
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 0;
spline.spline.fit_points = {point(0.0, 0.0), point(1.0, 1.0)};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:控制点与拟合点同时提供
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 2;
spline.spline.knots = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
spline.spline.control_points = {point(0.0, 0.0), point(1.0, 1.0),
point(2.0, 0.0)};
spline.spline.fit_points = {point(0.0, 0.0), point(1.0, 1.0)};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:节点数不满足「控制点数 + 次数 + 1」
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 2;
spline.spline.knots = {0.0, 0.0, 0.0, 1.0, 1.0};
spline.spline.control_points = {point(0.0, 0.0), point(1.0, 1.0),
point(2.0, 0.0)};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:节点向量必须非降序
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 2;
spline.spline.knots = {0.0, 0.0, 0.0, 2.0, 1.0, 1.0};
spline.spline.control_points = {point(0.0, 0.0), point(1.0, 1.0),
point(2.0, 0.0)};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:有理样条权重数不匹配
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 2;
spline.spline.rational = true;
spline.spline.knots = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
spline.spline.control_points = {point(0.0, 0.0), point(1.0, 1.0),
point(2.0, 0.0)};
spline.spline.weights = {1.0, 1.0};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:权重必须大于 0
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 2;
spline.spline.rational = true;
spline.spline.knots = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
spline.spline.control_points = {point(0.0, 0.0), point(1.0, 1.0),
point(2.0, 0.0)};
spline.spline.weights = {1.0, 0.0, 1.0};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:非有理样条不能提供权重
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 2;
spline.spline.knots = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
spline.spline.control_points = {point(0.0, 0.0), point(1.0, 1.0),
point(2.0, 0.0)};
spline.spline.weights = {1.0, 1.0, 1.0};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// SPLINE:拟合点法不支持 rational
DwgWriteDocument document;
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.rational = true;
spline.spline.fit_points = {point(0.0, 0.0), point(1.0, 1.0)};
document.entities.push_back(spline);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:无边界路径
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:多段线路径缺少多段线边界段
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
DwgWriteHatchPath path;
path.flags = 3; // 外部 + 多段线
DwgWriteHatchEdge edge;
edge.type = DwgWriteHatchEdgeType::Line;
path.edges.push_back(edge);
hatch.hatch.paths.push_back(path);
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:多段线边界顶点不足 2
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
DwgWriteHatchPath path;
path.flags = 3;
DwgWriteHatchEdge edge;
edge.type = DwgWriteHatchEdgeType::Polyline;
edge.vertices = {{0.0, 0.0, 0.0}};
path.edges.push_back(edge);
hatch.hatch.paths.push_back(path);
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:普通路径不能包含多段线边界段
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
DwgWriteHatchPath path;
path.flags = 1;
DwgWriteHatchEdge edge;
edge.type = DwgWriteHatchEdgeType::Polyline;
edge.vertices = {{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}};
path.edges.push_back(edge);
hatch.hatch.paths.push_back(path);
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:圆弧边界半径必须大于 0
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
DwgWriteHatchPath path;
DwgWriteHatchEdge edge;
edge.type = DwgWriteHatchEdgeType::Arc;
edge.radius = 0.0;
path.edges.push_back(edge);
hatch.hatch.paths.push_back(path);
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:椭圆边界轴比越界
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
DwgWriteHatchPath path;
DwgWriteHatchEdge edge;
edge.type = DwgWriteHatchEdgeType::Ellipse;
edge.major_axis = point(1.0, 0.0);
edge.ratio = 1.5;
path.edges.push_back(edge);
hatch.hatch.paths.push_back(path);
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:样条边界节点数不满足「控制点数 + 次数 + 1」
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
DwgWriteHatchPath path;
DwgWriteHatchEdge edge;
edge.type = DwgWriteHatchEdgeType::Spline;
edge.degree = 2;
edge.knots = {0.0, 0.0, 0.0, 1.0};
edge.control_points = {point(0.0, 0.0), point(1.0, 1.0),
point(2.0, 0.0)};
path.edges.push_back(edge);
hatch.hatch.paths.push_back(path);
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// HATCH:图案名必须为 ASCII
DwgWriteDocument document;
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
hatch.hatch.pattern_name = "螺旋";
DwgWriteHatchPath path;
DwgWriteHatchEdge edge;
edge.type = DwgWriteHatchEdgeType::Line;
path.edges.push_back(edge);
hatch.hatch.paths.push_back(path);
document.entities.push_back(hatch);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// INSERT 属性:标签不能为空
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
block.name = "B";
document.blocks.push_back(block);
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "B";
vde::foundation::DwgWriteAttrib attribute;
attribute.tag = "";
insert.insert.attributes.push_back(attribute);
document.entities.push_back(insert);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// INSERT 属性:标签必须为 ASCII
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
block.name = "B";
document.blocks.push_back(block);
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "B";
vde::foundation::DwgWriteAttrib attribute;
attribute.tag = "标号";
insert.insert.attributes.push_back(attribute);
document.entities.push_back(insert);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// INSERT 属性:属性值必须为 ASCII
DwgWriteDocument document;
vde::foundation::DwgWriteBlock block;
block.name = "B";
document.blocks.push_back(block);
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "B";
vde::foundation::DwgWriteAttrib attribute;
attribute.tag = "TAG";
attribute.text = "零件";
insert.insert.attributes.push_back(attribute);
document.entities.push_back(insert);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// ATTDEF:标签不能为空
DwgWriteDocument document;
DwgWriteEntity attdef;
attdef.kind = DwgWriteEntityKind::AttDef;
attdef.tag = "";
document.entities.push_back(attdef);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// ATTDEF:标签/提示/默认值必须为 ASCII
DwgWriteDocument document;
DwgWriteEntity attdef;
attdef.kind = DwgWriteEntityKind::AttDef;
attdef.tag = "TAG";
attdef.prompt = "提示";
document.entities.push_back(attdef);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
{
// ATTDEF:默认值必须为 ASCII
DwgWriteDocument document;
DwgWriteEntity attdef;
attdef.kind = DwgWriteEntityKind::AttDef;
attdef.tag = "TAG";
attdef.text = "默认";
document.entities.push_back(attdef);
const DwgWriteResult result =
vde::foundation::dwg_write_ac1015(document);
EXPECT_FALSE(result.ok);
}
}
TEST(DwgWriter, FileRoundtripMatchesMemory) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"EDGE", 2});
document.entities.push_back(
line(point(-1.0, -2.0, 0.0), point(3.0, 4.0, 5.0), "EDGE"));
const DwgWriteResult memory = vde::foundation::dwg_write_ac1015(document);
ASSERT_TRUE(memory.ok) << memory.error;
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_writer_roundtrip.dwg";
std::string error;
ASSERT_TRUE(vde::foundation::dwg_write_ac1015_file(path, document, &error))
<< error;
const DwgObjectDocument doc = decode_dwg_objects_file(path);
if (std::getenv("VDE_KEEP_DWG") == nullptr) std::remove(path.c_str());
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.file_size, memory.bytes.size());
expect_all_consistent(doc);
const auto lines = model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), 1u);
const auto& data = std::get<DwgLine>(lines[0]->data);
expect_near(data.start.z, 0.0);
expect_near(data.end.z, 5.0);
}
TEST(DwgWriter, Ac1032EmptyDocumentHasCompleteTables) {
DwgWriteDocument document;
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip_ac1032(document, &written);
ASSERT_TRUE(written.ok) << written.error;
// AC18 容器:明文头 + 内层加密头 + 页定位器 + 节描述表 + 数据页 + 头部副本
const DwgContainerInfo info =
parse_dwg_container(written.bytes.data(), written.bytes.size());
ASSERT_TRUE(info.parsed) << info.error;
EXPECT_EQ(info.version, DwgVersion::R2018);
EXPECT_EQ(info.version_magic, "AC1032");
EXPECT_EQ(info.layout, "AC18");
EXPECT_TRUE(info.file_size_matches);
EXPECT_TRUE(info.inner_header_ok);
EXPECT_TRUE(info.inner_header_crc_ok);
EXPECT_TRUE(info.second_header_ok);
EXPECT_EQ(info.file_size, written.bytes.size());
EXPECT_EQ(info.section_count, 8u);
for (const char* name : {"AcDb:Header", "AcDb:AuxHeader",
"AcDb:Classes", "AcDb:Handles", "AcDb:Template",
"AcDb:ObjFreeSpace", "AcDb:AcDbObjects",
"AcDb:RevHistory"}) {
const DwgSectionInfo* section = info.find_section(name);
ASSERT_NE(section, nullptr) << name;
// 压缩类节(1..8、13)走 LZ77,与真实空文档一致
EXPECT_EQ(section->compressed_code, 2u) << name;
EXPECT_GT(section->page_count, 0u) << name;
}
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R2018);
EXPECT_TRUE(doc.header_ok);
EXPECT_TRUE(doc.classes_ok);
EXPECT_TRUE(doc.header_end_sentinel_ok);
EXPECT_EQ(doc.objects.size(), written.object_count);
EXPECT_EQ(doc.objects.size(), 34u);
EXPECT_EQ(doc.count_type(DwgObjectType::BlockControl), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::Layer), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::Style), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::LType), 3u);
EXPECT_EQ(doc.count_type(DwgObjectType::BlockHeader), 2u);
EXPECT_EQ(doc.count_type(DwgObjectType::Block), 2u);
EXPECT_EQ(doc.count_type(DwgObjectType::EndBlk), 2u);
// AC1032 与 AC1015 写出的最小数据库对象集合一致
EXPECT_EQ(doc.count_type(DwgObjectType::Dictionary), 5u);
EXPECT_EQ(doc.count_type(DwgObjectType::DictionaryWDFlt), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::ClassPlaceholder), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::MLineStyle), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::LayoutObject), 2u);
EXPECT_EQ(doc.count_type(DwgObjectType::VPort), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::AppId), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::DimStyle), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::VxControl), 1u);
const DwgObject* model_block = doc.find_object(doc.header.model_space);
ASSERT_NE(model_block, nullptr);
ASSERT_TRUE(std::holds_alternative<DwgBlockHeader>(model_block->data));
EXPECT_EQ(std::get<DwgBlockHeader>(model_block->data).name,
"*Model_Space");
const DwgObject* layer_zero = doc.find_object(doc.header.clayer);
ASSERT_NE(layer_zero, nullptr);
ASSERT_TRUE(std::holds_alternative<vde::foundation::DwgLayer>(
layer_zero->data));
EXPECT_EQ(std::get<vde::foundation::DwgLayer>(layer_zero->data).name, "0");
expect_all_consistent(doc);
}
TEST(DwgWriter, Ac1032RoundtripAllEntityKinds) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"GEOM", 3});
document.entities.push_back(
line(point(0.0, 0.0, 0.0), point(10.0, 5.0, 0.0)));
document.entities.push_back(
line(point(1.5, -2.5, 3.5), point(4.25, 6.75, -8.125), "GEOM"));
DwgWriteEntity circle;
circle.kind = DwgWriteEntityKind::Circle;
circle.layer = "GEOM";
circle.center = point(2.0, 3.0, 4.0);
circle.radius = 7.5;
document.entities.push_back(circle);
DwgWriteEntity arc;
arc.kind = DwgWriteEntityKind::Arc;
arc.center = point(-1.0, 0.5, 0.0);
arc.radius = 2.25;
arc.start_angle = 0.5;
arc.end_angle = 3.0;
document.entities.push_back(arc);
DwgWriteEntity point_entity;
point_entity.kind = DwgWriteEntityKind::Point;
point_entity.location = point(9.0, -3.0, 1.0);
document.entities.push_back(point_entity);
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.insertion = point(1.0, 2.0, 0.0);
text.height = 2.5;
text.text = "VDE R2018";
text.rotation = 0.25;
document.entities.push_back(text);
DwgWriteEntity mtext;
mtext.kind = DwgWriteEntityKind::MText;
mtext.insertion = point(-4.0, 1.0, 0.0);
mtext.height = 1.75;
mtext.text = "MULTILINE";
document.entities.push_back(mtext);
DwgWriteEntity ellipse;
ellipse.kind = DwgWriteEntityKind::Ellipse;
ellipse.center = point(5.0, 5.0, 0.0);
ellipse.major_axis = point(4.0, 0.0, 0.0);
ellipse.axis_ratio = 0.5;
document.entities.push_back(ellipse);
DwgWriteEntity lwpolyline;
lwpolyline.kind = DwgWriteEntityKind::LwPolyline;
lwpolyline.vertices = {point(0.0, 0.0), point(4.0, 0.0),
point(4.0, 3.0)};
lwpolyline.bulges = {0.0, 0.5, 0.0};
lwpolyline.closed = true;
document.entities.push_back(lwpolyline);
DwgWriteEntity polyline2d;
polyline2d.kind = DwgWriteEntityKind::Polyline2D;
polyline2d.vertices = {point(10.0, 0.0), point(12.0, 2.0)};
polyline2d.start_widths = {0.2, 0.1};
polyline2d.end_widths = {0.3, 0.1};
document.entities.push_back(polyline2d);
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
spline.spline.degree = 2;
spline.spline.knots = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
spline.spline.control_points = {point(0.0, 0.0), point(1.0, 2.0),
point(3.0, 0.0)};
document.entities.push_back(spline);
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
hatch.hatch.solid = true;
hatch.hatch.seeds = {point(1.0, 1.0)};
DwgWriteHatchPath hatch_path;
DwgWriteHatchEdge hatch_edge;
hatch_edge.type = DwgWriteHatchEdgeType::Line;
hatch_edge.start = point(0.0, 0.0);
hatch_edge.end = point(4.0, 0.0);
hatch_path.edges.push_back(hatch_edge);
DwgWriteHatchEdge second_edge;
second_edge.type = DwgWriteHatchEdgeType::Line;
second_edge.start = point(4.0, 0.0);
second_edge.end = point(0.0, 3.0);
hatch_path.edges.push_back(second_edge);
DwgWriteHatchEdge third_edge;
third_edge.type = DwgWriteHatchEdgeType::Line;
third_edge.start = point(0.0, 3.0);
third_edge.end = point(0.0, 0.0);
hatch_path.edges.push_back(third_edge);
hatch.hatch.paths.push_back(hatch_path);
document.entities.push_back(hatch);
DwgWriteEntity attdef;
attdef.kind = DwgWriteEntityKind::AttDef;
attdef.insertion = point(0.0, 0.0);
attdef.height = 1.0;
attdef.text = "DEFAULT";
attdef.tag = "TAG";
attdef.prompt = "PROMPT";
document.entities.push_back(attdef);
vde::foundation::DwgWriteBlock block;
block.name = "B";
block.entities.push_back(line(point(0.0, 0.0), point(1.0, 1.0)));
document.blocks.push_back(block);
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "B";
insert.insertion = point(100.0, 50.0, 0.0);
vde::foundation::DwgWriteAttrib attribute;
attribute.tag = "NO";
attribute.text = "42";
attribute.insertion = point(101.0, 51.0, 0.0);
attribute.height = 1.0;
insert.insert.attributes.push_back(attribute);
document.entities.push_back(insert);
DwgWriteResult written;
const DwgObjectDocument doc =
roundtrip_ac1032(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R2018);
EXPECT_EQ(doc.objects.size(), written.object_count);
expect_all_consistent(doc);
const auto lines = model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), 2u);
const auto& elevated = std::get<DwgLine>(lines[1]->data);
expect_near(elevated.start.z, 3.5);
expect_near(elevated.end.z, -8.125);
std::uint64_t geom_layer = 0;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Layer) continue;
if (std::get<vde::foundation::DwgLayer>(object.data).name == "GEOM") {
geom_layer = object.handle;
}
}
ASSERT_NE(geom_layer, 0u);
EXPECT_EQ(lines[1]->common.layer, geom_layer);
const auto circles = model_entities(doc, DwgObjectType::Circle);
ASSERT_EQ(circles.size(), 1u);
expect_near(std::get<DwgCircle>(circles[0]->data).radius, 7.5);
const auto arcs = model_entities(doc, DwgObjectType::Arc);
ASSERT_EQ(arcs.size(), 1u);
expect_near(std::get<DwgArc>(arcs[0]->data).start_angle, 0.5);
expect_near(std::get<DwgArc>(arcs[0]->data).end_angle, 3.0);
const auto ellipses = model_entities(doc, DwgObjectType::Ellipse);
ASSERT_EQ(ellipses.size(), 1u);
expect_near(std::get<DwgEllipse>(ellipses[0]->data).ratio, 0.5);
const auto lwpolylines = model_entities(doc, DwgObjectType::LwPolyline);
ASSERT_EQ(lwpolylines.size(), 1u);
const auto& lw = std::get<vde::foundation::DwgLwPolyline>(
lwpolylines[0]->data);
ASSERT_EQ(lw.vertices.size(), 3u);
EXPECT_TRUE((lw.flags & 512u) != 0u);
expect_near(lw.vertices[1].bulge, 0.5);
const auto polylines = model_entities(doc, DwgObjectType::Polyline2D);
ASSERT_EQ(polylines.size(), 1u);
EXPECT_EQ(std::get<DwgPolyline2D>(polylines[0]->data).num_owned, 2u);
const auto splines = model_entities(doc, DwgObjectType::Spline);
ASSERT_EQ(splines.size(), 1u);
EXPECT_EQ(std::get<DwgSpline>(splines[0]->data).degree, 2);
const auto hatches = model_entities(doc, DwgObjectType::Hatch);
ASSERT_EQ(hatches.size(), 1u);
EXPECT_TRUE(std::get<DwgHatch>(hatches[0]->data).solid);
const auto inserts = model_entities(doc, DwgObjectType::Insert);
ASSERT_EQ(inserts.size(), 1u);
const auto& insert_data = std::get<DwgInsert>(inserts[0]->data);
EXPECT_TRUE(insert_data.has_attributes);
EXPECT_EQ(insert_data.num_owned, 1u);
const auto texts = model_entities(doc, DwgObjectType::Text);
ASSERT_EQ(texts.size(), 1u);
EXPECT_EQ(std::get<DwgText>(texts[0]->data).value, "VDE R2018");
const auto mtexts = model_entities(doc, DwgObjectType::MText);
ASSERT_EQ(mtexts.size(), 1u);
EXPECT_EQ(std::get<DwgMText>(mtexts[0]->data).value, "MULTILINE");
}
TEST(DwgWriter, Ac1032RoundtripUnicodeText) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"标注层", 3});
// TEXT / MTEXT:中文 + 拉丁 + 代理对(U+1F600)
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.layer = "标注层";
text.insertion = point(1.0, 2.0, 0.0);
text.height = 2.5;
text.text = "中文标注 ABC";
document.entities.push_back(text);
DwgWriteEntity mtext;
mtext.kind = DwgWriteEntityKind::MText;
mtext.layer = "标注层";
mtext.insertion = point(1.0, 5.0, 0.0);
mtext.height = 3.0;
mtext.text = "第一行\\P第二行 😀";
document.entities.push_back(mtext);
// 非 ASCII 块名 + 块内实体
vde::foundation::DwgWriteBlock block;
block.name = "零件块";
block.base_point = point(0.0, 0.0, 0.0);
block.entities.push_back(line(point(0.0, 0.0), point(1.0, 1.0)));
document.blocks.push_back(block);
// INSERT + 中文属性标签/值
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.layer = "标注层";
insert.block_name = "零件块";
insert.insertion = point(4.0, 4.0, 0.0);
vde::foundation::DwgWriteAttrib attrib;
attrib.insertion = point(4.5, 4.5, 0.0);
attrib.height = 0.5;
attrib.tag = "编号";
attrib.text = "部件-一";
insert.insert.attributes = {attrib};
document.entities.push_back(insert);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip_ac1032(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
const auto texts = model_entities(doc, DwgObjectType::Text);
ASSERT_EQ(texts.size(), 1u);
EXPECT_EQ(std::get<DwgText>(texts[0]->data).value, "中文标注 ABC");
const auto mtexts = model_entities(doc, DwgObjectType::MText);
ASSERT_EQ(mtexts.size(), 1u);
EXPECT_EQ(std::get<DwgMText>(mtexts[0]->data).value, "第一行\\P第二行 😀");
// 图层名(UTF-16 字符串流)逐字读回
bool layer_found = false;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Layer) continue;
if (std::get<vde::foundation::DwgLayer>(object.data).name == "标注层") {
layer_found = true;
}
}
EXPECT_TRUE(layer_found);
// 块名经 INSERT 的 BLOCK_HEADER 软指针回查
const auto inserts = model_entities(doc, DwgObjectType::Insert);
ASSERT_EQ(inserts.size(), 1u);
const DwgObject* block_header = doc.find_object(
std::get<DwgInsert>(inserts[0]->data).block_header);
ASSERT_NE(block_header, nullptr);
EXPECT_EQ(std::get<DwgBlockHeader>(block_header->data).name, "零件块");
// 属性标签与属性值
std::vector<const DwgObject*> attribs;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Attrib) continue;
if (object.common.owner != inserts[0]->handle) continue;
attribs.push_back(&object);
}
ASSERT_EQ(attribs.size(), 1u);
const auto& attrib_data =
std::get<vde::foundation::DwgAttrib>(attribs[0]->data);
EXPECT_EQ(attrib_data.tag, "编号");
EXPECT_EQ(attrib_data.text.value, "部件-一");
}
TEST(DwgWriter, Ac1032ManyEntitiesSpanMultipleDataPages) {
DwgWriteDocument document;
constexpr int kCount = 1200;
for (int i = 0; i < kCount; ++i) {
document.entities.push_back(line(point(i, 0.0), point(i, 100.0)));
}
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip_ac1032(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
// 单页 0x7400 字节;1200 条 LINE 的对象节必然跨页
const DwgContainerInfo info =
parse_dwg_container(written.bytes.data(), written.bytes.size());
ASSERT_TRUE(info.parsed) << info.error;
EXPECT_TRUE(info.file_size_matches);
const DwgSectionInfo* objects = info.find_section("AcDb:AcDbObjects");
ASSERT_NE(objects, nullptr);
EXPECT_GT(objects->page_count, 1u);
EXPECT_EQ(objects->pages.size(), objects->page_count);
const auto lines = model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), static_cast<std::size_t>(kCount));
for (int i = 0; i < kCount; ++i) {
const auto& data = std::get<DwgLine>(
lines[static_cast<std::size_t>(i)]->data);
expect_near(data.start.x, static_cast<double>(i));
expect_near(data.end.y, 100.0);
}
}
TEST(DwgWriter, Ac1032FileRoundtripMatchesMemory) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"EDGE", 2});
document.entities.push_back(
line(point(-1.0, -2.0, 0.0), point(3.0, 4.0, 5.0), "EDGE"));
const DwgWriteResult memory =
vde::foundation::dwg_write_ac1032(document);
ASSERT_TRUE(memory.ok) << memory.error;
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_writer_roundtrip_ac1032.dwg";
std::string error;
ASSERT_TRUE(
vde::foundation::dwg_write_ac1032_file(path, document, &error))
<< error;
const DwgObjectDocument doc = decode_dwg_objects_file(path);
if (std::getenv("VDE_KEEP_DWG") == nullptr) std::remove(path.c_str());
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R2018);
EXPECT_EQ(doc.file_size, memory.bytes.size());
expect_all_consistent(doc);
const auto lines = model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), 1u);
const auto& data = std::get<DwgLine>(lines[0]->data);
expect_near(data.start.z, 0.0);
expect_near(data.end.z, 5.0);
}
// ===================================================================
// R12(AC1009)原生写入(WS-3 M9)
// ===================================================================
namespace {
/// 写 → 读回(R12);失败时把解码告警一并输出
DwgObjectDocument roundtrip_ac1009(const DwgWriteDocument& document,
DwgWriteResult* written = nullptr) {
const DwgWriteResult result =
vde::foundation::dwg_write_ac1009(document);
if (written != nullptr) *written = result;
if (!result.ok) return {};
DwgObjectDocument doc =
decode_dwg_objects(result.bytes.data(), result.bytes.size());
if (!doc.ok) {
for (const auto& warning : doc.warnings) {
std::cout << " ! " << warning << "\n";
}
}
return doc;
}
/// R12 模型空间中指定类型的实体(owner==0:pre-R13 无模型空间句柄,
/// BLOCK…ENDBLK 之间的实体 owner 才被 link_r12_objects 置为块记录句柄)
std::vector<const DwgObject*> r12_model_entities(const DwgObjectDocument& doc,
DwgObjectType type) {
std::vector<const DwgObject*> result;
for (const auto& object : doc.objects) {
if (object.type != type) continue;
if (object.common.owner != 0) continue;
result.push_back(&object);
}
return result;
}
} // namespace
TEST(DwgWriter, R12EmptyDocument) {
DwgWriteDocument document;
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip_ac1009(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_TRUE(written.warnings.empty());
EXPECT_TRUE(doc.warnings.empty());
EXPECT_EQ(doc.version, DwgVersion::R12);
EXPECT_TRUE(doc.header_ok);
EXPECT_EQ(doc.objects.size(), 3u); // LAYER + STYLE + LTYPE
EXPECT_EQ(doc.objects.size(), written.object_count);
expect_all_consistent(doc);
EXPECT_EQ(doc.count_type(DwgObjectType::Layer), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::Style), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::LType), 1u);
EXPECT_EQ(doc.count_type(DwgObjectType::BlockHeader), 0u);
const auto layers =
r12_model_entities(doc, DwgObjectType::Layer);
ASSERT_EQ(layers.size(), 1u);
const auto& layer = std::get<vde::foundation::DwgLayer>(layers[0]->data);
EXPECT_EQ(layer.name, "0");
EXPECT_EQ(layer.color_index, 7u);
const auto styles = r12_model_entities(doc, DwgObjectType::Style);
ASSERT_EQ(styles.size(), 1u);
const auto& style =
std::get<vde::foundation::DwgStyle>(styles[0]->data);
EXPECT_EQ(style.name, "STANDARD");
EXPECT_EQ(style.font_file, "txt");
const auto ltypes = r12_model_entities(doc, DwgObjectType::LType);
ASSERT_EQ(ltypes.size(), 1u);
const auto& ltype =
std::get<vde::foundation::DwgLType>(ltypes[0]->data);
EXPECT_EQ(ltype.name, "CONTINUOUS");
EXPECT_EQ(ltype.description, "Solid line");
EXPECT_EQ(ltype.alignment, 65u);
}
TEST(DwgWriter, R12RoundtripAllEntityKinds) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"GEOM", 3});
// LINE:z 全零与 z 非零(R12 LINE 记录恒写 3RD)
document.entities.push_back(line(point(0.0, 0.0), point(10.0, 5.0)));
document.entities.push_back(line(point(1.5, -2.5, 3.5),
point(4.25, 6.75, -8.125), "GEOM"));
DwgWriteEntity circle;
circle.kind = DwgWriteEntityKind::Circle;
circle.layer = "GEOM";
circle.center = point(2.0, 3.0, 4.0);
circle.radius = 7.5;
document.entities.push_back(circle);
DwgWriteEntity arc;
arc.kind = DwgWriteEntityKind::Arc;
arc.center = point(-1.0, 0.5, 1.25);
arc.radius = 2.25;
arc.start_angle = 0.5;
arc.end_angle = 3.0;
document.entities.push_back(arc);
DwgWriteEntity point_entity;
point_entity.kind = DwgWriteEntityKind::Point;
point_entity.location = point(9.0, -3.0, 1.0);
document.entities.push_back(point_entity);
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.layer = "GEOM";
text.insertion = point(5.0, 6.0, 0.5);
text.height = 2.5;
text.rotation = 0.75;
text.text = "VDE DWG";
document.entities.push_back(text);
DwgWriteEntity attdef;
attdef.kind = DwgWriteEntityKind::AttDef;
attdef.insertion = point(1.0, 1.0, 0.25);
attdef.height = 1.5;
attdef.rotation = 0.5;
attdef.text = "VAL";
attdef.prompt = "Enter value";
attdef.tag = "TAG1";
attdef.attribute_flags = 2;
document.entities.push_back(attdef);
// POLYLINE_2D:统一 z=2.5、闭合、顶点宽度与凸度
DwgWriteEntity poly2d;
poly2d.kind = DwgWriteEntityKind::Polyline2D;
poly2d.layer = "GEOM";
poly2d.closed = true;
poly2d.vertices = {point(0.0, 0.0, 2.5), point(10.0, 0.0, 2.5),
point(10.0, 5.0, 2.5), point(0.0, 5.0, 2.5)};
poly2d.start_widths = {0.1, 0.0, 0.0, 0.0};
poly2d.end_widths = {0.2, 0.0, 0.0, 0.0};
poly2d.bulges = {0.0, 0.5, 0.0, 0.0};
document.entities.push_back(poly2d);
// POLYLINE_3D:闭合、绝对 WCS 坐标
DwgWriteEntity poly3d;
poly3d.kind = DwgWriteEntityKind::Polyline3D;
poly3d.closed = true;
poly3d.vertices = {point(0.0, 0.0, 0.0), point(1.0, 1.0, 5.0),
point(2.0, 0.0, -3.0)};
document.entities.push_back(poly3d);
// POLYLINE_MESH:M=2×N=3、M 方向闭合
DwgWriteEntity mesh;
mesh.kind = DwgWriteEntityKind::PolylineMesh;
mesh.mesh_m = 2;
mesh.mesh_n = 3;
mesh.mesh_closed_m = true;
mesh.vertices = {point(0.0, 0.0, 0.0), point(1.0, 0.0, 0.0),
point(2.0, 0.0, 0.0), point(0.0, 1.0, 0.25),
point(1.0, 1.0, 0.25), point(2.0, 1.0, 0.25)};
document.entities.push_back(mesh);
// POLYLINE_PFACE:4 坐标顶点 + 1 四角面 + 1 三角面(第 4 槽 0)
DwgWriteEntity pface;
pface.kind = DwgWriteEntityKind::PolylinePFace;
pface.layer = "GEOM";
pface.vertices = {point(0.0, 0.0, 0.0), point(1.0, 0.0, 0.0),
point(1.0, 1.0, 0.0), point(0.0, 1.0, 0.5)};
DwgWritePFaceFace quad;
quad.vertices[0] = 1;
quad.vertices[1] = 2;
quad.vertices[2] = 3;
quad.vertices[3] = 4;
pface.faces.push_back(quad);
DwgWritePFaceFace tri;
tri.vertices[0] = 1;
tri.vertices[1] = 2;
tri.vertices[2] = 3;
tri.vertices[3] = 0;
pface.faces.push_back(tri);
document.entities.push_back(pface);
DwgWriteEntity face;
face.kind = DwgWriteEntityKind::Face3D;
face.corners[0] = point(0.0, 0.0, 0.0);
face.corners[1] = point(4.0, 0.0, 0.0);
face.corners[2] = point(4.0, 3.0, 0.0);
face.corners[3] = point(0.0, 3.0, 2.0);
face.invis_flags = 0x03;
document.entities.push_back(face);
// 块定义(基点 z 非 0 时 R12 只存 2RD,z 如实丢失为 0)+ 属性标记
DwgWriteBlock block;
block.name = "BLK";
block.base_point = point(1.0, 2.0, 0.0);
block.entities.push_back(line(point(0.0, 0.0), point(1.0, 0.0)));
DwgWriteEntity block_attdef;
block_attdef.kind = DwgWriteEntityKind::AttDef;
block_attdef.insertion = point(0.0, 0.0);
block_attdef.height = 1.0;
block_attdef.text = "X";
block_attdef.prompt = "P";
block_attdef.tag = "T";
block.entities.push_back(block_attdef);
document.blocks.push_back(block);
// INSERT:非单位缩放 + 旋转 + 属性流(ATTRIB + SEQEND)
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.layer = "GEOM";
insert.block_name = "BLK";
insert.insertion = point(7.0, 8.0, 0.5);
insert.scale = point(2.0, 3.0, 0.5);
insert.rotation = 0.25;
DwgWriteAttrib attrib;
attrib.insertion = point(7.0, 8.0, 0.5);
attrib.height = 1.0;
attrib.text = "ABC";
attrib.tag = "TAG1";
attrib.flags = 4;
insert.insert.attributes.push_back(attrib);
document.entities.push_back(insert);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip_ac1009(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_TRUE(written.warnings.empty());
EXPECT_TRUE(doc.warnings.empty());
expect_all_consistent(doc);
EXPECT_EQ(doc.version, DwgVersion::R12);
// 对象数 = 表(LAYER2+BLOCK1+STYLE1+LTYPE1) + 模型空间 38 +
// BLOCKS 段 4(BLOCK+LINE+ATTDEF+ENDBLK)
EXPECT_EQ(doc.objects.size(), 47u);
EXPECT_EQ(doc.objects.size(), written.object_count);
// ---- LINE ----
const auto lines = r12_model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), 2u); // 模型空间 2 条(块内那条 owner 非零)
{
const auto& data = std::get<DwgLine>(lines[0]->data);
expect_near(data.start.x, 0.0);
expect_near(data.start.y, 0.0);
expect_near(data.start.z, 0.0);
expect_near(data.end.x, 10.0);
expect_near(data.end.y, 5.0);
expect_near(data.end.z, 0.0);
}
{
const auto& data = std::get<DwgLine>(lines[1]->data);
expect_near(data.start.x, 1.5);
expect_near(data.start.y, -2.5);
expect_near(data.start.z, 3.5);
expect_near(data.end.x, 4.25);
expect_near(data.end.y, 6.75);
expect_near(data.end.z, -8.125);
}
// ---- 图层引用:实体 layer 句柄指向 GEOM 表记录 ----
{
const DwgObject* layer =
doc.find_object(lines[1]->common.layer);
ASSERT_NE(layer, nullptr);
const auto& data =
std::get<vde::foundation::DwgLayer>(layer->data);
EXPECT_EQ(data.name, "GEOM");
EXPECT_EQ(data.color_index, 3u);
EXPECT_EQ(data.values, 0x40);
}
// ---- CIRCLE:z 经 elevation 字段还原 ----
const auto circles = r12_model_entities(doc, DwgObjectType::Circle);
ASSERT_EQ(circles.size(), 1u);
{
const auto& data =
std::get<vde::foundation::DwgCircle>(circles[0]->data);
expect_near(data.center.x, 2.0);
expect_near(data.center.y, 3.0);
expect_near(data.center.z, 4.0);
expect_near(data.radius, 7.5);
}
// ---- ARC ----
const auto arcs = r12_model_entities(doc, DwgObjectType::Arc);
ASSERT_EQ(arcs.size(), 1u);
{
const auto& data = std::get<DwgArc>(arcs[0]->data);
expect_near(data.center.x, -1.0);
expect_near(data.center.y, 0.5);
expect_near(data.center.z, 1.25);
expect_near(data.radius, 2.25);
expect_near(data.start_angle, 0.5);
expect_near(data.end_angle, 3.0);
}
// ---- POINT ----
const auto points = r12_model_entities(doc, DwgObjectType::Point);
ASSERT_EQ(points.size(), 1u);
{
const auto& data =
std::get<vde::foundation::DwgPoint>(points[0]->data);
expect_near(data.location.x, 9.0);
expect_near(data.location.y, -3.0);
expect_near(data.location.z, 1.0);
}
// ---- TEXT:rotation 尾字段 + elevation ----
const auto texts = r12_model_entities(doc, DwgObjectType::Text);
ASSERT_EQ(texts.size(), 1u);
{
const auto& data = std::get<DwgText>(texts[0]->data);
EXPECT_EQ(data.value, "VDE DWG");
expect_near(data.height, 2.5);
expect_near(data.rotation, 0.75);
expect_near(data.elevation, 0.5);
expect_near(data.insertion.x, 5.0);
expect_near(data.insertion.y, 6.0);
expect_near(data.insertion.z, 0.5);
}
// ---- ATTDEF(模型空间)----
const auto attdefs = r12_model_entities(doc, DwgObjectType::AttDef);
ASSERT_EQ(attdefs.size(), 1u);
{
const auto& data = std::get<DwgAttrib>(attdefs[0]->data);
EXPECT_EQ(data.text.value, "VAL");
EXPECT_EQ(data.prompt, "Enter value");
EXPECT_EQ(data.tag, "TAG1");
EXPECT_EQ(data.flags, 2u);
expect_near(data.text.height, 1.5);
expect_near(data.text.rotation, 0.5);
}
// ---- POLYLINE_2D ----
const auto polylines =
r12_model_entities(doc, DwgObjectType::Polyline2D);
ASSERT_EQ(polylines.size(), 1u);
const auto* polyline_object = polylines[0];
{
const auto& data =
std::get<DwgPolyline2D>(polyline_object->data);
EXPECT_EQ(data.flags & 0x01, 1u); // closed
expect_near(data.elevation, 2.5);
EXPECT_EQ(data.vertex_handles.size(), 4u);
EXPECT_EQ(data.num_owned, 4u);
EXPECT_NE(data.seqend, 0u);
}
// 顶点逐个核对(按句柄升序 = 写入顺序)
for (std::uint64_t handle :
std::get<DwgPolyline2D>(polyline_object->data).vertex_handles) {
const DwgObject* vertex_object = doc.find_object(handle);
ASSERT_NE(vertex_object, nullptr);
ASSERT_EQ(vertex_object->common.owner, polyline_object->handle);
}
{
const DwgObject* v0 = doc.find_object(
std::get<DwgPolyline2D>(polyline_object->data).vertex_handles[0]);
const DwgObject* v1 = doc.find_object(
std::get<DwgPolyline2D>(polyline_object->data).vertex_handles[1]);
ASSERT_NE(v0, nullptr);
ASSERT_NE(v1, nullptr);
const auto& d0 = std::get<DwgVertex2D>(v0->data);
const auto& d1 = std::get<DwgVertex2D>(v1->data);
expect_near(d0.start_width, 0.1);
expect_near(d0.end_width, 0.2);
expect_near(d0.bulge, 0.0);
expect_near(d0.location.x, 0.0);
expect_near(d0.location.y, 0.0);
expect_near(d0.location.z, 0.0); // 2D 顶点 z 由多段线 elevation 表达
expect_near(d1.start_width, 0.0);
expect_near(d1.end_width, 0.0);
expect_near(d1.bulge, 0.5);
expect_near(d1.location.x, 10.0);
}
// ---- POLYLINE_3D ----
const auto poly3ds =
r12_model_entities(doc, DwgObjectType::Polyline3D);
ASSERT_EQ(poly3ds.size(), 1u);
{
const auto& data = std::get<DwgPolyline3D>(poly3ds[0]->data);
EXPECT_EQ(data.flags, 0x09u); // 3D(0x08) | closed(0x01)
EXPECT_EQ(data.vertex_handles.size(), 3u);
EXPECT_NE(data.seqend, 0u);
const DwgObject* v1 = doc.find_object(data.vertex_handles[1]);
ASSERT_NE(v1, nullptr);
const auto& d1 = std::get<DwgVertex3D>(v1->data);
expect_near(d1.location.x, 1.0);
expect_near(d1.location.y, 1.0);
expect_near(d1.location.z, 5.0); // 3D 顶点保留绝对 z
}
// ---- POLYLINE_MESH ----
const auto meshes =
r12_model_entities(doc, DwgObjectType::PolylineMesh);
ASSERT_EQ(meshes.size(), 1u);
{
const auto& data =
std::get<DwgPolylineMesh>(meshes[0]->data);
EXPECT_EQ(data.flags, 0x11u); // MESH(0x10) | closed M(0x01)
EXPECT_EQ(data.num_m_verts, 2u);
EXPECT_EQ(data.num_n_verts, 3u);
EXPECT_EQ(data.vertex_handles.size(), 6u);
EXPECT_NE(data.seqend, 0u);
const DwgObject* v4 = doc.find_object(data.vertex_handles[4]);
ASSERT_NE(v4, nullptr);
EXPECT_EQ(v4->type, DwgObjectType::VertexMesh);
const auto& d4 = std::get<DwgVertex3D>(v4->data);
expect_near(d4.location.x, 1.0);
expect_near(d4.location.y, 1.0);
expect_near(d4.location.z, 0.25);
}
// ---- POLYLINE_PFACE:坐标顶点 + 面记录 ----
const auto pfaces =
r12_model_entities(doc, DwgObjectType::PolylinePFace);
ASSERT_EQ(pfaces.size(), 1u);
{
const auto& data =
std::get<DwgPolylinePFace>(pfaces[0]->data);
EXPECT_EQ(data.num_verts, 4u);
EXPECT_EQ(data.num_faces, 2u);
// 4 坐标顶点 + 2 面记录全部挂到顶点句柄向量
EXPECT_EQ(data.vertex_handles.size(), 6u);
EXPECT_NE(data.seqend, 0u);
const DwgObject* v3 = doc.find_object(data.vertex_handles[3]);
ASSERT_NE(v3, nullptr);
EXPECT_EQ(v3->type, DwgObjectType::VertexPFace);
const auto& d3 = std::get<DwgVertexPFace>(v3->data);
EXPECT_EQ(d3.flags, 0xC0);
expect_near(d3.location.z, 0.5);
const DwgObject* f1 = doc.find_object(data.vertex_handles[4]);
ASSERT_NE(f1, nullptr);
EXPECT_EQ(f1->type, DwgObjectType::VertexPFaceFace);
const auto& face1 = std::get<DwgVertexPFaceFace>(f1->data);
EXPECT_EQ(face1.index[0], 1u);
EXPECT_EQ(face1.index[1], 2u);
EXPECT_EQ(face1.index[2], 3u);
EXPECT_EQ(face1.index[3], 4u);
const DwgObject* f2 = doc.find_object(data.vertex_handles[5]);
ASSERT_NE(f2, nullptr);
const auto& face2 = std::get<DwgVertexPFaceFace>(f2->data);
EXPECT_EQ(face2.index[0], 1u);
EXPECT_EQ(face2.index[1], 2u);
EXPECT_EQ(face2.index[2], 3u);
EXPECT_EQ(face2.index[3], 0u); // 三角面第 4 槽空位
}
// ---- 3DFACE ----
const auto faces = r12_model_entities(doc, DwgObjectType::Face3D);
ASSERT_EQ(faces.size(), 1u);
{
const auto& data =
std::get<vde::foundation::DwgFace3D>(faces[0]->data);
expect_near(data.corner[0].x, 0.0);
expect_near(data.corner[1].x, 4.0);
expect_near(data.corner[2].y, 3.0);
expect_near(data.corner[3].z, 2.0);
EXPECT_EQ(data.invis_flags, 0x03);
}
// ---- INSERT + 属性流 ----
const auto inserts = r12_model_entities(doc, DwgObjectType::Insert);
ASSERT_EQ(inserts.size(), 1u);
{
const auto& data =
std::get<DwgInsert>(inserts[0]->data);
expect_near(data.insertion.x, 7.0);
expect_near(data.insertion.y, 8.0);
expect_near(data.insertion.z, 0.5);
expect_near(data.x_scale, 2.0);
expect_near(data.y_scale, 3.0);
expect_near(data.z_scale, 0.5);
expect_near(data.rotation, 0.25);
EXPECT_TRUE(data.has_attributes);
EXPECT_NE(data.seqend, 0u);
// block_header 指向名为 BLK 的块表记录
const DwgObject* header = doc.find_object(data.block_header);
ASSERT_NE(header, nullptr);
EXPECT_EQ(std::get<DwgBlockHeader>(header->data).name, "BLK");
}
const auto attribs = r12_model_entities(doc, DwgObjectType::Attrib);
ASSERT_EQ(attribs.size(), 1u);
{
const auto& data = std::get<DwgAttrib>(attribs[0]->data);
EXPECT_EQ(data.text.value, "ABC");
EXPECT_EQ(data.tag, "TAG1");
EXPECT_EQ(data.flags, 4u);
expect_near(data.text.height, 1.0);
}
// ---- 块定义:BLOCK/ENDBLK 句柄链 + 块内实体归属 ----
const auto headers =
r12_model_entities(doc, DwgObjectType::BlockHeader);
ASSERT_EQ(headers.size(), 1u);
{
const auto& data = std::get<DwgBlockHeader>(headers[0]->data);
EXPECT_EQ(data.name, "BLK");
expect_near(data.base_point.x, 1.0);
expect_near(data.base_point.y, 2.0);
expect_near(data.base_point.z, 0.0);
EXPECT_TRUE(data.has_attdefs); // 块内含 ATTDEF → 表 flag 0x02
EXPECT_NE(data.begin_block, 0u);
EXPECT_NE(data.end_block, 0u);
const DwgObject* begin = doc.find_object(data.begin_block);
ASSERT_NE(begin, nullptr);
EXPECT_EQ(begin->type, DwgObjectType::Block);
EXPECT_EQ(std::get<DwgBlock>(begin->data).name, "BLK");
const DwgObject* end = doc.find_object(data.end_block);
ASSERT_NE(end, nullptr);
EXPECT_EQ(end->type, DwgObjectType::EndBlk);
// 块内 LINE / ATTDEF 的 owner = 块记录句柄
int owned = 0;
for (const auto& object : doc.objects) {
if (object.common.owner != headers[0]->handle) continue;
if (object.type == DwgObjectType::Line) ++owned;
if (object.type == DwgObjectType::AttDef) ++owned;
}
EXPECT_EQ(owned, 2);
}
}
TEST(DwgWriter, R12Degradation) {
DwgWriteDocument document;
document.entities.push_back(line(point(0.0, 0.0), point(1.0, 0.0)));
// R12 无对应图元:逐条跳过并登记 warning
DwgWriteEntity mtext;
mtext.kind = DwgWriteEntityKind::MText;
mtext.text = "multi";
document.entities.push_back(mtext);
DwgWriteEntity ellipse;
ellipse.kind = DwgWriteEntityKind::Ellipse;
ellipse.center = point(0.0, 0.0);
ellipse.major_axis = point(4.0, 0.0, 0.0);
ellipse.axis_ratio = 0.5;
document.entities.push_back(ellipse);
DwgWriteEntity spline;
spline.kind = DwgWriteEntityKind::Spline;
document.entities.push_back(spline);
DwgWriteEntity hatch;
hatch.kind = DwgWriteEntityKind::Hatch;
document.entities.push_back(hatch);
DwgWriteEntity solid;
solid.kind = DwgWriteEntityKind::Solid3D;
solid.acis_data = "solid body satin";
document.entities.push_back(solid);
// LWPOLYLINE → POLYLINE_2D 转写
DwgWriteEntity lw;
lw.kind = DwgWriteEntityKind::LwPolyline;
lw.vertices = {point(0.0, 0.0), point(1.0, 1.0), point(2.0, 0.0)};
document.entities.push_back(lw);
// 顶点 z 不齐的 POLYLINE_2D → POLYLINE_3D 转写
DwgWriteEntity uneven;
uneven.kind = DwgWriteEntityKind::Polyline2D;
uneven.vertices = {point(0.0, 0.0, 0.0), point(1.0, 1.0, 2.0)};
document.entities.push_back(uneven);
DwgWriteResult written;
const DwgObjectDocument doc = roundtrip_ac1009(document, &written);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_TRUE(doc.ok) << doc.error;
ASSERT_EQ(written.warnings.size(), 7u);
// 5 条“无对应图元” + 2 条转写说明
int skipped = 0;
int converted = 0;
for (const auto& warning : written.warnings) {
if (warning.find("无对应图元") != std::string::npos) ++skipped;
if (warning.find("转写") != std::string::npos) ++converted;
}
EXPECT_EQ(skipped, 5);
EXPECT_EQ(converted, 2);
expect_all_consistent(doc);
EXPECT_EQ(doc.count_type(DwgObjectType::MText), 0u);
EXPECT_EQ(doc.count_type(DwgObjectType::Ellipse), 0u);
EXPECT_EQ(doc.count_type(DwgObjectType::Spline), 0u);
EXPECT_EQ(doc.count_type(DwgObjectType::Hatch), 0u);
EXPECT_EQ(doc.count_type(DwgObjectType::Solid3D), 0u);
// 转写结果:LWPOLYLINE 变 Polyline2D,z 不齐变 Polyline3D
EXPECT_EQ(r12_model_entities(doc, DwgObjectType::Polyline2D).size(), 1u);
EXPECT_EQ(r12_model_entities(doc, DwgObjectType::Polyline3D).size(), 1u);
const auto& converted_poly = std::get<DwgPolyline3D>(
r12_model_entities(doc, DwgObjectType::Polyline3D)[0]->data);
EXPECT_EQ(converted_poly.vertex_handles.size(), 2u);
}
TEST(DwgWriter, R12RejectsInvalidInput) {
const auto expect_failure = [](const DwgWriteDocument& document,
const char* what) {
const DwgWriteResult result =
vde::foundation::dwg_write_ac1009(document);
EXPECT_FALSE(result.ok) << what;
EXPECT_FALSE(result.error.empty()) << what;
EXPECT_TRUE(result.bytes.empty()) << what;
};
{ // TEXT 含非 ASCII
DwgWriteDocument document;
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.height = 1.0;
text.text = "温度 25\u00B0C";
document.entities.push_back(text);
expect_failure(document, "TEXT non-ascii");
}
{ // 图层名含非 ASCII
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"图层", 7});
expect_failure(document, "layer non-ascii");
}
{ // 图层名重复
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"A", 1});
document.layers.push_back(DwgWriteLayer{"A", 2});
expect_failure(document, "duplicate layer");
}
{ // 图层名为空
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"", 7});
expect_failure(document, "empty layer name");
}
{ // 图层名 32 字符(R12 名槽 32 字节,上限 31)
DwgWriteDocument document;
document.layers.push_back(
DwgWriteLayer{std::string(32, 'L'), 7});
expect_failure(document, "layer name too long");
}
{ // 实体引用不存在的图层
DwgWriteDocument document;
document.entities.push_back(line(point(0.0, 0.0), point(1.0, 0.0),
"NOPE"));
expect_failure(document, "unknown layer reference");
}
{ // INSERT 引用不存在的块
DwgWriteDocument document;
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "GHOST";
document.entities.push_back(insert);
expect_failure(document, "unknown block reference");
}
{ // INSERT 缩放为 0
DwgWriteDocument document;
DwgWriteBlock block;
block.name = "B";
document.blocks.push_back(block);
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "B";
insert.scale = point(0.0, 1.0, 1.0);
document.entities.push_back(insert);
expect_failure(document, "zero scale");
}
{ // 块自引用
DwgWriteDocument document;
DwgWriteBlock block;
block.name = "SELF";
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "SELF";
block.entities.push_back(insert);
document.blocks.push_back(block);
expect_failure(document, "self reference");
}
{ // 保留块名
DwgWriteDocument document;
DwgWriteBlock block;
block.name = "*Model_Space";
document.blocks.push_back(block);
expect_failure(document, "reserved block name");
}
{ // 块名 32 字符
DwgWriteDocument document;
DwgWriteBlock block;
block.name = std::string(32, 'B');
document.blocks.push_back(block);
expect_failure(document, "block name too long");
}
{ // PFACE 顶点索引越界
DwgWriteDocument document;
DwgWriteEntity pface;
pface.kind = DwgWriteEntityKind::PolylinePFace;
pface.vertices = {point(0.0, 0.0), point(1.0, 0.0)};
DwgWritePFaceFace face;
face.vertices[0] = 1;
face.vertices[1] = 2;
face.vertices[2] = 3; // 只有 2 个顶点
pface.faces.push_back(face);
document.entities.push_back(pface);
expect_failure(document, "pface index out of range");
}
{ // POLYLINE_2D 顶点数不足
DwgWriteDocument document;
DwgWriteEntity poly;
poly.kind = DwgWriteEntityKind::Polyline2D;
poly.vertices = {point(0.0, 0.0)};
document.entities.push_back(poly);
expect_failure(document, "polyline too few vertices");
}
}
TEST(DwgWriter, R12FileRoundtripMatchesMemory) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"EDGE", 2});
document.blocks.push_back(DwgWriteBlock{});
document.blocks.back().name = "SYM";
document.blocks.back().entities.push_back(
line(point(0.0, 0.0), point(1.0, 0.0)));
DwgWriteEntity insert;
insert.kind = DwgWriteEntityKind::Insert;
insert.block_name = "SYM";
insert.insertion = point(3.0, 4.0, 0.0);
document.entities.push_back(insert);
document.entities.push_back(
line(point(-1.0, -2.0, 0.0), point(3.0, 4.0, 5.0), "EDGE"));
const DwgWriteResult memory =
vde::foundation::dwg_write_ac1009(document);
ASSERT_TRUE(memory.ok) << memory.error;
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_writer_roundtrip_ac1009.dwg";
std::string error;
ASSERT_TRUE(
vde::foundation::dwg_write_ac1009_file(path, document, &error))
<< error;
// 文件字节与内存写出完全一致
std::ifstream file(path, std::ios::binary);
ASSERT_TRUE(file.good());
std::vector<std::uint8_t> on_disk(memory.bytes.size());
file.read(reinterpret_cast<char*>(on_disk.data()),
static_cast<std::streamsize>(on_disk.size()));
ASSERT_EQ(file.gcount(),
static_cast<std::streamsize>(memory.bytes.size()));
EXPECT_EQ(on_disk, memory.bytes);
file.close();
const DwgObjectDocument doc = decode_dwg_objects_file(path);
if (std::getenv("VDE_KEEP_DWG") == nullptr) std::remove(path.c_str());
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R12);
EXPECT_EQ(doc.file_size, memory.bytes.size());
expect_all_consistent(doc);
EXPECT_EQ(r12_model_entities(doc, DwgObjectType::Insert).size(), 1u);
EXPECT_EQ(r12_model_entities(doc, DwgObjectType::Line).size(), 1u);
}
/// 临时诊断:写出不同最小文档,用于 AutoCAD 打开性二分(VDE_KEEP_DWG=1 时启用)
TEST(DwgWriter, ProbeVariants) {
if (std::getenv("VDE_KEEP_DWG") == nullptr) GTEST_SKIP();
const std::string base = VDE_TEST_TEMP_DIR;
const auto dump = [&](const char* name, const DwgWriteDocument& doc,
bool r2018 = false) {
std::string error;
const std::string path = base + "/" + name;
const bool ok = r2018
? vde::foundation::dwg_write_ac1032_file(path, doc,
&error)
: vde::foundation::dwg_write_ac1015_file(path, doc,
&error);
std::cout << "PROBE " << name << " ok=" << ok << " err=" << error << "\n";
const DwgObjectDocument back = decode_dwg_objects_file(path);
std::cout << " decode ok=" << back.ok << " err=" << back.error
<< " objects=" << back.objects.size() << "\n";
for (const auto& o : back.objects) {
if (o.consistent && o.decoded) continue;
std::cout << " BAD h=" << o.handle << " " << o.type_name
<< " addr=" << o.address << " bitsize=" << o.bitsize
<< " data_res=" << o.data_residual_bits
<< " hdl_res=" << o.handle_residual_bits
<< " dec=" << o.decoded << " ok=" << o.consistent << "\n";
}
};
dump("probe_empty.dwg", DwgWriteDocument{});
dump("probe_empty_2018.dwg", DwgWriteDocument{}, true);
{
DwgWriteDocument doc;
doc.layers.push_back(DwgWriteLayer{"EDGE", 2});
dump("probe_layer.dwg", doc);
}
{
DwgWriteDocument doc;
doc.entities.push_back(line(point(0.0, 0.0, 0.0), point(1.0, 1.0, 0.0)));
dump("probe_line0.dwg", doc);
}
{
// 多实体种类混合:用于 AutoCAD accoreconsole 交叉校验(表/图层/实体)
DwgWriteDocument doc;
doc.layers.push_back(DwgWriteLayer{"GEOM", 3});
doc.entities.push_back(line(point(0.0, 0.0, 0.0), point(10.0, 5.0, 2.0)));
DwgWriteEntity circle;
circle.kind = DwgWriteEntityKind::Circle;
circle.layer = "GEOM";
circle.center = point(2.0, 3.0, 0.0);
circle.radius = 5.0;
doc.entities.push_back(circle);
DwgWriteEntity arc;
arc.kind = DwgWriteEntityKind::Arc;
arc.center = point(-1.0, 0.5, 0.0);
arc.radius = 2.25;
arc.start_angle = 0.5;
arc.end_angle = 3.0;
doc.entities.push_back(arc);
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.layer = "GEOM";
text.insertion = point(5.0, 6.0, 0.0);
text.height = 2.5;
text.text = "VDE DWG";
doc.entities.push_back(text);
DwgWriteEntity poly;
poly.kind = DwgWriteEntityKind::LwPolyline;
poly.closed = true;
poly.vertices = {point(-2.0, -3.0), point(4.0, -3.0), point(4.0, 6.0)};
doc.entities.push_back(poly);
dump("probe_mix.dwg", doc);
dump("probe_mix_2018.dwg", doc, true);
}
{
// 多边形网格:用于 AutoCAD accoreconsole 交叉校验(POLYLINE_MESH)
DwgWriteDocument doc;
DwgWriteEntity mesh;
mesh.kind = DwgWriteEntityKind::PolylineMesh;
mesh.mesh_m = 3;
mesh.mesh_n = 3;
for (int m = 0; m < 3; ++m) {
for (int n = 0; n < 3; ++n) {
mesh.vertices.push_back(point(2.0 * m, 2.0 * n,
1.0 * (m * n)));
}
}
doc.entities.push_back(mesh);
dump("probe_mesh.dwg", doc);
dump("probe_mesh_2018.dwg", doc, true);
}
{
// 多边形面网格:用于 AutoCAD accoreconsole 交叉校验(POLYLINE_PFACE)
DwgWriteDocument doc;
DwgWriteEntity pface;
pface.kind = DwgWriteEntityKind::PolylinePFace;
pface.vertices.push_back(point(0.0, 0.0, 0.0));
pface.vertices.push_back(point(10.0, 0.0, 0.0));
pface.vertices.push_back(point(10.0, 10.0, 0.0));
pface.vertices.push_back(point(0.0, 10.0, 0.0));
pface.vertices.push_back(point(5.0, 5.0, 8.0));
vde::foundation::DwgWritePFaceFace base;
base.vertices[0] = 1;
base.vertices[1] = 2;
base.vertices[2] = 3;
base.vertices[3] = 4;
pface.faces.push_back(base);
vde::foundation::DwgWritePFaceFace side;
side.vertices[0] = 1;
side.vertices[1] = 2;
side.vertices[2] = 5;
side.vertices[3] = 0;
pface.faces.push_back(side);
doc.entities.push_back(pface);
dump("probe_pface.dwg", doc);
dump("probe_pface_2018.dwg", doc, true);
}
if (std::getenv("VDE_PROBE_SPLIT") != nullptr) {
// 单实体变体:二分定位"哪种实体导致 AutoCAD 打开失败"
{
DwgWriteDocument doc;
DwgWriteEntity circle;
circle.kind = DwgWriteEntityKind::Circle;
circle.center = point(2.0, 3.0, 0.0);
circle.radius = 5.0;
doc.entities.push_back(circle);
dump("probe_one_circle.dwg", doc);
}
{
DwgWriteDocument doc;
DwgWriteEntity arc;
arc.kind = DwgWriteEntityKind::Arc;
arc.center = point(-1.0, 0.5, 0.0);
arc.radius = 2.25;
arc.start_angle = 0.5;
arc.end_angle = 3.0;
doc.entities.push_back(arc);
dump("probe_one_arc.dwg", doc);
}
{
DwgWriteDocument doc;
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.insertion = point(5.0, 6.0, 0.0);
text.height = 2.5;
text.text = "VDE DWG";
doc.entities.push_back(text);
dump("probe_one_text.dwg", doc);
}
{
DwgWriteDocument doc;
DwgWriteEntity poly;
poly.kind = DwgWriteEntityKind::LwPolyline;
poly.closed = true;
poly.vertices = {point(-2.0, -3.0), point(4.0, -3.0),
point(4.0, 6.0)};
doc.entities.push_back(poly);
dump("probe_one_lwpoly.dwg", doc);
}
{
DwgWriteDocument doc;
doc.entities.push_back(line(point(0.0, 0.0, 0.0),
point(1.0, 1.0, 0.0)));
doc.entities.push_back(line(point(2.0, 2.0, 0.0),
point(3.0, 5.0, 0.0)));
dump("probe_two_lines.dwg", doc);
}
{
// LINE + 2D 折线:折线的顶点/SEQEND 排在文件末尾,
// 用于验证"最后一个对象是简单实体"是否导致 AutoCAD 打不开
DwgWriteDocument doc;
doc.entities.push_back(line(point(0.0, 0.0, 0.0),
point(1.0, 1.0, 0.0)));
DwgWriteEntity pline;
pline.kind = DwgWriteEntityKind::Polyline2D;
pline.vertices = {point(-1.0, -2.0), point(3.0, -2.0),
point(3.0, 5.0)};
doc.entities.push_back(pline);
dump("probe_line_plus_pline.dwg", doc);
}
{
// 单个 LWPOLYLINE:无任何从属对象,最后一条记录是普通实体
DwgWriteDocument doc;
DwgWriteEntity poly;
poly.kind = DwgWriteEntityKind::LwPolyline;
poly.vertices = {point(0.0, 0.0, 0.0), point(1.0, 0.0, 0.0),
point(1.0, 1.0, 0.0)};
doc.entities.push_back(poly);
dump("v_lwpoly_only.dwg", doc);
}
{
// LINE + 3DFACE:末尾为普通实体,且无任何从属对象
DwgWriteDocument doc;
doc.entities.push_back(line(point(0.0, 0.0, 0.0),
point(1.0, 1.0, 0.0)));
DwgWriteEntity face;
face.kind = DwgWriteEntityKind::Face3D;
face.corners[0] = point(0.0, 0.0, 0.0);
face.corners[1] = point(1.0, 0.0, 0.0);
face.corners[2] = point(1.0, 1.0, 0.0);
face.corners[3] = point(0.0, 1.0, 0.0);
doc.entities.push_back(face);
dump("v_line_face.dwg", doc);
}
}
{
// ACIS 实体:载荷取自真实 AutoCAD 2000 BOX 实体的 SAT 文本
// (accoreconsole 生成、本仓解码器经 VDE_DWG_ACIS_DUMP 导出):
// 头部三行 + 完整实体记录、无 End 标记(version 1 分块编码自定
// 界);R2018 版由写侧按 version 2 追加结束标记。
DwgWriteDocument doc;
const std::string sat =
R"vdesat(400 112 1 0
16 Autodesk AutoCAD 20 ASM 225.1.0.65535 NT 24 Thu Oct 22 02:58:51 2026
1 9.999999999999999547e-07 1.000000000000000036e-10
body $-1 $1 $-1 $2 #
lump $-1 $-1 $3 $0 #
transform $-1 1 0 0 0 1 0 0 0 1 2.5 2.5 2.5 1 no_rotate no_reflect no_shear #
shell $-1 $-1 $-1 $4 $-1 $1 #
face $5 $6 $7 $3 $-1 $8 forward single #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $4 1 1 1 0 #
face $9 $10 $11 $3 $-1 $12 reversed single #
loop $-1 $-1 $13 $4 #
plane-surface $-1 0 0 2.5 0 0 1 1 0 0 forward_v I I I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $6 1 1 2 0 #
face $14 $15 $16 $3 $-1 $17 reversed single #
loop $-1 $-1 $18 $6 #
plane-surface $-1 0 0 -2.5 0 0 1 1 0 0 forward_v I I I I #
coedge $-1 $19 $20 $21 $22 forward $7 $-1 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $10 1 1 3 0 #
face $23 $24 $25 $3 $-1 $26 reversed single #
loop $-1 $-1 $27 $10 #
plane-surface $-1 0 -2.5 0 0 1 -0 -0 0 1 forward_v I I I I #
coedge $-1 $28 $29 $30 $31 forward $11 $-1 #
coedge $-1 $32 $13 $33 $34 forward $7 $-1 #
coedge $-1 $13 $32 $35 $36 forward $7 $-1 #
coedge $-1 $37 $38 $13 $22 reversed $39 $-1 #
edge $40 $41 $42 $21 $43 forward #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $15 1 1 6 0 #
face $44 $45 $46 $3 $-1 $47 reversed single #
loop $-1 $-1 $48 $15 #
plane-surface $-1 -2.5 0 0 1 0 0 0 0 -1 forward_v I I I I #
coedge $-1 $49 $35 $50 $51 forward $16 $-1 #
coedge $-1 $52 $18 $49 $53 forward $11 $-1 #
coedge $-1 $18 $52 $54 $55 forward $11 $-1 #
coedge $-1 $38 $37 $18 $31 reversed $39 $-1 #
edge $56 $57 $58 $30 $59 forward #
coedge $-1 $20 $19 $60 $61 forward $7 $-1 #
coedge $-1 $62 $63 $19 $34 reversed $46 $-1 #
edge $64 $42 $65 $33 $66 forward #
coedge $-1 $27 $67 $20 $36 reversed $16 $-1 #
edge $68 $69 $41 $35 $70 forward #
coedge $-1 $30 $21 $67 $71 forward $39 $-1 #
coedge $-1 $21 $30 $62 $72 reversed $39 $-1 #
loop $-1 $-1 $37 $45 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $22 1 1 24 0 #
vertex $73 $22 $74 #
vertex $75 $22 $76 #
straight-curve $-1 2.5 0 2.5 0 1 0 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $24 1 1 12 0 #
face $77 $-1 $39 $3 $-1 $78 reversed single #
loop $-1 $-1 $62 $24 #
plane-surface $-1 0 2.5 0 0 -1 0 0 0 -1 forward_v I I I I #
coedge $-1 $79 $60 $63 $80 forward $25 $-1 #
coedge $-1 $67 $27 $28 $53 reversed $16 $-1 #
coedge $-1 $60 $79 $27 $51 reversed $25 $-1 #
edge $81 $69 $82 $50 $83 forward #
coedge $-1 $29 $28 $79 $84 forward $11 $-1 #
edge $85 $58 $82 $49 $86 forward #
coedge $-1 $63 $62 $29 $55 reversed $46 $-1 #
edge $87 $88 $57 $54 $89 forward #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $31 1 1 21 0 #
vertex $90 $31 $91 #
vertex $92 $71 $93 #
straight-curve $-1 2.5 0 -2.5 0 -1 0 I I #
coedge $-1 $48 $50 $32 $61 reversed $25 $-1 #
edge $94 $65 $69 $60 $95 forward #
coedge $-1 $54 $33 $38 $72 forward $46 $-1 #
coedge $-1 $33 $54 $48 $80 reversed $46 $-1 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $34 1 1 16 0 #
vertex $96 $34 $97 #
straight-curve $-1 0 2.5 2.5 -1 0 0 I I #
coedge $-1 $35 $49 $37 $71 reversed $16 $-1 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $36 1 1 5 0 #
vertex $98 $61 $99 #
straight-curve $-1 0 -2.5 2.5 1 0 0 I I #
edge $100 $41 $58 $37 $101 forward #
edge $102 $42 $57 $38 $103 forward #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $41 1 1 25 0 #
point $-1 2.5 -2.5 2.5 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $42 1 1 26 0 #
point $-1 2.5 2.5 2.5 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $45 1 1 18 0 #
plane-surface $-1 2.5 0 0 -1 0 0 0 -0 1 forward_v I I I I #
coedge $-1 $50 $48 $52 $84 reversed $25 $-1 #
edge $104 $65 $88 $63 $105 forward #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $51 1 1 9 0 #
vertex $106 $84 $107 #
straight-curve $-1 -2.5 -2.5 0 0 0 -1 I I #
edge $108 $82 $88 $79 $109 forward #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $53 1 1 4 0 #
straight-curve $-1 0 -2.5 -2.5 -1 0 0 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $55 1 1 13 0 #
vertex $110 $55 $111 #
straight-curve $-1 0 2.5 -2.5 1 0 0 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $57 1 1 22 0 #
point $-1 2.5 2.5 -2.5 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $58 1 1 20 0 #
point $-1 2.5 -2.5 -2.5 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $61 1 1 10 0 #
straight-curve $-1 -2.5 0 2.5 0 -1 0 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $65 1 1 17 0 #
point $-1 -2.5 2.5 2.5 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $69 1 1 11 0 #
point $-1 -2.5 -2.5 2.5 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $71 1 1 19 0 #
straight-curve $-1 2.5 -2.5 0 0 0 -1 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $72 1 1 23 0 #
straight-curve $-1 2.5 2.5 0 0 0 -1 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $80 1 1 15 0 #
straight-curve $-1 -2.5 2.5 0 0 0 -1 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $82 1 1 8 0 #
point $-1 -2.5 -2.5 -2.5 #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $84 1 1 7 0 #
straight-curve $-1 -2.5 0 -2.5 0 1 0 I I #
persubent-acadSolidHistory-attrib $-1 $-1 $-1 $88 1 1 14 0 #
point $-1 -2.5 2.5 -2.5 #
)vdesat";
// 源码字面量在翻译阶段把 CRLF 规范化为 LF,这里还原成与真实
// DWG 载荷逐字节一致的 CRLF(5155 字节)
std::string sat_crlf;
for (char c : sat) {
if (c == '\n') sat_crlf += "\r\n";
else sat_crlf += c;
}
DwgWriteEntity solid;
solid.kind = DwgWriteEntityKind::Solid3D;
solid.acis_data = sat_crlf;
// 参考点 = BOX(0,0,0)-(5,5,5) 中心,与真实 refbox 记录一致
solid.acis_has_point = true;
solid.acis_point = {2.5, 2.5, 2.5};
doc.entities.push_back(solid);
dump("probe_acis.dwg", doc);
// R2018 目标:载荷降级为无几何空壳(AcDs 数据流未实现),
// 必须登记 warnings 且自解码一致
const auto written = vde::foundation::dwg_write_ac1032(doc);
ASSERT_TRUE(written.ok) << written.error;
ASSERT_EQ(written.warnings.size(), 1u);
EXPECT_NE(written.warnings[0].find("AcDs"), std::string::npos);
std::string error;
const std::string path2018 = base + "/probe_acis_2018.dwg";
EXPECT_TRUE(vde::foundation::dwg_write_ac1032_file(path2018, doc,
&error))
<< error;
const DwgObjectDocument back = decode_dwg_objects_file(path2018);
ASSERT_TRUE(back.ok) << back.error;
const auto solids = model_entities(back, DwgObjectType::Solid3D);
ASSERT_EQ(solids.size(), 1u);
const auto& acis_back = std::get<vde::foundation::DwgAcis>(
solids[0]->data);
EXPECT_TRUE(acis_back.data.empty()); // 载荷已如实丢弃
EXPECT_TRUE(acis_back.has_wireframe);
}
}
// ===================================================================
// DxfDocument → DWG 原生写入(WS-3 M3.8)
// ===================================================================
namespace {
/// 全部 12 类 DXF 实体 + 块定义 + 非 ASCII 文字(应被跳过并计数)
vde::foundation::DxfDocument make_all_kinds_drawing() {
using namespace vde::foundation;
DxfDocument drawing;
drawing.version = DxfVersion::R2018;
drawing.version_string = "AC1032";
drawing.layers.push_back(DxfLayer{"0", 7});
drawing.layers.push_back(DxfLayer{"GEOM", 3});
DxfBlock block;
block.name = "BLK";
DxfLine block_line;
block_line.start = Point3D(0.0, 0.0, 0.0);
block_line.end = Point3D(1.0, 0.0, 0.0);
block.entities.push_back(block_line);
drawing.blocks.push_back(block);
DxfLine line;
line.layer = "GEOM";
line.start = Point3D(0.0, 0.0, 0.0);
line.end = Point3D(10.0, 0.0, 0.0);
drawing.entities.push_back(line);
DxfCircle circle;
circle.center = Point3D(1.0, 2.0, 0.0);
circle.radius = 3.0;
drawing.entities.push_back(circle);
DxfArc arc;
arc.radius = 2.0;
arc.start_angle = 30.0; // 度 → 弧度
arc.end_angle = 120.0;
drawing.entities.push_back(arc);
DxfPoint point_entity;
point_entity.position = Point3D(4.0, 5.0, 6.0);
drawing.entities.push_back(point_entity);
DxfText text;
text.text_value = "HELLO";
text.insertion_point = Point3D(1.0, 1.0, 0.0);
text.height = 2.5;
text.rotation = 90.0;
drawing.entities.push_back(text);
DxfText mtext;
mtext.text_value = "MTEXT";
mtext.insertion_point = Point3D(2.0, 3.0, 0.0);
mtext.height = 1.5;
mtext.is_mtext = true;
drawing.entities.push_back(mtext);
DxfLwPolyline lw;
lw.vertices.push_back(DxfLwPolyline::Vertex{0.0, 0.0, 0.0});
lw.vertices.push_back(DxfLwPolyline::Vertex{4.0, 0.0, 0.5});
lw.vertices.push_back(DxfLwPolyline::Vertex{4.0, 3.0, 0.0});
lw.closed = true;
drawing.entities.push_back(lw);
DxfPolyline poly2d;
poly2d.vertices.push_back(DxfPolyline::Vertex{Point3D(0.0, 1.0, 0.0)});
poly2d.vertices.push_back(DxfPolyline::Vertex{Point3D(2.0, 1.0, 0.0)});
poly2d.vertices.push_back(DxfPolyline::Vertex{Point3D(2.0, 4.0, 0.0)});
drawing.entities.push_back(poly2d);
DxfPolyline poly3d;
poly3d.is_3d = true;
poly3d.vertices.push_back(DxfPolyline::Vertex{Point3D(0.0, 0.0, 0.0)});
poly3d.vertices.push_back(DxfPolyline::Vertex{Point3D(1.0, 1.0, 1.0)});
poly3d.vertices.push_back(DxfPolyline::Vertex{Point3D(2.0, 2.0, 4.0)});
drawing.entities.push_back(poly3d);
DxfInsert insert;
insert.block_name = "BLK";
insert.insertion_point = Point3D(5.0, 5.0, 0.0);
insert.scale_x = 2.0;
insert.scale_y = 2.0;
insert.scale_z = 2.0;
insert.rotation = 30.0;
drawing.entities.push_back(insert);
DxfEllipse ellipse;
ellipse.center = Point3D(3.0, 3.0, 0.0);
ellipse.major_axis_endpoint = Point3D(2.0, 0.0, 0.0);
ellipse.axis_ratio = 0.5;
drawing.entities.push_back(ellipse);
DxfSpline spline;
spline.degree = 2;
spline.control_points = {Point3D(0.0, 0.0, 0.0), Point3D(1.0, 2.0, 0.0),
Point3D(2.0, 0.0, 0.0)};
spline.knots = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
drawing.entities.push_back(spline);
Dxf3DFace face;
face.corners[0] = Point3D(0.0, 0.0, 0.0);
face.corners[1] = Point3D(3.0, 0.0, 0.0);
face.corners[2] = Point3D(0.0, 3.0, 0.0);
face.corners[3] = face.corners[2]; // 三角面
drawing.entities.push_back(face);
DxfHatch hatch;
hatch.is_solid = true;
hatch.pattern_angle = 45.0;
DxfHatch::BoundaryLoop loop;
loop.points = {Point3D(0.0, 0.0, 0.0), Point3D(2.0, 0.0, 0.0),
Point3D(2.0, 2.0, 0.0), Point3D(0.0, 2.0, 0.0)};
hatch.boundary_loops.push_back(loop);
drawing.entities.push_back(hatch);
// 非 ASCII:写入器只支持 ASCII → 跳过并计数
DxfText chinese;
chinese.text_value = "中文";
chinese.insertion_point = Point3D(0.0, 0.0, 0.0);
chinese.height = 1.0;
drawing.entities.push_back(chinese);
return drawing;
}
/// 统计模型空间指定类型的实体数
std::size_t count_type(const DwgObjectDocument& doc, DwgObjectType type) {
return model_entities(doc, type).size();
}
} // namespace
TEST(DwgWriter, DxfDrawingWritesAllKinds) {
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_from_dxf.dwg";
vde::foundation::DwgDrawingWriteReport report;
ASSERT_TRUE(vde::foundation::export_dwg_drawing(
path, make_all_kinds_drawing(), {}, &report))
<< report.error;
// 14 条输出(12 类实体一对一 + 3DFACE 与 3D 多段线各 1);中文被跳过
EXPECT_EQ(report.entities_written, 14);
EXPECT_EQ(report.entities_skipped, 1);
EXPECT_EQ(report.non_ascii_text_skipped, 1);
EXPECT_EQ(report.block_entities_written, 1);
EXPECT_EQ(report.blocks_written, 1u);
const DwgObjectDocument doc = decode_dwg_objects_file(path);
if (std::getenv("VDE_KEEP_DWG") == nullptr) std::remove(path.c_str());
ASSERT_TRUE(doc.ok) << doc.error;
expect_all_consistent(doc);
EXPECT_EQ(count_type(doc, DwgObjectType::Line), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Circle), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Point), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Text), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::MText), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::LwPolyline), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Polyline2D), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Polyline3D), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Face3D), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Insert), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Ellipse), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Spline), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Hatch), 1u);
// 3DFACE 保持为面(不再拆线);3D 多段线保持为 POLYLINE_3D
const auto faces = model_entities(doc, DwgObjectType::Face3D);
const auto& face = std::get<DwgFace3D>(faces[0]->data);
expect_near(face.corner[0].x, 0.0);
expect_near(face.corner[1].x, 3.0);
expect_near(face.corner[2].y, 3.0);
expect_near(face.corner[3].y, face.corner[2].y); // 三角面
const auto poly3d_objects = model_entities(doc, DwgObjectType::Polyline3D);
const auto& poly3d = std::get<DwgPolyline3D>(poly3d_objects[0]->data);
EXPECT_NE(poly3d.seqend, 0u);
std::vector<const DwgObject*> poly3d_vertices;
for (const auto& object : doc.objects) {
if (object.type != DwgObjectType::Vertex3D) continue;
if (object.common.owner != poly3d_objects[0]->handle) continue;
poly3d_vertices.push_back(&object);
}
ASSERT_EQ(poly3d_vertices.size(), 3u);
const auto& vertex0 = std::get<DwgVertex3D>(poly3d_vertices[0]->data);
const auto& vertex2 = std::get<DwgVertex3D>(poly3d_vertices[2]->data);
expect_near(vertex0.location.z, 0.0);
expect_near(vertex2.location.z, 4.0);
// 角度:DXF 度 → DWG 弧度
const auto arcs = model_entities(doc, DwgObjectType::Arc);
ASSERT_EQ(arcs.size(), 1u);
const auto& arc = std::get<DwgArc>(arcs[0]->data);
expect_near(arc.start_angle, 0.5235987755982988); // 30°
expect_near(arc.end_angle, 2.0943951023931953); // 120°
const auto lines = model_entities(doc, DwgObjectType::Line);
bool found_geom = false;
for (const auto* object : lines) {
if (object->common.layer != 0) found_geom = true;
}
EXPECT_TRUE(found_geom);
// 块定义与引用
const auto inserts = model_entities(doc, DwgObjectType::Insert);
const auto& insert = std::get<DwgInsert>(inserts[0]->data);
const DwgObject* block_header = doc.find_object(insert.block_header);
ASSERT_NE(block_header, nullptr);
EXPECT_EQ(std::get<DwgBlockHeader>(block_header->data).name, "BLK");
expect_near(insert.rotation, 0.5235987755982988); // 30°
expect_near(insert.x_scale, 2.0);
}
TEST(DwgWriter, DxfDrawingWritesAc1032) {
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_from_dxf_2018.dwg";
vde::foundation::DwgDrawingWriteReport report;
vde::foundation::DwgDrawingWriteOptions options;
options.version = DwgVersion::R2018;
ASSERT_TRUE(vde::foundation::export_dwg_drawing(
path, make_all_kinds_drawing(), options, &report))
<< report.error;
const DwgObjectDocument doc = decode_dwg_objects_file(path);
if (std::getenv("VDE_KEEP_DWG") == nullptr) std::remove(path.c_str());
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R2018);
expect_all_consistent(doc);
EXPECT_EQ(count_type(doc, DwgObjectType::Line), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Polyline3D), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Face3D), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Hatch), 1u);
// R2018 字符串流按 UTF-16 编码:中文文本不再跳过(R2000 目标仍计入跳过)
EXPECT_EQ(report.entities_written, 15);
EXPECT_EQ(report.entities_skipped, 0);
EXPECT_EQ(report.non_ascii_text_skipped, 0);
bool chinese_found = false;
for (const auto* object : model_entities(doc, DwgObjectType::Text)) {
if (std::get<DwgText>(object->data).value == "中文") {
chinese_found = true;
}
}
EXPECT_TRUE(chinese_found);
}
TEST(DwgWriter, DxfDrawingWritesR12) {
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_from_dxf_r12.dwg";
vde::foundation::DwgDrawingWriteReport report;
vde::foundation::DwgDrawingWriteOptions options;
options.version = DwgVersion::R12;
ASSERT_TRUE(vde::foundation::export_dwg_drawing(
path, make_all_kinds_drawing(), options, &report))
<< report.error;
// 15 条模型实体:R12 无 MTEXT/ELLIPSE/SPLINE/HATCH,中文非 ASCII 也跳过
EXPECT_EQ(report.entities_written, 10);
EXPECT_EQ(report.entities_skipped, 5);
EXPECT_EQ(report.non_ascii_text_skipped, 1);
EXPECT_EQ(report.block_entities_written, 1);
bool has_r12_warning = false;
for (const auto& warning : report.warnings) {
if (warning.find("LWPOLYLINE") != std::string::npos) {
has_r12_warning = true;
}
}
EXPECT_TRUE(has_r12_warning);
const DwgObjectDocument doc = decode_dwg_objects_file(path);
if (std::getenv("VDE_KEEP_DWG") == nullptr) std::remove(path.c_str());
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R12);
expect_all_consistent(doc);
EXPECT_EQ(count_type(doc, DwgObjectType::Line), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Circle), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Arc), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Point), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Text), 1u);
// LWPOLYLINE 在 R12 中转写为 POLYLINE_2D
EXPECT_EQ(count_type(doc, DwgObjectType::Polyline2D), 2u);
EXPECT_EQ(count_type(doc, DwgObjectType::Polyline3D), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Face3D), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::Insert), 1u);
EXPECT_EQ(count_type(doc, DwgObjectType::MText), 0u);
EXPECT_EQ(count_type(doc, DwgObjectType::Ellipse), 0u);
EXPECT_EQ(count_type(doc, DwgObjectType::Spline), 0u);
EXPECT_EQ(count_type(doc, DwgObjectType::Hatch), 0u);
}
TEST(DwgWriter, DxfSolidMapsToHatchAndR12Face) {
vde::foundation::DxfDocument drawing;
DxfSolid solid;
solid.layer = "0";
solid.corners[0] = Point3D(0.0, 0.0, 0.0);
solid.corners[1] = Point3D(10.0, 0.0, 0.0);
solid.corners[2] = Point3D(0.0, 10.0, 0.0);
solid.corners[3] = Point3D(10.0, 10.0, 0.0);
drawing.entities.push_back(solid);
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_from_dxf_solid.dwg";
vde::foundation::DwgDrawingWriteReport report;
ASSERT_TRUE(vde::foundation::export_dwg_drawing(
path, drawing, {}, &report)) << report.error;
{
const DwgObjectDocument doc = decode_dwg_objects_file(path);
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(count_type(doc, DwgObjectType::Hatch), 1u);
const auto hatches = model_entities(doc, DwgObjectType::Hatch);
const auto& hatch = std::get<vde::foundation::DwgHatch>(hatches[0]->data);
EXPECT_TRUE(hatch.solid);
ASSERT_EQ(hatch.paths.size(), 1u);
ASSERT_EQ(hatch.paths[0].edges.size(), 1u);
EXPECT_EQ(hatch.paths[0].edges[0].vertices.size(), 4u);
}
vde::foundation::DwgDrawingWriteOptions r12;
r12.version = DwgVersion::R12;
ASSERT_TRUE(vde::foundation::export_dwg_drawing(
path, drawing, r12, &report)) << report.error;
{
const DwgObjectDocument doc = decode_dwg_objects_file(path);
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(count_type(doc, DwgObjectType::Face3D), 1u);
const auto faces = model_entities(doc, DwgObjectType::Face3D);
const auto& face = std::get<DwgFace3D>(faces[0]->data);
expect_near(face.corner[2].y, 10.0);
expect_near(face.corner[3].x, 10.0);
}
if (std::getenv("VDE_KEEP_DWG") == nullptr) std::remove(path.c_str());
}
TEST(DwgWriter, DxfDrawingRejectsUnsupportedInput) {
vde::foundation::DwgDrawingWriteReport report;
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_from_dxf_bad.dwg";
// 空文档
EXPECT_FALSE(vde::foundation::export_dwg_drawing(path, {}, {}, &report));
EXPECT_FALSE(report.ok);
EXPECT_FALSE(report.error.empty());
// 不支持的版本(R14 无原生写入器)
vde::foundation::DwgDrawingWriteOptions options;
options.version = DwgVersion::R14;
EXPECT_FALSE(vde::foundation::export_dwg_drawing(
path, make_all_kinds_drawing(), options, &report));
EXPECT_FALSE(report.ok);
EXPECT_NE(report.error.find("R12"), std::string::npos);
}
// ===================================================================
// FMT-13:R12(AC1009)合成 fixture —— 不依赖 tests/data/dwg_r12
// ===================================================================
namespace {
std::string r12_synth_path(const char* name) {
return std::string(VDE_TEST_TEMP_DIR) + "/" + name;
}
/// 用公共 DwgWriter 生成含 LINE/CIRCLE/ARC/TEXT + 两个图层的 R12 文档。
bool write_synthetic_r12_fixture(const std::string& path, std::string* error = nullptr) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
document.layers.push_back(DwgWriteLayer{"R12_LAYER", 3});
DwgWriteEntity line;
line.kind = DwgWriteEntityKind::Line;
line.layer = "R12_LAYER";
line.start = DwgWritePoint{1.0, 2.0, 0.0};
line.end = DwgWritePoint{3.0, 4.0, 5.0};
document.entities.push_back(line);
DwgWriteEntity circle;
circle.kind = DwgWriteEntityKind::Circle;
circle.layer = "R12_LAYER";
circle.center = DwgWritePoint{10.0, 20.0, 4.0};
circle.radius = 2.5;
document.entities.push_back(circle);
DwgWriteEntity arc;
arc.kind = DwgWriteEntityKind::Arc;
arc.layer = "0";
arc.center = DwgWritePoint{-1.0, 0.5, 1.25};
arc.radius = 2.25;
arc.start_angle = 0.5;
arc.end_angle = 3.0;
document.entities.push_back(arc);
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.layer = "0";
text.insertion = DwgWritePoint{5.0, 6.0, 0.5};
text.height = 2.5;
text.rotation = 0.75;
text.text = "R12 SYNTH";
document.entities.push_back(text);
std::string write_error;
const bool ok = vde::foundation::dwg_write_ac1009_file(path, document, &write_error);
if (error) *error = write_error;
return ok;
}
std::string read_binary_file(const std::string& path) {
std::ifstream in(path, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
}
} // namespace
TEST(DwgWriter, R12SyntheticWriteReadRoundtrip) {
const std::string path = r12_synth_path("dwg_writer_synthetic_r12.dwg");
std::string write_error;
ASSERT_TRUE(write_synthetic_r12_fixture(path, &write_error)) << write_error;
const std::string bytes = read_binary_file(path);
ASSERT_GE(bytes.size(), 6u);
EXPECT_EQ(bytes.substr(0, 6), "AC1009");
const DwgObjectDocument doc =
decode_dwg_objects(reinterpret_cast<const std::uint8_t*>(bytes.data()),
bytes.size());
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.version, DwgVersion::R12);
EXPECT_TRUE(doc.error.empty());
// 图层表:0 + R12_LAYER;颜色索引与句柄可解析
const auto layers = r12_model_entities(doc, DwgObjectType::Layer);
ASSERT_EQ(layers.size(), 2u);
std::map<std::string, std::uint16_t> layer_colors;
for (const auto* layer : layers) {
const auto& data = std::get<vde::foundation::DwgLayer>(layer->data);
layer_colors[data.name] = data.color_index;
}
ASSERT_NE(layer_colors.find("R12_LAYER"), layer_colors.end());
EXPECT_EQ(layer_colors.at("R12_LAYER"), 3u);
// LINE
const auto lines = r12_model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(lines.size(), 1u);
{
const auto& data = std::get<DwgLine>(lines[0]->data);
expect_near(data.start.x, 1.0);
expect_near(data.start.y, 2.0);
expect_near(data.start.z, 0.0);
expect_near(data.end.x, 3.0);
expect_near(data.end.y, 4.0);
expect_near(data.end.z, 5.0);
const DwgObject* layer = doc.find_object(lines[0]->common.layer);
ASSERT_NE(layer, nullptr);
EXPECT_EQ(std::get<vde::foundation::DwgLayer>(layer->data).name, "R12_LAYER");
}
// CIRCLE / ARC / TEXT
const auto circles = r12_model_entities(doc, DwgObjectType::Circle);
ASSERT_EQ(circles.size(), 1u);
{
const auto& data = std::get<DwgCircle>(circles[0]->data);
expect_near(data.center.x, 10.0);
expect_near(data.center.y, 20.0);
expect_near(data.center.z, 4.0);
expect_near(data.radius, 2.5);
}
const auto arcs = r12_model_entities(doc, DwgObjectType::Arc);
ASSERT_EQ(arcs.size(), 1u);
{
const auto& data = std::get<DwgArc>(arcs[0]->data);
expect_near(data.center.x, -1.0);
expect_near(data.center.y, 0.5);
expect_near(data.center.z, 1.25);
expect_near(data.radius, 2.25);
expect_near(data.start_angle, 0.5);
expect_near(data.end_angle, 3.0);
}
const auto texts = r12_model_entities(doc, DwgObjectType::Text);
ASSERT_EQ(texts.size(), 1u);
{
const auto& data = std::get<DwgText>(texts[0]->data);
EXPECT_EQ(data.value, "R12 SYNTH");
expect_near(data.insertion.x, 5.0);
expect_near(data.insertion.y, 6.0);
expect_near(data.height, 2.5);
expect_near(data.rotation, 0.75);
}
std::remove(path.c_str());
}
TEST(DwgWriter, R12SyntheticImportsThroughNativePath) {
const std::string path = r12_synth_path("dwg_writer_synthetic_r12_import.dwg");
ASSERT_TRUE(write_synthetic_r12_fixture(path));
const vde::foundation::DwgImportResult imported =
vde::foundation::import_dwg(path);
if (!imported.success) {
for (const auto& message : imported.messages)
std::cout << " ! " << message << "\n";
}
ASSERT_TRUE(imported.success) << "原生 R12 导入应成功";
ASSERT_TRUE(imported.has_drawing);
EXPECT_EQ(imported.dwg_version, DwgVersion::R12);
EXPECT_EQ(imported.dwg_version_name, "R12");
EXPECT_FALSE(imported.converter_used);
const auto& drawing = imported.drawing;
EXPECT_EQ(drawing.entities.size(), 4u);
EXPECT_EQ(drawing.entities_skipped, 0);
std::size_t lines = 0, circles = 0, arcs = 0, texts = 0;
std::set<std::string> entity_layers;
for (const auto& entity : drawing.entities) {
if (std::get_if<vde::foundation::DxfLine>(&entity)) {
++lines;
const auto& line = std::get<vde::foundation::DxfLine>(entity);
expect_near(line.start.x(), 1.0);
expect_near(line.end.z(), 5.0);
entity_layers.insert(line.layer);
} else if (std::get_if<vde::foundation::DxfCircle>(&entity)) {
++circles;
const auto& circle = std::get<vde::foundation::DxfCircle>(entity);
expect_near(circle.radius, 2.5);
entity_layers.insert(circle.layer);
} else if (std::get_if<vde::foundation::DxfArc>(&entity)) {
++arcs;
entity_layers.insert(std::get<vde::foundation::DxfArc>(entity).layer);
} else if (std::get_if<vde::foundation::DxfText>(&entity)) {
++texts;
const auto& text = std::get<vde::foundation::DxfText>(entity);
EXPECT_EQ(text.text_value, "R12 SYNTH");
entity_layers.insert(text.layer);
}
}
EXPECT_EQ(lines, 1u);
EXPECT_EQ(circles, 1u);
EXPECT_EQ(arcs, 1u);
EXPECT_EQ(texts, 1u);
EXPECT_NE(entity_layers.count("R12_LAYER"), 0u);
EXPECT_NE(entity_layers.count("0"), 0u);
// 图层表 + 无重复
std::set<std::string> layer_names;
for (const auto& layer : drawing.layers) layer_names.insert(layer.name);
EXPECT_NE(layer_names.count("R12_LAYER"), 0u);
EXPECT_EQ(drawing.entities.size(), lines + circles + arcs + texts);
std::remove(path.c_str());
}
TEST(DwgWriter, R12SyntheticEmptyAndTruncatedDiagnostics) {
// 空 R12:writer 可写;导入端必须诚实(成功但无图元,或失败且带诊断)。
const std::string empty_path = r12_synth_path("dwg_writer_synthetic_r12_empty.dwg");
DwgWriteDocument empty;
std::string write_error;
ASSERT_TRUE(vde::foundation::dwg_write_ac1009_file(empty_path, empty, &write_error))
<< write_error;
const auto empty_import = vde::foundation::import_dwg(empty_path);
if (!empty_import.success) {
EXPECT_FALSE(empty_import.messages.empty());
} else {
EXPECT_FALSE(empty_import.has_drawing && !empty_import.drawing.entities.empty());
}
std::remove(empty_path.c_str());
// 截断 R12:读取/导入都不得崩溃,必须有明确诊断。
const std::string path = r12_synth_path("dwg_writer_synthetic_r12_truncated.dwg");
ASSERT_TRUE(write_synthetic_r12_fixture(path));
std::string bytes = read_binary_file(path);
ASSERT_GT(bytes.size(), 64u);
bytes.resize(48);
const std::string truncated_path =
r12_synth_path("dwg_writer_synthetic_r12_truncated_48.dwg");
{
std::ofstream out(truncated_path, std::ios::binary | std::ios::trunc);
out.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
}
const DwgObjectDocument decoded =
decode_dwg_objects(reinterpret_cast<const std::uint8_t*>(bytes.data()),
bytes.size());
EXPECT_FALSE(decoded.ok);
EXPECT_FALSE(decoded.error.empty());
const auto imported = vde::foundation::import_dwg(truncated_path);
EXPECT_FALSE(imported.success);
EXPECT_FALSE(imported.messages.empty());
std::remove(path.c_str());
std::remove(truncated_path.c_str());
}
// ═══════════════════════════════════════════════════════════
// FMT-14:DWG 原生 ACIS 载荷 → B-Rep(writer → import 回读)
// ═══════════════════════════════════════════════════════════
namespace {
std::string read_text_file(const std::string& path) {
std::ifstream in(path, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
}
} // namespace
TEST(DwgWriter, ImportAcisPayloadFromWriterRoundTrip) {
const auto box = vde::brep::make_box(2.0, 3.0, 4.0);
const std::string sat_path =
std::string(VDE_TEST_TEMP_DIR) + "/fmt14_box_acis.sat";
ASSERT_TRUE(vde::io::export_acis_sat(box, sat_path));
const std::string sat = read_text_file(sat_path);
std::remove(sat_path.c_str());
ASSERT_FALSE(sat.empty());
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
DwgWriteEntity solid;
solid.kind = DwgWriteEntityKind::Solid3D;
solid.acis_data = sat;
document.entities.push_back(solid);
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/fmt14_acis_roundtrip.dwg";
std::string error;
ASSERT_TRUE(vde::foundation::dwg_write_ac1015_file(path, document, &error))
<< error;
const vde::foundation::DwgImportResult imported =
vde::foundation::import_dwg(path);
std::remove(path.c_str());
std::string messages;
for (const auto& m : imported.messages) messages += m + "\n";
ASSERT_TRUE(imported.success) << messages;
ASSERT_EQ(imported.bodies.size(), 1u) << messages;
EXPECT_GT(imported.bodies[0].num_faces(), 0u) << messages;
EXPECT_GT(imported.bodies[0].num_vertices(), 0u) << messages;
EXPECT_NE(messages.find("ACIS embedding parsed"), std::string::npos)
<< messages;
}
TEST(DwgWriter, ImportForeignAcisPayloadDegradesWithoutCrash) {
// 无 VDE 标记的第三方 ACIS 文本:不得伪装成 B-Rep,也不得崩溃。
const std::string foreign =
"700 0 1 0\n"
"# foreign ACIS (no VDE payload)\n"
"-0 body $-1 -1 $-1 $1 $-1 $-1 #\n";
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
DwgWriteEntity solid;
solid.kind = DwgWriteEntityKind::Solid3D;
solid.acis_data = foreign;
document.entities.push_back(solid);
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/fmt14_foreign_acis.dwg";
std::string error;
ASSERT_TRUE(vde::foundation::dwg_write_ac1015_file(path, document, &error))
<< error;
const vde::foundation::DwgImportResult imported =
vde::foundation::import_dwg(path);
std::remove(path.c_str());
EXPECT_TRUE(imported.bodies.empty());
std::string messages;
for (const auto& m : imported.messages) messages += m + "\n";
EXPECT_NE(messages.find("ACIS"), std::string::npos) << messages;
EXPECT_FALSE(imported.success) << messages;
}
// ═══════════════════════════════════════════════════════════
// FMT-15:AcDs 丢弃计数(R2018)vs R2000 正常写出
// ═══════════════════════════════════════════════════════════
TEST(DwgWriter, AcdsAcisDropCountR2018VsR2000) {
const std::string sat =
"700 0 1 0\n"
"# VDE raw ACIS payload for drop-count test\n"
"-0 body $-1 -1 $-1 $1 $-1 $-1 #\n";
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
const DwgWriteEntityKind kinds[2] = {DwgWriteEntityKind::Solid3D,
DwgWriteEntityKind::Region};
for (const auto kind : kinds) {
DwgWriteEntity entity;
entity.kind = kind;
entity.acis_data = sat;
document.entities.push_back(entity);
}
const auto r2018 = vde::foundation::dwg_write_ac1032(document);
ASSERT_TRUE(r2018.ok) << r2018.error;
EXPECT_EQ(r2018.acis_payloads_dropped, 2u);
EXPECT_EQ(r2018.warnings.size(), 2u);
const auto r2000 = vde::foundation::dwg_write_ac1015(document);
ASSERT_TRUE(r2000.ok) << r2000.error;
EXPECT_EQ(r2000.acis_payloads_dropped, 0u);
EXPECT_TRUE(r2000.warnings.empty());
}
TEST(DwgWriter, ImportWriterR2018ReportsNoAcDsData) {
// writer 未实现 AcDs 数据流,R2018 输出对象不应被误报为 has_ds_data;
// 该测试守住 import 侧计数字段与“不虚报 AcDs”的默认值。
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"0", 7});
DwgWriteEntity solid;
solid.kind = DwgWriteEntityKind::Solid3D;
solid.acis_data =
"700 0 1 0\n# VDE raw ACIS payload\n"
"-0 body $-1 -1 $-1 $1 $-1 $-1 #\n";
document.entities.push_back(solid);
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/fmt15_writer_r2018_acds.dwg";
std::string error;
ASSERT_TRUE(vde::foundation::dwg_write_ac1032_file(path, document, &error))
<< error;
const vde::foundation::DwgImportResult imported =
vde::foundation::import_dwg(path);
std::remove(path.c_str());
EXPECT_EQ(imported.acds_objects, 0u);
EXPECT_EQ(imported.acds_bytes, 0u);
for (const auto& message : imported.messages) {
EXPECT_EQ(message.find("AcDs data stream present"), std::string::npos)
<< message;
}
}
// ═══════════════════════════════════════════════════════════
// Wave21 A54:UTF-8(中文/非 ASCII)路径写盘 + 读回
// ═══════════════════════════════════════════════════════════
TEST(DwgWriter, Utf8PathFileRoundtrip) {
DwgWriteDocument document;
document.layers.push_back(DwgWriteLayer{"EDGE", 2});
document.entities.push_back(
line(point(-1.0, -2.0, 0.0), point(3.0, 4.0, 5.0), "EDGE"));
const DwgWriteResult memory = vde::foundation::dwg_write_ac1015(document);
ASSERT_TRUE(memory.ok) << memory.error;
const std::string path =
std::string(VDE_TEST_TEMP_DIR) + "/dwg_writer_模型_①.dwg";
std::error_code ec;
std::filesystem::remove(vde::foundation::path_from_utf8(path), ec);
std::string error;
ASSERT_TRUE(vde::foundation::dwg_write_ac1015_file(path, document, &error))
<< error;
EXPECT_TRUE(std::filesystem::exists(vde::foundation::path_from_utf8(path)));
const DwgObjectDocument doc = decode_dwg_objects_file(path);
std::filesystem::remove(vde::foundation::path_from_utf8(path), ec);
ASSERT_TRUE(doc.ok) << doc.error;
EXPECT_EQ(doc.file_size, memory.bytes.size());
expect_all_consistent(doc);
const auto model_lines = model_entities(doc, DwgObjectType::Line);
ASSERT_EQ(model_lines.size(), 1u);
const auto& data = std::get<DwgLine>(model_lines[0]->data);
expect_near(data.start.z, 0.0);
expect_near(data.end.z, 5.0);
}