tsdraft
v0.6.2
Published
2D CAD drawings as TypeScript: write the drawing in code, export DXF, plot sheets to PDF, read DXF, and preview it live with the tsdraft command.
Maintainers
Readme
Building design, written as code, so agents can do it and engineers can check it.
tsdraft is the first part: an MIT TypeScript library that writes 2D CAD drawings. It
writes DXF for CAD programs and SVG for a preview, plots sheets to PDF, and reads DXF
files to inspect them. Before it writes a DXF, it refuses a drawing that breaks a rule
the leading CAD app holds a file to, such as a layer name that makes that app discard
the whole file. tsdraft dev redraws the drawing on every save, so you, or an agent,
can see each change.
Can I generate a DXF from TypeScript? Yes. npm install tsdraft, draw with
createDrawing, and toDXF(drawing) returns the DXF as a string, R2018 by default or
R12. It needs no CAD program and runs in Node or a browser.
Does tsdraft write DWG? No. The library writes DXF, which CAD programs open. DWG writing is planned for tsdraft Pro, a paid hosted writer that is not part of the npm package. Pro is not for sale yet; it has a waitlist.
Working as an agent? Connect tsdraft's MCP server, npx tsdraft mcp (in Claude Code,
claude mcp add tsdraft -- npx tsdraft mcp), and read the guide for agents first.
The picture is what toSVG writes for
examples/outdoor-kitchen.ts, restyled for this
page with a color for each layer. Its dimensions are tsdraft's dimensions, in
feet and inches, and its notes are leaders. The stipple and hatching are small
functions in the example that draw lines, and every size comes from a few
parameters at the top of the file.
examples has the drawings and how to run them.
A smaller drawing, start to finish:
import { createDrawing, DASHED, PHANTOM, toDXF, toSVG } from "tsdraft";
const lot = { width: 120, depth: 80 }; // ft
const setbacks = { front: 20, side: 10, rear: 15 };
const house = { width: 56, depth: 32 };
const x = (lot.width - house.width) / 2;
const y = setbacks.front + 5;
// Linetype patterns are in drawing units. At a scale of 4, DASHED draws dashes of 2 ft and
// PHANTOM long dashes of 5 ft.
const d = createDrawing({ units: "ft", linetypeScale: 4 });
d.layer("C-PROP", { color: 3, linetype: PHANTOM, lineweight: 0.5 }, () => {
d.rect([0, 0], lot.width, lot.depth);
d.text([2, -6], `LOT ${lot.width}' x ${lot.depth}'`, { height: 3 });
});
d.layer("C-SETB", { color: 4, linetype: DASHED, lineweight: 0.25 }, () => {
const { front, side, rear } = setbacks;
d.rect([side, front], lot.width - side * 2, lot.depth - front - rear);
});
d.layer("A-BLDG", { color: 1, lineweight: 0.7 }, () => {
d.rect([x, y], house.width, house.depth);
d.text([x + 2, y + house.depth / 2], "PROPOSED RESIDENCE", { height: 3, lineweight: 0.35 });
});
d.layer("C-PAVE", { color: 8, lineweight: 0.25 }, () => {
d.line([x + 40, y], [x + 40, -12]);
d.line([x + 52, y], [x + 52, -12]);
});
d.layer("C-ROAD", { color: 2, lineweight: 0.35 }, () => {
d.line([-10, -12], [lot.width + 10, -12]);
d.text([lot.width / 2 - 10, -18], "MAIN STREET", { height: 3 });
});
d.layer("V-TREE", { color: 3, lineweight: 0.18 }, () => {
for (let i = 0; i < 6; i++) d.circle([15 + i * 18, 72], 5);
});
const drawing = d.build();
const dxf = toDXF(drawing);
const svg = toSVG(drawing);A drawing is a program, so dimensions can come from variables, drawings can
share code, and changes show up as diffs. examples/plan-set
is a 15-sheet permit set for a house, every sheet drawn from one model of it.
This is its first floor plan:
Coordinates are in drawing units with Y pointing up, as in CAD. Angles are in degrees, counterclockwise from +X. Layer colors are numbers from the color index CAD programs share.
toSVG draws the drawing as it plots, an insert as what its block draws where the insert
puts it. Each layer is drawn in the color the leading CAD app plots its
index in, from 1 to 255. Index 7 is black on paper and white on a dark screen, so it is drawn
in currentColor. A layer that is off or frozen, or whose plot is false, is left out.
toSVG(drawing, { drawNoPlot: true }) draws the layers that do not plot too, to see
construction lines. The other options are margin, in drawing units, and strokeWidth, the
width in screen pixels of a line of the default lineweight. A picture too large to write, such
as blocks nested to draw millions of entities, is refused with a RangeError that says where
it stopped, as the preview of blocks says.
Entities go on layer 0 unless they are drawn inside a layer() callback.
Linetypes and lineweights are set on a layer, and an entity draws ByLayer unless
it is given its own. DASHED, HIDDEN, CENTER, PHANTOM, DASHDOT and DOT
are the leading CAD app's own definitions, and a layer or an entity may name one, as
linetype: "HIDDEN", or pass it, as linetype: HIDDEN: either defines it on first use. A linetype
of your own is a { name, pattern } definition, with dashes as positive lengths, gaps as negative
ones and dots as 0. Pass the definition, as linetype: FENCE: a literal name other than the
library's and Continuous, ByLayer and ByBlock is a compile error. Its marks draw texts
along the line, such as a water main's letters, and shapes of a firm's shape file, such as a
fence's circles, which parseShx reads and the plot takes as shx. parseLinetypes reads
a firm's linetype file. A polyline's linetypeGeneration runs the pattern on past its vertices. A
lineweight is in millimetres.
A text is in the style Standard unless it names another. d.textStyle({ name,
font }) defines one, with a font from FONTS: SHX fonts the leading CAD app installs and
TrueType fonts Windows installs. align and valign justify a text to its
point:
d.textStyle({ name: "NOTES", font: "Arial" });
d.text([60, 40], "PROPOSED RESIDENCE", { height: 3, style: "NOTES", align: "center" });A symbol that repeats is a block: drawn once, and placed by inserts that scale and turn it. A block's attributes are texts that each insert sets a value for, and that a drafter can edit in CAD:
d.block("TREE", {}, () => {
d.circle([0, 0], 1);
d.attribute("TAG", [0, 1.2], { value: "T-0", height: 0.3, align: "center" });
});
d.layer("V-TREE", {}, () => {
d.insert("TREE", [15, 72], { scale: 5, attributes: { TAG: "T-1" } });
d.insert("TREE", [33, 72], { scale: [6, 5], rotation: 30 });
});An entity on layer 0 of a block draws as the layer of each insert does.
Blocks has the rest.
A drawing can reference another drawing's DWG file as an xref, and place it as it places a block. A CAD program loads the file each time it opens the drawing:
d.xref("BASE", "../civil/base.dwg");
d.layer("X-BASE", { color: 8 }, () => d.insert("BASE", [0, 0]));The path leads from the folder of the drawing's file. toSVG draws the referenced drawing
when it is given one, as toSVG(drawing, { xrefs: { BASE: base } }). Xrefs
has the rest.
A polyline's sides can be arcs and can be drawn wide, and the builder makes a few shapes of them, each one polyline in the file:
d.polyline(hook, false, { radius: 1.5, width: 0.625 }); // a bar's hook with its bend
d.cloud(outline, { arc: 3.75 }); // a revision cloud that bulges out of its outline
d.donut([10, 10], 0.5); // a filled dotbulges makes any side an arc, and widths gives each side a width at each end.
Polylines has the rest.
An ellipse is drawn by its center, the end of its major axis and the ratio of its axes, and a spline through fit points, as a top of bank or a tree line is drawn through located shots, or by its control points:
d.ellipse([10, 5], [4, 0], 0.5);
d.spline(bank); // the cubic CAD programs fit through the points
d.controlSpline(controlPoints, { degree: 3 });Ellipses and splines has arcs of an ellipse, tangents, closed splines, knots and weights.
A point marks a survey shot or a benchmark. Every point plots as the figure the drawing's point style gives, at a size in drawing units or as a share of the view:
const d = createDrawing({ pointStyle: { figure: "cross", circle: true, size: 0.25 } });
d.point([10, 5]);Points has the figures and their sizes.
A paragraph is one text whose lines wrap at a width, which a CAD user edits as one. A line feed starts a new paragraph, and small helpers write the codes for a numbered list, an underline, bold, a stacked fraction and a superscript:
import { list, stack, underline } from "tsdraft";
d.paragraph(
`GENERAL NOTES\n${list([`${underline("VERIFY")} ALL DIMENSIONS.`, `GAP 3${stack("1", "2")}"`])}`,
{
at: [1, 20], // the upper left corner of its box
width: 4,
height: 0.1,
},
);measureParagraph gives its lines and its box. Paragraphs has the codes
it takes and what it refuses.
An image is a PNG or a JPEG drawn at a size in drawing units, such as an aerial under a plan or
a vicinity map on a sheet. The drawing holds the file's path and its pixels, which rasterSize
reads, and the plot draws the file from the bytes it is given:
import { rasterSize } from "tsdraft";
const pixels = rasterSize(readFileSync("aerial.png"))!;
d.image("aerial.png", [0, 0], {
pixels,
width: 400,
clip: [
[20, 20],
[380, 260],
],
});
toPDF(drawing, { images: { "aerial.png": readFileSync("aerial.png") } });Images has clips, frames and transparency, and what lint refuses.
An underlay places a page of a PDF under the sheet, such as an architect's plan, by its path,
its page and the page's size, which pdfPageSize reads. The plot draws it from a picture of the
page you give it, since tsdraft draws no PDF. Underlays has the rest.
A hatch fills the area inside its loops solid, a loop inside another a hole, draws a pattern of the firm's pattern file there, or runs from one color to another as a gradient. A wipeout hides what is drawn before it, and a paragraph's mask hides it behind the text:
d.hatch([
{
points: [
[0, 0],
[96, 0],
[96, 8],
[0, 8],
],
},
{
points: [
[36, 0],
[72, 0],
[72, 8],
[36, 8],
],
},
]);
d.paragraph("VERIFY IN FIELD", { at: [40, 20], height: 3, mask: true });Fills, wipeouts and masks has holes and arcs, patterns, gradients, wipeouts' frames, a dimension style's mask and the draw order.
A dimension measures between two points and writes what it measures. In the file it is a dimension, which a CAD program measures again when its points move, and a dimension style says how it is drawn:
d.dimensionStyle({
name: "PLAN",
textHeight: 0.9,
arrowhead: "architectural-tick",
arrowSize: 0.6,
extensionOffset: 0.6,
extensionBeyond: 1.2,
textGap: 0.6,
precision: 1,
suffix: "'",
});
d.linearDimension([34, 30], [86, 30], [0, 22], { style: "PLAN" }); // 52.0'alignedDimension measures along the two points, and a style with units: "architectural"
writes feet and inches, as 4'-1 7/16", with the fraction on the line or stacked. A style puts
the text above the line or in a break of it and moves a text with no room out on a leader, and
a dimension takes words below its line. Dimensions has the rest.
A layout is a sheet of paper that a CAD program shows as a tab and plots as one page. What is drawn
inside layout is on the sheet, in inches of paper, and a viewport shows the model on it at a
scale:
d.layer("G-ANNO-NPLT", { color: 8, plot: false });
d.layout("C-101", { paper: "ARCH D" }, (sheet) => {
d.rect([0.5, 0.5], sheet.width - 1, sheet.height - 1);
d.layer("G-ANNO-NPLT", {}, () =>
sheet.viewport([1, 1], 34, 22, { center: [50, 70], ratio: 120 }),
);
});The file opens on the sheet with its page setup: DWG To PDF.pc3, the paper, 1:1 and
monochrome.ctb. toSVG(drawing, { layout: "C-101" }) draws the sheet as it plots, and
toPDF(drawing) plots every sheet to one PDF, a page each, with no CAD program.
Layouts has the rest, Plotting the PDF, and
Title blocks shows a title block whose fields are attributes.
Text, symbols and dashes are sized by how large they plot. fromPaper turns a
length on paper into drawing units for one scale, given as a plain ratio:
import { fromPaper } from "tsdraft";
const view = { ratio: 120, units: "ft" } as const; // 1" = 10'
const note = fromPaper(3 / 32, "in", view); // 0.9375 ft
d.text([0, 0], "PROPERTY LINE", { height: note });A paper unit other than "in" or "mm", and a length or a ratio of 0 or less,
are compile errors as literals and thrown errors otherwise. fromPaper also
throws when the result would be written as 0 or is 1e100 or more.
examples/site-plan.ts is a whole sheet, and the file
to copy to start a drawing: its plan is in model space, with its annotation
sized this way, and its border, title block and notes are on a layout.
Status
tsdraft is early, and its API still changes. Until 1.0 any release, a patch
included, can break code written for the one before. CHANGELOG.md
starts each breaking change with Breaking:.
It draws layers, lines, polylines with arcs and widths, rectangles, revision clouds, donuts, circles, arcs, ellipses, splines, points, single-line text and paragraph text that wraps to a width, with lists, tab stops and columns, linetypes and lineweights, an entity's own color by index or as a true color and its own transparency, text styles and justification, blocks with attributes of one line or of paragraph text and inserts of them, references to DWG files as xrefs, hatches, wipeouts and masks, raster images, PDF underlays, tables, and leaders. Its dimensions are linear, aligned, radius, diameter, jogged radius, angular, arc length and ordinate, in decimal units or feet and inches with stacked fractions, with words below the line and text on a leader. It draws layouts with a page setup and viewports, which can freeze their own layers or twist the view, and layers that are off, frozen, locked or not plotted. A firm's standard sets its layers, pens, text and sheets. It writes DXF, not DWG.
Most of this is R2018 only, the default. An R12 drawing keeps to layers, lines, straight
polylines, circles, arcs and single-line text, and lint refuses the rest.
It does not yet have overlines in paragraph text, viewports that are not rectangles, attributes that are hidden or constant, papers in portrait, or colors of a color book. Dimensions lists what its dimensions do not draw yet.
readDXF reads an ASCII DXF into a model to inspect, which is not a drawing tsdraft can edit
and write again. Reading drawing files has what it reads.
The rules tsdraft checks are the leading CAD app's. Other CAD programs may hold a drawing to other rules.
Install
npm install tsdrafttsdraft is an ES module and needs Node 22 or later. The library imports nothing at run time, from npm or from Node.
The tsdraft command and its MCP server
The package comes with a command that runs a drawing file and shows the drawing, with no CAD
program. For an agent, tsdraft mcp serves it as an MCP server with two tools: render builds
the file and answers with a short brief, each error and warning at the call that made it, and
the pictures as images; execute runs a program over builds, so the agent reads only what it
asks for. The server also serves the loop as a skill, draw, to a harness that loads skills from
MCP servers.
claude mcp add tsdraft -- npx tsdraft mcp # the MCP server, for an agent in Claude Code
npx tsdraft render plan.ts # writes an SVG and a PNG, and prints JSON with what lint found
npx tsdraft render plan.ts --pdf # also plots each sheet, and the set, to PDF
npx tsdraft dev plan.ts # a live preview that redraws on every saveA drawing file is a TypeScript module whose default export is a drawing. The command needs
Node 22.18 or later, and the project's package.json must say "type": "module".
docs/command.md describes the commands, the MCP server and the JSON, and
the guide for agents describes the loop to work in.
The PNG is drawn by @resvg/resvg-js, the package's one dependency. npm installs it, with a
native build for the platform, unless optional packages are left out. Only the command and
tsdraft/png load it. Installed with --omit=optional, the command still writes the SVG and
the JSON, and the JSON says why there is no PNG.
A script on Node draws the same PNG without the command:
import { toPNG } from "tsdraft/png";
writeFileSync("plan.png", await toPNG(drawing, { width: 800 }));toPNG(drawing, options) takes the options of toSVG, such as layout and view, and
width, 1600 pixels by default, and gives the bytes of the PNG tsdraft render writes of that
picture, with the same fonts. tsdraft itself runs in a browser and loads no renderer, so
toPNG comes from tsdraft/png. It draws in a process of its own, which does not keep the
script from ending, so a picture the renderer stops on rejects with a RendererCrashed and
the script goes on. Without the renderer it rejects with a PngUnavailable that says why.
DXF versions
A drawing is written for one DXF version. createDrawing() targets R2018, the
newest published version, in use since 2018. createDrawing({ dxf: "R12" })
targets R12, the oldest version still in common use, which nearly every CAD program can open. toDXF(drawing) writes
the drawing's version, and toDXF(drawing, { version }) writes another one.
| | "R2018", the default | "R12" |
| ------------------------- | ----------------------------------------------------------------------- | -------------------------------------------------- |
| Format code | AC1032 | AC1009 |
| Drawing units | Stored in $INSUNITS. | Not stored. Set them in CAD with UNITS. |
| Polylines | LWPOLYLINE | POLYLINE with VERTEX records |
| Polyline arcs and widths | Yes | No |
| Paragraph text | MTEXT | No |
| Text | UTF-8 | ASCII, with \U+XXXX escapes for other characters |
| Layer names | Up to 255 characters, except those in the rules | Up to 31 letters, digits, $, - and _ |
| Linetypes, lineweights | Yes | No: only Continuous, and no lineweights |
| Layer off, frozen, locked | Yes, and not plotted | No |
| Dimensions | Linear and aligned | No |
R12 gets no new features. Choose it only for a program that cannot read R2018. CAD needs the stored units to scale a drawing inserted into another, and the leading CAD app shows layer names from an R12 file in upper case.
Checks
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 the mistake shows up where you made it.
- In the editor. Layer names, colors, sizes and fonts written as literals are checked by the TypeScript compiler, and the error message says what is wrong.
- 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.
- Before export.
lint(drawing)returns every problem in a drawing, andtoDXFthrows aDrawingErrorthat lists them instead of writing a file that app would reject.
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"'.
createDrawing().layer("A WALL"); // fine
lint(drawing, { version: "R12" }); // check a drawing against another versionReference
The docs list every page: the guide for agents first, then the drawing, text,
sheets, standards, a firm's files and the command. They ship in the package, in
node_modules/tsdraft/docs/, and are published at tsdraft.com/docs.
Development
Contributors need Node 24 or later and pnpm. Node runs the TypeScript source directly, so tests and examples need no build step.
pnpm install
pnpm verify # format check, lint, type check, tests, package smoke test
pnpm example # writes the examples to examples/out/ as DXF and SVG
pnpm readme:drawing # redraws the pictures in docs/assets/ from the examples
pnpm bench # times the command from a save to the redrawn pictureDEVELOPMENT.md covers the package smoke test, the TypeScript compilers and releasing.
Contributing
tsdraft is developed in a private repository and published here through pull
requests from the export branch. Issues are welcome.
CONTRIBUTING.md says how changes arrive.
