Building a PhysCAD plugin

Plugins add your own commands, toolbar buttons and panels to PhysCAD. They are plain JavaScript, run in a sandbox, and need no build tools. Plugins are a Labs feature: they may change as we go.

Getting started

  1. In PhysCAD, open Preferences, Labs and turn on Third-party plugins.
  2. Download the example plugin and install it: Preferences, Plugins, Install plugin…, then pick the file.
    Download Hello Box (example)
  3. Click its toolbar button, type a size and click Make box. That is a whole plugin: a command, a button and a panel.
  4. Unzip the file (it is a normal zip) to see its five small files, then change them to make your own.

What a plugin is made of

A plugin is a folder with these files. To share it, zip the folder's contents and rename the zip to name.physcadplugin.

FileWhat it is
plugin.jsonIts name, id, version, permissions and panels.
main.jsIts code: registers commands and toolbar buttons.
panel.html, panel.jsA panel (optional), with the code behind it.
icon.svgIts icon (optional).

plugin.json

{
  "id": "com.yourname.hello-box",
  "name": "Hello Box",
  "version": "1.0.0",
  "author": "Your name",
  "description": "One or two plain sentences: what it does.",
  "apiVersion": 1,
  "main": "main.js",
  "icon": "icon.svg",
  "permissions": ["model.write", "ui"],
  "network": [],
  "ui": [{ "id": "panel", "title": "Hello Box", "html": "panel.html", "place": "right" }],
  "translations": { "de": { "Make box": "Box erstellen" } }
}

main.js

This is the whole Hello Box plugin. It registers a command that makes a cube, and a toolbar button that opens its panel.

physcad.commands.register({
  id: "makeBox",
  title: "Make box",
  description: "Make a cube at the origin. size: its edge length in mm.",
  params: { type: "object", properties: { size: { type: "number" } }, additionalProperties: false },
  async run({ size = 20 }) {
    if (!(size > 0)) throw new Error("The size must be more than 0");
    await physcad.commands.run("model.box", { origin: [0, 0, 0], size: [size, size, size] });
    await physcad.storage.set("lastSize", size);
    await physcad.ui.toast(physcad.tx("Made a {0} mm box", size), "success");
    return { size };
  },
});
physcad.commands.register({ id: "open", title: "Hello Box", run: () => physcad.ui.openPanel("panel") });
physcad.ui.addToolbarButton({ command: "open", tip: physcad.tx("Hello Box") });

Everything one command changes in the model is one Undo step, named after your plugin.

Panels

Panels use PhysCAD's own look through a small UI kit, so your controls look and work like the app's.

<p class="note">Makes a cube at the origin.</p>
<pc-row label="Size"><pc-number id="size" kind="length" value="20" min="0.01"></pc-number></pc-row>
<div class="actions"><button class="primary" id="make">Make box</button></div>
<script src="panel.js"></script>
const size = document.getElementById("size"), make = document.getElementById("make");
physcad.storage.get("lastSize").then((v) => { if (typeof v === "number") size.value = v; });
make.addEventListener("click", async () => {
  try { await physcad.commands.run("makeBox", { size: size.value }); }
  catch (e) { await physcad.ui.toast(e.message, "error"); }
});

The API

Everything is on the global physcad, and every call returns a Promise.

CallWhat it does
physcad.commands.register({...})Adds a command of your own.
physcad.commands.run(name, args)Runs any PhysCAD command your permissions allow (model.box, model.extrude, sketch.create, cam.*, ...), or one of your own by its short name.
physcad.ui.addToolbarButton, openPanel, closePanel, toastButtons, panels and messages.
physcad.model.selection(), document()Reads what's picked and the document.
physcad.events.on(name, fn)selectionChanged, documentOpened, documentSaved, toolChanged, unitsChanged.
physcad.storage.get/set/remove/keysYour plugin's own saved settings (256 KB).
physcad.files.open/save/pickFolder/read/write/listFiles, through PhysCAD's dialogs or folders the person approved.
physcad.net.fetch(url, options)Internet access to the domains you list (desktop app only).
physcad.units(), physcad.tx(text)The document's units, and your translations.

PhysCAD's commands are the same as its AI (MCP) tools, so the tool list your AI assistant sees is also the list of commands a plugin can run.

Permissions

Ask only for what you use. People see the list in plain words before they install.

PermissionLets the plugin
model.readSee the document and the selection, inspect bodies.
model.writeChange the model.
camWork with CAM setups and toolpaths.
viewMove the camera and change the selection.
filesOpen and save files.
uiAdd toolbar buttons and panels.
networkReach the https domains listed in "network".

Plugins can never change PhysCAD's settings, other plugins or the license.

Rules

Testing (Developer mode)

  1. Keep your plugin as a folder while you work on it.
  2. In Preferences, Plugins, click Developer: load from folder… and pick the folder.
  3. Edit the files: PhysCAD reloads the plugin each time you save. Errors show under the plugin in Preferences, Plugins.

Building one with an AI

You don't need to be a programmer. Point your AI assistant at this page, describe what you want the plugin to do, and ask it to write the files. If your assistant is connected to PhysCAD through MCP, it can load your plugin folder in Developer mode, test its commands and read its errors itself.

Sharing your plugin

  1. Zip the folder's contents (plugin.json at the top of the zip) and rename it to yourplugin.physcadplugin.
  2. Post it in the plugins forum on the PhysCAD Discord: one thread per plugin, with what it does and a screenshot. Questions and updates go in the same thread.
  3. Others install it with Preferences, Plugins, Install plugin…. Plugins that aren't signed by PhysCAD show a warning before they install, so only install plugins from people you trust.