@rulecad/cad 0.7.12 → 0.7.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +6 -1
- package/EXAMPLES.md +386 -0
- package/README.md +73 -15
- package/TUTORIAL.md +226 -0
- package/bin/cad.mjs +97 -31
- package/lib/animate.mjs +71 -0
- package/lib/assembly.mjs +31 -5
- package/lib/clone.mjs +150 -0
- package/lib/design.mjs +75 -33
- package/lib/errors.mjs +20 -2
- package/lib/host.mjs +7 -0
- package/lib/jsonedit.mjs +17 -11
- package/lib/layout.mjs +2 -2
- package/lib/links.mjs +23 -7
- package/lib/live-shape.mjs +131 -0
- package/lib/live.mjs +2 -2
- package/lib/optimise.mjs +87 -23
- package/lib/parts.mjs +26 -0
- package/lib/player.mjs +225 -0
- package/lib/preview.mjs +3 -1
- package/lib/rules.mjs +2 -2
- package/lib/sandbox-hooks.mjs +9 -6
- package/lib/sandbox-run.mjs +24 -10
- package/lib/sandbox.mjs +27 -15
- package/lib/schema.mjs +1 -7
- package/lib/workspace.mjs +8 -3
- package/package.json +17 -7
- package/schemas/assembly.schema.json +1 -1
- package/schemas/object.schema.json +1 -1
- package/schemas/problem.schema.json +106 -4
- package/schemas/rules.schema.json +3 -1
package/AGENTS.md
CHANGED
|
@@ -44,10 +44,15 @@ variable says which rule it comes from. Follow these steps; they're what makes a
|
|
|
44
44
|
- Holes and slots get the printer's hole compensation; parts that slide together get its fit clearance. Chamfer
|
|
45
45
|
bottom edges.
|
|
46
46
|
- An overhang warning means supports: say so in `orientation`, or change the shape or orientation.
|
|
47
|
+
- A part that moves by bending (a spring, a snap, a hinge with no pin) is a flexure from `v.flexures`: drawn flat, so it
|
|
48
|
+
bends within its layers; give its thickness in whole lines (`x1c.line`), and check its strain at the travel against
|
|
49
|
+
the material's limit with `v.flexures.printable`. PLA creeps held bent and fatigues; PETG or nylon for springs.
|
|
47
50
|
- Use the print-aware features in `v.shapes` before drawing your own: `countersink`, `teardropHole`, `nutPocket`,
|
|
48
51
|
`crushRibs`, `wallOpening` print without supports as drawn. Use `v.ops.union/subtract/intersect` for booleans: the
|
|
49
52
|
workspace can switch them to Manifold (faster, watertight) without touching the design.
|
|
50
|
-
- Designs run sandboxed: they may import only `@jscad/modeling`, `@rulecad/shapes` and files in
|
|
53
|
+
- Designs run sandboxed: they may import only `@jscad/modeling`, `@rulecad/shapes`, `@rulecad/flexures` and files in
|
|
54
|
+
their project, and can't write files or start programs. To see a value while designing, `console.log` it: it comes
|
|
55
|
+
out with the command's output. A misspelt `v.name` stops the build and says which name was meant.
|
|
51
56
|
- Something printed under the part and removed afterwards is a named `support` part (`return { cap, pillar }`, both
|
|
52
57
|
described in the .json's `parts`), not part of the shape. Look at `cad preview --explode` too.
|
|
53
58
|
|
package/EXAMPLES.md
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
# Worked examples: from parts to a mechanism
|
|
2
|
+
|
|
3
|
+
[The tutorial](TUTORIAL.md) ends with a wall hook checked against its screw. These examples carry on from there, one
|
|
4
|
+
for each thing the [README](README.md) describes after that: parts, variants and releases, joints and poses, a model
|
|
5
|
+
from elsewhere, an optimised part, a board, and a mechanism printed flat. Each is a few files and the commands to run
|
|
6
|
+
them. The tests write every file here and run every command, so what you read is what works.
|
|
7
|
+
|
|
8
|
+
They start from a workspace `npm create rulecad my-designs` makes, as the tutorial does, and run in order: later
|
|
9
|
+
examples use earlier files where it saves repeating them.
|
|
10
|
+
|
|
11
|
+
## 1. Parts: a support that breaks away, and a second filament
|
|
12
|
+
|
|
13
|
+
A tag with a raised legend on top, printed as one object in two filaments, standing on a breakaway brim. `main`
|
|
14
|
+
returns named parts; the `.json` says what each is for.
|
|
15
|
+
|
|
16
|
+
```json projects/home/objects/tag.json
|
|
17
|
+
{
|
|
18
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
19
|
+
"name": "tag",
|
|
20
|
+
"title": "Name tag",
|
|
21
|
+
"version": "0.1.0",
|
|
22
|
+
"uses": ["x1c"],
|
|
23
|
+
"material": "PLA",
|
|
24
|
+
"orientation": "Flat, legend up, on a brim that snaps off",
|
|
25
|
+
"parts": {
|
|
26
|
+
"base": { "label": "Tag" },
|
|
27
|
+
"legend": { "label": "Raised legend", "filament": "legend" },
|
|
28
|
+
"brim": { "role": "support", "label": "Breakaway brim" }
|
|
29
|
+
},
|
|
30
|
+
"variables": {
|
|
31
|
+
"length": { "value": 50, "unit": "mm", "rule": "Room for a short name", "label": "Length", "show": true, "tweak": { "min": 30, "max": 90 } },
|
|
32
|
+
"width": { "value": 20, "unit": "mm", "rule": "Two lines of text", "label": "Width", "show": true },
|
|
33
|
+
"thick": { "value": "x1c.layer * 8", "unit": "mm", "rule": "Eight layers: stiff enough to pin to a coat" },
|
|
34
|
+
"raise": { "value": "x1c.layer * 3", "unit": "mm", "rule": "The legend stands three layers proud, so the second filament has layers of its own" },
|
|
35
|
+
"inset": { "value": "x1c.line * 4", "unit": "mm", "rule": "The legend's border, in from the edge" },
|
|
36
|
+
"brim": { "value": 4, "unit": "mm", "rule": "A brim this wide holds a thin tag flat while it prints" },
|
|
37
|
+
"brim_gap": { "value": "x1c.support_gap", "unit": "mm", "rule": "The gap a breakaway support leaves, so it snaps off" }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The brim is a frame round the tag one layer tall, a support gap below the tag's edge; the check knows a `support`
|
|
43
|
+
part is printed and removed, so the tag resting on it is not an overhang.
|
|
44
|
+
|
|
45
|
+
```js projects/home/objects/tag.js
|
|
46
|
+
// A name tag: a slab, a raised legend in a second filament, on a breakaway brim.
|
|
47
|
+
import jscad from '@jscad/modeling'
|
|
48
|
+
const { cuboid } = jscad.primitives
|
|
49
|
+
const { subtract } = jscad.booleans
|
|
50
|
+
|
|
51
|
+
export function main (v) {
|
|
52
|
+
const base = cuboid({ size: [v.length, v.width, v.thick], center: [0, 0, v.thick / 2] })
|
|
53
|
+
const legend = cuboid({ size: [v.length - 2 * v.inset, v.width - 2 * v.inset, v.raise], center: [0, 0, v.thick + v.raise / 2] })
|
|
54
|
+
const ring = subtract(cuboid({ size: [v.length + 2 * v.brim, v.width + 2 * v.brim, v.thick], center: [0, 0, v.thick / 2] }), cuboid({ size: [v.length - 2 * v.brim, v.width - 2 * v.brim, v.thick], center: [0, 0, v.thick / 2] }))
|
|
55
|
+
const brim = subtract(ring, cuboid({ size: [v.length + 2 * v.brim, v.width + 2 * v.brim, v.thick], center: [0, 0, v.thick / 2 + v.brim_gap + v.x1c.layer] }))
|
|
56
|
+
return { base, legend, brim }
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The second filament is named in the project: a slot in the AMS.
|
|
61
|
+
|
|
62
|
+
```json projects/home/project.json
|
|
63
|
+
{
|
|
64
|
+
"name": "home",
|
|
65
|
+
"title": "Things for the house",
|
|
66
|
+
"printer": "x1c",
|
|
67
|
+
"filaments": { "base": { "slot": 1 }, "legend": { "slot": 2 } }
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
npx cad check home/tag
|
|
73
|
+
npx cad export home/tag
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The print file groups the three parts as one object, the legend on its own slot, the brim marked as support.
|
|
77
|
+
|
|
78
|
+
## 2. Variants, then a release
|
|
79
|
+
|
|
80
|
+
A variant is a named set of inputs, kept beside the design rather than as a copy: a long tag next to the standard
|
|
81
|
+
one, a short one beside it. They are `variants` entries in the `.json` (values a variable's `tweak` range allows),
|
|
82
|
+
built as `home/tag:long`.
|
|
83
|
+
|
|
84
|
+
```json projects/home/objects/tag.json
|
|
85
|
+
{
|
|
86
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
87
|
+
"name": "tag",
|
|
88
|
+
"title": "Name tag",
|
|
89
|
+
"version": "0.1.0",
|
|
90
|
+
"uses": ["x1c"],
|
|
91
|
+
"material": "PLA",
|
|
92
|
+
"orientation": "Flat, legend up, on a brim that snaps off",
|
|
93
|
+
"parts": {
|
|
94
|
+
"base": { "label": "Tag" },
|
|
95
|
+
"legend": { "label": "Raised legend", "filament": "legend" },
|
|
96
|
+
"brim": { "role": "support", "label": "Breakaway brim" }
|
|
97
|
+
},
|
|
98
|
+
"variants": { "long": { "length": 80 }, "short": { "length": 35 } },
|
|
99
|
+
"variables": {
|
|
100
|
+
"length": { "value": 50, "unit": "mm", "rule": "Room for a short name", "label": "Length", "show": true, "tweak": { "min": 30, "max": 90 } },
|
|
101
|
+
"width": { "value": 20, "unit": "mm", "rule": "Two lines of text", "label": "Width", "show": true },
|
|
102
|
+
"thick": { "value": "x1c.layer * 8", "unit": "mm", "rule": "Eight layers: stiff enough to pin to a coat" },
|
|
103
|
+
"raise": { "value": "x1c.layer * 3", "unit": "mm", "rule": "The legend stands three layers proud, so the second filament has layers of its own" },
|
|
104
|
+
"inset": { "value": "x1c.line * 4", "unit": "mm", "rule": "The legend's border, in from the edge" },
|
|
105
|
+
"brim": { "value": 4, "unit": "mm", "rule": "A brim this wide holds a thin tag flat while it prints" },
|
|
106
|
+
"brim_gap": { "value": "x1c.support_gap", "unit": "mm", "rule": "The gap a breakaway support leaves, so it snaps off" }
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
npx cad variants home/tag
|
|
113
|
+
npx cad check home/tag:long
|
|
114
|
+
npx cad compare home/tag short long
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`compare` puts two variants (or two releases) side by side: what differs in their values and their sizes.
|
|
118
|
+
|
|
119
|
+
Versions are git tags, so a release needs the workspace in git. `--dry-run` shows the bump a change would need
|
|
120
|
+
without making it: here the first release of a new object is `0.1.0` as written.
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
npx cad release home/tag -m "First tag" --dry-run
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## 3. Joints and poses: a lid on a hinge
|
|
127
|
+
|
|
128
|
+
A box with a lid is one assembly, with the lid on a revolute joint between two anchors. The base and the lid name
|
|
129
|
+
their anchors in their `.json`; the poses say where the joint stands.
|
|
130
|
+
|
|
131
|
+
```json projects/home/objects/base.json
|
|
132
|
+
{
|
|
133
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
134
|
+
"name": "base",
|
|
135
|
+
"title": "Box base",
|
|
136
|
+
"version": "0.1.0",
|
|
137
|
+
"uses": ["x1c"],
|
|
138
|
+
"anchors": { "hinge": { "at": [0, "depth / 2", "height"], "axis": [1, 0, 0] } },
|
|
139
|
+
"variables": {
|
|
140
|
+
"width": { "value": 40, "unit": "mm", "rule": "The box's width", "label": "Width", "show": true },
|
|
141
|
+
"depth": { "value": 30, "unit": "mm", "rule": "Its depth", "label": "Depth", "show": true },
|
|
142
|
+
"height": { "value": 16, "unit": "mm", "rule": "Its height", "label": "Height", "show": true },
|
|
143
|
+
"wall": { "value": "x1c.line * 3", "unit": "mm", "rule": "Three lines" }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```js projects/home/objects/base.js
|
|
149
|
+
// An open box: a tray with walls, the hinge along the middle of its back edge.
|
|
150
|
+
import jscad from '@jscad/modeling'
|
|
151
|
+
const { cuboid } = jscad.primitives
|
|
152
|
+
const { subtract } = jscad.booleans
|
|
153
|
+
|
|
154
|
+
export function main (v) {
|
|
155
|
+
const outer = cuboid({ size: [v.width, v.depth, v.height], center: [0, 0, v.height / 2] })
|
|
156
|
+
const inner = cuboid({ size: [v.width - 2 * v.wall, v.depth - 2 * v.wall, v.height], center: [0, 0, v.height / 2 + v.wall] })
|
|
157
|
+
return subtract(outer, inner)
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```json projects/home/objects/lid.json
|
|
162
|
+
{
|
|
163
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
164
|
+
"name": "lid",
|
|
165
|
+
"title": "Box lid",
|
|
166
|
+
"version": "0.1.0",
|
|
167
|
+
"uses": ["x1c"],
|
|
168
|
+
"anchors": { "hinge": { "at": [0, "depth / 2", 0], "axis": [1, 0, 0] } },
|
|
169
|
+
"variables": {
|
|
170
|
+
"width": { "value": 40, "unit": "mm", "rule": "The box's width", "label": "Width", "show": true },
|
|
171
|
+
"depth": { "value": 30, "unit": "mm", "rule": "Its depth", "label": "Depth", "show": true },
|
|
172
|
+
"thick": { "value": "x1c.layer * 10", "unit": "mm", "rule": "Ten layers" }
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
```js projects/home/objects/lid.js
|
|
178
|
+
// A flat lid, hinged along its back edge.
|
|
179
|
+
import jscad from '@jscad/modeling'
|
|
180
|
+
|
|
181
|
+
export function main (v) {
|
|
182
|
+
return jscad.primitives.cuboid({ size: [v.width, v.depth, v.thick], center: [0, 0, v.thick / 2] })
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
The assembly: the lid's hinge anchor sits on the base's, and the joint turns it from there. Angle 0 is shut; the lid
|
|
187
|
+
opens by turning back over the hinge, so the open pose is negative.
|
|
188
|
+
|
|
189
|
+
```json projects/home/assemblies/box.json
|
|
190
|
+
{
|
|
191
|
+
"title": "Box with its lid",
|
|
192
|
+
"kind": "fit",
|
|
193
|
+
"items": [
|
|
194
|
+
{ "id": "base", "object": "base" },
|
|
195
|
+
{ "id": "lid", "object": "lid" }
|
|
196
|
+
],
|
|
197
|
+
"joints": [
|
|
198
|
+
{ "id": "hinge", "type": "revolute", "parent": "base.hinge", "child": "lid.hinge", "min": -120, "max": 0, "label": "Lid" }
|
|
199
|
+
],
|
|
200
|
+
"poses": { "shut": { "hinge": 0 }, "open": { "hinge": -110 } }
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
```sh
|
|
205
|
+
npx cad check home/box
|
|
206
|
+
npx cad check home/box --pose open
|
|
207
|
+
npx cad check home/box --sweep --steps 12
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Every pose is checked for parts overlapping; the sweep turns the hinge through its range and names where anything
|
|
211
|
+
collides on the way. The link carries the joint as a slider: `npx cad link home/box --md`.
|
|
212
|
+
|
|
213
|
+
## 4. A model from elsewhere: measured into rules, and matched
|
|
214
|
+
|
|
215
|
+
Something you must fit (a socket, a maker's part) is best measured from its model, not guessed. Here the model is the
|
|
216
|
+
box base exported as a 3MF, standing in for a file from a model site: `cad measure` reads its sections into a draft
|
|
217
|
+
rule set, and `cad match` lays the model over a design and reports how far apart their surfaces are.
|
|
218
|
+
|
|
219
|
+
```sh
|
|
220
|
+
npx cad export home/base
|
|
221
|
+
npx cad measure exports/home/base@0.1.0.3mf --rules basemodel --source "exports/home/base@0.1.0.3mf" --licence "CC0" --no-pictures
|
|
222
|
+
npx cad match home/base exports/home/base@0.1.0.3mf --no-pictures --samples 3000
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
The rule set `rules/basemodel.json` has every value as `kind: derived` with the file it came from: rename its
|
|
226
|
+
placeholder names (`size_x`, `sectionA_w`) for what they are before a design uses them. The match of the design
|
|
227
|
+
against its own export is exact; against someone else's model, a deliberate clearance shows as an even offset.
|
|
228
|
+
|
|
229
|
+
## 5. An optimised part, analysed
|
|
230
|
+
|
|
231
|
+
A lever arm: where to put material so it bends least under a load, with bosses kept round the bolt and the pin. The
|
|
232
|
+
problem is a file in the project's `problems/`.
|
|
233
|
+
|
|
234
|
+
```json projects/home/problems/arm.json
|
|
235
|
+
{
|
|
236
|
+
"title": "Lever arm",
|
|
237
|
+
"uses": ["m4", "x1c"],
|
|
238
|
+
"material": "pla",
|
|
239
|
+
"thickness": 4,
|
|
240
|
+
"cell": 1.5,
|
|
241
|
+
"volume": 0.4,
|
|
242
|
+
"filter": 3,
|
|
243
|
+
"space": { "at": [25, 0], "size": [62, 24] },
|
|
244
|
+
"keep": [{ "at": [0, 0], "r": 5 }, { "at": [50, 0], "r": 4 }],
|
|
245
|
+
"avoid": [{ "at": [0, 0], "r": "m4.clearance / 2" }, { "at": [50, 0], "r": 1.6 }],
|
|
246
|
+
"supports": [{ "at": [0, 0], "r": "m4.clearance / 2" }],
|
|
247
|
+
"loads": [{ "at": [50, 0], "r": 1.6, "force": [0, -10], "case": "down" }]
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
The design reads the result through a `problem` variable and draws it; `analysis` has `cad check` report its
|
|
252
|
+
stiffness and peak stress on finer cells.
|
|
253
|
+
|
|
254
|
+
```json projects/home/objects/arm.json
|
|
255
|
+
{
|
|
256
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
257
|
+
"name": "arm",
|
|
258
|
+
"title": "Lever arm",
|
|
259
|
+
"version": "0.1.0",
|
|
260
|
+
"uses": ["x1c"],
|
|
261
|
+
"material": "PLA",
|
|
262
|
+
"orientation": "Flat on the bed",
|
|
263
|
+
"variables": {
|
|
264
|
+
"problem": { "type": "problem", "value": "arm", "choice": ["arm"] },
|
|
265
|
+
"thick": { "value": 4, "unit": "mm", "rule": "As the problem's thickness" }
|
|
266
|
+
},
|
|
267
|
+
"analysis": [{ "problem": "arm", "cell": 0.5, "max_deflection": 1 }]
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
```js projects/home/objects/arm.js
|
|
272
|
+
// The optimised arm, extruded to its thickness: the holes come out true, cut from the outline.
|
|
273
|
+
import jscad from '@jscad/modeling'
|
|
274
|
+
|
|
275
|
+
export function main (v) {
|
|
276
|
+
return jscad.extrusions.extrudeLinear({ height: v.thick }, v.shapes.optimisedOutline(v.problems[v.problem]))
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
```sh
|
|
281
|
+
npx cad optimise home/arm
|
|
282
|
+
npx cad check home/arm
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
`optimise` draws the layout in `.preview/` and reports how far the load moves; the result is kept until the problem
|
|
286
|
+
or the solver changes. `check` analyses the part as drawn, on cells half the size and again a quarter bigger, and says
|
|
287
|
+
whether the stiffness has settled.
|
|
288
|
+
|
|
289
|
+
## 6. A board, and a case that reads it
|
|
290
|
+
|
|
291
|
+
A four-key pad: keys in a matrix, one diode each, and a Pico beside them. Every track is drawn by rule; the
|
|
292
|
+
checks run the fab's design rules.
|
|
293
|
+
|
|
294
|
+
```json projects/home/objects/pad.json
|
|
295
|
+
{
|
|
296
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
297
|
+
"name": "pad",
|
|
298
|
+
"title": "Four-key pad",
|
|
299
|
+
"version": "0.1.0",
|
|
300
|
+
"engine": "pcb",
|
|
301
|
+
"uses": ["mx", "pico", "sod123", "m2", "jlcpcb"],
|
|
302
|
+
"variables": {
|
|
303
|
+
"rows": { "value": 2, "rule": "Two rows" },
|
|
304
|
+
"cols": { "value": 2, "rule": "Two columns" },
|
|
305
|
+
"track": { "value": "jlcpcb.track_min * 2.5", "unit": "mm", "rule": "Tracks well over the fab's least" },
|
|
306
|
+
"clear": { "value": "jlcpcb.gap_min * 2.5", "unit": "mm", "rule": "Clearance well over the fab's least" },
|
|
307
|
+
"thickness": { "value": "jlcpcb.thickness", "unit": "mm", "rule": "The usual board" },
|
|
308
|
+
"margin": { "value": 5, "unit": "mm", "rule": "Board edge beyond the keys" },
|
|
309
|
+
"mcu_gap": { "value": 2, "unit": "mm", "rule": "Between the keys and the Pico" },
|
|
310
|
+
"mcu_side": { "value": "left", "choice": ["left", "right"], "rule": "Which way the Pico's USB faces" },
|
|
311
|
+
"mcu_row": { "value": 0, "rule": "Which key row the Pico sits beside" },
|
|
312
|
+
"mount_holes": { "value": 1, "rule": "M2 holes in the corners" },
|
|
313
|
+
"controller": { "value": "beside", "choice": ["beside", "under"], "rule": "The Pico beside the keys" }
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
```js projects/home/objects/pad.js
|
|
319
|
+
// A key matrix on the keyboard recipe: the board and its matrix (rows, columns, GPIO) for the firmware.
|
|
320
|
+
export function main (v) {
|
|
321
|
+
const keys = v.pcb.grid(v.rows, v.cols, v.mx.pitch)
|
|
322
|
+
return v.pcb.keyboard(v, keys)
|
|
323
|
+
}
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
```sh
|
|
327
|
+
npx cad check home/pad
|
|
328
|
+
npx cad export home/pad
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
A case reads the board rather than copying its numbers: `"uses": ["home/pad"]`, then `v.pad.board_w` and
|
|
332
|
+
`v.pad.board_holes` are the board's own.
|
|
333
|
+
|
|
334
|
+
## 7. A mechanism printed flat
|
|
335
|
+
|
|
336
|
+
A leaf spring and a block that flicks, drawn in one plane and printed on its side. The lamina's checks come out with
|
|
337
|
+
the print checks: it prints free, its sequence is clear, and a hard stop catches the leaf before its strain limit.
|
|
338
|
+
|
|
339
|
+
```json projects/home/objects/flick.json
|
|
340
|
+
{
|
|
341
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
342
|
+
"name": "flick",
|
|
343
|
+
"title": "Flicker",
|
|
344
|
+
"version": "0.1.0",
|
|
345
|
+
"uses": ["x1c", "pla"],
|
|
346
|
+
"material": "PLA",
|
|
347
|
+
"orientation": "On its side: the plane of the drawing is the bed",
|
|
348
|
+
"variables": {
|
|
349
|
+
"len": { "value": 20, "unit": "mm", "rule": "The leaf's length", "label": "Leaf", "show": true },
|
|
350
|
+
"t": { "value": "x1c.line * 2", "unit": "mm", "rule": "Two lines thick: it bends within its layers" },
|
|
351
|
+
"fillet": { "value": "x1c.line * 2", "unit": "mm", "rule": "Rounded where it meets its blocks: a square corner is where a print yields" },
|
|
352
|
+
"h": { "value": 10, "unit": "mm", "rule": "The print's height, the leaf's width: tall, so the tip can't flex out of the plane" },
|
|
353
|
+
"gap": { "value": "x1c.print_gap", "unit": "mm", "rule": "The gap that prints apart" },
|
|
354
|
+
"post": { "value": "x1c.print_gap + x1c.line", "unit": "mm", "rule": "How far the stop's lip stands over the tip block" },
|
|
355
|
+
"bend": { "value": 0.3, "unit": "mm", "rule": "How far the state bends it" },
|
|
356
|
+
"block": { "value": 4, "unit": "mm", "rule": "The tip block, the root, the frame's bars" }
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
```js projects/home/objects/flick.js
|
|
362
|
+
// A leaf from a root block to a tip block, bent by a state. The frame is one piece: the root, a floor under
|
|
363
|
+
// everything, and a post past the tip with a lip over it, the hard stop.
|
|
364
|
+
import jscad from '@jscad/modeling'
|
|
365
|
+
const rect = (x0, y0, x1, y1) => jscad.primitives.polygon({ points: [[x0, y0], [x1, y0], [x1, y1], [x0, y1]] })
|
|
366
|
+
|
|
367
|
+
export function main (v) {
|
|
368
|
+
const f = v.flexures, b = v.block, tipTop = b / 2, floor = 0 - b - b, postX = v.len + b + v.gap
|
|
369
|
+
return f.lamina({
|
|
370
|
+
height: v.h, gap: v.gap, material: f.flexureMaterial({ modulus: v.pla.modulus, strength: v.pla.strength }),
|
|
371
|
+
ground: [rect(0 - b, floor, 0, b), rect(0 - b, floor, postX + b, floor + b), rect(postX, floor, postX + b, tipTop + v.post + b), rect(v.len, tipTop + v.post, postX + b, tipTop + v.post + b)],
|
|
372
|
+
bodies: { tip: rect(v.len - b / 2, 0 - b / 2, v.len + b, tipTop) },
|
|
373
|
+
joints: { leaf: { kind: 'leaf', params: { length: v.len, thickness: v.t, fillet: v.fillet }, at: [0 - 1, 0], from: 'ground', to: 'tip' } },
|
|
374
|
+
states: { bent: { leaf: v.bend } }, sequence: ['rest', 'bent', 'rest']
|
|
375
|
+
})
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
```sh
|
|
380
|
+
npx cad check home/flick
|
|
381
|
+
npx cad animate home/flick
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
`animate` writes the sequence as an animated SVG in `.preview/`, the leaf drawn bent, and a page that plays it with
|
|
385
|
+
the force on each step. The launcher in `examples/bench/projects/launcher` is the same idea at full size: a flooded
|
|
386
|
+
frame, catches, a trigger, and a shot.
|
package/README.md
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
The `cad` command: design physical things in [JSCAD](https://github.com/jscad/OpenJSCAD.org) from rules you can trace,
|
|
4
4
|
check them for printing and for fit, version them by what fits, share them as links, and export them for the printer
|
|
5
|
-
(
|
|
5
|
+
(a 3MF in Bambu Studio's layout with each part on its own filament; an STL from the model's link). Every measurement in a design is a variable,
|
|
6
6
|
and every variable says which rule it comes from: the printer, a standard (Cherry MX), or something you measured.
|
|
7
7
|
|
|
8
8
|
```sh
|
|
9
|
+
npm create rulecad my-designs # a workspace with a soap tray to start from; or by hand:
|
|
9
10
|
mkdir my-designs && cd my-designs
|
|
10
11
|
npm init -y && npm pkg set type=module
|
|
11
12
|
npm install @rulecad/cad @jscad/modeling
|
|
@@ -16,6 +17,10 @@ npx cad export bathroom/soap-tray # a 3MF for your slicer, in exports/
|
|
|
16
17
|
npx cad link bathroom/soap-tray # a link that opens the model in a browser (nothing is uploaded)
|
|
17
18
|
```
|
|
18
19
|
|
|
20
|
+
**New here?** [The tutorial](TUTORIAL.md) walks from an empty folder to a part of your own, checked against the thing
|
|
21
|
+
it fits, in about twenty minutes. This page is the reference. [The worked examples](EXAMPLES.md) go on from there: parts, variants and a release, a lid on a hinge, a model
|
|
22
|
+
measured into rules, an optimised arm, a board, and a mechanism printed flat, each tested as written.
|
|
23
|
+
|
|
19
24
|
```
|
|
20
25
|
rules/ shared facts, one file per set: x1c.json (printer), mx.json (Cherry MX). A set from
|
|
21
26
|
@rulecad/rules is copied here the first time a design uses it: yours to keep and correct
|
|
@@ -36,6 +41,7 @@ npx cad check bathroom/soap-tray --set width=60 # change variables that have a
|
|
|
36
41
|
npx cad variants keyboard/keycap # named sets of inputs: keyboard/keycap:legend-f
|
|
37
42
|
npx cad compare keyboard/keycap dsa legend-f # two variants (or releases) side by side
|
|
38
43
|
npx cad import-kle layout.json keyboard/mine # a layout from keyboard-layout-editor.com, into layouts/
|
|
44
|
+
npx cad clone <github link to a .kicad_pcb> sea_picro # a published board's circuit, into rules/, to redraw by rules
|
|
39
45
|
npx cad measure cover.3mf --rules cover # a model from elsewhere, measured into draft rules
|
|
40
46
|
npx cad match legion-go/rail cover.3mf # how closely a design matches that model, both ways
|
|
41
47
|
```
|
|
@@ -43,6 +49,22 @@ npx cad match legion-go/rail cover.3mf # how closely a design matches
|
|
|
43
49
|
The keyboard examples below come from the project rulecad grew out of: a keycap, a switch plate, a case and their
|
|
44
50
|
circuit boards.
|
|
45
51
|
|
|
52
|
+
## Looking into a design, and changing it
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
npx cad values bathroom/soap-tray # its variables, resolved (names after it pick some; --json)
|
|
56
|
+
npx cad why bathroom/soap-tray.wall # what a value reads, down to the rules and their sources
|
|
57
|
+
npx cad set bathroom/soap-tray length=120 # change a variable, keeping the file's layout
|
|
58
|
+
npx cad set bathroom/soap-tray --new lip=3 --unit mm --rule "A lip to hold the soap"
|
|
59
|
+
npx cad add fit bathroom/tray-on-shelf --a tray.body --b shelf.body --min 0.2 --max 1 --note "Tray on its shelf"
|
|
60
|
+
npx cad check bathroom/soap-tray --json # findings as data: each with a level, a code, its numbers
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`set` and `add` (`fit`, `joint`, `item`, `loop`, `pose`) refuse an edit that leaves the file broken: its JSON, its
|
|
64
|
+
schema, or variables that no longer resolve. Options go before or after the names; a misspelt command or option is
|
|
65
|
+
refused by name. `cad --version` says which cad this is. Two switches for the tool itself: `CAD_NO_CACHE=1` builds
|
|
66
|
+
afresh, and `CAD_TRUST=1` runs designs in the tool's own process (see Sandbox).
|
|
67
|
+
|
|
46
68
|
## The workspace
|
|
47
69
|
|
|
48
70
|
The workspace is `$CAD_WORKSPACE`, else the nearest folder at or above the current one with a `cad.config.json`,
|
|
@@ -68,8 +90,8 @@ else the current folder:
|
|
|
68
90
|
`cad link <project>/<object>` (and `cad show`) prints three links to the model page (`viewUrl`; a public one by default):
|
|
69
91
|
view the model in a browser, or download its STL or 3MF as soon as the link opens. The model travels in the link's
|
|
70
92
|
fragment, which never reaches the web host, so the public page can open private designs. A link records exactly
|
|
71
|
-
which version it holds (`keyboard/keycap@0.1.0`, or `+changes` if it isn't a release). The page is
|
|
72
|
-
|
|
93
|
+
which version it holds (`keyboard/keycap@0.1.0`, or `+changes` if it isn't a release). The page is `@rulecad/viewer`: host your
|
|
94
|
+
own copy anywhere and name it in `viewUrl`.
|
|
73
95
|
|
|
74
96
|
- **Rules:** a link carries the model's values and the rules they use (value, source, kind, date checked) for the
|
|
75
97
|
page's Rules panel; estimates are marked there. A rule set with `"private": true` stays out of links, and
|
|
@@ -127,8 +149,17 @@ which version it holds (`keyboard/keycap@0.1.0`, or `+changes` if it isn't a rel
|
|
|
127
149
|
text so punctuation stays small, each straight stroke thickened to whole print lines, dots drawn round, and ↑ ↓ ← →
|
|
128
150
|
as arrows), `anchor(face, size, at)` (where something goes on a face, by anchor), and `cut(a, b)` (a subtract that
|
|
129
151
|
only touches a's faces near b; JSCAD's own splits every face along b's planes, which can leave slivers far away).
|
|
152
|
+
- `v.flexures`: compliant mechanisms (`@rulecad/flexures`): `leaf`, `fixedGuided`, `parallelGuide`, `serpentine`,
|
|
153
|
+
`notchHinge`, `bistable`, `livingHinge` as shapes, each with a `<name>Model(shape, { E })` giving its stiffness and the
|
|
154
|
+
strain at a travel, and `printable(shape, printer, material, model)` for how it prints. Drawn flat: bending along y,
|
|
155
|
+
width up z, so a flexure bends within its layers.
|
|
130
156
|
- A rule set can also carry `"footprints"` (pad data from @rulecad/pcb's `fromKicad`, with its source), which a
|
|
131
157
|
board's code reads as `v.parts.footprints.usb_c`.
|
|
158
|
+
- `cad clone` writes a rule set with a `"circuit"`: a published board's parts, pads, nets, planes and outline, read
|
|
159
|
+
from its `.kicad_pcb` (a file, or a GitHub link: pinned to the commit, with the repository's licence). A board's code
|
|
160
|
+
reads it as `v.sea_picro.circuit` and builds from it with @rulecad/pcb's `clone`. Its rules (`width`, `height`,
|
|
161
|
+
`thickness`, `layers`) are `kind: derived`, with the source and its licence. A reciprocal licence (CERN-OHL-S,
|
|
162
|
+
share-alike) carries over to boards made from it: keep such a rule set apart from your others.
|
|
132
163
|
- An object or rule set named with a hyphen (`mx-switch`) is read with an underscore in expressions (`mx_switch.housing_top`),
|
|
133
164
|
since a hyphen there is a minus.
|
|
134
165
|
- Rules carry `kind`: spec (manufacturer), default (slicer), community (rule of thumb) or measured (your own test
|
|
@@ -227,15 +258,22 @@ other items' variables, so a keycap can sit exactly where its slot meets the swi
|
|
|
227
258
|
- **Hosts:** the core (resolve, build, check) reads and writes through a host (`lib/host.mjs`): Node's file system
|
|
228
259
|
for the CLI, or `memoryHost(files)`, a workspace in memory (reading the tool's own packages from disk), for tests
|
|
229
260
|
and, later, a browser. `useHost(h)` switches. The model-link building (`lib/links.mjs`) and the joints' data for the
|
|
230
|
-
page (`motionData`) are in the library too, so other programs make the same links without the CLI
|
|
261
|
+
page (`motionData`) are in the library too, so other programs make the same links without the CLI:
|
|
262
|
+
`import { memoryHost, useHost } from '@rulecad/cad/host'`, and likewise `/design`, `/assembly`, `/links`, `/edit`,
|
|
263
|
+
`/explain`, `/schema`, `/errors`, `/optimise`, `/rules`, `/workspace`, `/versions`, `/layout`, `/preview`, `/targets`, `/threemf`.
|
|
231
264
|
- **Engines:** designs get `v.ops` (union, subtract, intersect): JSCAD's booleans by default, Manifold's (faster,
|
|
232
265
|
watertight; `npm install manifold-3d`) with `"engine": "manifold"` in the object's JSON or `CAD_ENGINE=manifold`.
|
|
233
|
-
- **Sandbox:** a JSCAD design runs in a separate Node process under Node's permission model
|
|
234
|
-
|
|
235
|
-
import only `@jscad/modeling`, `@rulecad/shapes
|
|
236
|
-
`
|
|
237
|
-
|
|
238
|
-
the
|
|
266
|
+
- **Sandbox:** a JSCAD design runs in a separate Node process under Node's permission model. Whatever it does, it
|
|
267
|
+
can't write or delete files, start programs or workers, or load addons, and it reads only the workspace and the
|
|
268
|
+
packages designs use. To keep it to making shapes it may import only `@jscad/modeling`, `@rulecad/shapes`,
|
|
269
|
+
`@rulecad/flexures` and files in its own project (no `node:` modules), `fetch` and its kin are gone, and it is
|
|
270
|
+
started with none of your environment (no tokens). Those shut the ordinary roads to the network, not every one:
|
|
271
|
+
before Node 25 the permission model doesn't cover the network, so a design written to get round them might still
|
|
272
|
+
reach it. Run only designs you trust. It gets its values as data and returns its parts; what it prints
|
|
273
|
+
(`console.log`) comes out on stderr. A workspace that trusts its designs sets `"sandbox": false` in cad.config.json
|
|
274
|
+
(or `CAD_TRUST=1`). Board designs (engine pcb) run in the tool's process, with the board engine.
|
|
275
|
+
- **Mistakes are named:** a design that reads a variable that isn't there (`v.widht`) stops with the name it
|
|
276
|
+
probably meant, not a shape drawn from `undefined`; errors in a design carry its file and line.
|
|
239
277
|
- **Units:** `cad check` warns where an expression mixes units (millimetres added to degrees, a sine of a length) or
|
|
240
278
|
comes to another unit than its variable says (mm × mm with unit mm). Counts (keys, u, rows) are plain numbers.
|
|
241
279
|
- **Progress:** `--progress` shows what a long check or sweep is building; a program passes `progress` to
|
|
@@ -387,7 +425,7 @@ It listens on this computer only. To watch from elsewhere, reach it through a pr
|
|
|
387
425
|
A design uses it through a variable of type `problem`, which picks a problem by name:
|
|
388
426
|
|
|
389
427
|
```json
|
|
390
|
-
"problem": { "type": "problem", "value": "lever", "choice": ["lever", "lever:both"] }
|
|
428
|
+
"problem": { "type": "problem", "value": "lever", "choice": ["lever", "lever:both", "none"] }
|
|
391
429
|
```
|
|
392
430
|
|
|
393
431
|
```js
|
|
@@ -396,6 +434,10 @@ export function main (v) {
|
|
|
396
434
|
}
|
|
397
435
|
```
|
|
398
436
|
|
|
437
|
+
A variant that sets the problem to `none` builds without optimising (`v.problems` then lacks it): a test rig drawn on a
|
|
438
|
+
plain base, or the parts a problem's regions name (`{ "part": "body:regions.mounts" }`), which must not need the problem
|
|
439
|
+
they feed.
|
|
440
|
+
|
|
399
441
|
`optimisedOutline` gives the outline as a 2D shape, its keep regions added and avoid regions cut exactly, so bolt
|
|
400
442
|
holes come out true.
|
|
401
443
|
|
|
@@ -459,6 +501,8 @@ they bend. To check a final shape, give the analysis a smaller `cell`:
|
|
|
459
501
|
The part is then analysed on those cells and again on cells a quarter bigger. For each load case, `cad check`
|
|
460
502
|
reports:
|
|
461
503
|
- **Stiffness and its change:** under 5% from the bigger cells and it has settled; more, and the cells are too big.
|
|
504
|
+
- **Members too thin for the cells:** a warning when more than 5% of the part lies in members under four cells
|
|
505
|
+
thick (what an opening of two cells takes away), where a cell's stiffness is a rough figure: use a smaller cell.
|
|
462
506
|
- **Peak stress, where it is, and the safety factor:** against the material's strength (its weaker one across a
|
|
463
507
|
print's layers, where the rule set has `strength_z`), and its fatigue strength where it has one. With
|
|
464
508
|
`min_safety`, a case below it fails. Peak stress at a sharp inside corner keeps rising as cells shrink, so read it
|
|
@@ -481,7 +525,7 @@ The analysis is a design aid, not engineering sign-off: anything that could hurt
|
|
|
481
525
|
|
|
482
526
|
A circuit board is an object whose .json says `"engine": "pcb"`. Its `main(v)` returns a
|
|
483
527
|
[tscircuit](https://github.com/tscircuit/tscircuit) board (circuits as code, from npm) built with `v.pcb`
|
|
484
|
-
(`
|
|
528
|
+
(`@rulecad/pcb`: `npm install @rulecad/pcb` in the workspace); the build runs it into Circuit JSON, and the bare board, drilled, becomes a part, so
|
|
485
529
|
previews, links, `cad show` and fit assemblies work as for printed parts. The copper (tracks, pads, vias) is drawn in the
|
|
486
530
|
2D views, not in 3D.
|
|
487
531
|
|
|
@@ -518,14 +562,18 @@ npx cad export keyboard/macropad-pcb # <name>-fab.zip for JLCPCB, KiCad files
|
|
|
518
562
|
(with its Pico, `pcb-with-pico`) under a plate thinned by `top_clearance` for the header joints, in a case built
|
|
519
563
|
from the board's numbers, every fit checked (switches on the board, plate over it, board and Pico in the case).
|
|
520
564
|
- **The matrix:** `<name>-matrix.json` gives each key's row and column and each line's GPIO, for firmware.
|
|
521
|
-
- **Versions:**
|
|
522
|
-
overridden in package.json so every part of it agrees.
|
|
565
|
+
- **Versions:** `@rulecad/pcb` pins tscircuit's packages to exact versions (tscircuit moves fast).
|
|
523
566
|
|
|
524
567
|
### Speed
|
|
525
568
|
Fit checks find closest gaps through a grid of triangles (nearest cubes first), and remember overlap volumes between
|
|
526
569
|
placed parts in `.cache/overlaps.json`, keyed by each part's build and placement: the staggered keyboards' checks take
|
|
527
570
|
about 10 s, and a re-check after an edit measures only what the edit moved. The build cache keeps its 800 most
|
|
528
|
-
recently used builds
|
|
571
|
+
recently used builds, keyed by what a build is made from: the design's code, its values resolved, its parts' names
|
|
572
|
+
and roles, the rule sets and the tool's own code; a rule's words, a label or the version change nothing it builds,
|
|
573
|
+
so they don't start one. A lamina mechanism's slow stages (its physics, a flooded frame's probe) are kept apart in
|
|
574
|
+
`.cache/stages/`, keyed by their own inputs, so a build that changes only what comes after them (a strap, a check)
|
|
575
|
+
takes minutes rather than the twenty a launcher's physics takes; the design's sandbox reads them and hands back what
|
|
576
|
+
it made for the tool to write. `CAD_NO_CACHE=1` computes everything afresh (and keeps it).
|
|
529
577
|
|
|
530
578
|
### Assemblies: values from other parts
|
|
531
579
|
(An object can also read another object directly, outside any assembly: see "Other objects' numbers" under Variables.
|
|
@@ -537,6 +585,16 @@ builds an item only for its values: the PCB build's print plates (`staggered-pcb
|
|
|
537
585
|
the board's numbers without the board being on the bed. In assemblies a board counts as a reference: checked, drawn
|
|
538
586
|
faint, never in a print file.
|
|
539
587
|
|
|
588
|
+
## Mechanisms printed flat
|
|
589
|
+
|
|
590
|
+
A design whose `main` returns `v.flexures.lamina({ … })` (see @rulecad/flexures, Lamina mechanisms) is a mechanism
|
|
591
|
+
drawn in one plane and printed on its side. `cad check` adds its own findings to the print checks: that it prints
|
|
592
|
+
free (nothing within the printer's gap of what it isn't joined to), that every step of its sequence is clear, that
|
|
593
|
+
nothing jams, that its catches hold, and that every flexure has a hard stop before its strain limit. `cad animate
|
|
594
|
+
<project>/<object>` writes the sequence, start to finish, as an animated SVG in .preview/: the frame drawn once, the
|
|
595
|
+
moving bodies in blue and the flexures in orange, drawn bent, holding a moment at each state. Under it, each driven step's force against its travel, with a marker that follows the frames; a mechanism that fires adds the shot in slow motion. The printer's
|
|
596
|
+
`print_gap` is the gap the checks hold it to.
|
|
597
|
+
|
|
540
598
|
## Checks
|
|
541
599
|
|
|
542
600
|
- **Closed solids:** every edge must be matched by faces on the other side. T-junctions (an edge covered by several
|