jflib-dxf 0.2.2

DXF import (ASCII): tables, blocks and entities of model space, without a GPU
Documentation
  • Coverage
  • 41.23%
    167 out of 405 items documented0 out of 67 items with examples
  • Size
  • Source code size: 158.7 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 12.1 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • JFLibrary/JFLib
    3 2 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Graphsto

jflib-dxf

DXF import for JFLib. It reads AutoCAD's ASCII exchange format, from R12 to 2018, into a plain data model that needs no GPU: the header's variables, the tables of layers, linetypes, text styles, block records and dimension styles, blocks with their entities, and the entities of the drawing. A CAD view, a converter or a test reads the same model; jflib-render2d maps it into a scene as it is read (render2d::dxf). It is plain Rust, without the standard library and without a line of unsafe code.

How it is used

use jflib::{dxf, plat};

// a small file in memory: everything kept
let Some(bytes) = plat::read_file("plan.dxf") else { return };
let doc = match dxf::load(&bytes, &dxf::Limits::DEFAULT) {
    Ok(doc) => doc,
    Err(_) => return,                    // Binary, Invalid or Limit, with a text
};
for e in &doc.entities {
    let layer = doc.name(e.common.layer);
    match &e.kind {
        dxf::Kind::Line { a, b } => { /* ... */ }
        dxf::Kind::Polyline(p) => { /* vertices with bulges and widths */ }
        _ => {}
    }
}

// a large one piece by piece: the sink gets the header, the tables, each block and each entity
struct Count(u64);
impl dxf::Sink for Count {
    fn entity(&mut self, _e: dxf::Entity, _names: &dxf::Names) {
        self.0 += 1;
    }
}
let Some(file) = plat::File::open("city.dxf", plat::FO_READ) else { return };
let mut at = 0u64;
let mut source = |buf: &mut [u8]| {
    let n = file.read_at(at, buf).unwrap_or(0);
    at += n as u64;
    n
};
let mut count = Count(0);
let _ = dxf::read(&mut source, &dxf::Limits::DEFAULT, &mut count);

The reader knows LINE, POINT, CIRCLE, ARC, ELLIPSE, SPLINE (also HELIX), LWPOLYLINE and POLYLINE with their vertices (bulges, widths, 3D polylines, polyface and polygon meshes), TEXT, ATTRIB, ATTDEF, MTEXT (with R2018's columns), INSERT with MINSERT's array and its attributes, HATCH (paths of lines, arcs, elliptic arcs, splines and bulged polylines; the pattern's line families; solid and gradient), SOLID, TRACE, 3DFACE, DIMENSION and ACAD_TABLE (through their anonymous blocks), LEADER, WIPEOUT, RAY and XLINE. Other kinds are counted by name. Strings before AutoCAD 2007 are decoded from the header's code page (the Western and Cyrillic pages, the DOS pages and the East Asian ones: Shift_JIS, GBK, EUC-KR, Big5), later ones as UTF-8, and \U+XXXX and \M+NXXXX everywhere. mtext::parse turns MTEXT's inline format codes into paragraphs of formatted runs, specials TEXT's %% codes into runs with their underline, overline and strike-through. aci_rgb gives AutoCAD's colour index, font_substitute which font of JFLib's repository stands in for a text style's font: SHX fonts belong to Autodesk, and JFLib carries no Microsoft fonts.

Files are untrusted input

Limits bounds the length of a line, the pairs, bytes and points of one entity, the entities, points, blocks, names and table entries that are kept. A count in a file (vertices, edges, knots) is never used to allocate: lists grow as their items arrive. Nothing recurses. A value that is not a number where one belongs is skipped with a note, a group code that is not a number ends the file with Error::Invalid, a binary DXF is Error::Binary. A file that ends early gives what was read.

How to get it

Like all of JFLib, the crate comes through the one dependency jflib, as jflib::dxf. All of JFLib's crates share one version and are released together. It runs on every platform JFLib supports.

What's new in 0.2.2

The crate is new: it reads DXF files in the ASCII format of R12 to 2018 into a model without a GPU, with the East Asian code pages. render2d::dxf draws what it reads. Everything is in the changelog.

Documentation

License

MIT or Apache-2.0, at your choice.