Skip to main content

cadkit_dgn/
lib.rs

1//! Clean-room reader for MicroStation DGN V7 (ISFF) and V8 (OLE compound file) drawings.
2//!
3//! Contract used by the `cadkit` facade: [`sniff`] and [`read`]. The [`native`] module
4//! exposes the lossless layers (container streams, pages, raw elements, decoded element
5//! data) for advanced users.
6//!
7//! Layering: container ([`cfb`], zlib pages / V7 record stream) -> records
8//! ([`native`]) -> mapping to [`cadkit_core::Document`].
9
10pub mod cfb;
11pub mod native;
12pub use native::structure::{StructureReport, check_structure};
13#[cfg(feature = "write")]
14pub mod writer;
15#[cfg(feature = "write")]
16pub use writer::{
17    DimensionPolicy, SeedPrepareOptions, StripSeedOptions, StripSeedReport, StrippedSeed, V8Seed,
18    WriteOptions, repack_v8, seed_has_raster_template, strip_seed_v8, write_v8, write_v8_with_seed,
19};
20pub mod palette;
21pub mod symbology;
22pub use symbology::Symbology;
23
24mod budget;
25mod cfb_tree;
26#[cfg(test)]
27mod fuzz_tests;
28mod le;
29mod level_names;
30mod map;
31mod text;
32mod zlib;
33
34use cadkit_core::{Document, Error, ReadOptions, Result, Summary};
35
36/// Returns true when `bytes` look like a DGN file: a V7 design file header, or a compound
37/// file whose root storage holds the `Dgn~H` stream (other OLE files such as `.doc` or
38/// `.xls` are rejected). Reads only headers and the compound file directory.
39pub fn sniff(bytes: &[u8]) -> bool {
40    native::v7::sniff(bytes) || native::v8::sniff(bytes)
41}
42
43/// Inspects the DGN V7 TCB or V8 summary/model header without decoding graphic elements.
44pub fn inspect(bytes: &[u8], options: &ReadOptions) -> Result<Summary> {
45    if native::v7::sniff(bytes) {
46        return native::v7::inspect(bytes, options);
47    }
48    if bytes.starts_with(&cfb::SIGNATURE) {
49        return native::v8::inspect(bytes, options);
50    }
51    Err(Error::UnknownFormat)
52}
53
54/// Reads a whole DGN V7 or V8 file into the format-neutral model.
55///
56/// Coordinates are converted from storage units (UOR) to the master units of the first
57/// model: `(uor - global_origin) / uor_per_master`. Recoverable problems are reported in
58/// [`Document::warnings`]; `Err` is returned only when nothing useful can be read.
59pub fn read(bytes: &[u8], options: &ReadOptions) -> Result<Document> {
60    if native::v7::sniff(bytes) {
61        return map::v7::read(bytes, options);
62    }
63    if bytes.starts_with(&cfb::SIGNATURE) {
64        return map::v8::read(bytes, options);
65    }
66    Err(Error::UnknownFormat)
67}