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.
Site plan
Drawn by tsdraft 0.6.0. Move a value and it redraws here.
No warnings. The house keeps every setback.
- 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.
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".
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()));toSVG writes for the example, with a color given to each layer. Open full sizeA 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.
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.
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.
plan.ts const name = layers.wall; // "A/WALL" d.layer(name); // DrawingError: Layer "A/WALL": Layer // names cannot contain < > / \ " : ; // ? * | = ` ^ , or line breaksWhen 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 renderreports the file, line and column of that call.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 fileBefore export
lintreturns every problem in a drawing.toDXFthrows 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.
# 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{
"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.
- Edit the drawing file.
- Render it with
npx tsdraft render. - Read the JSON. It says whether the drawing breaks a rule.
- Look at the PNG. Only the picture shows whether it is the drawing you meant.
- 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.
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.
| Format | tsdraft | Notes |
|---|---|---|
| DXF R2018 | Writes | The default, format code AC1032. It stores units, linetypes, lineweights, paragraph text and dimensions. |
| DXF R12 | Writes | Format code AC1009, for a program that cannot read R2018. It stores none of those and gets no new features. |
| SVG | Writes | The drawing as it plots, or one sheet of it. The drawings on this page are SVG. |
| PNG | Writes | The command draws one on each render, for a person or an agent to look at. |
| Plots | Every sheet of a drawing to one PDF, a page for each. | |
| DXF | Reads | An ASCII DXF into a model to inspect. The command draws it as a picture. |
| DXF | Edits | An 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. |
| DWG | Planned | A 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
lintholds 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
lintgrows 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
lintfound.
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.