Repository navigation
Native DWG Reader & Writer - #7
Merged
Merged
Conversation
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
marked this pull request as draft
November 10, 2026 06:41
SkryxDev
marked this pull request as ready for review
November 10, 2026 13:40
SkryxDev
approved these changes
Nov 10, 2026
SkryxDev
left a comment
Member
There was a problem hiding this comment.
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.
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.
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)
Changes to existing code
These are the only edits outside the new files, so they are the part to review closely:
Choosing the reader
DRAFTCRAFT_DWG_READER=acadrust|native|auto, or io::read_dwg_with(bytes, DwgReader).
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:
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
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)
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