Skip to main content

Your first generator

A generator is a folder in a git repository with two files: a manifest that lists its parameters, and the code that makes the model.

flange/
pdm-generator.json
flange.ts
flange/pdm-generator.json
{
"schema": "pdm-generator",
"schemaVersion": "1.0.0",
"name": "Flange",
"description": "Flat flange with a bore and a bolt circle",
"runtime": "node",
"entry": "flange.ts",
"parameters": [
{ "name": "outerDiameter", "label": "Outer diameter", "type": "number", "unit": "mm", "default": 120, "min": 40, "max": 500 },
{ "name": "thickness", "label": "Thickness", "type": "number", "unit": "mm", "default": 12, "min": 2, "max": 80 },
{ "name": "holeCount", "label": "Holes", "type": "integer", "default": 6, "min": 3, "max": 24 }
],
"outputs": [{ "key": "flange", "kind": "part" }]
}
flange/flange.ts
import { defineGenerator } from '@classcad/pdm-generator'

export default defineGenerator(async ({ params, api, savePart }) => {
const { outerDiameter, thickness, holeCount } = params
const part = await api.v1.part.create({ name: 'Flange' })
const geometry = await api.v1.part.entityInjection({ id: part, name: 'Geometry' })
const disc = await api.v1.solid.cylinder({ id: geometry, diameter: outerDiameter, height: thickness })
const tools = []
for (let i = 0; i < holeCount; i++) {
const a = (2 * Math.PI * i) / holeCount
const translation = [outerDiameter * 0.35 * Math.cos(a), outerDiameter * 0.35 * Math.sin(a), 0]
tools.push(await api.v1.solid.cylinder({ id: geometry, diameter: 10, height: thickness + 2, translation }))
}
await api.v1.solid.subtraction({ id: geometry, target: disc, tools })
await savePart({ key: 'flange', name: `Flange ${outerDiameter}x${thickness}` })
})

api is the ClassCAD API, the same one documented on classcad.ch.

Into the PDM​

  1. Commit the folder and push it to a public GitHub repository.
  2. In the PDM, make a document of the kind Code.
  3. On its Code tab, give the repository's URL, a ref (main, a tag or a commit) and the directory (flange). Save, then Sync.
  4. Execute shows the form. Fill it in, run it, and the flange shows up as a document in the generator's folder.

When the generator does what it should, make a version of the code document and release it. Parts it made can only be released once the code that made them is.