← Files AircadARCHIVED FILE
skills/write-a-command/references/making-commands.md
8.39 KB · Nov 30, 2026 · 07:41 UTC
<!-- Written by `yarn gen:plugin` from Aircad's agent prompt (src/agent/model.ts, COMMAND_WRITING_GUIDE); do not edit. -->
# Making commands
How to make a command. When the user asks for a command, a tool, or something they can run again ("make me a command that draws a slot"), save it with save_command instead of running it once. The source is the whole command, one module: it imports what it uses from "aircad", exports const manifest, and its default export is async main(cad). A command behaves like the editor's own: it prompts on the command line, shows its options in the options bar, and draws a live preview on the canvas.
The manifest. export const manifest = { name, kind: "command", description, capabilities, inputs, options, api: 1 } is plain data: text in quotes, numbers, true or false, lists, and { } groups; no variables, calls, or spreads. capabilities is required: list what the command does ("document:read", "document:write", "selection", "view", "comments:write"), or it does not save. Inputs (InputSpec) are keyed by name in the order main asks for them; options (OptionSpec) are keyed by name and each has a default.
Inputs or options. An input is something the user picks or types on the canvas for this one run: points, lengths, angles, entities, edges. An option is a setting with a default that the options bar shows and the command remembers: a mode, a count, a toggle, and every fixed list (alignment, corner style, direction), which is { type: "choice", default, choices } under options and shows as a dropdown, the way Dimension's Align and Offset's Corner do. Never declare a fixed list as an input. Read an option as cad.options.Name where it is used, not once at the top, so a change while an input is asked counts; a command whose options change its result after its last input awaits cad.input.confirm({ prompt, onChange }) so the user can change them while the preview follows, then presses Enter.
How a command runs. main runs once, start to finish. Each await cad.input.point(name), nextPoint (a point list: null on Enter), length, angle, number, text, choice, entities, subEntities, or pieces asks for the input declared under that name and resolves with its value; picks snap, typed coordinates, relative @x,y, lengths, and angles work, and a length or angle typed at a point with from locks it. Esc throws Cancelled, which ends the run and undoes it. Everything main writes is one undo step when it returns. A caller can supply inputs by name (run_command, cad.commands.run, or the user typing Name input=value on the command line), and supplied inputs are not asked.
Previews are displays. const preview = cad.display.show() shows values on the canvas until the run ends; preview.set(value, values, or null) replaces them, and { style: "guide" } draws construction lines and markers. Give an input onChange: it runs with the tentative value as the pointer moves (a point is the pointer position; a length is its distance from from, or along along), so draw there everything the known inputs determine: for a circle from center and radius, the circle follows the pointer while the radius is picked; for a slot from start, end, and width, the center line while end is picked, then the slot. Values made inside onChange are freed when it next runs unless a display holds them, so show them rather than keeping them. cad.display.ghost(ids, transform) draws existing entities moved without copying them. Displays are never in the file and never snapped to. Keep onChange fast: no queries over the whole file, no selection or view changes.
An example, a ring from its center and two radii:
```js
import { Curve } from "aircad"; export const manifest = { name: "Ring", kind: "command", description: "Two circles around one center.", capabilities: ["document:read", "document:write"], inputs: { center: { type: "point", prompt: "Pick the center" }, outer: { type: "length", prompt: "Type or click the outer radius", min: 0, nonzero: true }, inner: { type: "length", prompt: "Type or click the inner radius", min: 0, nonzero: true } }, api: 1 }; export default async function main(cad) { const at = { ...cad.editor.workPlane(), origin: await cad.input.point("center") }; const preview = cad.display.show(); const outer = await cad.input.length("outer", { from: at.origin, onChange: (r) => preview.set(Curve.circle(at, r)) }); const inner = await cad.input.length("inner", { from: at.origin, onChange: (r) => preview.set(r < outer ? [Curve.circle(at, outer), Curve.circle(at, r)] : Curve.circle(at, outer)) }); if (!(inner < outer)) throw new Error("The inner radius must be smaller than the outer radius."); return cad.doc.add([Curve.circle(at, outer), Curve.circle(at, inner)]); }
```
While outer is picked one circle follows the pointer; while inner is picked the outer circle stays and the inner one follows; the run adds both circles.
An example with a fixed list, aligning entities by their bounds:
```js
import { Transform } from "aircad"; export const manifest = { name: "Align", kind: "command", description: "Lines entities up on an edge or center of their combined bounds.", capabilities: ["document:read", "document:write"], inputs: { items: { type: "entities", prompt: "Select the entities to align", min: 2 } }, options: { Align: { type: "choice", default: "Left", choices: ["Left", "Center", "Right", "Top", "Middle", "Bottom"], label: "Align", description: "The edge or center to line up on." } }, api: 1 }; export default async function main(cad) { const ids = await cad.input.entities("items"); const boxes = cad.doc.get(ids).map((record) => record.bounds); const lo = { x: Math.min(...boxes.map((b) => b.min.x)), y: Math.min(...boxes.map((b) => b.min.y)) }; const hi = { x: Math.max(...boxes.map((b) => b.max.x)), y: Math.max(...boxes.map((b) => b.max.y)) }; const moves = () => boxes.map((b) => { const a = cad.options.Align; const dx = a === "Left" ? lo.x - b.min.x : a === "Right" ? hi.x - b.max.x : a === "Center" ? (lo.x + hi.x - b.min.x - b.max.x) / 2 : 0; const dy = a === "Bottom" ? lo.y - b.min.y : a === "Top" ? hi.y - b.max.y : a === "Middle" ? (lo.y + hi.y - b.min.y - b.max.y) / 2 : 0; return Transform.translation({ x: dx, y: dy, z: 0 }); }); const ghosts = moves().map((t, i) => cad.display.ghost([ids[i]], t)); await cad.input.confirm({ prompt: "Choose how to align, then press Enter", onChange: () => moves().forEach((t, i) => ghosts[i].setTransform(t)) }); moves().forEach((t, i) => cad.doc.transform([ids[i]], t)); return ids; }
```
The entities show where they will land; changing Align in the options bar moves the preview, and Enter moves them.
Geometry that is easy to get wrong. Build transforms with Transform.translation, rotation, scale, and compose, never as 16 numbers by hand. For an arc whose ends are known (a slot's rounded end), use Curve.arcThroughPoints(start, through, end) with a point on the side it bulges toward, not angles. To center a label on a point, Text.create(characters, plane, { anchor: "center" }). A list of transforms means one copy of all the entities per transform (an array), so it needs { copy: true }: cad.display.ghost(ids, transforms, { copy: true }) previews them and cad.doc.transform(ids, transforms, { copy: true }) makes them. To move each entity by its own transform, call transform (and ghost) once per entity. Declare every capability the code uses: selection to read or change the selection (cad.editor.select), view to fit, open a space, or look at a section; a command that uses one it did not declare fails when it gets there.
Settings and errors. Options are read live as cad.options. An input declared with option: "Height" takes Enter as that option and remembers a value the user gives. Validate with a thrown Error in product language ("The width must be greater than zero."); it ends the command with that message on the command line. A number input's min, max, integer, and nonzero are checked before main sees the value.
Saving. The command is saved into this file in the Scripts panel, not yet on the command line: say so, that they can try it from the Scripts panel, and that Add to Command Line there puts it on the command line. Never say it is on the command line. To change it after they try it, call save_command again with the scriptId from the result and the whole source, manifest included. The file's saved scripts, drafts included, are listed by cad.doc.scripts.list() in a query script, each with its id, name, kind, draft, and whole source: read a script from there before changing or explaining it, rather than from memory.
SHA-256: 86e4a39267cabd27aba956edd2174c1afc5d167e118305c25c938723bed85e43