Skip to content
cadkithome
Menu

Format · DGN

DGN V7 and V8 for Rust, Python and JavaScript

Read DGN V7 and V8 and write seed-based DGN V8 with cadkit in Rust, Python, JavaScript and C: levels, tags, cells, raster attachments and acceptance status.

What is supported

FormatReadWriteStatus
DGN V7Readincluding tagsNo writerImplemented, including tags.
DGN V8ReadSubsetseed-based V8 writerLocal tests pass; external application acceptance is pending. Writing needs a seed file.

Supported

  • DGN V8 (compound file) and DGN V7 (ISFF) reading, with every model of a V8 file, 2D and 3D
  • Levels as layers, the colour table, master and sub units and the font table
  • Lines, line and point strings, shapes, arcs, ellipses, curves, B-splines, text and text nodes, complex chains and shapes, cells and shared cells
  • Tag sets and tags attached to their elements (V8); raster attachments (V8)
  • Seed-based DGN V8 writing of a documented element subset, seed stripping and a structural report (check-dgn)

Not supported yet

  • Writing DGN V7
  • A built-in blank seed: writing needs a seed made from a file of your own
  • Writing shared-cell instances, edited raster attachments, splines, hatches, dimensions and fitted or oblique text
  • Extended colours (index above 255): the RGB value is not decoded, so these elements read with their layer colour
  • Dimensions and multi-lines (kept as unknown entities) and 3D surface and solid headers (their components are read as a plain group)
  • External application acceptance of written files: not recorded yet

Read a DGN file

V7 and V8 files open with the same call; the format is detected from the bytes. Levels appear as layers.

src/main.rs
fn main() -> Result<(), cadkit::Error> {
let doc = cadkit::open("plan.dgn")?; // V7 or V8
println!("{:?} {} levels", doc.source.format, doc.layers.len());
std::fs::write("plan.svg", cadkit::to_svg(&doc, &cadkit::SvgOptions::new()))?;
Ok(())
}

Write DGN V8 from a seed

Strip a seed from a template of your own, adapt the drawing to it and write. The guide explains each step, the JavaScript and C variants and the writer's limits. No external acceptance is claimed yet.

src/main.rs
use cadkit::ReadOptions;
use cadkit::dgn::{SeedPrepareOptions, StripSeedOptions, V8Seed, WriteOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. A seed: a DGN V8 file whose tables you may hand on, without its drawing content.
let template = std::fs::read("template.dgn")?;
let options = StripSeedOptions::new().with_model_name(Some("Model"));
let stripped = cadkit::strip_dgn_seed(&template, &options, &ReadOptions::default())?;
let report = &stripped.report;
println!("kept: {} named levels, {} tag sets", report.named_levels, report.tag_sets);
let seed = V8Seed::from_bytes(&stripped.bytes)?;
// 2. Adapt the drawing to the seed (model, units, 2D to 3D, colours), then write.
let doc = seed.prepare_document(&cadkit::open("plan.dxf")?, &SeedPrepareOptions::new())?;
let bytes = cadkit::to_dgn_with_seed(&doc, &seed, &WriteOptions::new())?;
std::fs::write("converted.dgn", &bytes)?;
// 3. Check the container and records with cadkit's own structural report.
let structure = cadkit::dgn::check_structure(&bytes, &ReadOptions::default())?;
println!("structure warnings: {}", structure.warnings.len());
Ok(())
}

From the shell

shell
cadkit strip-seed template.dgn seed.dgn --model-name Model --report > seed-report.json
cadkit layers seed.dgn # review the level names the seed keeps
cadkit convert drawing.dgn converted.dgn --seed seed.dgn
cadkit check-dgn converted.dgn > structure.json # exits 1 when the report has warnings

Reference