acadrust 0.2.0
A pure Rust library for reading and writing CAD files (DXF and DWG).
acadrust provides comprehensive support for the DXF and DWG file formats with a focus on correctness, type safety, and completeness. Inspired by ACadSharp, this library brings full-featured CAD file manipulation to the Rust ecosystem.
β¨ Features
Core Capabilities
- π Read & Write DXF β Full support for both ASCII and Binary DXF formats
- π Read & Write DWG β Native DWG binary input/output for R13 through R2018 (8 versions)
- π Type Safe β Leverages Rust's type system with strongly-typed entities, tables, and objects
- π Encoding Support β Automatic code page detection and character encoding for pre-2007 files (~40 code pages via
encoding_rs) - π‘οΈ Failsafe Mode β Optional error-tolerant parsing that collects diagnostics instead of aborting
- π Notifications β Structured diagnostic system reporting unsupported elements, warnings, and errors
- π Handle Resolution β Automatic owner handle assignment and handle tracking after read
- β Unknown Entity Preservation β Unrecognized entity types are preserved as
UnknownEntitywith common fields intact
File Version Support
| Version Code | AutoCAD Version | DXF Read | DXF Write | DWG Read | DWG Write |
|---|---|---|---|---|---|
| AC1009 | R12 | β | β | β | β |
| AC1012 | R13 | β | β | β | β |
| AC1014 | R14 | β | β | β | β |
| AC1015 | 2000 | β | β | β | β |
| AC1018 | 2004 | β | β | β | β |
| AC1021 | 2007 (UTF-8) | β | β | β | β |
| AC1024 | 2010 | β | β | β | β |
| AC1027 | 2013 | β | β | β | β |
| AC1032 | 2018+ | β | β | β | β |
Supported Entity Types (41)
Basic Entities
- Point β Single point in 3D space
- Line β Line segment between two points
- Circle β Circle defined by center and radius
- Arc β Circular arc with start and end angles
- Ellipse β Ellipse or elliptical arc
Polylines
- Polyline β 2D polyline with optional bulge
- Polyline2D β Heavy 2D polyline with vertex entities
- Polyline3D β 3D polyline
- LwPolyline β Lightweight polyline (optimized 2D)
- PolyfaceMesh β 3D mesh defined by vertices and faces
- PolygonMesh β 3D polygon surface mesh (MΓN grid)
Text & Annotations
- Text β Single-line text
- MText β Multi-line formatted text
- AttributeDefinition β Block attribute template
- AttributeEntity β Block attribute instance
- Tolerance β Geometric tolerancing symbols
Dimensions & Leaders
- Dimension β Various dimension types (linear, angular, radial, etc.)
- Leader β Leader line with annotation
- MultiLeader β Modern multi-leader with advanced formatting
- Table β Table with cells, rows, and columns
Complex Entities
- Spline β NURBS curve
- Hatch β Filled region with pattern
- Solid β 2D filled polygon
- Face3D β 3D triangular/quadrilateral face
- Mesh β Subdivision mesh surface
Blocks & References
- Block / BlockEnd β Block definition markers
- Insert β Block reference (instance)
- Seqend β Sequence end marker for complex entities
Construction Geometry
- Ray β Semi-infinite line
- XLine β Infinite construction line
Advanced Entities
- Viewport β Paper space viewport
- RasterImage β Embedded or linked raster image
- Solid3D β 3D solid with ACIS data
- Region β 2D region with ACIS data
- Body β 3D body with ACIS data
- MLine β Multi-line with style
- Wipeout β Masking region
- Shape β Shape reference
- Underlay β PDF/DWF/DGN underlay reference
- Ole2Frame β OLE 2.0 embedded object
- UnknownEntity β Preserves common fields for unrecognized entity types
Table System
Complete support for all standard tables:
| Table | Description |
|---|---|
| Layer | Drawing layers with color, linetype, and visibility |
| LineType | Line patterns and dash definitions |
| TextStyle | Font and text formatting settings |
| DimStyle | Dimension appearance and behavior |
| BlockRecord | Block definition registry |
| AppId | Application identifier registry |
| View | Named view configurations |
| VPort | Viewport configurations |
| UCS | User coordinate system definitions |
Objects (Non-Graphical Elements)
- Dictionary / DictionaryWithDefault β Key-value storage for objects
- DictionaryVariable β Named variable in a dictionary
- Group β Named entity collections
- Layout β Model/paper space layout definitions
- MLineStyle β Multi-line style definitions
- MultiLeaderStyle β Multi-leader style definitions
- TableStyle β Table formatting styles
- PlotSettings β Print/plot configurations
- Scale β Annotation scale definitions
- ImageDefinition / ImageDefinitionReactor β Raster image definitions and reactors
- XRecord β Extended data records
- SortEntitiesTable β Entity draw order
- VisualStyle β 3D visual style definitions
- Material β Material definitions
- GeoData β Geolocation data
- SpatialFilter β Spatial clipping filter
- RasterVariables β Raster display settings
- BookColor β Color book (DBCOLOR) entries
- PlaceHolder β Placeholder objects
- WipeoutVariables β Wipeout display settings
CLASSES Section
Full support for the CLASSES section β reading, storing, and writing DXF class definitions with all standard fields (class name, DXF name, application name, proxy flags, instance count).
Extended Data (XData)
Full support for application-specific extended data:
- String, binary, and numeric values
- 3D points, directions, and displacements
- Layer references and database handles
- Nested data structures with control strings
Reactors & Extension Dictionaries
Full support for entity/object reactor chains (group code 102 {ACAD_REACTORS}) and extension dictionaries ({ACAD_XDICTIONARY}), read and written for all entity and object types.
π¦ Installation
Add acadrust to your Cargo.toml:
[]
= "0.1.8"
Or install via cargo:
π Quick Start
Reading a DXF File
use ;
Reading with Failsafe Mode
use ;
use DxfReaderConfiguration;
Writing a DXF File
use ;
Reading a DWG File
use DwgReader;
Writing a DWG File
use ;
use *;
use ;
Working with Layers
use ;
Creating Complex Entities
use ;
ποΈ Architecture
acadrust uses a trait-based design for maximum flexibility and extensibility:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CadDocument β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β βββββββββββββββ ββββββββββββββββ βββββββββββββββββββββββ β
β β Header β β Tables β β Entities β β
β β Variables β β β β β β
β βββββββββββββββ β - Layers β β - Lines, Circles β β
β β - LineTypes β β - Polylines, Arcs β β
β βββββββββββββββ β - Styles β β - Text, Dimensions β β
β β Blocks β β - DimStyles β β - Hatches, Splines β β
β β β β - VPorts β β - 3D, Mesh, Images β β
β βββββββββββββββ ββββββββββββββββ βββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββ ββββββββββββββββββββ β
β β Objects β β Notifications β β
β β Dictionaries, Groups, Styles, β β Warnings, Errorsβ β
β β Layouts, XRecords, Materials β β Diagnostics β β
β ββββββββββββββββββββββββββββββββββββ ββββββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β Classes ββ
β β DXF class definitions (name, app, proxy flags, count) ββ
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Core Traits
| Trait | Purpose |
|---|---|
Entity |
Base trait for all graphical entities |
TableEntry |
Base trait for table entries (layers, styles, etc.) |
CadObject |
Common interface for all CAD objects |
Key Types
| Type | Description |
|---|---|
CadDocument |
Central document container |
DxfReader |
DXF file reader (ASCII and binary) |
DxfWriter |
DXF file writer |
DwgReader |
DWG binary file reader |
DwgWriter |
DWG binary file writer |
DxfReaderConfiguration |
Reader options (failsafe mode) |
Handle |
Unique object identifier |
Vector2 / Vector3 |
2D and 3D coordinate types |
Color |
CAD color (indexed or true color) |
LineWeight |
Line thickness enumeration |
Transform |
Transformation matrices |
NotificationCollection |
Parse diagnostics and warnings |
βοΈ Dependencies
acadrust is built on a foundation of high-quality Rust crates:
| Crate | Purpose |
|---|---|
thiserror / anyhow |
Error handling |
nom |
Parser combinators for binary parsing |
byteorder |
Cross-platform byte order handling |
flate2 |
Compression/decompression |
nalgebra |
Linear algebra and transformations |
indexmap |
Ordered hash maps |
rayon |
Parallel iterators |
encoding_rs |
Character encoding support |
bitflags |
Type-safe bitflags |
π§ͺ Testing
Run the test suite:
# Run all tests
# Run with output
# Run specific test
Generate DWG Samples
Generate a full set of DWG test files for verification in AutoCAD, IntelliCAD, or BricsCAD:
This produces 40 DWG files in target/dwg_samples/:
| Folder | Contents |
|---|---|
individual/ |
One file per entity type (32 files) β line, circle, arc, spline, hatch, dimensions, mesh, etc. |
by_version/ |
Showcase file per DWG version (5 files) β R2000 through R2018 |
stress/ |
Larger files for stress testing (3 files) β 200β500 entities each |
Generate a comprehensive matrix of every entity type Γ every DWG version (R13βR2018):
This produces 216 DWG files in target/entities_dwg/<VERSION>/ β 27 entity types across 8 versions.
Run benchmarks:
οΈ Roadmap
- ASCII DXF read/write
- Binary DXF read/write
- Full entity, table, and object coverage
- CLASSES section support
- Character encoding / code page support
- Failsafe (error-tolerant) reading mode
- Unknown entity preservation
- DWG binary write (R13, R14, R2000, R2004, R2007, R2010, R2013, R2018)
- DWG binary read (R13 through R2018)
- Geometric operations (offset, trim, extend)
- SVG/PDF export
- Spatial indexing for large drawings
π License
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.
π Acknowledgments
- ACadSharp - The C# library that inspired this project
- The Rust community for excellent tooling and libraries
π Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions