Skip to content
cadkithome

Convert DXF to SVG

cadkit renders one model of a document as a standalone SVG. The reader does not matter: the same code converts DWG, DGN and CityGML files, because every reader produces the same format-neutral document.

src/main.rs
use cadkit::SvgOptions;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = cadkit::open("plan.dxf")?;
let options = SvgOptions::new()
.with_model(0) // model space; paper layouts follow
.with_width_px(1600.0) // the height follows the drawing's aspect ratio
.with_stroke_px(1.0)
.with_background(None::<String>) // transparent
.with_text(true);
// Hidden and frozen layers are left out; cadkit::to_svg_all_layers includes them.
std::fs::write("plan.svg", cadkit::to_svg(&doc, &options))?;
Ok(())
}
shell
cadkit convert plan.dxf plan.svg --width 1600
cadkit convert plan.dxf walls.svg --width 1600 --layer WALLS # one layer
cadkit convert plan.dxf layout.svg --model 1 # a paper layout
cadkit convert - plan.svg --explode-inserts --units mm < plan.dxf # from stdin
  • The viewBox is in drawing units, with the Y axis flipped so the drawing is not upside down: SVG Y is the negated drawing Y. The width you choose sets the pixel size; the height follows the drawing’s aspect ratio.
  • Each layer is a <g data-layer="NAME"> group, so a viewer can show and hide layers with CSS or script. Strokes use vector-effect="non-scaling-stroke", so their width stays in pixels when the SVG is scaled.
  • Layers that are hidden or frozen in the drawing are left out unless you ask for them (include_hidden_layers, includeHiddenLayers, cadkit::to_svg_all_layers).
  • Block references are expanded, and text is drawn as SVG text, unless you turn either off (text, expand_blocks).

Models are listed in doc.models (Rust and Python) or doc.info().models (JavaScript); for DWG and DXF, index 0 is model space and the paper layouts follow.

to_svg builds the whole SVG in memory. For files from other people, use the bounded variant (cadkit::to_svg_bounded in Rust, toSvgBounded in JavaScript): it either returns a complete SVG within the byte budget you give or fails with a limit error, never a truncated drawing. See Handling untrusted files.