651 lines
26 KiB
C++
651 lines
26 KiB
C++
// 官方 DXF 格式插件(VDE 微内核 P2-3;纯 C ABI,不链接内核)。
|
||
//
|
||
// 目标:以插件形式读取 DXF 中的 3D/2D 面片实体,经宿主 body_from_mesh 创建实体。
|
||
//
|
||
// 支持子集:
|
||
// - 3DFACE(3/4 顶点 → 三角/四边形,四边形拆 2 三角形)—— 立方体线框面片夹具;
|
||
// - LWPOLYLINE(闭合多边形,扇形三角化,Z 取 38 高程);
|
||
// - POLYLINE + VERTEX(闭合多边形,扇形三角化);
|
||
// - CIRCLE(按 32 边形近似,用于面片边界)。
|
||
// 不支持(已知限制):LINE/ARC/SPLINE/INSERT 等不形成面片;多段线凸度(bulge)忽略;
|
||
// 3D 实体(3DSOLID/REGION)不解析;图层/颜色等属性忽略。
|
||
#include <cctype>
|
||
#include <cmath>
|
||
#include <charconv>
|
||
#include <cstdint>
|
||
#include <cstdlib>
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "../common/format_plugin_common.h"
|
||
|
||
namespace {
|
||
|
||
using vde_plugin_common::MeshAccumulator;
|
||
using vde_plugin_common::fail;
|
||
using vde_plugin_common::read_file_bytes;
|
||
using vde_plugin_common::validate_read_host;
|
||
using vde_plugin_common::validate_write_host;
|
||
using vde_plugin_common::write_file_bytes;
|
||
|
||
constexpr std::size_t kMaxTriangles = 50000000u;
|
||
// 写出上限:每三角一个 3DFACE,20M 三角对应约 1.2GB 文本,防御性上限。
|
||
constexpr std::size_t kMaxWriteTriangles = 20000000u;
|
||
constexpr double kWriteDeflection = 0.01;
|
||
constexpr double kPi = 3.14159265358979323846;
|
||
|
||
struct DxfPair {
|
||
int code = 0;
|
||
std::string value;
|
||
};
|
||
|
||
std::string trim(const std::string& text) {
|
||
std::size_t begin = 0, end = text.size();
|
||
while (begin < end && std::isspace(static_cast<unsigned char>(text[begin]))) ++begin;
|
||
while (end > begin && std::isspace(static_cast<unsigned char>(text[end - 1]))) --end;
|
||
return text.substr(begin, end - begin);
|
||
}
|
||
|
||
bool split_pairs(const std::string& text, std::vector<DxfPair>* pairs) {
|
||
if (!pairs) return false;
|
||
pairs->clear();
|
||
std::size_t pos = 0;
|
||
std::vector<std::string> lines;
|
||
while (pos <= text.size()) {
|
||
std::size_t line_end = text.find('\n', pos);
|
||
if (line_end == std::string::npos) line_end = text.size();
|
||
lines.push_back(trim(text.substr(pos, line_end - pos)));
|
||
if (line_end == text.size()) break;
|
||
pos = line_end + 1;
|
||
}
|
||
if (lines.size() < 2) return false;
|
||
for (std::size_t i = 0; i + 1 < lines.size(); i += 2) {
|
||
char* end = nullptr;
|
||
const long code = std::strtol(lines[i].c_str(), &end, 10);
|
||
if (end == lines[i].c_str()) continue;
|
||
DxfPair pair;
|
||
pair.code = static_cast<int>(code);
|
||
pair.value = lines[i + 1];
|
||
pairs->push_back(std::move(pair));
|
||
}
|
||
return !pairs->empty();
|
||
}
|
||
|
||
double to_double(const std::string& text, double fallback = 0.0) {
|
||
char* end = nullptr;
|
||
const double value = std::strtod(text.c_str(), &end);
|
||
if (end == text.c_str()) return fallback;
|
||
return value;
|
||
}
|
||
|
||
int to_int(const std::string& text, int fallback = 0) {
|
||
char* end = nullptr;
|
||
const long value = std::strtol(text.c_str(), &end, 10);
|
||
if (end == text.c_str()) return fallback;
|
||
return static_cast<int>(value);
|
||
}
|
||
|
||
struct DxfEntity {
|
||
std::string type;
|
||
std::vector<DxfPair> fields;
|
||
};
|
||
|
||
std::vector<DxfEntity> extract_entities(const std::vector<DxfPair>& pairs) {
|
||
std::vector<DxfEntity> entities;
|
||
bool in_entities_section = false;
|
||
bool in_entity = false;
|
||
for (std::size_t i = 0; i < pairs.size(); ++i) {
|
||
const DxfPair& pair = pairs[i];
|
||
if (pair.code == 0) {
|
||
const std::string value = trim(pair.value);
|
||
if (value == "SECTION") {
|
||
// 下一个 2 组码给出 section 名称
|
||
if (i + 1 < pairs.size() && pairs[i + 1].code == 2)
|
||
in_entities_section = trim(pairs[i + 1].value) == "ENTITIES";
|
||
in_entity = false;
|
||
continue;
|
||
}
|
||
if (value == "ENDSEC") {
|
||
in_entities_section = false;
|
||
in_entity = false;
|
||
continue;
|
||
}
|
||
if (in_entities_section) {
|
||
DxfEntity entity;
|
||
entity.type = value;
|
||
entities.push_back(std::move(entity));
|
||
in_entity = true;
|
||
continue;
|
||
}
|
||
}
|
||
if (in_entities_section && in_entity && !entities.empty())
|
||
entities.back().fields.push_back(pair);
|
||
}
|
||
return entities;
|
||
}
|
||
|
||
/** 读取实体字段:返回指定组码的最后一个值。 */
|
||
bool field_value(const DxfEntity& entity, int code, std::string* out) {
|
||
bool found = false;
|
||
for (const auto& field : entity.fields) {
|
||
if (field.code == code) {
|
||
*out = field.value;
|
||
found = true;
|
||
}
|
||
}
|
||
return found;
|
||
}
|
||
|
||
double field_double(const DxfEntity& entity, int code, double fallback = 0.0) {
|
||
std::string value;
|
||
if (!field_value(entity, code, &value)) return fallback;
|
||
return to_double(value, fallback);
|
||
}
|
||
|
||
void add_triangle(MeshAccumulator& mesh, const double p0[3], const double p1[3],
|
||
const double p2[3]) {
|
||
const uint32_t a = mesh.add_vertex(p0[0], p0[1], p0[2]);
|
||
const uint32_t b = mesh.add_vertex(p1[0], p1[1], p1[2]);
|
||
const uint32_t c = mesh.add_vertex(p2[0], p2[1], p2[2]);
|
||
mesh.add_triangle(a, b, c);
|
||
}
|
||
|
||
void add_polygon(MeshAccumulator& mesh, const std::vector<double>& xy, double z) {
|
||
const std::size_t n = xy.size() / 2;
|
||
if (n < 3) return;
|
||
std::vector<uint32_t> indices;
|
||
indices.reserve(n);
|
||
for (std::size_t i = 0; i < n; ++i)
|
||
indices.push_back(mesh.add_vertex(xy[2 * i], xy[2 * i + 1], z));
|
||
for (std::size_t i = 2; i < indices.size(); ++i) {
|
||
mesh.add_triangle(indices[0], indices[i - 1], indices[i]);
|
||
if (mesh.triangle_count() > kMaxTriangles) return;
|
||
}
|
||
}
|
||
|
||
int dxf_read_file(const VdePluginHost* host, const char* path, VdeBody** bodies,
|
||
std::size_t* count) {
|
||
if (bodies) *bodies = nullptr;
|
||
if (count) *count = 0;
|
||
if (!bodies || !count) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "dxf: 输出指针为空");
|
||
const int host_rc = validate_read_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
std::string text;
|
||
if (!path || !read_file_bytes(path, &text))
|
||
return fail(host, VDE_PLUGIN_ERR_IO, "dxf: 无法读取输入文件");
|
||
std::vector<DxfPair> pairs;
|
||
if (!split_pairs(text, &pairs))
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE, "dxf: 无法解析组码对");
|
||
const std::vector<DxfEntity> entities = extract_entities(pairs);
|
||
if (entities.empty())
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE, "dxf: ENTITIES 段为空");
|
||
|
||
MeshAccumulator mesh;
|
||
std::size_t handled = 0;
|
||
for (std::size_t ei = 0; ei < entities.size(); ++ei) {
|
||
const DxfEntity& entity = entities[ei];
|
||
if (entity.type == "3DFACE") {
|
||
double p[4][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
|
||
for (int k = 0; k < 4; ++k) {
|
||
p[k][0] = field_double(entity, 10 + k);
|
||
p[k][1] = field_double(entity, 20 + k);
|
||
p[k][2] = field_double(entity, 30 + k);
|
||
}
|
||
add_triangle(mesh, p[0], p[1], p[2]);
|
||
const bool quad = !(p[3][0] == p[2][0] && p[3][1] == p[2][1] &&
|
||
p[3][2] == p[2][2]);
|
||
if (quad) add_triangle(mesh, p[0], p[2], p[3]);
|
||
++handled;
|
||
} else if (entity.type == "LWPOLYLINE") {
|
||
std::vector<double> xy;
|
||
for (const auto& field : entity.fields) {
|
||
if (field.code == 10) xy.push_back(to_double(field.value));
|
||
else if (field.code == 20) xy.push_back(to_double(field.value));
|
||
}
|
||
const double z = field_double(entity, 38, 0.0);
|
||
add_polygon(mesh, xy, z);
|
||
++handled;
|
||
} else if (entity.type == "POLYLINE") {
|
||
std::vector<double> xy;
|
||
double z = 0.0;
|
||
// 后续 VERTEX 子实体(直到 SEQEND)
|
||
for (std::size_t vi = ei + 1; vi < entities.size(); ++vi) {
|
||
if (entities[vi].type == "SEQEND") break;
|
||
if (entities[vi].type != "VERTEX") continue;
|
||
const double x = field_double(entities[vi], 10);
|
||
const double y = field_double(entities[vi], 20);
|
||
const double vz = field_double(entities[vi], 30);
|
||
xy.push_back(x);
|
||
xy.push_back(y);
|
||
z = vz;
|
||
}
|
||
add_polygon(mesh, xy, z);
|
||
++handled;
|
||
} else if (entity.type == "CIRCLE") {
|
||
const double cx = field_double(entity, 10);
|
||
const double cy = field_double(entity, 20);
|
||
const double cz = field_double(entity, 30);
|
||
const double r = field_double(entity, 40);
|
||
if (r > 0.0) {
|
||
const int segments = 32;
|
||
std::vector<double> xy;
|
||
for (int k = 0; k < segments; ++k) {
|
||
const double angle = 2.0 * kPi * k / segments;
|
||
xy.push_back(cx + r * std::cos(angle));
|
||
xy.push_back(cy + r * std::sin(angle));
|
||
}
|
||
add_polygon(mesh, xy, cz);
|
||
++handled;
|
||
}
|
||
}
|
||
if (mesh.triangle_count() > kMaxTriangles) break;
|
||
}
|
||
if (mesh.empty())
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE,
|
||
"dxf: 未找到可生成面片的实体(支持 3DFACE/LWPOLYLINE/POLYLINE/CIRCLE)");
|
||
(void)handled;
|
||
return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "dxf");
|
||
}
|
||
|
||
int dxf_read_memory(const VdePluginHost* host, const void* data, std::size_t size,
|
||
VdeBody** bodies, std::size_t* count) {
|
||
(void)host;
|
||
(void)data;
|
||
(void)size;
|
||
if (bodies) *bodies = nullptr;
|
||
if (count) *count = 0;
|
||
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "dxf: 仅支持 read_file(P2-3 最小实现)");
|
||
}
|
||
|
||
// ── 写出(P4-34 / P4-47 / P4-63):host body_tessellate → DXF R12 ASCII 3DFACE ──
|
||
// P4-63 起统一走 DxfWriteSink:write_file 直接按 1 MiB 块缓冲写盘(不再先构造完整
|
||
// string),write_memory sink 到 string(按三角数 reserve 预估);两路径字节一致。
|
||
constexpr std::size_t kMaxWriteBodies = 10000;
|
||
constexpr std::size_t kMaxLayerNameLength = 31; // R12 图层名上限
|
||
|
||
class DxfWriteSink {
|
||
public:
|
||
static constexpr std::size_t kBlockSize = 1u << 20; // 1 MiB
|
||
|
||
explicit DxfWriteSink(std::string* memory) : memory_(memory) {}
|
||
explicit DxfWriteSink(std::FILE* file) : file_(file) { block_.reserve(kBlockSize); }
|
||
|
||
bool write(const char* data, std::size_t size) {
|
||
if (failed_) return false;
|
||
if (memory_ != nullptr) {
|
||
memory_->append(data, size);
|
||
return true;
|
||
}
|
||
block_.append(data, size);
|
||
if (block_.size() >= kBlockSize) return flush();
|
||
return true;
|
||
}
|
||
bool write(const std::string& text) { return write(text.data(), text.size()); }
|
||
bool flush() {
|
||
if (failed_) return false;
|
||
if (block_.empty()) return true;
|
||
if (file_ == nullptr || std::fwrite(block_.data(), 1, block_.size(), file_) !=
|
||
block_.size()) {
|
||
failed_ = true;
|
||
return false;
|
||
}
|
||
block_.clear();
|
||
return true;
|
||
}
|
||
bool finish() { return flush() && !failed_; }
|
||
bool failed() const { return failed_; }
|
||
void reserve(std::size_t extra) {
|
||
if (memory_ != nullptr && extra > 0) memory_->reserve(memory_->size() + extra);
|
||
}
|
||
|
||
private:
|
||
std::string* memory_ = nullptr;
|
||
std::FILE* file_ = nullptr;
|
||
std::string block_;
|
||
bool failed_ = false;
|
||
};
|
||
|
||
struct LayerSpec {
|
||
std::string name;
|
||
int aci_color = 7;
|
||
};
|
||
|
||
void dxf_append_int(std::string* out, int value) {
|
||
char buffer[16];
|
||
const auto result = std::to_chars(buffer, buffer + sizeof(buffer), value);
|
||
out->append(buffer, static_cast<std::size_t>(result.ptr - buffer));
|
||
}
|
||
|
||
void append_pair(DxfWriteSink* out, int code, const std::string& value) {
|
||
char buffer[16];
|
||
const auto result = std::to_chars(buffer, buffer + sizeof(buffer), code);
|
||
const std::size_t length = static_cast<std::size_t>(result.ptr - buffer);
|
||
buffer[length] = '\n';
|
||
out->write(buffer, length + 1);
|
||
out->write(value);
|
||
out->write("\n", 1);
|
||
}
|
||
|
||
void append_pair_double(DxfWriteSink* out, int code, double value) {
|
||
char buffer[64];
|
||
std::snprintf(buffer, sizeof(buffer), "%.9g", value);
|
||
char code_buffer[16];
|
||
const auto result = std::to_chars(code_buffer, code_buffer + sizeof(code_buffer), code);
|
||
const std::size_t code_length = static_cast<std::size_t>(result.ptr - code_buffer);
|
||
code_buffer[code_length] = '\n';
|
||
out->write(code_buffer, code_length + 1);
|
||
out->write(buffer, std::strlen(buffer));
|
||
out->write("\n", 1);
|
||
}
|
||
|
||
/// 环境变量开关:0/false/off/no → true(大小写不敏感)。
|
||
bool env_disabled(const char* variable) {
|
||
const char* raw = std::getenv(variable);
|
||
if (raw == nullptr || *raw == '\0') return false;
|
||
std::string value(raw);
|
||
for (char& c : value) {
|
||
if (c >= 'A' && c <= 'Z') c = static_cast<char>(c - 'A' + 'a');
|
||
}
|
||
return value == "0" || value == "false" || value == "off" || value == "no";
|
||
}
|
||
|
||
std::string trim_ascii(const std::string& text) {
|
||
std::size_t begin = 0;
|
||
std::size_t end = text.size();
|
||
while (begin < end && (text[begin] == ' ' || text[begin] == '\t' ||
|
||
text[begin] == '\r' || text[begin] == '\n'))
|
||
++begin;
|
||
while (end > begin && (text[end - 1] == ' ' || text[end - 1] == '\t' ||
|
||
text[end - 1] == '\r' || text[end - 1] == '\n'))
|
||
--end;
|
||
return text.substr(begin, end - begin);
|
||
}
|
||
|
||
/// 可选:VDE_DXF_BODY_NAMES=名字1,名字2,...(C ABI 不暴露 body 名,环境变量供宿主/脚本注入)。
|
||
std::vector<std::string> env_body_names() {
|
||
std::vector<std::string> names;
|
||
const char* raw = std::getenv("VDE_DXF_BODY_NAMES");
|
||
if (raw == nullptr || *raw == '\0') return names;
|
||
const std::string text(raw);
|
||
std::size_t pos = 0;
|
||
while (pos <= text.size()) {
|
||
const std::size_t comma = text.find(',', pos);
|
||
const std::size_t end = comma == std::string::npos ? text.size() : comma;
|
||
names.push_back(trim_ascii(text.substr(pos, end - pos)));
|
||
if (comma == std::string::npos) break;
|
||
pos = comma + 1;
|
||
}
|
||
return names;
|
||
}
|
||
|
||
bool layer_name_used(const std::vector<LayerSpec>& layers, const std::string& name) {
|
||
for (const LayerSpec& layer : layers) {
|
||
if (layer.name == name) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// 图层名净化:仅保留 [A-Za-z0-9_.-],其余替换 '_';空名回退 BODY_<index>;
|
||
/// 截断到 31 字符;重名追加 _N 保证唯一(仍 <=31)。
|
||
std::string sanitize_layer_name(const std::string& raw, std::size_t index,
|
||
std::vector<LayerSpec>* layers) {
|
||
std::string candidate;
|
||
candidate.reserve(raw.size());
|
||
for (const char c : raw) {
|
||
const bool allowed = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
|
||
(c >= '0' && c <= '9') || c == '_' || c == '-' ||
|
||
c == '.';
|
||
candidate.push_back(allowed ? c : '_');
|
||
}
|
||
if (candidate.empty()) candidate = "BODY_" + std::to_string(index);
|
||
if (candidate.size() > kMaxLayerNameLength) candidate.resize(kMaxLayerNameLength);
|
||
const std::string base = candidate;
|
||
int suffix = 1;
|
||
while (layers != nullptr && layer_name_used(*layers, candidate)) {
|
||
const std::string suffix_text = "_" + std::to_string(suffix++);
|
||
const std::size_t keep = kMaxLayerNameLength > suffix_text.size()
|
||
? kMaxLayerNameLength - suffix_text.size()
|
||
: 1;
|
||
candidate = base.substr(0, keep) + suffix_text;
|
||
}
|
||
return candidate;
|
||
}
|
||
|
||
/// 增强模式(默认):每 body 一个独立图层 BODY_<i>(可被 VDE_DXF_BODY_NAMES 覆盖),
|
||
/// ACI 颜色 1–7 循环。VDE_DXF_WRITE_LAYERS=0 时回退旧行为(全部 layer 0、无 62 组码)。
|
||
std::vector<LayerSpec> make_layer_specs(std::size_t count) {
|
||
std::vector<LayerSpec> layers;
|
||
layers.reserve(count);
|
||
const std::vector<std::string> custom_names = env_body_names();
|
||
for (std::size_t i = 0; i < count; ++i) {
|
||
const std::string raw = (i < custom_names.size() && !custom_names[i].empty())
|
||
? custom_names[i]
|
||
: ("BODY_" + std::to_string(i));
|
||
LayerSpec spec;
|
||
spec.name = sanitize_layer_name(raw, i, &layers);
|
||
spec.aci_color = static_cast<int>(1 + (i % 7));
|
||
layers.push_back(std::move(spec));
|
||
}
|
||
return layers;
|
||
}
|
||
|
||
void append_3dface(DxfWriteSink* out, const std::string& layer, int aci_color,
|
||
bool enhanced, const double* positions, std::size_t vertex_count,
|
||
std::uint32_t a, std::uint32_t b, std::uint32_t c) {
|
||
// R12 3DFACE:8=图层,62=ACI 颜色(增强模式);10/20/30 = 第 1 点,
|
||
// 11/21/31 = 第 2 点,12/22/32 = 第 3 点,13/23/33 = 第 4 点(三角形时与第 3 点相同)。
|
||
append_pair(out, 0, "3DFACE");
|
||
append_pair(out, 8, layer);
|
||
if (enhanced) {
|
||
char color[16];
|
||
const auto result = std::to_chars(color, color + sizeof(color), aci_color);
|
||
append_pair(out, 62, std::string(color, static_cast<std::size_t>(result.ptr - color)));
|
||
}
|
||
const std::uint32_t corners[4] = {a, b, c, c};
|
||
for (int k = 0; k < 4; ++k) {
|
||
const std::uint32_t index = corners[k];
|
||
if (static_cast<std::size_t>(index) >= vertex_count) continue;
|
||
append_pair_double(out, 10 + k, positions[static_cast<std::size_t>(index) * 3 + 0]);
|
||
append_pair_double(out, 20 + k, positions[static_cast<std::size_t>(index) * 3 + 1]);
|
||
append_pair_double(out, 30 + k, positions[static_cast<std::size_t>(index) * 3 + 2]);
|
||
}
|
||
}
|
||
|
||
int dxf_write_impl(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
|
||
DxfWriteSink* sink) {
|
||
const int host_rc = validate_write_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
if (!sink) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "dxf: 输出 sink 为空");
|
||
if (!bodies || count == 0)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "dxf: 无实体可写");
|
||
if (count > kMaxWriteBodies)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG,
|
||
"dxf: body 数超过上限(10000)");
|
||
|
||
const bool enhanced = !env_disabled("VDE_DXF_WRITE_LAYERS");
|
||
const std::vector<LayerSpec> layers = enhanced ? make_layer_specs(count)
|
||
: std::vector<LayerSpec>{};
|
||
|
||
append_pair(sink, 0, "SECTION");
|
||
append_pair(sink, 2, "HEADER");
|
||
append_pair(sink, 9, "$ACADVER");
|
||
append_pair(sink, 1, "AC1009"); // R12
|
||
append_pair(sink, 0, "ENDSEC");
|
||
append_pair(sink, 0, "SECTION");
|
||
append_pair(sink, 2, "TABLES");
|
||
append_pair(sink, 0, "TABLE");
|
||
append_pair(sink, 2, "LAYER");
|
||
const std::size_t table_count = 1 + (enhanced ? count : 0);
|
||
append_pair(sink, 70, std::to_string(table_count));
|
||
append_pair(sink, 0, "LAYER");
|
||
append_pair(sink, 2, "0");
|
||
append_pair(sink, 70, "0");
|
||
append_pair(sink, 62, "7");
|
||
append_pair(sink, 6, "CONTINUOUS");
|
||
if (enhanced) {
|
||
for (const LayerSpec& layer : layers) {
|
||
append_pair(sink, 0, "LAYER");
|
||
append_pair(sink, 2, layer.name);
|
||
append_pair(sink, 70, "0");
|
||
append_pair(sink, 62, std::to_string(layer.aci_color));
|
||
append_pair(sink, 6, "CONTINUOUS");
|
||
}
|
||
}
|
||
append_pair(sink, 0, "ENDTAB");
|
||
append_pair(sink, 0, "ENDSEC");
|
||
append_pair(sink, 0, "SECTION");
|
||
append_pair(sink, 2, "ENTITIES");
|
||
|
||
std::size_t written_triangles = 0;
|
||
for (std::size_t i = 0; i < count; ++i) {
|
||
const std::string layer_name = enhanced ? layers[i].name : std::string("0");
|
||
const int aci_color = enhanced ? layers[i].aci_color : 7;
|
||
VdeMeshData mesh{};
|
||
mesh.struct_size = static_cast<std::uint32_t>(sizeof(VdeMeshData));
|
||
struct MeshReleaser {
|
||
const VdePluginHost* host;
|
||
VdeMeshData* mesh;
|
||
~MeshReleaser() {
|
||
if (!host || !mesh || !host->free_buffer) return;
|
||
if (mesh->positions) host->free_buffer(mesh->positions);
|
||
if (mesh->indices) host->free_buffer(mesh->indices);
|
||
if (mesh->normals) host->free_buffer(mesh->normals);
|
||
}
|
||
} releaser{host, &mesh};
|
||
if (host->body_tessellate(bodies[i], kWriteDeflection, &mesh) != VDE_PLUGIN_OK)
|
||
return fail(host, VDE_PLUGIN_ERR_INTERNAL, "dxf: body_tessellate 失败");
|
||
if (!mesh.positions || mesh.num_vertices == 0 || !mesh.indices ||
|
||
mesh.num_triangles == 0)
|
||
return fail(host, VDE_PLUGIN_ERR_INTERNAL,
|
||
"dxf: body_tessellate 返回空网格(无三角面)");
|
||
sink->reserve(mesh.num_triangles * 340);
|
||
for (std::size_t tri = 0; tri < mesh.num_triangles; ++tri) {
|
||
if (written_triangles >= kMaxWriteTriangles)
|
||
return fail(host, VDE_PLUGIN_ERR_BUFFER, "dxf: 三角面数超过写出上限");
|
||
const std::uint32_t a = mesh.indices[tri * 3 + 0];
|
||
const std::uint32_t b = mesh.indices[tri * 3 + 1];
|
||
const std::uint32_t c = mesh.indices[tri * 3 + 2];
|
||
if (static_cast<std::size_t>(a) >= mesh.num_vertices ||
|
||
static_cast<std::size_t>(b) >= mesh.num_vertices ||
|
||
static_cast<std::size_t>(c) >= mesh.num_vertices)
|
||
return fail(host, VDE_PLUGIN_ERR_INTERNAL,
|
||
"dxf: body_tessellate 返回越界三角面索引");
|
||
append_3dface(sink, layer_name, aci_color, enhanced, mesh.positions,
|
||
mesh.num_vertices, a, b, c);
|
||
++written_triangles;
|
||
}
|
||
}
|
||
append_pair(sink, 0, "ENDSEC");
|
||
append_pair(sink, 0, "EOF");
|
||
if (written_triangles == 0)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "dxf: 无三角面可写");
|
||
return sink->failed() ? fail(host, VDE_PLUGIN_ERR_IO, "dxf: 写文件失败")
|
||
: VDE_PLUGIN_OK;
|
||
}
|
||
|
||
int dxf_write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies,
|
||
std::size_t count) {
|
||
if (!path) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "dxf: path 为空");
|
||
std::FILE* file = std::fopen(path, "wb");
|
||
if (!file) return fail(host, VDE_PLUGIN_ERR_IO, "dxf: 写文件失败");
|
||
DxfWriteSink sink(file);
|
||
const int rc = dxf_write_impl(host, bodies, count, &sink);
|
||
const bool ok = (rc == VDE_PLUGIN_OK) && sink.finish();
|
||
std::fclose(file);
|
||
if (!ok) {
|
||
std::remove(path);
|
||
return rc != VDE_PLUGIN_OK ? rc
|
||
: fail(host, VDE_PLUGIN_ERR_IO, "dxf: 写文件失败");
|
||
}
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
int dxf_write_memory(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
|
||
VdeBuffer* out) {
|
||
if (out) {
|
||
out->data = nullptr;
|
||
out->size = 0;
|
||
}
|
||
if (!out) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "dxf: 输出缓冲为空");
|
||
std::string text;
|
||
text.reserve(64 * 1024);
|
||
DxfWriteSink sink(&text);
|
||
const int rc = dxf_write_impl(host, bodies, count, &sink);
|
||
if (rc != VDE_PLUGIN_OK || !sink.finish())
|
||
return rc != VDE_PLUGIN_OK ? rc
|
||
: fail(host, VDE_PLUGIN_ERR_IO, "dxf: 写出失败");
|
||
void* buffer = host->alloc(text.size());
|
||
if (!buffer) return fail(host, VDE_PLUGIN_ERR_BUFFER, "dxf: 输出内存分配失败");
|
||
std::memcpy(buffer, text.data(), text.size());
|
||
out->data = buffer;
|
||
out->size = text.size();
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
int dxf_probe(const void* head, std::size_t head_size) {
|
||
if (!head || head_size < 8) return 0;
|
||
const char* data = static_cast<const char*>(head);
|
||
const std::string text(data, head_size);
|
||
if (text.find("ENTITIES") != std::string::npos) return 90;
|
||
if (text.find("ACADVER") != std::string::npos) return 80;
|
||
if (text.find("SECTION") != std::string::npos) return 70;
|
||
return 0;
|
||
}
|
||
|
||
const char* dxf_describe() {
|
||
return "VDE official DXF format (read 3DFACE/polyline/circle mesh subset; write host "
|
||
"tessellation -> R12 3DFACE with per-body layers + ACI colors; "
|
||
"VDE_DXF_WRITE_LAYERS=0 disables layers)";
|
||
}
|
||
|
||
const VdeFormatPlugin kFormat = [] {
|
||
VdeFormatPlugin format{};
|
||
format.struct_size = static_cast<uint32_t>(sizeof(VdeFormatPlugin));
|
||
format.format_id = "dxf";
|
||
format.extensions = ".dxf";
|
||
format.probe = &dxf_probe;
|
||
format.read_file = &dxf_read_file;
|
||
format.read_memory = &dxf_read_memory;
|
||
format.write_file = &dxf_write_file;
|
||
format.write_memory = &dxf_write_memory;
|
||
format.describe = &dxf_describe;
|
||
return format;
|
||
}();
|
||
|
||
} // namespace
|
||
|
||
extern "C" {
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) {
|
||
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI;
|
||
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
|
||
info->id = "com.vde.official.dxf";
|
||
info->name = "VDE Official DXF Format";
|
||
info->version = "1.0.0";
|
||
info->kind = VDE_PLUGIN_KIND_FORMAT;
|
||
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
info->capabilities = "format:dxf.read;format:dxf.write";
|
||
info->license = "Apache-2.0";
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host,
|
||
VdePluginRegistration* reg) {
|
||
if (!reg || reg->struct_size < sizeof(VdePluginRegistration))
|
||
return VDE_PLUGIN_ERR_ABI;
|
||
const int host_rc = vde_plugin_common::validate_common_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
reg->kind = VDE_PLUGIN_KIND_FORMAT;
|
||
reg->format = &kFormat;
|
||
reg->cae_solver = nullptr;
|
||
reg->cam_strategy = nullptr;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
|
||
|
||
} // extern "C"
|