@rulecad/cad 0.7.11 → 0.7.25
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/README.md +74 -16
- package/TUTORIAL.md +225 -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 +85 -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 +16 -7
- package/schemas/assembly.schema.json +1 -1
- package/schemas/object.schema.json +1 -1
- package/schemas/problem.schema.json +98 -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/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,9 @@ 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.
|
|
22
|
+
|
|
19
23
|
```
|
|
20
24
|
rules/ shared facts, one file per set: x1c.json (printer), mx.json (Cherry MX). A set from
|
|
21
25
|
@rulecad/rules is copied here the first time a design uses it: yours to keep and correct
|
|
@@ -36,6 +40,7 @@ npx cad check bathroom/soap-tray --set width=60 # change variables that have a
|
|
|
36
40
|
npx cad variants keyboard/keycap # named sets of inputs: keyboard/keycap:legend-f
|
|
37
41
|
npx cad compare keyboard/keycap dsa legend-f # two variants (or releases) side by side
|
|
38
42
|
npx cad import-kle layout.json keyboard/mine # a layout from keyboard-layout-editor.com, into layouts/
|
|
43
|
+
npx cad clone <github link to a .kicad_pcb> sea_picro # a published board's circuit, into rules/, to redraw by rules
|
|
39
44
|
npx cad measure cover.3mf --rules cover # a model from elsewhere, measured into draft rules
|
|
40
45
|
npx cad match legion-go/rail cover.3mf # how closely a design matches that model, both ways
|
|
41
46
|
```
|
|
@@ -43,6 +48,22 @@ npx cad match legion-go/rail cover.3mf # how closely a design matches
|
|
|
43
48
|
The keyboard examples below come from the project rulecad grew out of: a keycap, a switch plate, a case and their
|
|
44
49
|
circuit boards.
|
|
45
50
|
|
|
51
|
+
## Looking into a design, and changing it
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npx cad values bathroom/soap-tray # its variables, resolved (names after it pick some; --json)
|
|
55
|
+
npx cad why bathroom/soap-tray.wall # what a value reads, down to the rules and their sources
|
|
56
|
+
npx cad set bathroom/soap-tray length=120 # change a variable, keeping the file's layout
|
|
57
|
+
npx cad set bathroom/soap-tray --new lip=3 --unit mm --rule "A lip to hold the soap"
|
|
58
|
+
npx cad add fit bathroom/tray-on-shelf --a tray.body --b shelf.body --min 0.2 --max 1 --note "Tray on its shelf"
|
|
59
|
+
npx cad check bathroom/soap-tray --json # findings as data: each with a level, a code, its numbers
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`set` and `add` (`fit`, `joint`, `item`, `loop`, `pose`) refuse an edit that leaves the file broken: its JSON, its
|
|
63
|
+
schema, or variables that no longer resolve. Options go before or after the names; a misspelt command or option is
|
|
64
|
+
refused by name. `cad --version` says which cad this is. Two switches for the tool itself: `CAD_NO_CACHE=1` builds
|
|
65
|
+
afresh, and `CAD_TRUST=1` runs designs in the tool's own process (see Sandbox).
|
|
66
|
+
|
|
46
67
|
## The workspace
|
|
47
68
|
|
|
48
69
|
The workspace is `$CAD_WORKSPACE`, else the nearest folder at or above the current one with a `cad.config.json`,
|
|
@@ -68,8 +89,8 @@ else the current folder:
|
|
|
68
89
|
`cad link <project>/<object>` (and `cad show`) prints three links to the model page (`viewUrl`; a public one by default):
|
|
69
90
|
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
91
|
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
|
-
|
|
92
|
+
which version it holds (`keyboard/keycap@0.1.0`, or `+changes` if it isn't a release). The page is `@rulecad/viewer`: host your
|
|
93
|
+
own copy anywhere and name it in `viewUrl`.
|
|
73
94
|
|
|
74
95
|
- **Rules:** a link carries the model's values and the rules they use (value, source, kind, date checked) for the
|
|
75
96
|
page's Rules panel; estimates are marked there. A rule set with `"private": true` stays out of links, and
|
|
@@ -127,8 +148,17 @@ which version it holds (`keyboard/keycap@0.1.0`, or `+changes` if it isn't a rel
|
|
|
127
148
|
text so punctuation stays small, each straight stroke thickened to whole print lines, dots drawn round, and ↑ ↓ ← →
|
|
128
149
|
as arrows), `anchor(face, size, at)` (where something goes on a face, by anchor), and `cut(a, b)` (a subtract that
|
|
129
150
|
only touches a's faces near b; JSCAD's own splits every face along b's planes, which can leave slivers far away).
|
|
151
|
+
- `v.flexures`: compliant mechanisms (`@rulecad/flexures`): `leaf`, `fixedGuided`, `parallelGuide`, `serpentine`,
|
|
152
|
+
`notchHinge`, `bistable`, `livingHinge` as shapes, each with a `<name>Model(shape, { E })` giving its stiffness and the
|
|
153
|
+
strain at a travel, and `printable(shape, printer, material, model)` for how it prints. Drawn flat: bending along y,
|
|
154
|
+
width up z, so a flexure bends within its layers.
|
|
130
155
|
- A rule set can also carry `"footprints"` (pad data from @rulecad/pcb's `fromKicad`, with its source), which a
|
|
131
156
|
board's code reads as `v.parts.footprints.usb_c`.
|
|
157
|
+
- `cad clone` writes a rule set with a `"circuit"`: a published board's parts, pads, nets, planes and outline, read
|
|
158
|
+
from its `.kicad_pcb` (a file, or a GitHub link: pinned to the commit, with the repository's licence). A board's code
|
|
159
|
+
reads it as `v.sea_picro.circuit` and builds from it with @rulecad/pcb's `clone`. Its rules (`width`, `height`,
|
|
160
|
+
`thickness`, `layers`) are `kind: derived`, with the source and its licence. A reciprocal licence (CERN-OHL-S,
|
|
161
|
+
share-alike) carries over to boards made from it: keep such a rule set apart from your others.
|
|
132
162
|
- An object or rule set named with a hyphen (`mx-switch`) is read with an underscore in expressions (`mx_switch.housing_top`),
|
|
133
163
|
since a hyphen there is a minus.
|
|
134
164
|
- Rules carry `kind`: spec (manufacturer), default (slicer), community (rule of thumb) or measured (your own test
|
|
@@ -227,15 +257,22 @@ other items' variables, so a keycap can sit exactly where its slot meets the swi
|
|
|
227
257
|
- **Hosts:** the core (resolve, build, check) reads and writes through a host (`lib/host.mjs`): Node's file system
|
|
228
258
|
for the CLI, or `memoryHost(files)`, a workspace in memory (reading the tool's own packages from disk), for tests
|
|
229
259
|
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
|
|
260
|
+
page (`motionData`) are in the library too, so other programs make the same links without the CLI:
|
|
261
|
+
`import { memoryHost, useHost } from '@rulecad/cad/host'`, and likewise `/design`, `/assembly`, `/links`, `/edit`,
|
|
262
|
+
`/explain`, `/schema`, `/errors`, `/optimise`, `/rules`, `/workspace`, `/versions`, `/layout`, `/preview`, `/targets`, `/threemf`.
|
|
231
263
|
- **Engines:** designs get `v.ops` (union, subtract, intersect): JSCAD's booleans by default, Manifold's (faster,
|
|
232
264
|
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
|
|
265
|
+
- **Sandbox:** a JSCAD design runs in a separate Node process under Node's permission model. Whatever it does, it
|
|
266
|
+
can't write or delete files, start programs or workers, or load addons, and it reads only the workspace and the
|
|
267
|
+
packages designs use. To keep it to making shapes it may import only `@jscad/modeling`, `@rulecad/shapes`,
|
|
268
|
+
`@rulecad/flexures` and files in its own project (no `node:` modules), `fetch` and its kin are gone, and it is
|
|
269
|
+
started with none of your environment (no tokens). Those shut the ordinary roads to the network, not every one:
|
|
270
|
+
before Node 25 the permission model doesn't cover the network, so a design written to get round them might still
|
|
271
|
+
reach it. Run only designs you trust. It gets its values as data and returns its parts; what it prints
|
|
272
|
+
(`console.log`) comes out on stderr. A workspace that trusts its designs sets `"sandbox": false` in cad.config.json
|
|
273
|
+
(or `CAD_TRUST=1`). Board designs (engine pcb) run in the tool's process, with the board engine.
|
|
274
|
+
- **Mistakes are named:** a design that reads a variable that isn't there (`v.widht`) stops with the name it
|
|
275
|
+
probably meant, not a shape drawn from `undefined`; errors in a design carry its file and line.
|
|
239
276
|
- **Units:** `cad check` warns where an expression mixes units (millimetres added to degrees, a sine of a length) or
|
|
240
277
|
comes to another unit than its variable says (mm × mm with unit mm). Counts (keys, u, rows) are plain numbers.
|
|
241
278
|
- **Progress:** `--progress` shows what a long check or sweep is building; a program passes `progress` to
|
|
@@ -371,7 +408,8 @@ worked out again when something it depends on changes.
|
|
|
371
408
|
|
|
372
409
|
A 3D result with a build direction is made printable before it's drawn: built up layer by layer from the bed, every
|
|
373
410
|
cell that nothing holds up (an icicle, a loose bit, a sheet hanging under an edge: whatever the optimiser's smooth
|
|
374
|
-
overhang filter let through) is emptied, with whatever rested on it; a bridge may run out half the printer's span
|
|
411
|
+
overhang filter let through) is emptied, with whatever rested on it; a bridge may run out half the printer's span,
|
|
412
|
+
in the optimiser's filter and in the cleaning alike, so a flat underside can open into holes that wide.
|
|
375
413
|
Kept regions always stay, and the optimiser is made to hold them up: a kept cell with nothing under it costs it as
|
|
376
414
|
much as the whole part's compliance. If there wasn't material enough to stand a kept region on, `cad optimise` says
|
|
377
415
|
how much of it is left hanging. Then the drawn surface is tipped back to the printer's overhang limit and any tip left
|
|
@@ -386,7 +424,7 @@ It listens on this computer only. To watch from elsewhere, reach it through a pr
|
|
|
386
424
|
A design uses it through a variable of type `problem`, which picks a problem by name:
|
|
387
425
|
|
|
388
426
|
```json
|
|
389
|
-
"problem": { "type": "problem", "value": "lever", "choice": ["lever", "lever:both"] }
|
|
427
|
+
"problem": { "type": "problem", "value": "lever", "choice": ["lever", "lever:both", "none"] }
|
|
390
428
|
```
|
|
391
429
|
|
|
392
430
|
```js
|
|
@@ -395,6 +433,10 @@ export function main (v) {
|
|
|
395
433
|
}
|
|
396
434
|
```
|
|
397
435
|
|
|
436
|
+
A variant that sets the problem to `none` builds without optimising (`v.problems` then lacks it): a test rig drawn on a
|
|
437
|
+
plain base, or the parts a problem's regions name (`{ "part": "body:regions.mounts" }`), which must not need the problem
|
|
438
|
+
they feed.
|
|
439
|
+
|
|
398
440
|
`optimisedOutline` gives the outline as a 2D shape, its keep regions added and avoid regions cut exactly, so bolt
|
|
399
441
|
holes come out true.
|
|
400
442
|
|
|
@@ -458,6 +500,8 @@ they bend. To check a final shape, give the analysis a smaller `cell`:
|
|
|
458
500
|
The part is then analysed on those cells and again on cells a quarter bigger. For each load case, `cad check`
|
|
459
501
|
reports:
|
|
460
502
|
- **Stiffness and its change:** under 5% from the bigger cells and it has settled; more, and the cells are too big.
|
|
503
|
+
- **Members too thin for the cells:** a warning when more than 5% of the part lies in members under four cells
|
|
504
|
+
thick (what an opening of two cells takes away), where a cell's stiffness is a rough figure: use a smaller cell.
|
|
461
505
|
- **Peak stress, where it is, and the safety factor:** against the material's strength (its weaker one across a
|
|
462
506
|
print's layers, where the rule set has `strength_z`), and its fatigue strength where it has one. With
|
|
463
507
|
`min_safety`, a case below it fails. Peak stress at a sharp inside corner keeps rising as cells shrink, so read it
|
|
@@ -480,7 +524,7 @@ The analysis is a design aid, not engineering sign-off: anything that could hurt
|
|
|
480
524
|
|
|
481
525
|
A circuit board is an object whose .json says `"engine": "pcb"`. Its `main(v)` returns a
|
|
482
526
|
[tscircuit](https://github.com/tscircuit/tscircuit) board (circuits as code, from npm) built with `v.pcb`
|
|
483
|
-
(`
|
|
527
|
+
(`@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
|
|
484
528
|
previews, links, `cad show` and fit assemblies work as for printed parts. The copper (tracks, pads, vias) is drawn in the
|
|
485
529
|
2D views, not in 3D.
|
|
486
530
|
|
|
@@ -517,14 +561,18 @@ npx cad export keyboard/macropad-pcb # <name>-fab.zip for JLCPCB, KiCad files
|
|
|
517
561
|
(with its Pico, `pcb-with-pico`) under a plate thinned by `top_clearance` for the header joints, in a case built
|
|
518
562
|
from the board's numbers, every fit checked (switches on the board, plate over it, board and Pico in the case).
|
|
519
563
|
- **The matrix:** `<name>-matrix.json` gives each key's row and column and each line's GPIO, for firmware.
|
|
520
|
-
- **Versions:**
|
|
521
|
-
overridden in package.json so every part of it agrees.
|
|
564
|
+
- **Versions:** `@rulecad/pcb` pins tscircuit's packages to exact versions (tscircuit moves fast).
|
|
522
565
|
|
|
523
566
|
### Speed
|
|
524
567
|
Fit checks find closest gaps through a grid of triangles (nearest cubes first), and remember overlap volumes between
|
|
525
568
|
placed parts in `.cache/overlaps.json`, keyed by each part's build and placement: the staggered keyboards' checks take
|
|
526
569
|
about 10 s, and a re-check after an edit measures only what the edit moved. The build cache keeps its 800 most
|
|
527
|
-
recently used builds
|
|
570
|
+
recently used builds, keyed by what a build is made from: the design's code, its values resolved, its parts' names
|
|
571
|
+
and roles, the rule sets and the tool's own code; a rule's words, a label or the version change nothing it builds,
|
|
572
|
+
so they don't start one. A lamina mechanism's slow stages (its physics, a flooded frame's probe) are kept apart in
|
|
573
|
+
`.cache/stages/`, keyed by their own inputs, so a build that changes only what comes after them (a strap, a check)
|
|
574
|
+
takes minutes rather than the twenty a launcher's physics takes; the design's sandbox reads them and hands back what
|
|
575
|
+
it made for the tool to write. `CAD_NO_CACHE=1` computes everything afresh (and keeps it).
|
|
528
576
|
|
|
529
577
|
### Assemblies: values from other parts
|
|
530
578
|
(An object can also read another object directly, outside any assembly: see "Other objects' numbers" under Variables.
|
|
@@ -536,6 +584,16 @@ builds an item only for its values: the PCB build's print plates (`staggered-pcb
|
|
|
536
584
|
the board's numbers without the board being on the bed. In assemblies a board counts as a reference: checked, drawn
|
|
537
585
|
faint, never in a print file.
|
|
538
586
|
|
|
587
|
+
## Mechanisms printed flat
|
|
588
|
+
|
|
589
|
+
A design whose `main` returns `v.flexures.lamina({ … })` (see @rulecad/flexures, Lamina mechanisms) is a mechanism
|
|
590
|
+
drawn in one plane and printed on its side. `cad check` adds its own findings to the print checks: that it prints
|
|
591
|
+
free (nothing within the printer's gap of what it isn't joined to), that every step of its sequence is clear, that
|
|
592
|
+
nothing jams, that its catches hold, and that every flexure has a hard stop before its strain limit. `cad animate
|
|
593
|
+
<project>/<object>` writes the sequence, start to finish, as an animated SVG in .preview/: the frame drawn once, the
|
|
594
|
+
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
|
|
595
|
+
`print_gap` is the gap the checks hold it to.
|
|
596
|
+
|
|
539
597
|
## Checks
|
|
540
598
|
|
|
541
599
|
- **Closed solids:** every edge must be matched by faces on the other side. T-junctions (an edge covered by several
|
package/TUTORIAL.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# Your first parts with cad
|
|
2
|
+
|
|
3
|
+
A walk from an empty folder to a part of your own, checked against the thing it fits. It takes about twenty minutes
|
|
4
|
+
and needs Node 20 or later. Every file and command here is run by the tests as written, so what you type is what works.
|
|
5
|
+
|
|
6
|
+
The words used:
|
|
7
|
+
|
|
8
|
+
- **Workspace**: the folder your designs live in.
|
|
9
|
+
- **Project**: a group of designs that belong together (`home`).
|
|
10
|
+
- **Object**: one design: a `.json` of its variables and a `.js` that draws its shape.
|
|
11
|
+
- **Rule set**: facts shared by designs, each with its source: a printer's limits (`x1c`), a screw's sizes (`m4`).
|
|
12
|
+
- **Assembly**: objects placed together, to check how they fit.
|
|
13
|
+
|
|
14
|
+
## 1. Start a workspace
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm create rulecad my-designs
|
|
18
|
+
cd my-designs
|
|
19
|
+
npx cad check home/soap-tray
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The workspace comes with one design, a soap tray, and `check` builds it and tests it for printing:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
Soap tray 0.1.0
|
|
26
|
+
✓ files match their schemas
|
|
27
|
+
✓ every measurement is a variable
|
|
28
|
+
✓ 110 × 75 × 12 mm, fits the bed
|
|
29
|
+
✓ 6955.8 mm² flat on the bed
|
|
30
|
+
✓ no overhangs past 45°
|
|
31
|
+
✓ closed solid
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The first check also copies the printer's rule set into `rules/x1c.json`. It is yours now: when you measure your own
|
|
35
|
+
printer, correct the values there and every design follows.
|
|
36
|
+
|
|
37
|
+
## 2. See where a number comes from
|
|
38
|
+
|
|
39
|
+
Open `projects/home/objects/soap-tray.json`. Each variable has a value and a `rule` saying why. A value can be a
|
|
40
|
+
number, or arithmetic over other names:
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
"wall": { "value": "x1c.line * 4", "unit": "mm", "rule": "Four print lines: stiff, and no gaps between lines" }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`x1c.line` is the printer's line width, from its rule set. Ask what any value rests on:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
npx cad why home/soap-tray.wall
|
|
50
|
+
npx cad rules home/soap-tray
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`why` follows one value down to the rules and their sources; `rules` lists every variable and rule the design uses.
|
|
54
|
+
|
|
55
|
+
## 3. Change it
|
|
56
|
+
|
|
57
|
+
Variables with a `tweak` range can be changed for one run, without touching the file, or for good:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
npx cad check home/soap-tray --set length=140
|
|
61
|
+
npx cad set home/soap-tray length=120
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`set` edits the `.json` and keeps the rest of the file as it was. It refuses a change that would leave the design
|
|
65
|
+
broken.
|
|
66
|
+
|
|
67
|
+
## 4. A part of your own
|
|
68
|
+
|
|
69
|
+
A wall hook: a back plate with a hole for an M4 screw, and an arm.
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
npx cad new home/hook
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
That makes `projects/home/objects/hook.json` and `hook.js` from a template. Replace the `.json` with the hook's
|
|
76
|
+
variables. The screw's sizes come from the `m4` rule set, which is copied into `rules/` the first time it is used:
|
|
77
|
+
|
|
78
|
+
```json projects/home/objects/hook.json
|
|
79
|
+
{
|
|
80
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
81
|
+
"name": "hook",
|
|
82
|
+
"title": "Wall hook",
|
|
83
|
+
"version": "0.1.0",
|
|
84
|
+
"uses": ["x1c", "m4"],
|
|
85
|
+
"material": "PETG",
|
|
86
|
+
"orientation": "On its side, so the layers run along the arm and it can't snap off across them.",
|
|
87
|
+
"variables": {
|
|
88
|
+
"width": { "value": 20, "unit": "mm", "rule": "Wide enough for a coat's loop", "label": "Width", "show": true, "tweak": { "min": 10, "max": 60 } },
|
|
89
|
+
"plate": { "value": 50, "unit": "mm", "rule": "The back plate: room for a screw above the arm", "label": "Plate", "show": true },
|
|
90
|
+
"reach": { "value": 30, "unit": "mm", "rule": "How far the arm stands out from the wall", "label": "Reach", "show": true, "tweak": { "min": 15, "max": 60 } },
|
|
91
|
+
"thick": { "value": "x1c.line * 10", "unit": "mm", "rule": "Ten print lines: stiff enough for a coat" },
|
|
92
|
+
"hole": { "value": "m4.clearance + x1c.hole_comp", "unit": "mm", "rule": "An M4 screw's clearance hole, widened for how holes print", "interface": true }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Then the shape. `main(v)` gets the variables as `v`, with the rule sets (`v.x1c.line`), print-aware shapes
|
|
98
|
+
(`v.shapes`) and booleans (`v.ops`). Z is up and the bed is z = 0: draw the part the way it will print.
|
|
99
|
+
|
|
100
|
+
```js projects/home/objects/hook.js
|
|
101
|
+
// A wall hook: a back plate with a screw hole, and an arm. It lies on its side to print (z is its width).
|
|
102
|
+
import jscad from '@jscad/modeling'
|
|
103
|
+
const { cuboid } = jscad.primitives
|
|
104
|
+
|
|
105
|
+
export function main (v) {
|
|
106
|
+
const plate = cuboid({ size: [v.plate, v.thick, v.width], center: [0, v.thick / 2, v.width / 2] })
|
|
107
|
+
const arm = cuboid({ size: [v.thick, v.reach, v.width], center: [(v.thick - v.plate) / 2, v.thick + v.reach / 2, v.width / 2] })
|
|
108
|
+
// a hole across the part as it prints: a teardrop, so its roof needs no support. It is a line longer than the
|
|
109
|
+
// plate is thick, so it comes out of the back cleanly
|
|
110
|
+
const screw = v.shapes.teardropHole({ d: v.hole, length: v.thick + v.x1c.line, at: [v.plate / 2 / 2, v.thick, v.width / 2], into: '-y' })
|
|
111
|
+
return v.ops.subtract(v.ops.union(plate, arm), screw)
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
npx cad check home/hook
|
|
117
|
+
npx cad preview home/hook
|
|
118
|
+
npx cad export home/hook
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`preview` draws six views as a picture in `.preview/` (it needs Chrome or Chromium). Look at it before you print.
|
|
122
|
+
`export` writes a 3MF for your slicer in `exports/`.
|
|
123
|
+
|
|
124
|
+
## 5. When something is wrong
|
|
125
|
+
|
|
126
|
+
The check names mistakes rather than drawing something else. The ones everyone makes first:
|
|
127
|
+
|
|
128
|
+
| You wrote | The check says |
|
|
129
|
+
|---|---|
|
|
130
|
+
| a number in the `.js`: `v.plate / 4` | `numbers in hook.js that should be variables: line 10: 4` |
|
|
131
|
+
| a variable that isn't there: `v.thik` | `v.thik: the design has no variable "thik" (did you mean "thick"?)` |
|
|
132
|
+
| a rule that isn't there: `m4.clearence` | `"m4.clearence": no rule "clearence" in m4 (hook.json: variables.hole.value)` |
|
|
133
|
+
| a misspelt key in the `.json`: `"lable"` | `variables.thick.lable: not allowed here (did you mean "label"?)` |
|
|
134
|
+
| nothing returned from `main` | `main() returned no shape: return a JSCAD solid, or named parts` |
|
|
135
|
+
|
|
136
|
+
Only 0, 1 and 2 may be written as numbers in a design: everything else is a variable, so that every measurement can
|
|
137
|
+
be traced. To see a value while you work, `console.log` it in `main`: it comes out with the check's output.
|
|
138
|
+
|
|
139
|
+
## 6. Check it against what it fits
|
|
140
|
+
|
|
141
|
+
A part is right when it fits the thing it is for. Model that thing as a **reference** object: drawn and checked
|
|
142
|
+
against, never printed. Here, the screw's shank:
|
|
143
|
+
|
|
144
|
+
```sh
|
|
145
|
+
npx cad new home/screw
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```json projects/home/objects/screw.json
|
|
149
|
+
{
|
|
150
|
+
"$schema": "https://jonibach.github.io/rulecad/schemas/object.schema.json",
|
|
151
|
+
"name": "screw",
|
|
152
|
+
"title": "M4 screw",
|
|
153
|
+
"version": "0.1.0",
|
|
154
|
+
"role": "reference",
|
|
155
|
+
"variables": {
|
|
156
|
+
"d": { "value": 4, "unit": "mm", "rule": "An M4 screw's thread", "interface": true },
|
|
157
|
+
"length": { "value": 16, "unit": "mm", "rule": "Long enough to go through the plate into a wall plug" },
|
|
158
|
+
"sides": { "value": 32, "rule": "Sides drawn round the shank" }
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```js projects/home/objects/screw.js
|
|
164
|
+
// An M4 screw's shank, lying along y: something the hook fits, not something to print.
|
|
165
|
+
import jscad from '@jscad/modeling'
|
|
166
|
+
|
|
167
|
+
export function main (v) {
|
|
168
|
+
return jscad.transforms.rotateX(Math.PI / 2, jscad.primitives.cylinder({ radius: v.d / 2, height: v.length, segments: v.sides }))
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
An assembly places objects together. Positions are expressions over the items' own variables, so the screw stays in
|
|
173
|
+
the hole when the hook changes:
|
|
174
|
+
|
|
175
|
+
```json projects/home/assemblies/hook-on-screw.json
|
|
176
|
+
{
|
|
177
|
+
"title": "Hook on its screw",
|
|
178
|
+
"kind": "fit",
|
|
179
|
+
"uses": ["x1c"],
|
|
180
|
+
"items": [
|
|
181
|
+
{ "id": "hook", "object": "hook" },
|
|
182
|
+
{ "id": "screw", "object": "screw", "at": ["hook.plate / 4", "hook.thick - screw.length / 2", "hook.width / 2"] }
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Now say how they should fit: the gap between the screw and its hole, between the printer's sliding fit and half a
|
|
188
|
+
millimetre. An object that returns one shape has one part, `body`.
|
|
189
|
+
|
|
190
|
+
```sh
|
|
191
|
+
npx cad add fit home/hook-on-screw --a hook.body --b screw.body --min x1c.fit --max 0.5 --note "The screw in its hole"
|
|
192
|
+
npx cad check home/hook-on-screw
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
Hook on its screw (fit: hook 0.1.0, screw 0.1.0)
|
|
197
|
+
✓ files match their schemas
|
|
198
|
+
✓ no parts overlap (2 object instances)
|
|
199
|
+
✓ The screw in its hole: closest gap 0.295 mm
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Change `hole` in the hook to something too tight and the check fails with the number. That is the loop: rules, a
|
|
203
|
+
shape from them, and a check against the thing it meets.
|
|
204
|
+
|
|
205
|
+
## 7. Share it
|
|
206
|
+
|
|
207
|
+
```sh
|
|
208
|
+
npx cad link home/hook --md
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
That prints one link that opens the model in a browser, with its rules and downloads. The model travels in the link
|
|
212
|
+
itself: nothing is uploaded.
|
|
213
|
+
|
|
214
|
+
## Where next
|
|
215
|
+
|
|
216
|
+
Each of these is a section of [the README](README.md):
|
|
217
|
+
|
|
218
|
+
- **Parts**: a design that returns several named parts, supports that break away, a second colour.
|
|
219
|
+
- **Variants**: named sets of inputs, side by side (`home/hook:long`).
|
|
220
|
+
- **Joints and poses**: things that move, checked in every position and swept for collisions.
|
|
221
|
+
- **Versions**: `cad release` compares a design with its last release and says whether an earlier print still fits.
|
|
222
|
+
- **Models from elsewhere**: measure someone else's STL into rules, and match your design against it.
|
|
223
|
+
- **Optimised parts**: the stiffest layout for a load, and an analysis of what you drew.
|
|
224
|
+
- **Boards**: circuit boards from the same rules as the case around them.
|
|
225
|
+
- **Mechanisms printed flat**: springs, catches and triggers in one print, checked through their whole cycle.
|