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
| Format | Read | Write | Status |
|---|---|---|---|
| DGN V7 | Readincluding tags | No writer | Implemented, including tags. |
| DGN V8 | Read | Subsetseed-based V8 writer | Local 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
unknownentities) 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.
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(())}import cadkit
doc = cadkit.read("plan.dgn") # V7 or V8print(doc.format, doc.version, [layer["name"] for layer in doc.layers])with open("plan.svg", "w", encoding="utf-8") as f: f.write(doc.to_svg(width=1200))import { readFileSync } from "node:fs";import { read } from "cadkit-wasm/node";
const doc = read(readFileSync("plan.dgn")); // V7 or V8console.log(doc.info().format, doc.layers().map((layer) => layer.name));doc.free();#include <stdio.h>#include "cadkit.h"
int main(void) { cadkit_document *doc = NULL; if (cadkit_read_file("plan.dgn", NULL, &doc) != CADKIT_OK) return 1; /* V7 or V8 */ cadkit_info info; if (cadkit_document_info(doc, &info) == CADKIT_OK) printf("format %d, %llu levels\n", (int)info.format, (unsigned long long)info.layer_count); cadkit_document_free(doc); return 0;}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.
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(())}import cadkit
# 1. A seed: a DGN V8 file whose tables you may hand on, without its drawing content.with open("template.dgn", "rb") as f: seed_bytes, report = cadkit.strip_dgn_seed(f.read(), model_name="Model")print("kept:", report["named_levels"], "named levels,", report["tag_sets"], "tag sets")seed = cadkit.V8Seed(seed_bytes)
# 2. Adapt the drawing to the seed (model, units, 2D to 3D, colours), then write.doc = seed.prepare_document(cadkit.read("plan.dxf"))with open("converted.dgn", "wb") as f: f.write(doc.to_dgn(seed))
# 3. Read the result back with cadkit's own reader.print(cadkit.read("converted.dgn"))From the shell
cadkit strip-seed template.dgn seed.dgn --model-name Model --report > seed-report.jsoncadkit layers seed.dgn # review the level names the seed keepscadkit convert drawing.dgn converted.dgn --seed seed.dgncadkit check-dgn converted.dgn > structure.json # exits 1 when the report has warnings