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
- In PhysCAD, open Preferences, Labs and turn on Third-party plugins.
- Download the example plugin and install it: Preferences, Plugins, Install plugin…, then pick the file.
Download Hello Box (example) - Click its toolbar button, type a size and click Make box. That is a whole plugin: a command, a button and a panel.
- 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.
| File | What it is |
|---|---|
plugin.json | Its name, id, version, permissions and panels. |
main.js | Its code: registers commands and toolbar buttons. |
panel.html, panel.js | A panel (optional), with the code behind it. |
icon.svg | Its 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" } }
}
- id: reverse-domain, lower case, unique to you (
com.yourname.thing). - version: x.y.z. Raise it every time you share a new copy.
- place:
"right"(the right-hand column) or"floating"(withwidthandheightin px). - Unknown fields are refused, so a typo shows up as an error when installing.
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"); }
});
<pc-number>is the app's value box: you can drag it, step it with the arrow keys, and it shows the document's units. Its.valueis always in mm. Listen toinput(while dragging) orchange(when set). It takesmin,maxandstep.- Also in the kit:
<pc-row>,<pc-segmented>,<button class="primary">,<div class="actions">. - Inline handlers like
onclick="..."don't run. Put your code in a<script src="panel.js">.
The API
Everything is on the global physcad, and every call returns a Promise.
| Call | What 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, toast | Buttons, 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/keys | Your plugin's own saved settings (256 KB). |
physcad.files.open/save/pickFolder/read/write/list | Files, 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.
| Permission | Lets the plugin |
|---|---|
model.read | See the document and the selection, inspect bodies. |
model.write | Change the model. |
cam | Work with CAM setups and toolpaths. |
view | Move the camera and change the selection. |
files | Open and save files. |
ui | Add toolbar buttons and panels. |
network | Reach the https domains listed in "network". |
Plugins can never change PhysCAD's settings, other plugins or the license.
Rules
- One file of code.
main.jsis a single script:importandrequiredon't work. TypeScript is fine if you compile it to one file. - No page, no Node, no direct internet in main.js. It runs in a sandboxed worker. Use
physcad.net.fetchfor the internet. - Lengths are mm and angles are degrees in every command, whatever units the document shows.
- Don't block. A plugin silent for 15 seconds, or one command running past 60 seconds, is stopped. Five errors in a minute stop it too, and a plugin stopped three times is turned off. Do long work in pieces with
await. - Check your inputs and throw short, plain errors ("The size must be more than 0").
Testing (Developer mode)
- Keep your plugin as a folder while you work on it.
- In Preferences, Plugins, click Developer: load from folder… and pick the folder.
- 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
- Zip the folder's contents (plugin.json at the top of the zip) and rename it to
yourplugin.physcadplugin. - 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.
- 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.