mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 10:47:31 -06:00
* Expose xdata through entities, properties.set and AutoLISP's -3 group An entity's extended data was read, kept and written back (#528) but invisible to the API: `entities` left it out, `properties.set` had no field for it, `entget` never returned the -3 group and `entmod` dropped it. Now `entities {xdata: true | "APP"}` and `properties` list it per application with values typed by group code, `properties.set {xdata}` replaces or removes one application's data after checking codes, values, braces and sizes, and AutoLISP's entget/entmod/entmake carry the -3 group, with `regapp` and the APPID table. The applications CADCraft writes from its own model are reserved (one list, now in cadcraft_doc, shared with the DXF reader). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Check xdata before any change in properties.set; cap entmake's xdata across applications Review follow-ups on #2469: - properties.set validated xdata after ensure_layer, so a request naming a new layer and carrying bad or oversized xdata left the layer behind on failure. The xdata checks now run first. - entmake capped each application's groups but not their sum; an entity could exceed MAX_XDATA_GROUPS across applications. The total is checked as entmod already does, with a test. - docs: the control-protocol xdata section is an h2 like its neighbours; the file-format-parity revision row no longer has a pipe inside a code span. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * properties.set checks every handle first; a dotted -3 group is a bad list Review follow-ups on #2469: - properties.set edited the objects before a handle that names nothing and then failed, keeping those edits (and a new layer). Every handle is checked before anything changes; a missing one is refused with its hex. - xdata_from_list read `(-3 . 7)` as "no applications" and let entmake/entmod succeed with nothing set. A -3 whose cdr is not a list is refused (listp), so both return nil as for any bad list. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * An xdata application's groups must be a proper list Review follow-up on #2469: `("ACME" . 7)` in a -3 group read as "no groups", which `entmod` takes as "remove ACME's data", and a dotted tail after real groups was dropped. The application's tail is checked as a list (listp) like the -3 group itself, so entmake/entmod answer nil and change nothing; test covers both shapes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Runs of one xdata application in any case are one application in the API Review follow-up on #2469: a file can carry `ACME` and `acme` runs on one entity. `entities` showed them as two JSON keys and `entget` as two lists, and writing either back through properties.set or entmod replaced the application twice, keeping only the last run's groups. Both projections now merge runs case-insensitively (first spelling, all the groups), as set_xdata_app already treats them; tests round-trip such an entity through both APIs. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Refuse raw ACAD xdata on a dimension, whose ACAD run is written from its overrides Review follow-up on #2469 (austin62jake): properties.set accepted `xdata: {"ACAD": …}` on a dimension and `entities` showed it, but the DXF writer emits the dimension's ACAD run (DSTYLE) from its overrides and the reader models it back, so the raw groups vanished on save. ACAD can't join RESERVED_XDATA_APPS (files carry ACAD data on other entities that CADCraft keeps raw), so the rule is per entity: `is_model_xdata_app(name, kind)` is the reserved list on any entity plus ACAD on a dimension. properties.set refuses it before anything changes, entmake/entmod answer nil. Tests cover both paths, including that ACAD on a line survives a DXF round trip and the dimension's own override does too. control-protocol.md says so. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(lisp): return and accept REAL for xdata group 1071 --------- Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: FredApps <fredapps@users.noreply.github.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
241 lines
20 KiB
Markdown
241 lines
20 KiB
Markdown
# CADCraft control protocol
|
||
|
||
Start the desktop app with `--control PORT` (or `CADCRAFT_CONTROL_PORT=PORT`). It listens on
|
||
`127.0.0.1:PORT` for JSON lines: one request per line, one reply per line. A port given with
|
||
`--control` wins over the variable; a bare `--control` keeps the variable's port.
|
||
|
||
```json
|
||
{"id": 1, "method": "cmdline.input", "params": {"text": "circle 0,0 5"}, "token": "<session token>"}
|
||
{"id": 1, "ok": true, "result": {"prompt": "Command:", "output": ["Command: CIRCLE"], ...}}
|
||
```
|
||
|
||
Every request must carry the session `token`. Because loopback TCP cannot tell local operating
|
||
system users apart, the app writes an unguessable per-session token to an owner-only file when the
|
||
control server starts, and refuses (`unauthorized`) any request without a matching token. The file
|
||
is `cadcraft-control-<port>.token` in the per-user runtime directory (`$XDG_RUNTIME_DIR`, else
|
||
`~/.cache`; `%LOCALAPPDATA%` on Windows); its path is logged at startup. A client reads the token
|
||
from that file — which requires the desktop user's own filesystem access — and includes it on each
|
||
line. The MCP bridge (`cadcraft-cli mcp --connect`) does this automatically.
|
||
|
||
The server checks the token only once it has read the whole line, so until a request on a
|
||
connection has carried the right token, a line may be at most 64 KiB; a longer one gets an error
|
||
reply and the connection is closed. To send a longer request (a script, a drawing as base64) on a
|
||
new connection, first send a short one with the token, such as `ping`, and wait for its reply.
|
||
The MCP bridge does this too.
|
||
|
||
Errors come back as `{"id": …, "ok": false, "error": "message"}`. An unknown method's error points
|
||
to `control.methods`, which lists the methods the server answers.
|
||
|
||
A line that is not a JSON object with a string `method` (an HTTP request, text that isn't JSON,
|
||
bytes that aren't UTF-8) gets one error reply and the server closes the connection; nothing sent
|
||
after it runs. Blank lines are skipped.
|
||
|
||
The server bounds what a client can hold: 16 connections at once (more get an error line and are
|
||
closed), 256 MiB per request line (64 KiB until the connection has shown the token; longer closes
|
||
the connection), 64 queued requests (more are
|
||
answered `{"ok": false, "error": "busy: …", "state": "not-run"}`), and 10 minutes idle or 30 seconds
|
||
per blocked reply write before the socket is closed.
|
||
|
||
The server waits up to 60 s for the app to answer a request. After that it replies with a timeout, and
|
||
`state` tells whether sending the request again is safe:
|
||
|
||
- `{"id": …, "ok": false, "error": "timeout", "state": "not-run"}`: the app had not started the request.
|
||
It is cancelled and will never run, so sending it again is safe.
|
||
- `{"id": …, "ok": false, "error": "timeout", "state": "may-have-run"}`: the app had already started it.
|
||
It may still finish and change the drawing, and its late result is discarded. Inspect the drawing
|
||
(`drawing.inspect`) before sending it again.
|
||
|
||
The table below describes top-level control-protocol methods: names for the request's `method`
|
||
field. `engine.commands` returns a separate command catalog, not a list of additional methods.
|
||
|
||
| Method | Params | What it does |
|
||
|---|---|---|
|
||
| `cmdline.input` | `{text}` | Type a line at the command line exactly like a user. Starts commands by name/alias and answers prompts (points `x,y`, `@dx,dy`, `@d<a`, distances, keywords, empty = Enter). Spaces act as Enter except at text prompts. |
|
||
| `cmdline.script` | `{text}` | Run a multi-line script (like `.scr` files). |
|
||
| `cmdline.key` | `{key: "enter"\|"escape"}` | Press Enter or Escape. Replies like `cmdline.input`: the state, `output`, and `error` when Enter was refused. |
|
||
| `cmdline.state` | | Current prompt, keywords, accepted input kinds, history tail. |
|
||
| `engine.execute` | `{command, params}` | Run any command with JSON parameters. Never opens a dialog. `command` is an alias of this method, with the same parameters and result. |
|
||
| `engine.commands` | | Every command: id, label, menu path, shortcut, aliases, params doc, enabled. |
|
||
| `drawing.inspect` | `{entities?, limit?}` | Drawing summary (counts, extents, layers, styles, blocks, layouts, selection, undo, and what reading the file left out: `skipped` records by section, `readNotes` in words) and entities with handles and geometry. |
|
||
| `ui.inspect` | | UI state, canvas rect, view, cursor and snap, performance counters (`uiFrames`, `passIndex` included); `closePrompt`: the drawing a "Save changes?" prompt is asking about (null when none); and actual egui keyboard focus (`focus.id`, `focus.label`, `focus.role`). |
|
||
| `ui.elements` | `{label?, role?, focused?}` | Observed toolbar, tab, drafting and layer-palette controls from the latest UI frame. Filters match exactly. Each entry has its real egui `id`, label, role, rectangle `[x,y,width,height]`, enabled/visible/selected state and current focus. |
|
||
| `ui.menu.list` / `ui.menu.invoke` | `{command}` | The menu tree; invoke an item like a click (interactive). |
|
||
| `ui.pointer` | `{x, y, space?: "world"\|"screen", button?, action?: "click"\|"move"}` | Click in the drawing area (world coordinates by default). Right button = Enter. |
|
||
| `ui.click` / `ui.move` | `{x, y, button?, shift?, ctrl?}` | Real egui pointer events in screen points (reach every widget). |
|
||
| `ui.drag` | `{x, y, to: [x, y], button?}` | Real press, move and release (drag the command line's top edge, the ViewCube ring…). |
|
||
| `ui.key` / `ui.text` | `{key, cmd?, shift?, alt?}` / `{text}` | Synthetic keyboard input. |
|
||
| `ui.set` | UiState fields | Show/hide palettes, toolbars, command line, ViewCube…; `theme: "system"\|"light"\|"dark"` (also `engine.execute` with `ui.theme {theme}`); `ui.inspect` reports the theme shown (`theme`: light/dark). `animations: "system"\|"on"\|"off"` (also `ui.animations {animations}`) shows or hides animation effects (dialog and menu fades, smoothed scrolling), `system` following the operating system's setting; `ui.inspect` reports whether they show (`animations`: true/false). `saveFormat: "dxf"\|"dwg"` (also `ui.saveformat {format}`) is the format Save As suggests for new drawings; `saveVersion: "2000"`…`"2018"` (also `ui.saveversion {version}`) the version it saves them in (SAVEAS also takes `version` and `binary` directly). |
|
||
| `ui.resize` | `{width, height}` | Resize the window. |
|
||
| `ui.focus` | | Activate the native window. Does not assign focus to a widget. |
|
||
| `ui.screenshot` | `{path?}` | PNG of the window (needs a presented frame). Bounded: at most 4 queued/in-flight/completed requests, 64 MiB per image and 64 MiB total (completed but undrained images still count), an 8 s deadline, and cancellation when the document or keyboard focus changes while the request is outstanding. A queued request adopts the settled window size when issued. After issue, a change in window size, scale, OS window focus, minimization or occlusion cancels it. Every reply carries `request`; failures include `ok:false,error`. Files are written only after the deadline and identity checks pass. Without `path`, each request gets a unique PNG in a private owner-only per-process directory under the temp dir (created exclusively; an existing symlink is never followed), so concurrent app instances cannot collide; the caller owns those files and should remove them when done. In the web build there is no file system: `path` is ignored and the PNG comes back inline (`pngBase64`, `width`, `height`, `bytes`). |
|
||
| `ui.render` | `{path?, width?, height?, fit?, window?, background?, plot_style?}` | Headless render. `fit` (default true) frames the whole drawing; `false` uses the current view; `window` `[[x0, y0], [x1, y1]]` shows just that area (drawing units). `background`: `"dark"` (the canvas; the default without a plot style), `"white"` (the default with one, as a plot prints on paper), `"paper"` or `"#rrggbb"`; colour 7 is drawn white on a dark background and black on a light one, as on a plot. `plot_style`: a plot style table, built-in (`monochrome.ctb` prints every colour black, `grayscale.ctb` grey; `default.ctb`, `default.stb`, `monochrome.stb`) or one the drawing holds; `"none"` (or `""`) draws every object with its own properties, also in a layout whose page setup shows plot styles (without `plot_style` such a layout draws with its table, as on screen). An unknown `background` or `plot_style` is refused. Replies `{pngBase64, width, height, bytes, path?}`. |
|
||
| `app.open` / `app.save` / `app.quit` | `{path}` | File operations. |
|
||
| `control.methods` | | Every method this server answers: `{method, params, description}`. Headless (`cadcraft-cli mcp`) lists only the methods it serves. |
|
||
| `ping` | | Replies `"pong"`. A cheap health check. |
|
||
|
||
To run a command listed by `engine.commands`, put its id in `params.command` and its arguments
|
||
in the nested `params.params` object, using `engine.execute` as the top-level method. This
|
||
includes `open` and `document.bytes`, and the UI commands in the desktop app's catalog.
|
||
`drawing.inspect` is also an engine command, but has an explicit top-level method; a dotted
|
||
command id alone does not make it a protocol method.
|
||
|
||
For example, inspect the drawing and list commands directly, then open a drawing and get its
|
||
DXF bytes through `engine.execute` (one request per line):
|
||
|
||
```json
|
||
{"id":2,"method":"drawing.inspect","params":{}}
|
||
{"id":3,"method":"engine.commands","params":{}}
|
||
{"id":4,"method":"engine.execute","params":{"command":"open","params":{"path":"examples/apartment.dxf"}}}
|
||
{"id":5,"method":"engine.execute","params":{"command":"document.bytes","params":{}}}
|
||
```
|
||
|
||
The `open` example assumes the app was started from the repository root; otherwise use a path
|
||
to an existing drawing accessible to the app. `document.bytes` defaults to DXF and returns
|
||
base64-encoded bytes in the result's `data` field. The `command` alias can be used identically:
|
||
|
||
```json
|
||
{"id":6,"method":"command","params":{"command":"document.bytes","params":{}}}
|
||
```
|
||
|
||
Sending `open` or `document.bytes` as the top-level `method` returns an `unknown method` error:
|
||
the protocol dispatches methods explicitly and does not fall through to the command registry.
|
||
`app.open` is a top-level convenience method for opening a drawing with `{path}`.
|
||
|
||
To read selected objects as structured geometry in a single query, execute `entities` with
|
||
`selected: true` (a boolean; omitted/false keeps the unfiltered selection behavior):
|
||
|
||
```json
|
||
{"id":7,"method":"engine.execute","params":{"command":"entities","params":{"selected":true,"limit":1000}}}
|
||
```
|
||
|
||
Only selected entities in the active space are returned; an empty selection returns an empty
|
||
page. The selection filter combines with `type`, `layer` and `window` before `offset`/`limit`.
|
||
The existing response is unchanged: `count` is all matches, `returned` is the page size and
|
||
`entities` contains handles, layers and geometry. This query does not change selection, drawing
|
||
contents or undo history. `entities` is a command, not a top-level protocol method.
|
||
|
||
## Extended data (xdata)
|
||
|
||
Other applications' extended data travels with an entity (it is read from the file, kept on
|
||
copies and written back with an APPID record for each application). `entities` leaves it out
|
||
unless asked: `xdata: true` adds an `xdata` object to each entity that has any, by application,
|
||
each group as a `[code, value]` pair typed by its code — strings for 1000, 1002 (`{` / `}`),
|
||
1003 (a layer name), 1004 (hex) and 1005 (a handle), a point `[x, y, z]` for 1010–1013 (its
|
||
1020 and 1030 groups folded in), numbers for 1040–1042, integers for 1070 and 1071. An
|
||
application name (or a list of names) instead of `true` keeps only the entities carrying that
|
||
application's data and shows just it. `properties` lists every application's data of the
|
||
objects it reports.
|
||
|
||
```json
|
||
{"id":8,"method":"engine.execute","params":{"command":"entities","params":{"xdata":"PE_URL","limit":100}}}
|
||
{"id":9,"method":"engine.execute","params":{"command":"properties.set","params":{"handles":["2A"],"xdata":{"ACME":[[1000,"D-07"],[1070,3],[1010,[1.5,2.5,0]]],"PE_URL":null}}}}
|
||
```
|
||
|
||
`properties.set` takes the same form: each application named is replaced by the groups given
|
||
(their `1001` left out) or removed by `null` (or an empty list); applications not named stay.
|
||
Names, codes, value types and ranges, brace nesting and sizes (16,384 groups per object, 255
|
||
bytes per string) are checked and a bad `xdata` changes nothing. The applications CADCraft
|
||
writes from its own model (`CADCRAFT`, `AcadAnnotative`, the layer transparency and
|
||
description applications) can't be set this way, nor can `ACAD` on a dimension, whose `ACAD`
|
||
run holds its dimension-style overrides (set those through `textHeight`, `arrowSize` and the
|
||
other dimension properties); `ACAD` data on any other object is kept as it is. The change is
|
||
one undo step, like any other property edit. In AutoLISP the same data is the `-3` group of `entget`, `entmod` and `entmake`
|
||
(see [autolisp.md](autolisp.md)).
|
||
|
||
The MCP server (`cadcraft-cli mcp --connect 127.0.0.1:PORT`) wraps this protocol; headless
|
||
(`cadcraft-cli mcp`) it serves the same methods from an in-process session.
|
||
MCP tool names are another interface: `execute` maps to `engine.execute`, `list_commands` to
|
||
`engine.commands`, and the MCP `open` tool to `app.open`. See [mcp.md](mcp.md).
|
||
|
||
`ui.key` queues real egui key events; its reply acknowledges the queue. Poll `ui.inspect` or
|
||
`ui.elements` for the resulting focus or state before sending the next action. Use Tab and
|
||
Shift-Tab to traverse controls and Enter/Space to activate them. The observed registry is not
|
||
the operating system's accessibility tree and does not establish physical input or screen-reader
|
||
acceptance. Unregistered controls still report their actual focus ID, with a null label/role.
|
||
|
||
The native host restores layout choices from `workspace.json` and saves changes atomically.
|
||
It stores palette/toolset visibility, selected toolset, collapsed groups, command-line history
|
||
height and related chrome choices. It does not store drawings, command buffers, dialogs or the
|
||
current Start tab. The default directory is `~/Library/Application Support/CADCraft` on macOS,
|
||
`%APPDATA%/CADCraft` on Windows, and `$XDG_CONFIG_HOME/cadcraft` or `~/.config/cadcraft` elsewhere.
|
||
Set `CADCRAFT_CONFIG_DIR` to a private directory for an isolated profile; an explicitly empty
|
||
value disables all settings reads and writes (workspace, logs, autosave, plotters). An invalid, oversized or newer-version file is
|
||
reported and left untouched, with persistence disabled for that launch. Repair or move that
|
||
file before restarting to resume saving workspace choices.
|
||
|
||
## Panel docking
|
||
|
||
Tool Sets, Layers, and Properties can be regrouped, split, floated within the app window, closed, and reopened. The drawing canvas stays visible and cannot be closed or turned into a tab. The document/session view is protected. Layouts, active tabs, panel visibility, and floating rectangles are saved with the app's existing UI preferences. These operations do not edit document contents.
|
||
|
||
Panel IDs: `toolSets`, `layers`, `properties`, `externalReferences`. The protected center is `canvas`.
|
||
|
||
`ui.dock` accepts `operation`; basic per-panel operations also require `panel`. The advanced `resizeSplit` operation below uses a split `path` instead of a panel. Basic operations are `open`, `close`, `activate`, `float` (with finite positive `rect: [x, y, width, height]`), or `move` (with `target` and `zone: center|tab|left|right|top|bottom|above|below`, default `center`). Center and tab group with the target; side zones split beside it; above and below add a group in a floating window. `open` and `activate` show a hidden panel at its last place and select it. `ui.dock.reset` restores the default arrangement. Invalid moves return an error without changing the layout.
|
||
|
||
The QuickCalc palette (`quickCalc`) is not part of the default arrangement. Typed `QUICKCALC`/`QC`
|
||
and `ui.panel.quickcalc` explicitly open and activate it: it floats the first time and later
|
||
reopens at its saved location. **Window > QuickCalc** and **Ctrl/Cmd+8** invoke
|
||
`ui.toggle.quickcalc` instead, closing the panel when present and reopening it otherwise.
|
||
The shortcut also works with text-field focus. Toggling preserves the running drawing command
|
||
and pending command-line input. The expression, history and calculator variables survive
|
||
closing/reopening within the same session; they are not saved across application restarts.
|
||
|
||
The External References palette (`externalReferences`) is not part of the default arrangement
|
||
either. Typed `XREF`/`XR` opens and activates it. **Window > Reference Manager** (also
|
||
`ui.menu.invoke` with `xref`) opens it too, but closes it when it is present, as Window >
|
||
QuickCalc does; closing leaves the running command and command-line input alone. `xref` with
|
||
JSON parameters lists the references and never opens or closes the palette.
|
||
|
||
Invoke these UI commands through `engine.execute`, for example `{"command":"ui.dock","params":{"operation":"float","panel":"layers","rect":[100,100,320,440]}}`.
|
||
|
||
Advanced `ui.dock` operations use the same validated transaction as pointer gestures:
|
||
`moveFloating` updates `rect`, `resizeSplit` accepts a boolean child `path` and
|
||
`size` (`{"Ratio":0.5}`, `{"FixedFirst":280}`, or `{"FixedSecond":280}`),
|
||
`setStackOpen` takes `open`, and `resizeStack` takes a point `height` or null.
|
||
A center or tab `move` can include `before` (panel ID or null to append) for tab order.
|
||
The equivalent `{"action": ...}` form accepts a serialized shared docking action;
|
||
this is also the form emitted by the renderer, and it takes every action of craft-ui's docking, group, column and window moves included. Protected-canvas and app-specific panel
|
||
rules apply equally to both forms.
|
||
|
||
## Command-line replies
|
||
|
||
The app and the headless session reply with the same fields.
|
||
|
||
`cmdline.state`, `cmdline.key` and `cmdline.input` return the command-line state:
|
||
|
||
| Field | Value |
|
||
|---|---|
|
||
| `prompt` | The prompt text (`"Command:"` when idle). |
|
||
| `running` | The id of the running command, or `null`. |
|
||
| `keywords` | The current prompt's options. |
|
||
| `accept` | The kinds of input the current prompt accepts, or `null` when idle. |
|
||
| `buffer` | Text typed in the command line but not yet submitted (always `""` headless). |
|
||
| `history` | The last 20 history lines. |
|
||
| `historyExpanded` | Whether the history is expanded (always `false` headless). |
|
||
|
||
`cmdline.input` adds:
|
||
|
||
- `output`: the history lines the input added. This includes the error message when the line was refused.
|
||
- `error`: the error message, only when the line was refused (unknown command, invalid input…).
|
||
|
||
A refused line still replies `"ok": true`, because the text was typed. Check `error`.
|
||
|
||
`app.open` without a string `path` fails with `"missing path"` before any command runs.
|
||
|
||
## Interface language
|
||
|
||
Select a language with `engine.execute` and the UI command `ui.language`:
|
||
|
||
```json
|
||
{"id":1,"method":"engine.execute","params":{"command":"ui.language","params":{"lang":"uk"}}}
|
||
```
|
||
|
||
Supported values are `auto`, `en`, `uk`, `pt-BR`, `de`, `es`, `cs`, `ru`, `fr`, `zh`, `it`, `ko`, `sv`, `ar`, `hu`, `tr`, `bs`, `el`, `ka` and `ta`.
|
||
Chinese needs an installed CJK font, Arabic an installed Arabic font, Tamil an installed Tamil font
|
||
or the `Taml` face from craft-fonts; Korean needs a font covering the Korean interface from the
|
||
optional craft-fonts input or the operating system; Georgian needs the `Geor` face from craft-fonts (an installed font
|
||
does not count). Without a suitable font, the saved choice is
|
||
retained and the result's `shown` is `en`. `ui.set` also accepts
|
||
`{"interfaceLanguage":"uk"}`; `ui.inspect` reports the saved choice in its UI state.
|
||
Invalid choices leave the current language unchanged. Menus, command labels and prompt text
|
||
returned by the control channel retain their English source text. See [localization.md](localization.md).
|