Skip to main content

Crate ezdwg_core

Crate ezdwg_core 

Source
Expand description

§ezdwg-core

Pure Rust DWG reader and native AC1015 (AutoCAD R2000) writer, used by the ezdwg Python package. This crate has no Python or PyO3 dependency.

§Install

cargo add ezdwg-core

§Read a DWG file

The decoder supports R13 / AC1012, R14 / AC1014, R2000 / AC1015, R2004 / AC1018, R2007 / AC1021, R2010 / AC1024, R2013 / AC1027 and R2018 / AC1032. Entity coverage varies by version; this is not a complete implementation of the DWG format.

use ezdwg_core::core::config::ParseConfig;
use ezdwg_core::dwg::{decoder::Decoder, file_open::read_file};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let bytes = read_file("drawing.dwg")?;
    let decoder = Decoder::new(&bytes, ParseConfig::default())?;
    decoder.ensure_supported()?;

    println!("DWG version: {}", decoder.version().as_str());
    let index = decoder.build_object_index()?;
    println!("Objects: {}", index.len());
    for object in &index.objects {
        let record = decoder.parse_object_record(object.offset)?;
        println!("Handle {:?}: {} bytes", object.handle, record.size);
    }
    Ok(())
}

This is a low-level API: Decoder exposes sections, object records, header variables and class maps. The entities, objects and bit modules provide version-specific decoding primitives. The Python Document / Layout query and DXF conversion APIs are separate from this crate.

§Write an AC1015 drawing

Build a WriterDocument and serialize it to bytes. Only AC1015 output is currently supported; the writer validates the supported entity data before encoding it.

use ezdwg_core::core::config::ParseConfig;
use ezdwg_core::dwg::decoder::Decoder;
use ezdwg_core::writer::{
    r2000::write_document, LineEntity, WriterConfig, WriterDocument, WriterEntity,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut drawing = WriterDocument::default();
    drawing.modelspace.push(WriterEntity::Line(LineEntity {
        common: Default::default(),
        start: (0.0, 0.0, 0.0),
        end: (10.0, 5.0, 0.0),
    }));

    let bytes = write_document(&drawing, &WriterConfig::default())?;
    // Save with std::fs::write("created.dwg", &bytes)? when needed.
    let decoder = Decoder::new(&bytes, ParseConfig::default())?;
    assert_eq!(decoder.version().as_str(), "AC1015");
    assert!(!decoder.build_object_index()?.is_empty());
    Ok(())
}

For large drawings, feed the entities to the incremental encoder instead of building a WriterDocument: each entity is written to its object record as it arrives and only the records stay in memory. The encoder needs the largest entity handle up front so it can number the tables above it.

use ezdwg_core::writer::{
    DocumentEncoder, DocumentTables, LayoutId, LineEntity, WriterConfig, WriterEntity,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut encoder = DocumentEncoder::new(DocumentTables::default(), WriterConfig::default(), 1_000)?;
    for handle in 1..=1_000u64 {
        let mut line = LineEntity {
            start: (0.0, handle as f64, 0.0),
            end: (10.0, handle as f64, 0.0),
            ..Default::default()
        };
        line.common.handle = Some(handle);
        encoder.push(LayoutId::Model, WriterEntity::Line(line))?;
    }
    let bytes = encoder.finish()?.into_bytes()?;
    assert_eq!(&bytes[..6], b"AC1015");
    Ok(())
}

The Rust API is evolving and follows the shared ezdwg release version. See the API reference and the project documentation for details. The repository’s full regression suite uses DWG fixtures outside this crate; run it from a source checkout. The packaged README examples can be tested independently with cargo test --doc.

§License

MIT. See the bundled LICENSE file or the repository license.

Modules§

bit
container
core
dwg
embedded_text
Byte-level heuristics for recovering text embedded in undecoded (custom / proxy) object payloads.
entities
io
objects
writer