Skip to content

Native DWG Reader & Writer - #7

Merged
SkryxDev merged 5 commits into
DraftCraft-DEV:mainfrom
withshalom:dwg-codec-upstream
Nov 10, 2026
Merged

SkryxDev merged 5 commits into
DraftCraft-DEV:mainfrom
withshalom:dwg-codec-upstream

Conversation

@withshalom

Copy link
Copy Markdown
Member

DWG codec (from BlackCAD) and a DWG reader that maps straight into the drawing

This adds draftcraft-dwg's own DWG codec, brought over from BlackCAD (withshalom/blackcad, cad-dwg, MIT OR Apache-2.0, written from the Open Design Alliance's public DWG specification), and a new io::dwg_read that maps the decoded records straight into DraftCraft's Drawing, with no DXF text in between.

The reader is opt-in. The default stays acadrust, so opening a file behaves exactly as it does today. Nothing in the ribbon, commands, UI or model types changes.

Why: opening a large DWG today means acadrust decodes it, writes it out as DXF text, and DraftCraft parses that text again. On a DWG of about 200,000 entities the native reading takes about a third of the time, with identical entities (numbers below).

Full write-up, also in the branch: docs/dwg.md.

What is BlackCAD's and what is new

Part Origin
DWG decoder (crates/dwg/src/read/, bits, crc, lz77) BlackCAD cad-dwg, with edits (below)
Per-object records (crates/dwg/src/records/) BlackCAD cad-model, without its editing API (edit.rs is not included)
DWG 2018 writer (crates/dwg/src/write/) BlackCAD. Not called by anything and has no tests in this PR
io::dwg_read (records → Drawing) New in this PR, not from BlackCAD
DwgReader selection, read_dwg_with, Coverage New
crates/dwg/src/acadrust/ The existing bridge, renamed from native (see below)
dwg_native_read tests, dwg_bench New

Edits made to BlackCAD's code so it passes this repo's rules: a 580-line writer dispatch split into four functions, one round-trip test split in two, wildcard imports expanded, as_chunks in place of chunks_exact (clippy on 1.99), and unwrap/expect/panic free as required.

Architecture

crates/dwg (draftcraft-dwg, layer 0, depends on no workspace crate)
read/ R13–2018+ DWG (except 2007) → one record per object
write/ 2018 (AC1032) DWG from records (not called yet)
records/ the per-object records
bits, crc, lz77 format primitives shared by both
acadrust/ the existing bridge: DWG ⇄ DXF bytes via acadrust (MPL-2.0, unmodified)
crates/io
dxf_read / dxf_write DXF ⇄ model (unchanged apart from the helpers below)
dwg_read DWG records → model (new)

•	One model. The records are decoded, mapped and dropped. Nothing holds or edits them.
•	One rule per conversion. Where a value means the same in DWG and DXF, dwg_read calls the DXF reader's own helper instead of repeating the rule: ocs_n, transparency_440, style_font_of, unescape_unicode, and dxf_raw's xdata rules.
•	Layering is unchanged: cargo xtask layers passes.

Changes to existing code

These are the only edits outside the new files, so they are the part to review closely:

•	crates/dwg: the module native is renamed acadrust, since it is the bridge to acadrust and "native" was misleading next to a real native reader. Every caller in crates/io and its tests is updated (draftcraft_dwg::native::… → draftcraft_dwg::acadrust::…). The cfg no_dwg_bridge becomes no_acadrust.
•	crates/dxf: unescape_unicode is now pub.
•	crates/io/src/dxf_read/{common,tables}.rs, dxf_read.rs, dxf_raw.rs: transparency_440, ocs_n, OWN_APPS and MAX_XDATA_GROUPS become pub(crate), and style_font is split into style_font_of(file, acad_face, own) plus a thin wrapper. No behaviour change.
•	crates/io/src/lib.rs: adds DwgReader { Acadrust, Native, Auto }, read_dwg_with, and a reader choice via DRAFTCRAFT_DWG_READER (default acadrust).
•	NOTICE: attribution for BlackCAD, and a note that acadrust is used unmodified.
•	The first commit also builds the acadrust bridge for wasm32 (acadrust already builds there); FreeBSD aarch64 stays excluded.

Choosing the reader

DRAFTCRAFT_DWG_READER=acadrust|native|auto, or io::read_dwg_with(bytes, DwgReader).

•	acadrust (default): today's path.
•	native: the new reader. What it left out is listed in the drawing's read notes.
•	auto: the native reading only when nothing was left out, otherwise acadrust.

The default stays acadrust until the native reader carries everything a real file holds (next section), so opening a file never loses something it keeps today.

What the native reader maps

LINE, POINT, CIRCLE, ARC, ELLIPSE, LWPOLYLINE, TEXT, MTEXT, INSERT (without attributes), SOLID, TRACE, 3DFACE, XLINE, RAY. Also layers, linetypes, text styles (font, annotative), blocks (anonymous ones numbered as AutoCAD numbers them in DXF), paper-space layouts, the main header variables, and other applications' extended data on entities. Mirrored OCS (extrusion z < 0) follows the DXF reader's conventions.

Not covered yet

Counted in dwg_read::Coverage; under auto the file goes through acadrust.

DIMENSION, HATCH, SPLINE, LEADER/MLEADER, POLYLINE (2D/3D/mesh), attributes, viewports, images, tables. Also mirrored TEXT/MTEXT, entity plot styles and materials, extension dictionaries, external references, and every non-graphic object DraftCraft keeps as foreign data (dictionaries, XRECORD, VISUALSTYLE, SCALE, MATERIAL, …). AutoCAD files always contain the latter, so auto falls back on them until foreign objects are mapped.

Results

Fidelity (crates/io/tests/it/dwg_native_read.rs), comparing against the acadrust path entity by entity (same handle, space, kind and properties), plus layers, linetypes, styles, block names and layouts:

•	Every mapped type, written as DWG 2000, 2004, 2010, 2013 and 2018: identical.
•	AutoCAD-saved MI-201 files R14–2018 (MI201_CORPUS, an external corpus, not committed): every mapped entity identical (52 of 54; the other 2 are dimensions). R14 files store no lineweights: acadrust reports 0.00 mm, the native reader ByLayer/Default, so R14 is compared without lineweights.
•	Damaged files (every 97th prefix, every 61st byte flipped) return an error or a drawing, never a panic.

Speed, opening a 2018 DWG of about 200,000 entities (release build, 2-core cloud machine, single runs):

Drawing acadrust (today) native
dwg_bench (in this PR), 200,000 entities 2,233–2,310 ms 676–765 ms
MI-201's entities copied 6,250 times, written by acadrust 2,176 ms 624 ms
the same, written by BlackCAD's writer 2,113 ms 565 ms

About 3.0–3.5× faster, with every mapped entity identical (200,000 of 200,000 in dwg_bench). The native time is BlackCAD's decode (about 0.3 s) plus the mapping. The acadrust time is its decode, writing about 30 MB of DXF text, and parsing it again.

Tests

•	cargo test -p draftcraft-dwg: the codec's own tests (corpus tests run when MI201_CORPUS is set).
•	cargo test -p draftcraft-io --test it dwg_native_read: the comparison above.
•	cargo run --release -p draftcraft-io --example dwg_bench -- [entities]: the speed measurement.
•	roundtrip::handle_limits::dwg_exhausted_handles_are_preserved_or_refused failed in one local release build, and fails there on main too (acadrust's DWG 2000 writer); it passes in a local debug build and in CI. This PR doesn't change the save path.
•	cargo fmt --check, clippy -D warnings (Rust 1.99), cargo xtask layers and the wasm32 check pass. The same changes passed CI (gate) on my fork before this PR.

BlackCAD's writer tests compared against cad-dxf output, so they are left out. The writer therefore ships untested here, until it is tested against DraftCraft's model.

Next (separate PRs)

1.	Foreign objects: carry non-graphic objects DraftCraft doesn't model into Drawing::foreign, so auto can become the default.
2.	DIMENSION, HATCH, SPLINE, POLYLINE and attributes.
3.	Write 2018 DWG from the model with the BlackCAD writer, with a read-back check, after stable handles for tables and blocks.
4.	acadrust behind a feature, as the fallback for 2007 files and older save-as versions.

Not in this PR

My fork's branding (logo, menu bar placement, PWA) and its web-deploy workflow are deliberately left out. This branch is your main plus five commits that touch only DWG.

🤖 Generated with Claude Code

https://claude.ai/code/session_01F67Rsr8EV2T1Jia8QwNpDr

Shalom and others added 5 commits November 9, 2026 22:17
acadrust already builds for wasm32-unknown-unknown (web-time clocks,
sequential fallbacks, file memory-mapping kept native), and the bridge
only passes bytes through a Cursor. build.rs and Cargo.toml no longer
leave it out on wasm32; FreeBSD aarch64 stays excluded. With no worker
started in the web app, DWG work runs in the page, as it does in a
library or test. The crate docs now say the catch_unwind guard is
native-only: wasm32 aborts on a panic.

Ported from withshalom/cadcraft acadrust-web (c2eaa7b), already live
on cadcraft.dev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F67Rsq8SMvTJUJTMsK8dUk
…st bridge named acadrust

draftcraft-dwg becomes DraftCraft's own DWG codec, from BlackCAD (withshalom/blackcad,
MIT OR Apache-2.0, written from the ODA's public DWG specification):

- read/: R13 to 2018 and later (except 2007) into the records of the file's objects
- write/: AutoCAD 2018 (AC1032) files from records
- records/: one plain record per DWG object (BlackCAD's cad-model without its editing API)
- bits, crc, lz77: the format's primitives, shared by both

The crate still depends on no other workspace crate (layer 0). Nothing calls the new reader
yet: draftcraft-io maps its records into the drawing in the next commits.

The acadrust bridge, called `native` (it builds on desktop targets), is now `acadrust`, named
for what it does: DWG to and from DXF bytes through the acadrust crate, the fallback for what
the reader and writer don't cover yet. Callers use draftcraft_dwg::acadrust::…; the cfg that
leaves it out is no_acadrust.

Lints: wildcard imports expanded, the writer's per-type dispatch split into four functions and
the bit-stream round-trip test into two (cognitive complexity). BlackCAD's writer tests compare
against cad-dxf output and are left out until the writer is tested against DraftCraft's model.

Tests: 92 pass with MI201_CORPUS set (the codec's corpus tests skip without it); 11 ignored
as in BlackCAD. clippy -D warnings, layers and the wasm32 check pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F67Rsr8EV2T1Jia8QwNpDr
A DWG reader in draftcraft-io that maps the records draftcraft-dwg's reader decodes straight
into the drawing, with no DXF in between: LINE, POINT, CIRCLE, ARC, ELLIPSE, LWPOLYLINE, TEXT,
MTEXT, INSERT (without attributes), SOLID, TRACE, 3DFACE, XLINE, RAY, layers, linetypes, text
styles, blocks (anonymous ones numbered as AutoCAD numbers them in DXF), paper-space layouts,
the main header variables and other applications' extended data. What it leaves out is counted
in dwg_read::Coverage.

Where a value means the same in DWG and DXF, it uses the DXF reader's own rule: ocs_n,
transparency_440, style_font_of (factored out of style_font), dxf_raw's xdata rules, and
unescape_unicode (now public in draftcraft-dxf).

Opt-in: DRAFTCRAFT_DWG_READER=native|auto|acadrust (default acadrust) and
read_dwg_with(bytes, DwgReader). `auto` uses the native reading only when nothing was left
out; the default stays acadrust until non-graphic objects are carried into foreign data.

Tests (tests/it/dwg_native_read.rs): every mapped type written as DWG 2000–2018, and AutoCAD's
MI-201 files R14–2018 (MI201_CORPUS), read identically to the acadrust path, entity by entity;
damaged files don't panic. Opening a 200k-entity DWG: 0.57–0.62 s native, 2.1–2.2 s acadrust.

roundtrip::handle_limits::dwg_exhausted_handles_are_preserved_or_refused fails as it does on
main (acadrust's DWG 2000 writer); nothing here changes the save path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F67Rsr8EV2T1Jia8QwNpDr
docs/dwg.md: how DraftCraft reads DWG (the codec in draftcraft-dwg, the mapping in io, acadrust
as the fallback), choosing the reader (DRAFTCRAFT_DWG_READER), what the native reader maps and
what it doesn't yet, how to run the fidelity tests (with and without MI201_CORPUS) and the
speed measurements.

crates/io/examples/dwg_bench.rs: builds a drawing of lines, arcs, circles and text (200,000
entities by default), saves it as DWG 2018 and opens it with both readers, timing each and
checking every entity comes out the same. Here: acadrust 2.2–2.3 s, native 0.68–0.77 s,
200,000 of 200,000 identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F67Rsr8EV2T1Jia8QwNpDr
Clippy 1.99's chunks_exact_to_as_chunks flags four chunks_exact calls with a constant size
(UTF-16 text in xrecords, extended data and linetype shapes, and the R2004 page header mask).
as_chunks gives arrays, so the indexing into each chunk goes too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F67Rsr8EV2T1Jia8QwNpDr
@withshalom
withshalom requested a review from SkryxDev November 10, 2026 06:27
@withshalom
withshalom marked this pull request as draft November 10, 2026 06:41
@withshalom
withshalom requested review from SkryxDev and removed request for SkryxDev November 10, 2026 06:41
@SkryxDev
SkryxDev marked this pull request as ready for review November 10, 2026 13:40

@SkryxDev SkryxDev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed: licensing (BlackCAD MIT/Apache-2.0, credited in NOTICE; acadrust unchanged), no panics or unsafe in non-test code, input-sized allocations are bounded, the native reader is opt-in (default stays acadrust), and the io::dwg_worker API is unchanged. CI gate is green, and cargo test -p draftcraft-dwg -p draftcraft-io passes locally on macOS.

Small nit for a follow-up: in crates/io/src/lib.rs the new DwgReader block landed in the middle of read_source's doc comment, so DwgReader's docs begin with read_source's first line.

@SkryxDev
SkryxDev merged commit 22ffdfa into DraftCraft-DEV:main Nov 10, 2026
1 check passed
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