Skip to content
cadkithome

Write DGN V8 from a seed

cadkit writes DGN V8 into a seed: an existing single-model V8 file whose tables (levels, fonts, colour table, text styles, views, settings, units) the output inherits. cadkit ships no seed of its own, because the file header and model header that every seed needs hold fields whose meaning is not decoded and whose values differ between the writers of the public samples. You make a seed from a drawing whose tables you may hand on, such as your organisation’s template.

  1. Strip a seed. strip-seed copies the template without its model’s drawing content and without the document properties that name people or organisations (author, company, manager, title, comments, thumbnail), and rebuilds the container so no removed byte survives. Names in the kept tables remain: level names, tag sets, cell and font names and the model description. The report counts them; review them before you hand the seed on. The result is not anonymous.
  2. Prepare the document. The writer needs one model whose dimension (2D or 3D) and units match the seed, colours that exist in the seed’s palette, and measured advances for centred or right-aligned text. In Rust and Python, V8Seed::prepare_document / seed.prepare_document does this by default: it selects the first design model, converts units, promotes a 2D drawing to a 3D seed, expands block references and normalises symbology (off-palette colours to the nearest palette index, ByBlock, millimetre lineweights, unknown fonts), polyline widths, multiline text, leaders, hatches and text alignment. SeedPrepareOptions turns each part off. The JavaScript and C APIs have no preparation step; their callers supply a matching document.
  3. Write and check. to_dgn writes the model into a copy of the seed. check_structure (Rust, and cadkit check-dgn) reports the container and record structure with stable warning codes.
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(())
}

The JavaScript version shows by hand what preparation involves. Two of its steps are placeholders you replace: every entity gets palette index 0, and text advances use the same character-count estimate as prepare_document. Measure text with the fonts you use for exact placement.

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

The CLI strips, checks and repacks seeds, and convert --seed writes a document as read, without preparation. A DXF or DWG file with paper layouts fails with DGN writer requires exactly one source model, and a 2D drawing into a 3D seed with source and seed model dimensions differ. For those conversions use Python or Rust.

New elements: lines, line strings, closed shapes, circles, ellipses, arcs, left, centre and right text at baseline, middle or top, complex chains and shapes, and ordinary cell groups. Bulged polylines become exact line and arc components; faces and meshes become shapes and cells; polygons with holes become a cell with hole-marked shapes. Levels, tag sets and tag values are writable, and strings default to UTF-16 (codepage selects a legacy 8-bit page).

Not supported by the writer yet, and rejected with an Unsupported error instead of being dropped: shared-cell instances, edited raster attachments, splines, hatches, dimensions, fitted or oblique text and polylines with a non-zero width. prepare_document normalises hatch geometry and polyline widths (with multiline text and leaders) for the writer; convert the others explicitly before writing. Colours must exist in the seed palette unless you give dgn.color_index yourself, and ByBlock colours need resolving first.

Writing into a seed that already holds drawing content requires clear_seed_model (CLI: --clear-seed-model). Rewriting an existing DGN with its own bytes as the seed keeps its unchanged raster attachments. The details, with the evidence for each layout, are in the DGN format notes.