Repository navigation
Draft: AutoCAD round-trip fixes (DWG R2000–R2018, DXF R12–R2018) - #1
Draft
nameloCmaS wants to merge 39 commits into
Draft
nameloCmaS wants to merge 39 commits into
nameloCmaS wants to merge 39 commits into
Conversation
nameloCmaS
force-pushed
the
roundtrip-fixes
branch
from
November 22, 2026 14:28
3dcddc2 to
3791838
Compare
* src/dwg.c (dwg_write_file): Unmap the file before shrinking it. Windows cannot change the size of a file with a mapped view, so ftruncate failed and the DWG kept the zero padding of the mapping. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/dwg2.spec (SORTENTSTABLE): DXF 330 for block_owner, the model or paper space block whose entities are sorted. * src/dynapi.c, doc/dynapi.texi: Regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/dwg.spec (DIMENSION_LINEAR): Write the oblique_angle 52 only when non-zero, as dim_rotation 50. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/bits.c (bit_write_T): Write an empty string as length 0, without a NUL. (bit_write_TU): Do not count or write a terminating NUL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/enc_macros.h (FIELD_TV): Convert a TU string of an r2007+ source with bit_write_T for a pre-r2007 target. (FIELD_T16): Write a TU string as 8-bit chars for a pre-r2007 target. * src/encode.c (encode_classes): Write appname and cppname with bit_write_T. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/dwg.spec (LAYER): In the encoder, clear the material only when it does not resolve to a MATERIAL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/dwg2.spec (BLOCKLOOKUPGRIP, BLOCKLOOKUPPARAMETER): Subclass markers AcDbBlockLookUpGrip and AcDbBlockLookUpParameter, as AutoCAD writes them. The CLASSES section keeps "Lookup". * src/dynapi.c: Regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/downconvert.c (downconvert_DIMSTYLE): Remove the ACAD_DSTYLE_DIMJAG and ACAD_DSTYLE_DIMTALN APPIDs and EED, whose values were guesses. AutoCAD cannot open a file whose DIMSTYLE has this EED, and AutoCAD LT 2027 writes neither the EED nor the APPIDs when saving to r2000. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A duration (TIMEBLL with tm_mday 0) is formatted as days and hh:mm:ss without strftime: the MSVC runtime aborts on %d with a day of 0 (invalid parameter handler). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bit_write_DD wrote mode 0 (the default) for values within 1e-12 of the default, which the reader returns as the default: the value changed on a rewrite (e.g. a LINE end y, an INSERT scale of 1.0000000000000002). AutoCAD writes such values with mode 1 or 2. bit_eq_DD (INSERT/MINSERT scale_flag) is bit-exact too, so both agree. The tolerance came with d2084ca (GH LibreDWG#326). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An r2004+ drawing has no VX table, but AutoCAD writes one to r2000: an empty record, then one per VIEWPORT entity of the current paper space, linked to it. AutoCAD also links the VERTEX of a POLYLINE and the ATTRIB of an INSERT with prev/next entity and nolinks. AutoCAD rejects an r2000 file of such a drawing without the VX table. HEADER.sections of an r2004+ source counts its section map, not the r13-r2000 locators. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AutoCAD keeps the owned action params and the value params in the AcDbAssocParamBasedActionBody (aab_version 1) for r2010 and older; r2013+ has them in the ASSOCACTION. AcDbAssocActionParam has no aap_version before r2013. The encoder writes ASSOCACTION class_version 1 without changing the object, and free() frees the params moved into the body: free() follows from_version, where the spec has them in the ASSOCACTION. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The array action bodies embed the parameters object of class paramblock: version, the array items (class AcDbAssocArrayItem), then the transform matrix (not in DXF). One soft pointer per item in AutoCAD's records. dynapi regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- version is dwg_version | maint_version << 16 (ODA 20.4.90), not << 8; - r2004 only: a TV "cn:" + C++ class name follows class_id, as in AutoCAD's r2004 files; - r2007+ proxy data for r2000/r2004 output: the string stream and its footer converted as AutoCAD writes them (UTF-16 to TV); - the DXF codes of maint_version (97) and dwg_version (71). dynapi regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As AutoCAD writes them: ACAD EED 1000 "AcRTDxfName", 1000 <the class's DXF name>, since r2000 has no "cn:" class name field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Their format changed in r2010. Saved to an older release, AutoCAD gives them the ACAD EED with the saved-by version, AutoCAD 2018 in all its older releases (r2000-r2013); do the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without a cell style override AutoCAD writes its default cell margin 0.06. In the DXF AutoCAD writes the classic table style for all versions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In r2007+ these bits are the start of the string stream; AutoCAD's r2004 files have varying values in them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As AutoCAD's R2004 and R2018 records: numrows * numcols cells, then the lookup table connection names (303, 304, 305). The JSON importer checks the counts accordingly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- AcDbBlockElement be_major as AutoCAD writes it per version; - BLOCKSCALEACTION: one byte 0 before the strings (r2007+); its meaning is unknown, 0 in all of AutoCAD's records seen; - PLANESURFACE: class_version 3 before r2010, 4 since. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
raw_same_in_r2013 decides whether to copy the raw r2018 record of a partly decoded dynamic block class to r2013 output. The raw bits are not the whole record in the r2013 layout and AutoCAD cannot read such output, so it is false for now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An R2018+ multi-line ATTRIB/ATTDEF keeps its text in mtext; for an older version text_value/default_value get its plain text. dwg_strip_mtext handles the format codes, escapes and stacked text of AutoCAD's MTEXT (also used for the r12 DXF TEXT of an MTEXT). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bits_test: bit_write_DD bit-exact next to the default, bit_write_UMC with the minimal number of bytes, an empty r2018 string in r2000 and an r2007+ TU string without a NUL. encode_test: example_2018.dwg saved by the encoder to r2000-r2013, against AutoCAD 2018's saves of the same drawing (example_2000 .. example_2013): the saved-by EED of VISUALSTYLE and SUN, ASSOCACTION class_version and the action body params, LAYER materials, DIMSTYLE EED, the r2000 VX table, and in r2000/r2004 the header GUIDs and class names. Also the AcRTDxfName EED of r2000 PROXY_OBJECTs, no unknown_54..57 in AutoCAD's r2007+ headers, and dwg_strip_mtext. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A multi-line attribute with enable_context has an embedded MTEXTOBJECTCONTEXTDATA (DXF 101 "Embedded Object"), not a SCALE. AutoCAD's DXF has no AcDbMTextAttributeObjectContextData marker and AutoCAD 2027 rejects one; in_dxf reads enable_context in AcDbAnnotScaleObjectContextData. dynapi regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After the 101 "Embedded Object" an MTEXT with columns has its column data; read the column heights (46) there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AutoCAD's r2007+ DXF has empty 1000 strings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The weight of a rational spline edge is 42 after its control point (10/20), as AutoCAD writes it and the DXF reference has it; 40 are the knots. in_dxf reads 42 and still accepts the old 40 form. dynapi regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The common entity fields of the embedded MTEXT of a multi-line attribute are DWG only: with DXF codes, the DXF import overwrote the attribute's own linetype scale, lineweight, layer etc. (e.g. 370 29, a raw DWG lineweight index, which AutoCAD rejects). The annotative data has a registered application handle and a BS (ODA). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The annotative flag and the 10/11 order (10 the x axis, 11 the insertion point) as in AutoCAD's DXF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The overall viewport of the current layout is id 1, the others 2, 3, ... in the order of the layout's viewports list (r2004+); AUDIT reports a detail viewport with id 1. The viewports of the other layouts are off (68 and 69 0), as AutoCAD writes them. Handles that may be absent (visualstyle, clip_boundary, named_ucs, background, shadeplot) are written only when set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DICTIONARY and DICTIONARYWDFLT leave out entries to objects of undecoded classes (UNKNOWN_OBJ) and to missing objects, which the DXF does not contain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The product maintenance version, not the DWG header byte (0 in AutoCAD 2027 DWGs). AutoCAD reads newer fields only from a high enough value: an r2018 MTEXT text frame (90 16) with 0 is rejected. Keep a non-zero DWG value when writing the same version, else use what AutoCAD LT 2027 writes for the target. dynapi regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AutoCAD rejects the whole R12 file on one invalid group. Writing an r13+ drawing as R12 DXF now does what AutoCAD does: - entities R12 has, converted (LWPOLYLINE to POLYLINE, MTEXT to TEXT, ...), others left out; no objects in entity lists; - symbol table and block names in the R12 form (upper case, no spaces), as AutoCAD writes them; - no CLASSES section, no EED on table records or table heads, no 66 and bare SEQEND for an INSERT without attribs, a BLOCK with its flags and base point; - $HANDLING 1 and the entity handles, which their EED refer to; - no TrueType fonts (txt instead), fixed-size strings of r2007+ drawings converted from TU. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
encode_test: example_2018.dwg as DXF r2000-r2018, against AutoCAD 2018's DXF of the same drawing (example_2000.dxf .. example_2018.dxf): $ACADMAINTVER (r2004-r2013), the VIEWPORT ids, rotated dimensions without 52, and the MTEXT embedded objects. As R12 DXF against example_r12.dxf: the sections, the entity types and the R12 layer names, and it reads back. in_dxf: an empty 1000 string in an r2018 XRECORD. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nameloCmaS
force-pushed
the
roundtrip-fixes
branch
from
November 23, 2026 22:41
3791838 to
2fc6462
Compare
* src/out_dxf.c (dxf_has_object): New. Whether the DXF writer writes an object: the checks and class dispatcher of dwg_dxf_variable_type, with a stub for the writers. (dxf_variable_type_class): New, the checks shared by both. * src/out_dxf.h: Declare dxf_has_object. * src/dwg.spec (DICTIONARY, DICTIONARYWDFLT): Leave out the entries to objects the DXF does not have, as for the unstable classes the release DXF writer leaves out (PERSUBENTMGR). AUDIT reports each such entry as erased. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| LOG_TRACE ("context.ownerhandle: 4.0.0 [H 0]\n"); | ||
| } | ||
| #endif | ||
| SUB_FIELD_HANDLE (context, scale, 2, 340); |
* src/out_dxf.c (dxf_entity_in_pspace): New. entmode 1, or 0 with the block of a layout other than model space as the owner. * src/out_dxf.h: Declare it. * src/common_entity_handle_data.spec: Write 67 1 for those, as AutoCAD does for the entities of the non-current layouts too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* src/out_dxf.c (dwg_dxf_object): r12 has a single paper space; leave out the VIEWPORTs of the other layouts, as AutoCAD does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An r2007+ layer name may start with '*' (the system layer *ADSK_SYSTEM_LIGHTS); AutoCAD drops it when saving to r2000 or r2004. * src/downconvert.c (downconvert_layer_name): New. * src/encode.h: Declare it. * src/encode.c (dwg_encode): Call it before r2007 from r2007+. * src/out_dxf.c (dxf_cvt_tablerecord): Print the layer name without the '*' for those versions, in the LAYER table and the references. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test/unit-testing/encode_test.c (older_release_count): Count the layer names with a leading '*'. (dxf_dangling_entries): New. (dxf_features_of): Count the 67 1 groups, the VIEWPORTs and the dictionary entries to absent objects. (dxf_as_acad_test): Check them and the layer names against AutoCAD's. (dxf_r12_test): Check the number of VIEWPORTs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nameloCmaS
force-pushed
the
roundtrip-fixes
branch
from
November 24, 2026 00:26
6a8c837 to
da90440
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft: AutoCAD round-trip fixes (DWG R2000–R2018, DXF R12–R2018)
This draft collects every change from testing LibreDWG's writers against AutoCAD LT 2027. It is a reference; the changes are offered upstream as smaller PRs. This branch is rebased on the current LibreDWG master.
How the changes were found
Drawings were converted to every version AutoCAD LT 2027 saves (DWG 2000–2018, DXF R12–2018), both by AutoCAD and by LibreDWG, and every output was opened in AutoCAD (AUDIT without fixing) and read back by LibreDWG. AutoCAD's own output was the reference for each format detail. Each fix states the rule and what AutoCAD writes or requires; ODA spec references are given where they apply.
The commits (one item each, in dependency order)
Build and tests
dec_macros: no MSVC strftime abort on durationsBit-level encoding
bits: bit-exact DD values;bit_eq_DDbit-exact too (the 1e-12 tolerance came with d2084ca, GH Version 12.2 loads this DWG much worse compared to v12.1 LibreDWG/libredwg#326)bits: empty T and r2007+ TU strings without a NUL (bits: empty T and r2007+ TU strings without a NUL LibreDWG/libredwg#1572)encode: TU strings of r2007+ drawings in pre-r2007 TV fields (encode: TU strings of r2007+ drawings in pre-r2007 TV fields LibreDWG/libredwg#1573)Writing the file
dwg_write_file: unmap before ftruncate (dwg_write_file: unmap before ftruncate LibreDWG/libredwg#1569)Down-conversion (newer drawings written as older versions)
free()frees the params moved into the body; both were leaks under ASan (json.test, example_2013/2018 → r2000).downconvert_DIMSTYLE: no guessed DIMJAG/DIMTALN EED (downconvert_DIMSTYLE: no guessed DIMJAG/DIMTALN EED LibreDWG/libredwg#1576)Objects
encode: no raw copies of partly decoded r2018 records in r2013DXF
$ACADMAINTVERas AutoCAD writes itin_dxf: the embedded column object of an r2018 MTEXT; empty xdata stringsout_dxf: valid R12 DXF from R13+ drawingsChecks
bits_test.candencode_test.ccompare the output with the AutoCAD-savedexample_*drawings (DWG r2000–r2013, DXF R12–r2018).Known gaps (not in this draft)
From a run over all of
test/test-data(264 drawings × 13 targets, both producers, both readers):2018/assoc*.dxfand others) are not accepted by AutoCAD.🤖 Generated with Claude Code