tsdraft

Write 2D CAD drawings in TypeScript.

tsdraft is a library and a command for drafters, developers and the coding agents they direct. It writes DXF for CAD programs, SVG for a preview and PDF sheets.

Read the guide for agents

Site plan

Drawn by tsdraft 0.6.0. Move a value and it redraws here.

checkIntent

No warnings. The house keeps every setback.

Design values
48'-0"
30'-0"
24'-0"
20'-0"
1.
Before it writes a DXF, tsdraft refuses a drawing that breaks one of the leading CAD app's file rules, and names the call that broke it.
2.
Move a design value under the drawing. tsdraft 0.6.0 draws it again in your browser, from the source below.
3.
The library is MIT and needs Node 22 or later. It imports nothing at run time, so it can draw in this page.
4.
tsdraft is early. Sheet G-002 says what it writes today, and sheet G-003 what is planned.
site-plan.ts
import { createDrawing, checkIntent, toDXF } from "tsdraft";

const d = createDrawing({ units: "ft", linetypeScale: 3 });
const width = 48, depth = 30;
const yard = 24, front = 20;
const west = 28, east = west + width, back = yard + depth;

d.layer("C-PROP", { color: 3, lineweight: 0.5 }, () => {
  d.rect([0, 0], 104, 84, { id: "lot" });
  // ... survey monuments
});
d.layer("A-BLDG", { color: 7, lineweight: 0.7 }, () => {
  d.polyline([
    [west, yard + 8], [east - 18, yard + 8], [east - 18, yard],
    [east, yard], [east, back], [west, back],
  ], true, { id: "house" });
});
// ... roof, paving, trees, setback lines and dimensions

const drawing = d.build();
const warnings = checkIntent(drawing, {
  setbacks: [{ property: "lot", footprints: ["house"],
    distance: [front, 8, 10, 8] }],
});
const dxf = toDXF(drawing);

A drawing is a program

Outdoor kitchen · 3/8" = 1'-0"

A drawing file is a TypeScript module. It calls one builder for layers, lines, text, dimensions in feet and inches, blocks, leaders, hatches and sheets, and exports the drawing. Sizes can come from variables, two drawings can share a function, and a change shows up as a diff.

The elevation and section below are what examples/outdoor-kitchen.ts draws. The wall, the counter and the run of cabinets are parameters at the top of the file. Change the grill from 36 to 42 inches and the cabinets move, the dimensions change and the note says 42".

examples/outdoor-kitchen.ts (excerpt)
import { createDrawing, toDXF } from "tsdraft";

const wall = { width: 132, height: 108, thickness: 8, stucco: 0.75 };
const counter = { height: 36, depth: 30, thickness: 1.5, overhang: 1.5 };
const run = [
  { kind: "refrigerator", width: 24 },
  { kind: "filler", width: 3 },
  { kind: "grill", width: 36 },
  { kind: "filler", width: 3 },
  { kind: "sink", width: 30 },
];
// ...

const d = createDrawing({ units: "in" });
d.layer("A-WALL", {}, () => {
  d.rect([0, 0], wall.width, wall.height);
});
d.layer("A-FLOR-CASE", {}, () => {
  for (const c of cabinets) {
    const faceTop = c.kind === "grill" ? panel.bottom : under;
    d.rect([c.x, toeKick.height], c.width, faceTop - toeKick.height);
  }
});
// ...

writeFileSync("outdoor-kitchen.dxf", toDXF(d.build()));
An outdoor kitchen drawn by tsdraft at 3/8 inch to the foot. On the left, an elevation of a stucco wall with an undercounter refrigerator, a built-in gas grill under a vent hood, a sink below a window and two shelves, with dimension strings and notes. On the right, a wall section through the grill from the footing to the attic.
What toSVG writes for the example, with a color given to each layer. Open full size

A set from one model

examples/plan-set is a 15-sheet permit set for a house. One model of the house draws every sheet: plans, elevations, sections and schedules, each on ARCH D with a title block. toPDF plots the set to one PDF, a page for each sheet, with no CAD program.

Sheet A-101 of the permit set, the first floor plan at 1/4 inch to 1 foot: walls, doors, windows, the stair and fixtures, with tags, room names, dimension strings, revision clouds, notes and a legend, inside a border with a title strip down the right side.
Sheet A-101, first floor plan. Open full size

Checks

Three checks before a DXF is written

The leading CAD app discards a whole drawing over one bad layer name, and it reports no line of your code when it does. tsdraft checks that app's rules at three points, so a mistake shows up where you made it. Other CAD programs may hold a drawing to other rules.

  1. TypeScript
    const d = createDrawing({ units: "ft", dxf: "R12" });
    
    d.layer("A WALL");
    // error TS2345: Argument of type '"A WALL"' is not assignable to parameter of
    // type '"R12 layer names can only use letters, digits, $, - and _. Use DXF
    // version \"R2018\" for other characters"'.

    In the editor

    The TypeScript compiler checks layer names, colors, sizes and fonts that you write as literals. Its error message says what is wrong.

  2. plan.ts
    const name = layers.wall; // "A/WALL"
    
    d.layer(name);
    // DrawingError: Layer "A/WALL": Layer
    // names cannot contain < > / \ " : ;
    // ? * | = ` ^ , or line breaks

    When the code runs

    The builder throws at the call that breaks a rule. This covers values the compiler cannot see, such as a name read from a file. tsdraft render reports the file, line and column of that call.

  3. TypeScript
    const problems = lint(drawing); // every problem, with the call that made it
    lint(drawing, { version: "R12" }); // against another DXF version
    
    toDXF(drawing); // throws a DrawingError that lists them, and writes no file

    Before export

    lint returns every problem in a drawing. toDXF throws an error that lists them, and writes no file.

The command

npx tsdraft · Node 22.18 or later

The package comes with a command that runs a drawing file and shows the drawing, with no CAD program. The project's package.json must say "type": "module".

render writes an SVG and a PNG to an out folder beside the file and prints one JSON object. The JSON lists what lint found, and each problem carries the file, line and column of the builder call that drew it. render exits 0 when the drawing has no lint errors and 2 when it has some.

dev serves a preview at a local address and redraws it each time you save the drawing file or a file it imports.

Shell
# An SVG and a PNG, and JSON with what lint found
npx tsdraft render plan.ts

# Also plots each sheet, and the set, to PDF
npx tsdraft render plan.ts --pdf

# A live preview that redraws on every save
npx tsdraft dev plan.ts
What render prints, shortened
{
  "ok": true,
  "diagnostics": [
    {
      "rule": "zero-length",
      "severity": "warning",
      "message": "line #4: the line has no length",
      "id": "walk",
      "call": { "file": "/work/plan.ts", "line": 18, "column": 5 },
      "layer": "A-BLDG"
    }
  ],
  "error": null,
  "outputs": { "svg": "/work/out/plan.svg", "png": "/work/out/plan.png" }
}

For agents

The guide ships in the package

An agent needs to see what it drew after every edit. npx tsdraft render gives it a PNG to look at and JSON to act on in a fraction of a second, with no CAD program.

  1. Edit the drawing file.
  2. Render it with npx tsdraft render.
  3. Read the JSON. It says whether the drawing breaks a rule.
  4. Look at the PNG. Only the picture shows whether it is the drawing you meant.
  5. Fix what they show, and render again.

A whole sheet in one picture draws its notes too small to read, so --view draws one rectangle of the sheet alone, such as the title block.

The guide for agents covers the setup, the loop, what the command prints and what tsdraft cannot draw yet. It is GUIDE.md in the package.

For your AGENTS.md
Draw with tsdraft. Read node_modules/tsdraft/GUIDE.md before the first drawing.
After each edit, run `npx tsdraft render <file>`. Fix every diagnostic in its JSON,
then look at the PNG it names before you hand the drawing back.

Status and formats

Version 0.6.0 · MIT

tsdraft is early, and its API still changes. Until 1.0 any release, a patch included, can break code written for the one before. The changelog starts each breaking change with Breaking:. The command has been run on Linux only.

What tsdraft writes, reads and edits
FormattsdraftNotes
DXF R2018WritesThe default, format code AC1032. It stores units, linetypes, lineweights, paragraph text and dimensions.
DXF R12WritesFormat code AC1009, for a program that cannot read R2018. It stores none of those and gets no new features.
SVGWritesThe drawing as it plots, or one sheet of it. The drawings on this page are SVG.
PNGWritesThe command draws one on each render, for a person or an agent to look at.
PDFPlotsEvery sheet of a drawing to one PDF, a page for each.
DXFReadsAn ASCII DXF into a model to inspect. The command draws it as a picture.
DXFEditsAn edit file erases, moves, stretches, changes and adds by handle. tsdraft write writes an edited copy once its checks pass, and never writes over the file. New in 0.6.0.
DWGPlannedA paid hosted service that reads, writes and edits DWG, the format most firms exchange drawings in. No date or price yet. The library on npm stays free under MIT.

The reference has a page for each part: layers, blocks, dimensions, text, hatches, layouts, plotting, standards, reading and editing.

Direction

Planned work · not in 0.6.0

tsdraft is meant to become the best drafting tool an agent has. The agent writes the drawing as code, sees it in well under a second, checks it and fixes it, many times a minute, with no CAD program in the loop. The first goal is 2D permit drawings from start to finish: site plans, building plans, details and sheets. 3D comes later.

Each row below says what 0.6.0 does today and what is planned. The planned work has no date, and none of it is in a release.

  • Drawing by intent

    Today
    You draw lines, rectangles, dimensions and sheets, and write your own function for a wall or a door.
    Direction
    A wall, a property line, a detail, a sheet and a title block become parts of the library. The drawing file says what a thing is, and tsdraft draws its lines.
  • Checks

    Today
    lint holds a drawing to the leading CAD app's file rules and to a firm's standard for layers, pens, text and sheets. tsdraft also warns about text that overlaps or leaves its border, and about a building that crosses a setback.
    Direction
    lint grows from file rules into drafting standards. A firm's standard is the first part of that.
  • Shared packages

    Today
    There are none. tsdraft has one built-in standard, and a firm writes its own details and symbols.
    Direction
    Details, symbols, title blocks and firm standards become npm packages that a drawing file imports.
  • A firm's own drawings

    Today
    tsdraft reads a firm's DXF, answers what a sheet shows and writes an edited copy.
    Planned
    Reading DWG. A job could then run on a machine with no CAD program: tsdraft reads the firm's drawings, draws or edits, checks its own output and plots, and a person spot-checks the result in their own CAD program.
  • Coding agents

    Today
    An agent uses the command and reads the guide in the package.
    Planned
    Plugins for coding agents that hand the agent the guide, the command, the picture of the drawing and what lint found.
  • Hosted service

    Today
    There is none. The library and the command run on your own machine.
    Planned
    A hosted service, with DWG for paid accounts. The library on npm stays free under MIT.

Start a drawing from the example sheet.

examples/site-plan.ts is a whole permit-style sheet with its plan, border, title block and notes. The guide says how to copy it and run it.