Skip to content

← Blog

Why a CAD program with native DWG in Rust

· Engineering

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:

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

Dependency diagram: the lince binary uses lince-comandos, lince-mcp and lince-modelado; lince-comandos uses lince-geo, lince-ui, lince-licencia and lince-nucleo; lince-mcp uses lince-api, which uses lince-lisp; lince-modelado uses truck and lince-nucleo; lince-pyo3 uses lince-nucleo; lince-nucleo and lince-lisp use acadrust.
The main dependencies between crates, taken from their 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:

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

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.