← DWG SDK
API Reference

JavaScript / WASM API

A WebAssembly module over the Rust core: open a DWG from bytes, read it, and compile the document — in the browser or Node.

Quick start

import init, { DwgDoc } from "./dwg_wasm.js";
import { Document } from "./dwg-db/document.js";      // flatc-generated TS accessors
import * as flatbuffers from "flatbuffers";

await init();                                          // load the wasm module

const bytes = new Uint8Array(await file.arrayBuffer());
const doc = new DwgDoc(bytes);                         // throws if not a readable DWG
console.log(doc.version, doc.objectCount);             // "AC1032", 229

const buf = doc.compile();                             // Uint8Array — the DWGD document
const document = Document.getRootAsDocument(new flatbuffers.ByteBuffer(buf));
console.log(document.layers(0).name());

doc.free();                                            // JS GC doesn't see the wasm heap

The JavaScript binding is a WebAssembly module (wasm-bindgen over the Rust core). It reads DWG in the browser or Node and returns the same FlatBuffers document the other languages get. The free read tier — no native library, no licence.

class DwgDoc

compile(): Uint8Array

An open DWG document. Construct from the file's bytes; drop it with `free()` (JS's GC does not see the WASM heap, so freeing is the caller's job). / export class DwgDoc { free(): void; [Symbol.dispose](): void; /** Compile to the FlatBuffers document (`schema/dwg_db.fbs`). Returns the buffer bytes; read them in JS with the generated TypeScript accessors.

constructor(bytes: Uint8Array)

Parse a DWG from its bytes. Every version from R13 to R2018.

Throws a JS `Error` if the bytes are not a DWG this build can read.

readonly objectCount: number

How many objects the document holds.

readonly version: string

The version string, e.g. `"AC1032"`.

Schemas

Generate TypeScript accessors with flatc --ts — concrete classes like Document, MText, Layer.

The document API

The idiomatic wrapper (dwg.ts) mirrors the .NET surface — read-only, since WASM is the free read tier.

import init, { DwgDoc } from "./dwg_wasm.js";
import { DwgDocument, EntityKind } from "./dwg";

await init();
const doc = DwgDocument.open(new Uint8Array(fileBytes), DwgDoc);

console.log(doc.version, doc.header.insUnits);

// iterate model space in draw order, dispatch on kind
for (const e of doc.modelSpace.entities()) {
  if (e.kind === EntityKind.Line)  { const l = e.asLine(); console.log(l.start, l.end); }
  if (e.kind === EntityKind.MText) console.log(e.asMText().text);
}

// or by type: doc.lines(), doc.circles(), doc.inserts(), doc.mtexts()
for (const layer of doc.layers()) console.log(layer.name);

doc.free();

Building an edit script

Note: the WASM build is the free read tier — it reads and compiles. Authoring (apply/write) runs on the native C/.NET path, but the edit-script format is the same:

import { EditScript, Op, OpKind, SetHeaderVar, IntValue, Value } from "./dwg-edit";
const fbb = new flatbuffers.Builder(256);
const shv = SetHeaderVar.createSetHeaderVar(
  fbb, fbb.createString("INSUNITS"), Value.IntValue,
  IntValue.createIntValue(fbb, BigInt(4)));
const op = Op.createOp(fbb, OpKind.SetHeaderVar, shv);
fbb.finish(EditScript.createEditScript(
  fbb, EditScript.createOpsVector(fbb, [op])));
const bytes = fbb.asUint8Array();