TypeScript library to read and write AutoCAD DWG and DXF files. Ported from the C# ACadSharp library.
Based on commit: https://github.com/DomCR/ACadSharp/commit/3010994939c1bc21df0c9e2931e9baee4564815a
Last compared upstream commit: https://github.com/DomCR/ACadSharp/commit/0e947316afb8bd12a3d538a075e518b324da7273 (see commit-by-commit audit).
- Read/Write DXF files (ASCII and binary)
- Read/Write DWG files
- Honor
$DWGCODEPAGEwhen reading DXF ASCII/binary strings and when writing legacy-codepage text - Extract and modify geometric entities (lines, arcs, circles, polylines, dimensions, etc.)
- Manage table entries (blocks, layers, line types, text styles, dimension styles)
- Convert CAD documents to SVG
- Full TypeScript type definitions
| DxfReader | DxfWriter | DwgReader | DwgWriter | |
|---|---|---|---|---|
| AC1009 | ✔️ | ❌ | ❌ | ❌ |
| AC1012 | ✔️ | ✔️ | ❌ | ❌ |
| AC1014 | ✔️ | ✔️ | ✔️ | ✔️ |
| AC1015 | ✔️ | ✔️ | ✔️ | ✔️ |
| AC1018 | ✔️ | ✔️ | ✔️ | ✔️ |
| AC1021 | ✔️ | ✔️ | ✔️ | ✔️ |
| AC1024 | ✔️ | ✔️ | ✔️ | ✔️ |
| AC1027 | ✔️ | ✔️ | ✔️ | ✔️ |
| AC1032 | ✔️ | ✔️ | ✔️ | ✔️ |
npm install @node-projects/acad-tsThe original C# ACadSharp implementation relies on reflection and attributes such as DxfCodeValue, DxfCollectionCodeValue, CadSystemVariable, DxfName, and DxfSubClass to build DXF maps and header-variable tables at runtime. TypeScript does not have the same runtime reflection model, so acad-ts now uses a checked-in metadata lookup table instead.
This metadata is what powers the rebuilt CadHeader.GetHeaderMap() / SetValue() path, DxfMap / DxfClassMap creation, DXF subclass lookup, and property conversions for dates, time spans, vectors, colors, reference types, and collection-coded values.
The metadata system has four parts:
src/Metadata/MetadataLookupTable.ts: a small aggregator that preserves a stable runtime import path.src/Metadata/LookupTables/*.ts: the hand-maintained domain lookup tables for DXF names, subclass markers, property codes, collection codes, system variables, and reference semantics.src/Metadata/MetadataStore.ts: runtime lookup helpers that expose cloned metadata for maps, properties, and system variables.- Runtime consumers such as
src/DxfMap.ts,src/DxfClassMap.ts,src/DxfPropertyBase.ts, andsrc/Header/CadHeader.ts.
Why this exists:
- It restores the C# mapping behavior with an explicit TypeScript source of truth that lives in this repo.
- It avoids a build-time or maintenance dependency on the upstream C# source tree.
- It avoids shipping a decorator or reflection dependency just to emulate ACadSharp's attribute model.
How the lookup table is maintained:
- The current lookup table was bootstrapped from the earlier generated metadata so the repo keeps the same runtime behavior without reintroducing the C# fetch step.
- Ongoing changes should be made directly in the matching file under
src/Metadata/LookupTables/when a newly ported type, DXF code, collection code, subclass marker, or system variable needs to be added or corrected. src/Metadata/MetadataLookupTable.tsaggregates those domain files into one array so the runtime import path stays stable.src/Metadata/MetadataStore.tsclones that table before exposing entries so runtime consumers do not mutate the source data.
The lookup table is committed. Normal builds and tests consume the checked-in metadata and do not require network access or any external source tree.
importfsfrom'fs';import{DwgReader}from'@node-projects/acad-ts';constbuffer=fs.readFileSync('drawing.dwg');constarrayBuffer=buffer.buffer.slice(buffer.byteOffset,buffer.byteOffset+buffer.byteLength);constdoc=DwgReader.readFromStream(arrayBuffer,(sender,e)=>{console.log(e.message);});// Access entities in model spacefor(constentityofdoc.modelSpace.entities){console.log(entity.constructor.name,entity.handle);}importfsfrom'fs';import{DxfReader}from'@node-projects/acad-ts';constbuffer=fs.readFileSync('drawing.dxf');constdata=newUint8Array(buffer.buffer,buffer.byteOffset,buffer.byteLength);constdoc=DxfReader.readFromStream(data,(sender,e)=>{console.log(e.message);});DXF string decoding follows $DWGCODEPAGE for both ASCII and binary DXF. Pass the raw file bytes as Uint8Array; if you decode the file to a JavaScript string before handing it to the reader, the original code page information is already lost.
importfsfrom'fs';import{CadDocument,DwgWriter,ACadVersion,Line,XYZ}from'@node-projects/acad-ts';constdoc=newCadDocument();doc.header.version=ACadVersion.AC1032;constline=newLine();line.startPoint=newXYZ(0,0,0);line.endPoint=newXYZ(100,100,0);doc.modelSpace.addEntity(line);constoutput=DwgWriter.writeToBuffer(doc);fs.writeFileSync('drawing.dwg',output);import{CadDocument,DxfWriter}from'@node-projects/acad-ts';constdoc=newCadDocument();doc.header.codePage='ANSI_1252';// ... add entities ...constoutput=newUint8Array(1024*1024);constwriter=newDxfWriter(output,doc);writer.write();doc.header.codePage controls the legacy encoding used for DWG/DXF text data. For exact non-UTF-8 ASCII DXF bytes, prefer a Uint8Array target. If you send ASCII DXF to a string-based text sink, that sink's own encoding policy still determines the final bytes.
import{SvgWriter}from'@node-projects/acad-ts';constoutput=newUint8Array(1024*1024);constsvgWriter=newSvgWriter(output,doc);svgWriter.write();import{CadDocument,Layer,Line,Circle,XYZ,Color}from'@node-projects/acad-ts';constdoc=newCadDocument();// Create a layerconstlayer=newLayer();layer.name='MyLayer';layer.color=Color.fromColorIndex(1);// Reddoc.layers.add(layer);// Add a lineconstline=newLine();line.startPoint=newXYZ(0,0,0);line.endPoint=newXYZ(10,5,0);line.layer=layer;doc.modelSpace.addEntity(line);// Add a circleconstcircle=newCircle();circle.center=newXYZ(5,5,0);circle.radius=3;doc.modelSpace.addEntity(circle);npm install
npm run buildThe compiled output will be in the dist/ directory.
npm testThe focused metadata regression coverage lives in tests/MetadataMappings.test.ts. It verifies lookup-table collection codes, reference-type handling, CadHeader variable application, and metadata-backed DxfMap / DxfClassMap creation.
See the docs folder for detailed documentation on specific topics:
- CadDocument - Document structure and collections
- CadObject - Base object properties
- Entities - Working with geometric entities
- Tables - Table entries (layers, line types, styles)
- Block Records - Blocks and block records
- Inserts - Block insertions
- Dimensions - Dimension entities
- Non-Graphical Objects - Layouts, dictionaries, etc.
- Reading files - Reader configuration and usage
- Writing files - Writer configuration and usage