Inspect a drawing and its layers
Inspection comes in two depths. A header inspection reads only what the file declares about itself: format, version, units, code page and, where the format records it, whether the drawing is 3D and which application wrote it. It does not decode entity streams, so it is cheap even for large files. A full read gives you every layer, block, model, entity and warning.
Header, layers, counts and warnings
Section titled “Header, layers, counts and warnings”Each program takes the file as its first argument and works the same for DWG, DGN and DXF.
use std::collections::BTreeMap;
fn main() -> Result<(), Box<dyn std::error::Error>> { let path = std::env::args().nth(1).ok_or("usage: inspect_drawing <file>")?; let header = cadkit::inspect_file(&path)?; // header only: no entity decoding println!("{:?} {} {:?}", header.format, header.version, header.units);
let doc = cadkit::open(&path)?; let mut per_layer: BTreeMap<&str, usize> = BTreeMap::new(); for entity in doc.models.first().map(|m| m.entities.as_slice()).unwrap_or_default() { *per_layer.entry(entity.layer.as_deref().unwrap_or("0")).or_default() += 1; } for layer in &doc.layers { let state = if layer.visible { "on" } else { "off" }; let count = per_layer.get(layer.name.as_str()).copied().unwrap_or(0); println!("{:<24} {state:<4} frozen={} {count} entities", layer.name, layer.frozen); } for warning in doc.warnings.iter().take(20) { println!("warning: {} {}", warning.code, warning.message); } Ok(())}import sysfrom collections import Counter
import cadkit
path = sys.argv[1]header = cadkit.inspect(path) # header only: no entity decodingprint(header["format"], header["version"], header["units"])
doc = cadkit.read(path)per_layer = Counter(entity["layer"] for entity in doc.entities()) # model 0for layer in doc.layers: state = "on" if layer["visible"] else "off" if layer["frozen"]: state += ", frozen" print(f"{layer['name']:<24} {state:<12} {per_layer[layer['name']]} entities")for warning in doc.warnings[:20]: print("warning:", warning["code"], warning["message"])import { readFileSync } from "node:fs";import { inspect, read } from "cadkit-wasm/node";
const bytes = readFileSync(process.argv[2]);console.log(inspect(bytes)); // header only: no entity decoding
const doc = read(bytes);const perLayer = new Map();for (const entity of doc.toJSON().models[0]?.entities ?? []) { perLayer.set(entity.layer, (perLayer.get(entity.layer) ?? 0) + 1);}for (const layer of doc.layers()) { const state = layer.visible ? "on" : "off"; console.log(layer.name.padEnd(24), state.padEnd(4), perLayer.get(layer.name) ?? 0, "entities");}for (const warning of doc.warnings().slice(0, 20)) { console.log("warning:", warning.code, warning.message);}doc.free();#include <stdio.h>#include "cadkit.h"
int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: inspect_drawing <file>\n"); return 2; } char *header = NULL; /* header only: no entity decoding */ if (cadkit_inspect_file(argv[1], NULL, &header) == CADKIT_OK) { printf("%s\n", header); cadkit_string_free(header); }
cadkit_document *doc = NULL; if (cadkit_read_file(argv[1], NULL, &doc) != CADKIT_OK) { fprintf(stderr, "%s\n", cadkit_last_error_message()); return 1; } /* Counts, the entity histogram per kind and the warning count as JSON. Layer names and states are in the full document JSON (cadkit_document_to_json). */ char *summary = NULL; if (cadkit_document_info_json(doc, &summary) == CADKIT_OK) { printf("%s\n", summary); cadkit_string_free(summary); } cadkit_document_free(doc); return 0;}The C API returns the header and the summary as JSON strings. cadkit_document_info gives
the counts as a plain struct, and the full document JSON (cadkit_document_to_json) has the
layer table.
From the shell
Section titled “From the shell”cadkit inspect plan.dwg --json # header only: format, version, units, code pagecadkit info plan.dwg --warnings # summary plus the first 20 warningscadkit layers plan.dgn --json # DGN levels are layerscadkit blocks plan.dxf # block definitions with entity countscadkit dump plan.dxf --kind arc # one line per entity of a kindReading the results
Section titled “Reading the results”- Layers. DGN levels are reported as layers. Each layer has
visible,frozenandlockedflags and a colour. An entity’slayernames its layer; entities without one belong to the default layer. - Entity kinds. Points, lines, polylines, circles, arcs, ellipses, splines, text and
multiline text, block references (
insert), hatches, dimensions, leaders, faces, polygons, meshes, images, viewports and groups. Records a reader cannot model becomeunknownentities rather than being dropped. - Warnings have a stable
code(for exampledgn.unknown_element.type_36), a message and, where known, the byte offset in the file. They mean the reader kept going past something it could not fully decode. - Format-specific data that the neutral model has no field for is kept in
propswith adwg.,dgn.,dxf.orgml.key prefix.
The full shape is in the document JSON schema.
Related
Section titled “Related”- DWG, DGN and DXF format pages
- Convert DXF to SVG
- Handling untrusted files before inspecting uploads