Skip to content

feat(DXF): export triangulated surfaces as DXF - #237

Open
francoisbonneau wants to merge 2 commits into
nextfrom
feat/dxf_triangles_export
Open

francoisbonneau wants to merge 2 commits into
nextfrom
feat/dxf_triangles_export

Conversation

@francoisbonneau

@francoisbonneau francoisbonneau commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds a dxf writer for TriangulatedSurface3D and PolygonalSurface3D.

  • ASCII DXF (R12, $ACADVER = AC1009), one 3DFACE per polygon. A triangle repeats its third corner as fourth one.
  • PolygonalSurface3D: polygons with 3 or 4 vertices. A polygon with more vertices raises a data error before the file is created.
  • 3DFACE rather than a polyface mesh: polyface vertex indices are 16-bit in strict readers (max 32,767 vertices). The DXF reader merges the colocated corners back on load.
  • One layer named after the surface (0 if unnamed). Characters forbidden in layer names are replaced by _; non-ASCII characters are written as \U+XXXX, which the DXF reader decodes.
  • Layer names are not strictly R12 (31 characters among A-Z 0-9 $ - _), so that the surface name is kept. Lenient readers accept them. A newer version (e.g. AC1015) is not declared, because it requires handles and complete TABLES, BLOCKS and OBJECTS sections.
  • Coordinates written with max_digits10 precision.

Code organization

  • internal/dxf_surface_output: single writer for both surface types (takes a SurfaceMesh3D).
  • internal/dxf_common: \U+XXXX encoding and decoding, and the number of 3DFACE corners, shared by the reader and the writer.

Tests

  • test-dxf: round trip of the new galeries_and_cylinder.og_tsf3d (17,072 triangles): same vertex and triangle counts, same positions.
  • test-dxf: small triangulated surface with large coordinates and name Galerie é:1, reloaded exactly, named Galerie é_1.
  • test-dxf: polygonal surface with a quad and a triangle, named Baie € (3-byte UTF-8 character), reloaded exactly with the same name.
  • test-dxf: a pentagon raises a data error and no file is written.
  • Python generic API test: dxf listed as an output extension of TriangulatedSurface3D and PolygonalSurface3D.

Not checked in a CAD tool (AutoCAD, etc.).

🤖 Generated with Claude Code

Write an ASCII DXF (R12) with one 3DFACE per triangle, on a layer named
after the surface. Forbidden layer characters are replaced by '_' and
non-ASCII characters are written as \U+XXXX escapes, decoded by the DXF
reader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@francoisbonneau francoisbonneau left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cross-repo review — feat/dxf_triangles_export

This is the only open PR on this branch, so there is nothing to check across repos. No other workspace repo depends on the TriangulatedSurface3D output extension list or on DXF.

Overall the writer is clean: it is short, uses the classic locale and max_digits10, checks the stream state after writing, and round-trip tests use the existing DXF reader. Nothing blocks the merge; points 2 and 4 are worth fixing first.

Design

  1. The \U+XXXX escape is encoded in one file and decoded in another. dxf_input.cpp already has append_utf8 / decode_unicode_escapes. dxf_triangulated_output.cpp reimplements the reverse UTF-8 decoding. A small internal dxf_strings header holding both directions would keep them consistent and let one test cover both.
  2. The declared version does not match the layer names written. The header sets $ACADVER = AC1009 (R12). R12 limits layer names to 31 characters from A-Z 0-9 $ - _, with no spaces. Yet the test writes Galerie \U+00E9_1. Lenient readers accept it, but a strict R12 reader may not. Either declare a newer version (e.g. AC1015, which accepts such names and \U+ escapes) or document that names are not strictly R12.
  3. The reader and writer cover different types. The reader produces PolygonalSurface3D, but only TriangulatedSurface3D can be exported. Since 3DFACE also handles quads, a PolygonalSurface3D output limited to polygons with 3 or 4 vertices would be almost free. Follow-up only, not blocking.

Readability / robustness

  1. Forbidden characters are incomplete. FORBIDDEN_LAYER_CHARACTERS lacks the comma ,, which AutoCAD also forbids in symbol table names.
  2. UTF-8 continuation bytes are not validated. layer_name does not check that the bytes after a leading byte match 10xxxxxx. A truncated sequence such as \xC3 followed by A swallows the ASCII character into a bogus code point. Checking (b & 0xC0) == 0x80 and falling back to _ would fix it. Bytes >= 0xF8 are also treated as 4-byte leaders.
  3. Tests and includes. No test covers "empty name → layer 0" or "code point > U+FFFF → _". dxf_triangulated_output.hpp uses std::string_view without including <string_view>.

Performance

  1. Large test file. tests/data/galeries_and_cylinder.og_tsf3d adds a 675 KB binary to a public repo. The in-memory test already covers precision and naming, so a much smaller mesh would probably do for the round trip, unless this file reproduces a specific real case.

The writer itself has no performance issue: one pass over triangles, no per-face allocation (the layer name is computed once), and no std::endl.

@francoisbonneau

Copy link
Copy Markdown
Member Author

🤖 claude-cross-repo-review: complete

Polygonal surfaces with polygons of 3 or 4 vertices are written as
3DFACE, like triangulated surfaces, by a shared writer. Polygons with
more vertices raise a data error before the file is created.

The \U+XXXX encoding and decoding of DXF strings, and the number of
3DFACE corners, are shared by the reader and the writers. The layer
names not being strictly R12 is documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@francoisbonneau francoisbonneau left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cross-repo review, 2nd pass — feat/dxf_triangles_export @ 17e0b19

This is still the only open PR on this branch. The new commit adds PolygonalSurface3D export and refactors the writer. Note that the existing approval predates this commit, and the diff has roughly doubled since then.

Follow-up on the previous review

# Point Status
1 Duplicated \U+XXXX escape logic ✅ Factored into dxf_common.hpp/.cpp
2 R12 declared, but layer names are not R12 ✅ Documented in write_dxf_surface
3 No PolygonalSurface3D output ✅ Added through the shared write_dxf_surface(SurfaceMesh3D)
4 Comma , missing from forbidden layer characters ❌ Still missing in FORBIDDEN_LAYER_CHARACTERS
5 UTF-8 continuation bytes not validated ❌ Not fixed, and now contradicted by the docs (see A)
6 Tests for empty name / > U+FFFF, <string_view> include ⚠️ Partly done: dxf_triangulated_output.hpp and dxf_polygonal_output.hpp still use std::string_view without <string_view>, and both test cases are still missing
7 675 KB binary test file ❌ Unchanged

New findings

A. Readability / robustness: docs and code disagree (dxf_common.hpp). The encode_unicode_escapes doc says "Invalid UTF-8 sequences … are replaced by '_'". In fact only stray continuation bytes and sequences truncated at the end of the string are replaced. \xC3 followed by A gives \U+00C1 and swallows the A. Overlong sequences and bytes >= 0xF8 also pass through. Checking (b & 0xC0) == 0x80 on each continuation byte would make the code match the docs. Since this is now a shared utility, fixing the code is better than weakening the doc.

B. Design (minor): unneeded check for triangulated surfaces. The DXFSurfaceWriter constructor walks every polygon to check that none has more than 4 vertices before opening the file. This is a good choice: no partial file is left, and it is tested. For TriangulatedSurface3D the check can never fail, but it costs one O(n) pass with no allocation, so it can stay as is.

C. Readability (minor): layer_name filters characters below 0x20 but not DEL (0x7F).

The rest looks good. The refactor removes about 170 duplicated lines, and the dxf_input.cpp changes are mechanical. The new tests cover a quad plus a triangle, a 3-byte UTF-8 name round trip, and the pentagon error with no file left behind.

Summary: nothing blocks the merge. I recommend fixing A and 4 before merging (both are small), then getting a fresh approval, since it predates this commit.

@francoisbonneau

Copy link
Copy Markdown
Member Author

🤖 claude-cross-repo-review: complete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants