Repository navigation
Plugin development
Advanced topic. This page is for plugin authors and developers.
State: the app has the wasm loader (integrations/plugin-wasm) and installs packages from a local folder (integrations/plugin-store, cgeocad plugin install). The install window of the store and a download from the internet come later.
A CgeoCAD plugin is a Rust crate that implements the Plugin trait of cgeocad-plugin-api. The same source compiles two ways:
- As a Rust crate. CgeoCAD links it into the app (the
builtinloader). Use this for fast debug and normal tests. - As a WebAssembly module. CgeoCAD runs it in a sandbox (the
wasmloader). The store gives plugins in this form.
A plugin can also bring a SCAD library, data files and templates. A plugin with only a SCAD library needs no code (runtime = "none").
plugins/<id>/
plugin.toml the manifest
Cargo.toml the plugin crate (pure, no WASM code)
src/ the Plugin implementation
scad/cgeocad/<id>.scad the SCAD library: use <cgeocad/<id>.scad>
data/ data files of the manifest
templates/ .scad files for the start page
examples/ example models
wasm/ a small cdylib crate for the module
The crate of the plugin depends only on cgeocad-plugin-api and on other crates of libs/. It does no I/O. All access to the outside goes through the Host that each call gets: log, progress, store, shape meshes, OpenSCAD builds and fetch.
The folder plugins/<id>/wasm/ has a crate of type cdylib with one line of code:
cgeocad_plugin_sdk::export_plugin!(cgeocad_plugin_slotted_angle::SlottedAngle);The type must implement Plugin and Default. On other targets than wasm32 the macro makes nothing, so cargo test --workspace builds the crate on the host.
rustup target add wasm32-unknown-unknown # one time
scripts/build-plugins.sh # all plugins
scripts/build-plugins.sh slotted-angle # one pluginThe script builds each wrapper crate for wasm32-unknown-unknown with the profile plugin (release, opt-level = 3, full LTO, strip) and simd128 in target/plugins/. Then it writes the store package dist/plugins/<id>-<version>.cgeocad-plugin.
The package is a zip file:
| Path | Content |
|---|---|
plugin.toml |
The manifest, with runtime = "wasm" (or "none") |
plugin.wasm |
The module |
scad/ |
The SCAD library |
data/, templates/, examples/, docs/
|
The other files |
README.md, LICENSE
|
Text for the user |
Other paths are refused. The same files give the same bytes, so a package has a stable SHA-256.
scripts/make-plugin-index.sh # unsigned
CGEOCAD_PLUGIN_KEY=path/to/key scripts/make-plugin-index.shThe script writes dist/plugins/index-1.json. Each version has the contract version, the URL, the SHA-256, the size, the licence and the minisign signature. The JSON Schema is https://cad.cgeosoft.com/schema/plugin-index-1.json. With CGEOCAD_PLUGIN_KEY, the script signs each package with minisign -S first. The signing key is not in the repository.
CgeoCAD installs a package only when the SHA-256 and the signature agree with the index and the store key. A changed byte stops the install.
A library uses only its own folder and the core library:
-
includeanduseofcgeocad/core.scad,cgeocad/core/...,cgeocad/<id>.scad,cgeocad/<id>/...or a file of the package relative to the file. -
importandsurfaceof a file of the package relative to the file, with a plain text path. - No absolute paths, no
.., no files of other plugins.
Templates and examples are models of the user. They can use other plugins, but also without absolute paths and ...
cgeocad plugin dev plugins/slotted-angle # the files and the SCAD warnings
cgeocad plugin dev plugins/slotted-angle rack.scad --wasm FILE # and the analysis of a modelcgeocad plugin dev FOLDER reads a plugin folder with the layout of a package. The first call counts the files. Each later call tells the files that changed since the call before, and the findings of the SCAD check as warnings. A dev plugin has no signature check, because you chose the folder.
With a model file, the command runs the analysis of the plugin on the model one time and writes the rows and findings as JSON. The code is the module of --wasm FILE (for example the output of scripts/build-plugins.sh) or FOLDER/plugin.wasm. Without a module, the builtin code of the plugin id runs with the SCAD library and data of the folder. The model does not need to include the plugin: the command includes it for this run only and writes nothing to its .yml.
A reload in the window after each change comes later.
The module is a plain core module, not a WebAssembly component. Version 1 of the ABI:
| Export | Type | What it does |
|---|---|---|
memory |
memory | The linear memory |
cgeocad_abi |
() -> i32 |
Gives 1 |
cgeocad_alloc |
(len) -> ptr |
Gives a buffer. 0: no memory |
cgeocad_free |
(ptr, len) |
Releases a buffer |
cgeocad_call |
(op, ptr, len) -> i64 |
Runs one function of the contract |
The ops are 1 describe, 2 analyze, 3 panel, 4 event, 5 act and 6 document. The input is JSON: {}, { "facts", "settings" }, { "id", "state" }, { "id", "event", "state" } or { "id", "input" }. The result of cgeocad_call is the pointer (high 32 bits) and the length (low 32 bits) of the reply. The reply starts with a tag byte: 0 and the JSON value, or 1 and the JSON of a failure { "code", "text" }.
The imports of the module cgeocad are the host functions of the contract: log, progress, store_get, store_set, shape_mesh, build_scad and fetch. The JSON shapes are those of the serde types of cgeocad-plugin-api. Thus the WIT file libs/plugin-api/wit/cgeocad-plugin.wit stays the description of the contract.
The SDK makes all of this. A plugin author writes only the Plugin implementation.
| Limit | Value |
|---|---|
| Files, clock, network, processes | None. No WASI imports |
| Memory of one instance | 256 MB (the host can change it) |
| Time of one call | 10 s (the host can change it) |
| Cancel |
progress gives false. The plugin has 200 ms to return |
fetch |
Only https URLs of the hosts in [permissions] net
|
build_scad |
Only with [permissions] engine = true
|
A trap, a timeout or too much memory stops only that instance of the plugin. The call gives a failure with the code trap, timeout, memory or cancelled, and the host can make a new instance. The geometry does not depend on plugin code, because it comes from OpenSCAD.
analyze must be pure: the same facts and settings give the same result. The host can run more instances of one module at the same time, for example for the layers of the slicer. Each instance starts with Default::default().
The test suite of CgeoCAD is private. It builds the modules of the plugins of this repository and compares them with the builtin plugins: the JSON of all functions must be equal. Test your own plugin as a normal Rust crate, with cargo test on the host. cgeocad plugin dev FOLDER FILE.scad --wasm FILE runs the analysis of your module on a model.
Home · Glossary · Problems and answers · Download · Website
The source of these pages is the docs/wiki/ folder of the repository. Changes that you make on the wiki website are lost at the next update.
Start
Look at the model
Furniture
Projects
More
Advanced