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.
The same task in four languages
Section titled “The same task in four languages”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(())}import cadkit
doc = cadkit.read("plan.dxf")svg = doc.to_svg( model=0, # model space; paper layouts follow width=1600, # pixels; the height follows the drawing's aspect ratio stroke=1.0, # stroke width in pixels background=None, # transparent; or a CSS colour such as "#ffffff" text=True, include_hidden_layers=False,)with open("plan.svg", "w", encoding="utf-8") as f: f.write(svg)import { readFileSync, writeFileSync } from "node:fs";import { read } from "cadkit-wasm/node";
const doc = read(readFileSync("plan.dxf"));const svg = doc.toSvg({ model: 0, // model space; paper layouts follow widthPx: 1600, // the height follows the drawing's aspect ratio strokePx: 1, background: null, // transparent; or a CSS colour such as "#ffffff" text: true, includeHiddenLayers: false,});writeFileSync("plan.svg", svg);doc.free();#include <stdio.h>#include "cadkit.h"
int main(void) { cadkit_document *doc = NULL; if (cadkit_read_file("plan.dxf", NULL, &doc) != CADKIT_OK) { fprintf(stderr, "%s\n", cadkit_last_error_message()); return 1; } cadkit_svg_options options; cadkit_svg_options_init(&options); /* always start from the defaults */ options.model_index = 0; /* model space; paper layouts follow */ options.width_px = 1600; options.stroke_px = 1.0; options.background = NULL; /* transparent; or a CSS colour such as "#ffffff" */ options.text = 1;
char *svg = NULL; int rc = 1; if (cadkit_document_to_svg(doc, &options, &svg) == CADKIT_OK) { FILE *out = fopen("plan.svg", "wb"); if (out) { fputs(svg, out); fclose(out); rc = 0; } cadkit_string_free(svg); } else { fprintf(stderr, "%s\n", cadkit_last_error_message()); } cadkit_document_free(doc); return rc;}From the shell
Section titled “From the shell”cadkit convert plan.dxf plan.svg --width 1600cadkit convert plan.dxf walls.svg --width 1600 --layer WALLS # one layercadkit convert plan.dxf layout.svg --model 1 # a paper layoutcadkit convert - plan.svg --explode-inserts --units mm < plan.dxf # from stdinWhat the SVG contains
Section titled “What the SVG contains”- The
viewBoxis in drawing units, with the Y axis flipped so the drawing is not upside down: SVG Y is the negated drawing Y. Thewidthyou 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 usevector-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.
Large or untrusted drawings
Section titled “Large or untrusted drawings”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.