Model Files
A project has three important files:
text
my-print/
cadscript.config.ts
model.ts
package.jsonCreate one with cadscript init ./my-print.
cadscript.config.ts selects the model, parameters, and dedicated Part Studio target. model.ts is the source of truth. .cadscript/ contains local ownership state and immutable plans and should not be committed.
Each model declares units once. Every feature has a required stable ID. yield* records a feature and returns a symbolic reference for later operations.
ts
import { angle, defineModel, length, lengthParam, sketch } from "onshape-cadscript";
export default defineModel({
name: "revolved-knob",
units: "mm",
parameters: {
height: lengthParam(24),
radius: lengthParam(15),
},
async *build(cad, p) {
const profile = yield* cad.sketch({
id: "profile",
plane: cad.front,
entities: [
sketch.line("axis", [0, 0], [0, p.height], { construction: true }),
sketch.line("bottom", [0, 0], [p.radius * 0.72, 0]),
sketch.bezier("grip", [
[p.radius * 0.72, 0],
[p.radius, p.height * 0.2],
[p.radius, p.height * 0.8],
[p.radius * 0.72, p.height],
]),
sketch.line("top", [p.radius * 0.72, p.height], [0, p.height]),
],
});
const knob = yield* cad.revolve({
id: "knob",
profile,
axis: cad.sketchEntity(profile, "axis"),
revolveType: "FULL",
angle: angle(360),
});
yield* cad.fillet({ id: "soft-edges", edges: cad.edges(knob), radius: length(1.2) });
},
});