Why a CAD program with native DWG in Rust
LinceCAD is a 2D and 3D CAD program that opens, edits and saves DWG and DXF on its own, without converting them. It is written in Rust, and so is the library that reads and writes the format. This post explains why, what Rust gave us and what it costs. The figures were measured on the repository on the day of publication.
The problem: a DWG is a binary you didn't write
DWG is a compressed binary format with no official public specification. A file holds sections, object maps with offsets, lengths declared inside the file itself and strings whose encoding depends on the version. A CAD program opens DWG files that arrive by email, were written by another program or by a release from twenty years ago, and are sometimes damaged. Every length and every offset is untrusted input.
In C or C++, a bogus length can end in an out-of-bounds read: a crash or, at worst, a security hole exploitable with a crafted file. In safe Rust the same access is a controlled panic, not corrupted memory. Rust does not make the reader correct, but it changes the kind of failure: from undefined behaviour to an error you can catch.
How LinceCAD opens a DWG
The format is read and written by acadrust, a pure-Rust DWG and
DXF library by Hakan AK, licensed under MPL-2.0. LinceCAD uses a fork with its own fixes, whose source is published
as the licence requires (cadfv branch). It opens DWG
from R2.5 to 2018, the format used by AutoCAD 2018 to 2027, and DXF from R12 to 2018. On top of it, the
lince-nucleo crate adds three defences:
- A cap on decompressed memory. A damaged file can declare gigabytes of data. The reader stops at 64 times the file size (at least 512 MB) and returns an error instead of exhausting memory.
- Caught panics. The whole read runs inside
std::panic::catch_unwind. If the reader panics, the user gets a "could not open" message and the drawing already open stays intact. - Recovery mode.
RECOVERreads whatever it can from a damaged file in tolerant mode and applies acadrust's safe repairs, with a report.
There is also pure-Rust fuzzing: a test takes healthy DWG and DXF files, mutates them (flipped bits, truncation, inserted bytes, huge lengths) and checks that opening each copy always ends in success or an error, within a time limit, without hanging or killing the process. Caught panics are listed by location: they are reader bugs that get fixed in the fork.
What about unsafe? Our own code has 31 lines, all of them system calls (clipboard, printers, Windows
named pipes, libc signals on Linux); none in DWG reading. In the fork, only in the file
mmap, the free-memory query and a test-only allocator.
Performance without a garbage collector
A real drawing can hold hundreds of thousands of entities. Rust gives control over memory layout without collector pauses: the document is compacted on open, tessellation is spread over several cores with rayon, selection uses an R-tree spatial index (rstar) and the scene is redrawn on the GPU with wgpu, with a CPU fallback. Undo stores the differences of each step, not copies of the drawing. We give no speed figures here: they depend heavily on the drawing and the machine.
One codebase for Windows and Linux
The UI is built with eframe/egui, in immediate mode, on top of winit
and wgpu (DirectX 12, Vulkan or OpenGL depending on the machine). The same code builds for Windows x64 and Linux
x86_64. What changes per platform is contained: measured today, 54 platform cfg attributes in about
195,000 lines of our own Rust (comments and tests included). For example, the local API uses a named pipe on Windows
and a Unix socket on Linux, and on Linux SIGTERM and SIGHUP save a recovery copy before exiting. A script builds a
commit from scratch on Linux (WSL2 or native) and runs Clippy and the tests. It ships as an .exe
installer on Windows and as .deb, .tar.gz and Snap on Linux:
how to open a DWG on Linux.
Building for release
The repository is a Cargo workspace, split into crates to build in parallel and rebuild only what changes:
lince-idioma → lince-nucleo → lince-comandos → the lince
binary. Tests use incremental release; what ships comes from a distribucion profile with
LTO, a single codegen unit and stripped symbols, much slower to build. The result is a single executable: the
installed lince.exe on Windows is 62.6 MB.
LinceCAD's crates
Cargo.toml files (not all of them). (*) lince-idioma is used by almost all.There is the binary, 11 crates of our own (336 .rs files and about 1,190 #[test]
functions) and the acadrust fork (about 232,000 lines and 1,710 more tests). The names are Spanish:
lince-nucleo(core): the document without a window: DWG and DXF I/O, geometry, text, scene, indexes and undo.lince-comandos: the commands and their dialogs.lince-modelado: solids with truck, a B-rep kernel in Rust, translated to ACIS so they are stored in the DWG.lince-geo: coordinate systems and GIS formats (SHP, KML/KMZ, GeoJSON).lince-lisp: an interpreter for a subset of AutoLISP.lince-apiandlince-mcp: the local JSON API and an MCP server on top of it, to drive the CAD from an AI.lince-pyo3: thelince_nucleoPython module (automate DWG with Python); not on PyPI yet.lince-ui,lince-idiomaandlince-licencia: theme, languages and licence.
The ecosystem: what we didn't have to write
Much of what a CAD program needs already existed as crates: spade for
the constrained Delaunay triangulation of the terrain model, proj4rs
for reprojection, las for LAS/LAZ point clouds,
hayro to read PDF, pdf-writer
to plot to PDF and ocrs for OCR, all in Rust. The trade-off: the
Cargo.lock lists 645 packages (our own included, for all platforms), third-party code that has to be
watched; their licences are collected with cargo-about into the notices shipped with the program. That
is why some pieces are hand-written: the MCP server is JSON-RPC 2.0 in a few hundred lines, without the async runtime
the SDK required.
How we know the DWG comes out right
Compiling does not mean AutoCAD will open the file. There are end-to-end tests per command family: LinceCAD draws
from a script, saves to DWG, and another CAD program, with no window, opens it and runs
AUDIT; the target is zero errors. A different CAD program is used as a second, independent reader, with the same
AUDIT. There are also round-trip tests, entity by entity, in DWG 2018 and 2000 and in DXF. And saving
never writes over the original: it writes alongside, flushes to disk, keeps the previous file as .bak
and only then replaces it.
What Rust doesn't solve
- Compile times. The most visible daily cost, and the reason for the crates and profiles.
- Panics are still bugs. They are caught in every command step and every frame: the operation is undone, a report is written and the program carries on; after three failed frames in a row it saves the drawings for recovery at the next start and closes. But they still have to be fixed.
- The borrow checker and a UI with shared state. The drawing is only touched from the window thread; the API and the MCP server send each request through a channel.
- The format is still closed. Compatibility is earned by reading real drawings and comparing with AutoCAD, not by the language.
LinceCAD 1.0 is available for Windows and Linux, with a free 30-day trial and a perpetual licence from €200 (launch offer). It is commercial, closed-source software; the acadrust fork is published under MPL-2.0. Download · See pricing · License and third-party notices.