@rulecad/pcb 0.5.3 → 0.5.4

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/CHANGELOG.md ADDED
@@ -0,0 +1,31 @@
1
+ # Changelog: @rulecad/pcb
2
+
3
+ What changed in each release, for someone upgrading a workspace. A **drilled size** here is a dimension the fab
4
+ makes (a hole's diameter in the drill file). Each release says whether any drilled or printed size moved, and why.
5
+
6
+ ## 0.5.4: a board's mounting holes named by thread (Fixings 3)
7
+
8
+ ### What reads fixings now
9
+
10
+ - `mount(thread, x, y, { fit, d, keepout, set, sets })` sizes a board's own mounting hole from the thread's rule set
11
+ through `@rulecad/fixings`: `mount('M2', x, y)` is `m2.clearance`, 2.4 mm, the normal clearance hole of ISO 273. The
12
+ fab drills it, so no printer's compensation is added. `{ fit: 'close' }` and `{ fit: 'loose' }` take ISO 273's other
13
+ two holes.
14
+ - `{ d }` keeps a hole the board's maker gives (a Raspberry Pi Pico's 2.1 mm), and refuses one no wider than the thread.
15
+ - `mount` refuses a non-thread, a whole screw's name, a hole the screw would not pass, an unknown option or fit.
16
+ - The keyboard recipe's corner holes (`mount_holes: 1`) are now `mount('M2', ...)`.
17
+ - New dependency: `@rulecad/fixings`.
18
+
19
+ ### Drilled and printed sizes that moved
20
+
21
+ None. The keyboard recipe's corner holes stay 2.4 mm (`m2.clearance`, as before). The thin-picro module in the
22
+ repository keeps the Pico's 2.1 mm holes through `{ d: 2.1 }`, so it fits cases made for a Pico; its fab files are
23
+ unchanged.
24
+
25
+ ### What a workspace should change
26
+
27
+ - A board that typed its mounting hole's diameter can name the thread instead: `holes: [mount('M2', 10, 5)]`. Typed
28
+ holes keep working.
29
+ - In cad, a case under a board can read the board's holes (`v.pcb.board_holes`), draw an insert's boss under each
30
+ with `v.fixings.insertBoss('M2', ...)`, and the assembly can screw the board down with a `fasten` through the
31
+ board's own holes (`"holes": "pcb.board"`). cad's EXAMPLES, chapter 6, does this end to end.
package/README.md CHANGED
@@ -36,6 +36,15 @@ export function main(v) {
36
36
  - **Parts:** `xiao`, `qwiic`, `jstph` and `header` take their sizes and pinouts from rule sets in @rulecad/rules (each
37
37
  value with its source); `part(name, footprint, x, y)` takes any footprinter name: `soic8`, `0603`, `sot23`,
38
38
  `jst4_sh`, `pinrow4` (SMD or through-hole). Options: `rotation` (0, 90, 180, 270), `layer`, `mpn`, `labels`.
39
+ - **Mounting holes, by thread:** `mount('M2', x, y)` is a board's own hole for an M2 screw, sized from the thread's
40
+ rule set through @rulecad/fixings: `m2.clearance`, 2.4 mm (ISO 273's normal clearance hole). The fab drills it, so
41
+ nothing is added for a printer. `{ fit: 'close' }` or `{ fit: 'loose' }` take ISO 273's other two holes; `{ d: 2.1 }`
42
+ takes a hole the board's maker gives (a Pico's), refused where it is no wider than the thread; `{ keepout: 1.5 }` keeps
43
+ the router's tracks that far from the hole's centre. Put the result in the board's `holes`: `holes: [mount('M2', 10,
44
+ 5)]`. In a cad design, `v.pcb.mount` reads the workspace's own thread set (`rules/m2.json`) before the kit's. The
45
+ keyboard recipe's corner holes (`mount_holes: 1`) are `mount('M2')`. A case reads them (`board_holes`) and puts an
46
+ insert's boss under each (`v.fixings.insertBoss('M2', ...)`); an assembly then screws the board down with `fasten`
47
+ and `"holes": "<item>.board"` (cad's EXAMPLES, chapter 6).
39
48
  - **Parts from KiCad:** `fromKicad(text)` turns a KiCad footprint (`.kicad_mod`) into pad data (y up) and its
40
49
  courtyard. Keep the result in a rule set's `"footprints"` with its source and pin names (`labels: { pin1: 'GND' }`),
41
50
  then place it with `footprint(name, data, x, y, o)`: a design reads rule sets, never files or the network.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/pcb",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "Circuit boards from rules: parts from rule sets or any standard footprint, quarter-turn rotation, nets and autorouting; keyboards with every matrix track drawn by rule. Gerbers, KiCad files and a JLCPCB BOM. Circuit JSON written directly: no React.",
5
5
  "type": "module",
6
6
  "exports": "./pcb.mjs",
@@ -9,6 +9,7 @@
9
9
  "circuit.mjs",
10
10
  "kicad.mjs",
11
11
  "README.md",
12
+ "CHANGELOG.md",
12
13
  "LICENSE"
13
14
  ],
14
15
  "license": "MIT",
@@ -37,6 +38,7 @@
37
38
  "dependencies": {
38
39
  "@flatten-js/core": "1.6.14",
39
40
  "@jscad/modeling": "^2.12.0",
41
+ "@rulecad/fixings": "^0.1.3",
40
42
  "@rulecad/rules": "^0.3.7",
41
43
  "@resvg/resvg-js": "2.6.2",
42
44
  "@rulecad/smooth": "^0.1.2",
package/pcb.mjs CHANGED
@@ -21,6 +21,7 @@ import { createRequire } from 'node:module';
21
21
  // set into the current folder when it reads one
22
22
  const loadRuleSet = (name) => createRequire(import.meta.url)(`@rulecad/rules/${name}.json`);
23
23
  import jscad from '@jscad/modeling';
24
+ import { fixingModel } from '@rulecad/fixings';
24
25
 
25
26
  // A rows × cols grid of keys at the key pitch, centred on the origin: [{ r, c, x, y, n }], n counting from 1, row 0
26
27
  // at the back (+y)
@@ -34,6 +35,51 @@ export function grid(rows, cols, pitch) {
34
35
  const pad = (pin, x, y, hole, outer) => ({ pin, x, y, hole, outer }); // plated, round, relative to the part
35
36
  const hole = (x, y, d) => ({ x, y, d });
36
37
 
38
+ // ---------- mounting holes, by thread ----------
39
+ // Which figure of a thread's rule set each fit of hole is: ISO 273's close, normal and loose clearance holes
40
+ const MOUNT_FITS = { clearance: 'clearance', close: 'clearance_close', loose: 'clearance_loose' };
41
+ const shown = (x) => (x === undefined ? 'nothing' : JSON.stringify(x) ?? String(x));
42
+
43
+ /**
44
+ * A board's mounting hole, named by its screw's thread and sized from the thread's rule set (@rulecad/fixings reads
45
+ * it): { x, y, d, keepout?, thread, fit } as a board's `holes` take them. The fab drills it, so the hole is the figure
46
+ * as it stands, with nothing added for a printer: mount('M2', x, y) is m2.clearance, 2.4 mm.
47
+ * @param {string} thread the screw's thread: 'M2', 'M2.5', 'M3', '4-40'
48
+ * @param {number} x @param {number} y where, in mm, in the board's frame
49
+ * @param {{ fit?: 'clearance' | 'close' | 'loose', d?: number, keepout?: number, set?: object, sets?: object }} [o]
50
+ * fit: the clearance hole's fit (default the normal one); d: a hole the board's maker gives instead (a Pico's 2.1 mm),
51
+ * held to pass the thread; keepout: a radius from the hole's centre the router keeps tracks out of (a screw head's
52
+ * room); set: the thread's figures as a design reads them (v.m2), used first; sets: a workspace's rule sets by name,
53
+ * whose set for this thread is read before the kit's (cad binds them for v.pcb.mount)
54
+ * @returns {{ x: number, y: number, d: number, keepout?: number, thread: string, fit: string }}
55
+ */
56
+ export function mount(thread, x, y, o = {}) {
57
+ const what = 'mount', example = "mount('M2', 10, 5) or mount('M2', 10, 5, { fit: 'close' })";
58
+ if (o == null || typeof o !== 'object' || Array.isArray(o)) throw new Error(`${what}: the options are an object ({ fit, d, keepout }), not ${shown(o)}: ${example}`);
59
+ const known = ['fit', 'd', 'keepout', 'set', 'sets'], odd = Object.keys(o).filter((k) => !known.includes(k));
60
+ if (odd.length) throw new Error(`${what}: no option ${odd.map((k) => `"${k}"`).join(', ')} (there are ${known.join(', ')}): ${example}`);
61
+ for (const [k, n] of [['x', x], ['y', y]]) if (typeof n !== 'number' || !Number.isFinite(n)) throw new Error(`${what}: ${k} is a number, in mm in the board's frame (got ${shown(n)}): ${example}`);
62
+ const fit = o.fit ?? 'clearance';
63
+ if (!(fit in MOUNT_FITS)) throw new Error(`${what}: fit is ${Object.keys(MOUNT_FITS).join(', ')} (got ${shown(fit)})`);
64
+ if (o.keepout != null && !(typeof o.keepout === 'number' && o.keepout > 0)) throw new Error(`${what}: keepout is a radius above 0, in mm from the hole's centre (got ${shown(o.keepout)})`);
65
+ if (o.set != null && (typeof o.set !== 'object' || Array.isArray(o.set))) throw new Error(`${what}: set is the thread's figures as a design reads them (v.m2), not ${shown(o.set)}`);
66
+ let m;
67
+ try { m = fixingModel(thread, {}, { sets: o.sets }); } catch (e) { throw new Error(`${what}: ${String(e.message).replace(/^fixingModel: /, '')}`); } // (a name that is no thread, a thread with no rule set)
68
+ if (m.item !== 'thread') throw new Error(`${what}: name the thread alone ('${m.thread}'), not ${shown(thread)}: the hole is for any screw on it`);
69
+ if (o.set?.thread != null && String(o.set.thread) !== m.thread) throw new Error(`${what}: set is ${shown(o.set.thread)}'s figures, and the hole is for ${m.thread}`);
70
+ const figure = MOUNT_FITS[fit], own = o.set?.[figure], ruled = m.figures[figure]?.value;
71
+ let d;
72
+ if (o.d != null) {
73
+ if (!(typeof o.d === 'number' && o.d > 0)) throw new Error(`${what}: d is the hole's diameter, in mm, above 0 (got ${shown(o.d)})`);
74
+ if (o.d <= m.major) throw new Error(`${what}: a ${o.d} mm hole is no wider than ${m.thread}'s ${m.major} mm thread (${m.set}.major_diameter): the screw would not pass. ${m.thread}'s clearance hole is ${ruled ?? 'not in its rule set'}${ruled != null ? ' mm' : ''}`);
75
+ d = o.d;
76
+ } else {
77
+ d = typeof own === 'number' ? own : ruled;
78
+ if (typeof d !== 'number') throw new Error(`${what}: ${m.thread} has no ${figure} in its rule set (${m.set}): give the hole as d`);
79
+ }
80
+ return { ...hole(x, y, d), ...(o.keepout != null ? { keepout: o.keepout } : {}), thread: m.thread, fit: o.d != null ? 'given' : fit };
81
+ }
82
+
37
83
  // An MX switch, PCB mount: two pins, the centre post and the two fixing pegs
38
84
  export function mx(name, x, y, m, sch = {}) {
39
85
  return { name, ftype: 'simple_switch', mpn: 'Cherry MX (PCB mount)', package: 'Cherry MX, PCB mount', x, y, layer: 'top', sch,
@@ -141,7 +187,7 @@ export function fromLayout(list, pitch) {
141
187
  // under: the Pico on header pins under the board, components down, turned so its two rows of pins
142
188
  // lie in the gaps either side of key row mcu_row + 1, its USB out the mcu_side edge; the matrix is
143
189
  // drawn by rule and the connections to the Pico autorouted
144
- // mcu_gap, mcu_side, mcu_row, mount_holes (1: an M2 hole in each corner), thickness
190
+ // mcu_gap, mcu_side, mcu_row, mount_holes (1: an M2 hole in each corner, mount('M2'): m2.clearance), thickness
145
191
  // matrix: each key's row and column and the GPIO each row and column uses, for checks and firmware.
146
192
  // Lanes beside the Pico, nearest first: each runs from `from` to `to` (its pin's height) and then in to its pin,
147
193
  // across every lane nearer than it, so a lane may be given a nearer place only if its run covers none of the other
@@ -345,7 +391,7 @@ export function keyboard(v, keys) {
345
391
  ...(v.margin >= text * 2 ? [{ text: 'rulecad: diodes point to the rows', x: (keyBox.left + keyBox.right) / 2, y: bottom + v.margin / 2, size: text, layer: 'bottom' }] : [])];
346
392
  const board = { center: { x: cx, y: cy }, width: right - left, height: top - bottom, thickness: v.thickness, autoroute: under,
347
393
  silk_line: v.jlcpcb.silk_line, silk_text: v.jlcpcb.silk_text, via_hole: 0.3, via_pad: 0.3 + 2 * v.jlcpcb.annular, fab: v.jlcpcb, // (vias with the ring the fab recommends; the fab's minimums for the router)
348
- parts, traces, silk: marks, holes: corners.map(([x, y]) => hole(x, y, v.m2.clearance)) };
394
+ parts, traces, silk: marks, holes: corners.map(([x, y]) => mount('M2', x, y, { set: v.m2 })) }; // (the m2 rules' clearance hole, as the fab drills it)
349
395
  const matrix = {
350
396
  keys: keys.map((k) => ({ key: `K${k.n}`, ...(k.label ? { label: k.label } : {}), row: k.r, col: k.c })),
351
397
  rows: Object.fromEntries(rows.map((r) => [r, rowPin[r].gpio])), cols: Object.fromEntries(cols.map((c) => [c, colPin[c].gpio])),