Skip to content

Draft: AutoCAD round-trip fixes (DWG R2000–R2018, DXF R12–R2018) - #1

Draft
nameloCmaS wants to merge 39 commits into
masterfrom
roundtrip-fixes
Draft

nameloCmaS wants to merge 39 commits into
masterfrom
roundtrip-fixes

Conversation

@nameloCmaS

@nameloCmaS nameloCmaS commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

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 durations

Bit-level encoding

Writing the file

Down-conversion (newer drawings written as older versions)

  • r2000 from r2004+: the VX table and sub-entity links
  • r2010 and older from r2013+: parameter-based action bodies. The ASSOCACTION keeps its class_version (the encoder writes 1), and free() frees the params moved into the body; both were leaks under ASan (json.test, example_2013/2018 → r2000).
  • r2000 PROXY_OBJECTs from r2004+ carry their DXF name
  • VISUALSTYLE and SUN saved to an older release
  • multi-line attributes as single-line text for older versions
  • downconvert_DIMSTYLE: no guessed DIMJAG/DIMTALN EED (downconvert_DIMSTYLE: no guessed DIMJAG/DIMTALN EED LibreDWG/libredwg#1576)
  • TABLESTYLE: default cell margins
  • older releases: layer names without a leading '*' (*ADSK_SYSTEM_LIGHTS)

Objects

DXF

  • hatch and mpolygon spline weights are 42
  • no 52 in a rotated dimension for 0.0 (DXF: no 52 in a rotated dimension for 0.0 LibreDWG/libredwg#1571)
  • ATTRIB/ATTDEF: the embedded MTEXT's DWG-only fields
  • MTEXT, VIEWPORT ids and optional handles, dictionary entries to unwritten objects
  • $ACADMAINTVER as AutoCAD writes it
  • in_dxf: the embedded column object of an r2018 MTEXT; empty xdata strings
  • out_dxf: valid R12 DXF from R13+ drawings
  • no dictionary entries to objects of classes left out of the DXF
  • 67 for the entities of every paper space layout
  • R12 DXF: only the viewports of the current layout

Checks

  • Checked as a whole at the branch tip: the CI of this repository runs on the PR (gcc/clang, autotools and CMake, big-endian, ASan/UBSan, release distcheck).
  • Unit tests in bits_test.c and encode_test.c compare the output with the AutoCAD-saved example_* 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):

  • R2007–R2018 DWGs written from R2000/R2004 drawings mostly do not open in AutoCAD.
  • R2018 DXFs: AutoCAD rejects the section/detail view styles ("bad viewstyle display name").
  • Some LibreDWG DXFs make AutoCAD's open hang; DXF rejects in SHAPE, IMAGE, MESH, constraint and ACIS data.
  • 14 test-data DXFs written by libdxfrw (2018/assoc*.dxf and others) are not accepted by AutoCAD.
  • BLOCKSCALEACTION writes one byte 0 before its strings (r2007+); its meaning is unknown.

🤖 Generated with Claude Code

nameloCmaS and others added 27 commits November 23, 2026 22:38
* 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>
nameloCmaS and others added 7 commits November 23, 2026 22:40
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>
* 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>
Comment thread src/dwg2.spec
LOG_TRACE ("context.ownerhandle: 4.0.0 [H 0]\n");
}
#endif
SUB_FIELD_HANDLE (context, scale, 2, 340);
nameloCmaS and others added 4 commits November 24, 2026 00:20
* 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>
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