@rulecad/cad 0.7.97 → 0.9.0

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.
Files changed (86) hide show
  1. package/AGENTS.md +26 -10
  2. package/CHANGELOG.md +442 -2
  3. package/EXAMPLES.md +48 -42
  4. package/README.md +226 -73
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2253 -514
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1854 -437
  9. package/lib/beams.mjs +256 -52
  10. package/lib/belts.mjs +66 -15
  11. package/lib/board-work.mjs +12 -0
  12. package/lib/bought.mjs +309 -53
  13. package/lib/buildpage.mjs +179 -29
  14. package/lib/carried.mjs +201 -0
  15. package/lib/clone.mjs +194 -42
  16. package/lib/design.mjs +734 -113
  17. package/lib/drive.mjs +187 -63
  18. package/lib/edit.mjs +900 -191
  19. package/lib/errors.mjs +16 -3
  20. package/lib/explain.mjs +62 -13
  21. package/lib/fasten.mjs +2710 -502
  22. package/lib/fittings.mjs +715 -121
  23. package/lib/fixings.mjs +21 -9
  24. package/lib/framebuild.mjs +792 -176
  25. package/lib/framecheck.mjs +1901 -368
  26. package/lib/framegen.mjs +100 -22
  27. package/lib/frameheat.mjs +225 -42
  28. package/lib/frameprint.mjs +513 -108
  29. package/lib/frames.mjs +81 -17
  30. package/lib/framesize.mjs +792 -120
  31. package/lib/frametip.mjs +235 -0
  32. package/lib/gait.mjs +135 -23
  33. package/lib/grip.mjs +62 -13
  34. package/lib/guides.mjs +1403 -230
  35. package/lib/host.mjs +149 -18
  36. package/lib/inplace.mjs +171 -36
  37. package/lib/jigs.mjs +360 -78
  38. package/lib/joins.mjs +697 -118
  39. package/lib/jsonedit.mjs +88 -26
  40. package/lib/kept.mjs +84 -0
  41. package/lib/kit.mjs +438 -68
  42. package/lib/layout.mjs +3 -1
  43. package/lib/links.mjs +149 -42
  44. package/lib/list.mjs +326 -64
  45. package/lib/live-guard.mjs +68 -0
  46. package/lib/live-shape.mjs +96 -24
  47. package/lib/live.mjs +189 -59
  48. package/lib/loads.mjs +579 -130
  49. package/lib/machine.mjs +372 -74
  50. package/lib/magnets.mjs +162 -44
  51. package/lib/measured.mjs +417 -94
  52. package/lib/mesh.mjs +526 -119
  53. package/lib/meshcmd.mjs +215 -60
  54. package/lib/motors.mjs +1022 -212
  55. package/lib/motorsvg.mjs +114 -32
  56. package/lib/names.mjs +41 -0
  57. package/lib/optimise.mjs +979 -283
  58. package/lib/order.mjs +519 -112
  59. package/lib/panels.mjs +482 -94
  60. package/lib/parts.mjs +78 -23
  61. package/lib/pins.mjs +163 -44
  62. package/lib/plates.mjs +224 -36
  63. package/lib/player.mjs +37 -9
  64. package/lib/power.mjs +64 -13
  65. package/lib/preview.mjs +83 -16
  66. package/lib/profilemeasure.mjs +572 -129
  67. package/lib/rules.mjs +38 -4
  68. package/lib/sandbox-hooks.mjs +154 -12
  69. package/lib/sandbox-preload.mjs +2 -2
  70. package/lib/sandbox-run.mjs +127 -12
  71. package/lib/sandbox.mjs +632 -35
  72. package/lib/schema.mjs +43 -11
  73. package/lib/seats.mjs +227 -49
  74. package/lib/services.mjs +261 -52
  75. package/lib/sheet.mjs +851 -178
  76. package/lib/slotnuts.mjs +213 -34
  77. package/lib/targets/gist.mjs +4 -1
  78. package/lib/targets.mjs +42 -6
  79. package/lib/task.mjs +376 -79
  80. package/lib/versions.mjs +113 -28
  81. package/lib/wires.mjs +228 -45
  82. package/lib/workspace.mjs +216 -17
  83. package/package.json +19 -19
  84. package/schemas/assembly.schema.json +2 -2
  85. package/schemas/object.schema.json +1 -1
  86. package/schemas/project.schema.json +1 -1
package/lib/magnets.mjs CHANGED
@@ -9,8 +9,12 @@ import { profile, holesIn, fixingsOf } from './fasten.mjs';
9
9
 
10
10
  const { vec3 } = jscad.maths;
11
11
 
12
- const mm = (n) => String(Number(Number(n).toFixed(3))), r3 = (x) => +x.toFixed(3);
13
- const unit = (v) => { const l = Math.hypot(...v); return v.map((c) => c / l); };
12
+ const mm = (n) => String(Number(Number(n).toFixed(3))),
13
+ r3 = (x) => +x.toFixed(3);
14
+ const unit = (v) => {
15
+ const l = Math.hypot(...v);
16
+ return v.map((c) => c / l);
17
+ };
14
18
  const top = (n) => String(n).split(/[.@]/)[0];
15
19
  /** How far a pocket as drawn may be from one the magnet's fits draw and still be taken for it, mm across. */
16
20
  const TOL = 0.06;
@@ -26,15 +30,34 @@ const FITS = ['press', 'glue', 'ribs'];
26
30
  * @returns {object} the fit, with magnet and a fastener's name
27
31
  */
28
32
  export function magnetOfFit(x, F) {
29
- const p = x.fastener, label = x.note || x.label || `${x.a} / ${x.b}`;
30
- if (typeof p.magnet !== 'string' || !p.magnet.trim()) throw new Error(`fit ${label}: fastener as an object names magnets: { "magnet": "disc magnet 8x3", "count": 2 } (got ${JSON.stringify(p)})`);
33
+ const p = x.fastener,
34
+ label = x.note || x.label || `${x.a} / ${x.b}`;
35
+ if (typeof p.magnet !== 'string' || !p.magnet.trim())
36
+ throw new Error(
37
+ `fit ${label}: fastener as an object names magnets: { "magnet": "disc magnet 8x3", "count": 2 } (got ${JSON.stringify(p)})`,
38
+ );
31
39
  const stray = Object.keys(p).filter((k) => !MAGNET_KEYS.includes(k));
32
- if (stray.length) throw new Error(`fit ${label}: magnets have no "${stray.join('", "')}": a fastener naming magnets takes ${MAGNET_KEYS.join(', ')} (the assembly schema's magnets)`);
33
- if (x.count != null && p.count != null) throw new Error(`fit ${label}: give the magnets' count once, in the fastener ({ "magnet": ..., "count": 2 })`);
34
- if (p.fit != null && !FITS.includes(p.fit)) throw new Error(`fit ${label}: a magnet's fit is ${FITS.join(', ')} (got ${JSON.stringify(p.fit)})`);
40
+ if (stray.length)
41
+ throw new Error(
42
+ `fit ${label}: magnets have no "${stray.join('", "')}": a fastener naming magnets takes ${MAGNET_KEYS.join(', ')} (the assembly schema's magnets)`,
43
+ );
44
+ if (x.count != null && p.count != null)
45
+ throw new Error(`fit ${label}: give the magnets' count once, in the fastener ({ "magnet": ..., "count": 2 })`);
46
+ if (p.fit != null && !FITS.includes(p.fit))
47
+ throw new Error(`fit ${label}: a magnet's fit is ${FITS.join(', ')} (got ${JSON.stringify(p.fit)})`);
35
48
  let name;
36
- try { name = F.magnetModel(p.magnet.trim()).name; } catch (e) { throw new Error(`fit ${label}: ${e.message.replace(/^magnetModel: /, '')}`); }
37
- return { fastens: true, ...x, magnet: { ...p, magnet: p.magnet.trim() }, fastener: name, ...(p.count != null || x.count != null ? { count: p.count ?? x.count } : {}) };
49
+ try {
50
+ name = F.magnetModel(p.magnet.trim()).name;
51
+ } catch (e) {
52
+ throw new Error(`fit ${label}: ${e.message.replace(/^magnetModel: /, '')}`);
53
+ }
54
+ return {
55
+ fastens: true,
56
+ ...x,
57
+ magnet: { ...p, magnet: p.magnet.trim() },
58
+ fastener: name,
59
+ ...(p.count != null || x.count != null ? { count: p.count ?? x.count } : {}),
60
+ };
38
61
  }
39
62
 
40
63
  /**
@@ -42,52 +65,147 @@ export function magnetOfFit(x, F) {
42
65
  * @param {object} x the fit, with magnet (magnetOfFit) @param {object} s the built assembly @param {Function} f the findings' sink, as checkAssembly's
43
66
  */
44
67
  export function magnetFit(x, s, f) {
45
- const label = x.note || x.label || `${x.a} to ${x.b}`, code = 'fit.magnet', p = x.magnet, refs = { a: x.a, b: x.b, magnet: x.fastener };
46
- const fail = (text, short, more = {}) => f('fail', `${label}: ${text}`, short ?? `${label}: magnets`, { code, refs, ...more });
68
+ const label = x.note || x.label || `${x.a} to ${x.b}`,
69
+ code = 'fit.magnet',
70
+ p = x.magnet,
71
+ refs = { a: x.a, b: x.b, magnet: x.fastener };
72
+ const fail = (text, short, more = {}) =>
73
+ f('fail', `${label}: ${text}`, short ?? `${label}: magnets`, { code, refs, ...more });
47
74
  const { F, material } = fixingsOf(s.project);
48
- const count = x.count != null ? x.num(x.count) : 1, load = p.load != null ? x.num(p.load) : null, gap = p.gap != null ? x.num(p.gap) : 0;
49
- if (!(Number.isInteger(count) && count >= 1)) return fail(`count is how many magnets, a whole number from 1 (got ${count})`);
75
+ const count = x.count != null ? x.num(x.count) : 1,
76
+ load = p.load != null ? x.num(p.load) : null,
77
+ gap = p.gap != null ? x.num(p.gap) : 0;
78
+ if (!(Number.isInteger(count) && count >= 1))
79
+ return fail(`count is how many magnets, a whole number from 1 (got ${count})`);
50
80
  let seats;
51
- try { seats = (p.fit ? [p.fit] : FITS).map((fit) => F.magnetSeatModel(p.magnet, { fit, material, gap, count, ...(load != null ? { load } : {}) })); } catch (e) { return fail(e.message.replace(/^magnet\w*: /, '')); }
81
+ try {
82
+ seats = (p.fit ? [p.fit] : FITS).map((fit) =>
83
+ F.magnetSeatModel(p.magnet, { fit, material, gap, count, ...(load != null ? { load } : {}) }),
84
+ );
85
+ } catch (e) {
86
+ return fail(e.message.replace(/^magnet\w*: /, ''));
87
+ }
52
88
  const m = seats[0].magnet;
53
- if (m.shape !== 'disc') return fail(`${m.name}: a block magnet's pocket is not round, and only round pockets are found in the parts here; check its pocket with v.fixings.magnetSeatModel`, `${label}: block magnet`);
89
+ if (m.shape !== 'disc')
90
+ return fail(
91
+ `${m.name}: a block magnet's pocket is not round, and only round pockets are found in the parts here; check its pocket with v.fixings.magnetSeatModel`,
92
+ `${label}: block magnet`,
93
+ );
54
94
  const named = (q, n) => q.name === n || q.leaf === n || q.item === n || q.name.startsWith(`${n}.`);
55
95
  const sides = [x.a, x.b].map((n) => ({ name: n, parts: s.parts.filter((q) => !q.hardware && named(q, top(n))) }));
56
96
  const missing = sides.find((sd) => !sd.parts.length);
57
97
  if (missing) return fail(`no part "${missing.name}"`, `${label}: no part`);
58
- const R = m.diameter / 2 + 2, wants = seats.map((st) => ({ fit: st.fit, d: st.pocket.diameter, rules: st.pocket.rules }));
98
+ const R = m.diameter / 2 + 2,
99
+ wants = seats.map((st) => ({ fit: st.fit, d: st.pocket.diameter, rules: st.pocket.rules }));
59
100
  // The pockets: round holes about a fit's pocket, measured along their axis: the hole open at one end, its floor at the other
60
- const found = [], odd = [];
61
- for (const sd of sides) for (const q of sd.parts) for (const h of holesIn(q)) {
62
- const want = wants.find((w) => Math.abs(h.d - w.d) <= TOL), near = !want && Math.abs(h.d - m.diameter) <= 0.6;
63
- if (!want && !near) continue;
64
- const O = q.m ? vec3.transform([0, 0, 0], h.at, q.m) : h.at, e = q.m ? vec3.transform([0, 0, 0], h.at.map((c, i) => c + h.axis[i]), q.m) : h.at, D = unit(e.map((c, i) => c - O[i]));
65
- const segs = profile(q.triangles, O, D, R).segs.filter((g) => g.solid || g.hole);
66
- const holes = segs.filter((g) => g.hole);
67
- if (!holes.length) continue;
68
- // the pocket's run: its hole segments side by side (a lead-in's cone is one of its own), and the floor past one end
69
- const hole = holes.reduce((a, b) => (b.z1 - b.z0 > a.z1 - a.z0 ? b : a));
70
- let i0 = segs.indexOf(hole), i1 = i0;
71
- while (segs[i0 - 1]?.hole && Math.abs(segs[i0 - 1].z1 - segs[i0].z0) < 1e-4) i0--;
72
- while (segs[i1 + 1]?.hole && Math.abs(segs[i1 + 1].z0 - segs[i1].z1) < 1e-4) i1++;
73
- const before = segs[i0 - 1], after = segs[i1 + 1], run = { z0: segs[i0].z0, z1: segs[i1].z1 };
74
- const floorSeg = before?.solid && !after ? before : after?.solid && !before ? after : null; // (blind: material on one side only)
75
- const where = O.map(r3);
76
- if (near) { odd.push({ part: sd.name, d: r3(h.d), at: where }); continue; }
77
- found.push({ part: sd.name, fit: want.fit, rules: want.rules, d: r3(h.d), depth: r3(run.z1 - run.z0), floor: floorSeg ? r3(floorSeg.z1 - floorSeg.z0) : null, at: where });
78
- }
79
- for (const o of odd) fail(`${o.part}'s pocket at ${o.at.map(mm).join(', ')} is drawn ${mm(o.d)} mm, and ${m.name} wants ${wants.map((w) => `${mm(w.d)} mm ${w.fit === 'ribs' ? 'with ribs' : `for a ${w.fit} fit`} (${w.rules.join(', ')})`).join(' or ')}: ${o.d < Math.min(...wants.map((w) => w.d)) ? 'too small, it will not go in' : 'too big, it will be loose'}`, `${label}: pocket ${o.d < Math.min(...wants.map((w) => w.d)) ? 'small' : 'big'} in ${o.part}`, { at: o.at, values: { part: o.part, drawn: o.d } });
101
+ const found = [],
102
+ odd = [];
103
+ for (const sd of sides)
104
+ for (const q of sd.parts)
105
+ for (const h of holesIn(q)) {
106
+ const want = wants.find((w) => Math.abs(h.d - w.d) <= TOL),
107
+ near = !want && Math.abs(h.d - m.diameter) <= 0.6;
108
+ if (!want && !near) continue;
109
+ const O = q.m ? vec3.transform([0, 0, 0], h.at, q.m) : h.at,
110
+ e = q.m
111
+ ? vec3.transform(
112
+ [0, 0, 0],
113
+ h.at.map((c, i) => c + h.axis[i]),
114
+ q.m,
115
+ )
116
+ : h.at,
117
+ D = unit(e.map((c, i) => c - O[i]));
118
+ const segs = profile(q.triangles, O, D, R).segs.filter((g) => g.solid || g.hole);
119
+ const holes = segs.filter((g) => g.hole);
120
+ if (!holes.length) continue;
121
+ // the pocket's run: its hole segments side by side (a lead-in's cone is one of its own), and the floor past one end
122
+ const hole = holes.reduce((a, b) => (b.z1 - b.z0 > a.z1 - a.z0 ? b : a));
123
+ let i0 = segs.indexOf(hole),
124
+ i1 = i0;
125
+ while (segs[i0 - 1]?.hole && Math.abs(segs[i0 - 1].z1 - segs[i0].z0) < 1e-4) i0--;
126
+ while (segs[i1 + 1]?.hole && Math.abs(segs[i1 + 1].z0 - segs[i1].z1) < 1e-4) i1++;
127
+ const before = segs[i0 - 1],
128
+ after = segs[i1 + 1],
129
+ run = { z0: segs[i0].z0, z1: segs[i1].z1 };
130
+ const floorSeg = before?.solid && !after ? before : after?.solid && !before ? after : null; // (blind: material on one side only)
131
+ const where = O.map(r3);
132
+ if (near) {
133
+ odd.push({ part: sd.name, d: r3(h.d), at: where });
134
+ continue;
135
+ }
136
+ found.push({
137
+ part: sd.name,
138
+ fit: want.fit,
139
+ rules: want.rules,
140
+ d: r3(h.d),
141
+ depth: r3(run.z1 - run.z0),
142
+ floor: floorSeg ? r3(floorSeg.z1 - floorSeg.z0) : null,
143
+ at: where,
144
+ });
145
+ }
146
+ for (const o of odd)
147
+ fail(
148
+ `${o.part}'s pocket at ${o.at.map(mm).join(', ')} is drawn ${mm(o.d)} mm, and ${m.name} wants ${wants.map((w) => `${mm(w.d)} mm ${w.fit === 'ribs' ? 'with ribs' : `for a ${w.fit} fit`} (${w.rules.join(', ')})`).join(' or ')}: ${o.d < Math.min(...wants.map((w) => w.d)) ? 'too small, it will not go in' : 'too big, it will be loose'}`,
149
+ `${label}: pocket ${o.d < Math.min(...wants.map((w) => w.d)) ? 'small' : 'big'} in ${o.part}`,
150
+ { at: o.at, values: { part: o.part, drawn: o.d } },
151
+ );
80
152
  if (odd.length) return;
81
- if (found.length < count) return fail(`${count} × ${m.name} wanted, and ${found.length ? `only ${found.length} pocket${found.length > 1 ? 's' : ''} about its ${wants.map((w) => mm(w.d)).join(' or ')} mm ${found.length > 1 ? 'are' : 'is'}` : 'no pocket about its size is'} drawn in ${x.a} or ${x.b}: draw them with v.fixings.magnetSeat`, `${label}: pockets missing`, { values: { count, found: found.length } });
153
+ if (found.length < count)
154
+ return fail(
155
+ `${count} × ${m.name} wanted, and ${found.length ? `only ${found.length} pocket${found.length > 1 ? 's' : ''} about its ${wants.map((w) => mm(w.d)).join(' or ')} mm ${found.length > 1 ? 'are' : 'is'}` : 'no pocket about its size is'} drawn in ${x.a} or ${x.b}: draw them with v.fixings.magnetSeat`,
156
+ `${label}: pockets missing`,
157
+ { values: { count, found: found.length } },
158
+ );
82
159
  let bad = 0;
83
160
  for (const k of found) {
84
- if (k.depth < m.thickness - TOL) { bad++; fail(`${k.part}'s pocket at ${k.at.map(mm).join(', ')} is ${mm(k.depth)} mm deep, and ${m.name} is ${mm(m.thickness)} mm thick: it stands ${mm(r3(m.thickness - k.depth))} mm proud of the face`, `${label}: pocket shallow in ${k.part}`, { at: k.at, values: { depth: k.depth, thickness: m.thickness } }); continue; }
85
- if (k.floor != null) { try { // (the floor held to fixings.magnet_floor by the seat's own refusal)
86
- F.magnetSeatModel(p.magnet, { fit: k.fit, material, thickness: r3(k.depth + k.floor), recess: Math.max(0, r3(k.depth - m.thickness)) }); } catch (e) { bad++; fail(`${k.part}'s pocket at ${k.at.map(mm).join(', ')}: ${e.message.replace(/^magnetSeatModel: /, '')}`, `${label}: floor thin in ${k.part}`, { at: k.at, values: { floor: k.floor } }); } }
161
+ if (k.depth < m.thickness - TOL) {
162
+ bad++;
163
+ fail(
164
+ `${k.part}'s pocket at ${k.at.map(mm).join(', ')} is ${mm(k.depth)} mm deep, and ${m.name} is ${mm(m.thickness)} mm thick: it stands ${mm(r3(m.thickness - k.depth))} mm proud of the face`,
165
+ `${label}: pocket shallow in ${k.part}`,
166
+ { at: k.at, values: { depth: k.depth, thickness: m.thickness } },
167
+ );
168
+ continue;
169
+ }
170
+ if (k.floor != null) {
171
+ try {
172
+ // (the floor held to fixings.magnet_floor by the seat's own refusal)
173
+ F.magnetSeatModel(p.magnet, {
174
+ fit: k.fit,
175
+ material,
176
+ thickness: r3(k.depth + k.floor),
177
+ recess: Math.max(0, r3(k.depth - m.thickness)),
178
+ });
179
+ } catch (e) {
180
+ bad++;
181
+ fail(
182
+ `${k.part}'s pocket at ${k.at.map(mm).join(', ')}: ${e.message.replace(/^magnetSeatModel: /, '')}`,
183
+ `${label}: floor thin in ${k.part}`,
184
+ { at: k.at, values: { floor: k.floor } },
185
+ );
186
+ }
187
+ }
87
188
  }
88
189
  if (bad) return;
89
- const fits = [...new Set(found.map((k) => k.fit))], seat = seats.find((st) => st.fit === fits[0]), parts = [...new Set(found.map((k) => k.part))];
90
- f('pass', `${label}: ${count} × ${m.name} in ${fits.join(' and ')} pockets drawn ${[...new Set(found.map((k) => mm(k.d)))].join(' and ')} mm (${seat.pocket.rules.join(', ')}) in ${parts.join(' and ')}, each deep enough for the magnet`, null, { code, refs, values: { count, pocket: seat.pocket.diameter, grams: m.grams } });
91
- const pull = seat.findings.find((g) => g.code === 'magnet.pull'), hold = seat.findings.find((g) => g.code === 'magnet.hold');
92
- f(hold ? hold.level : 'info', `${label}: ${pull.text}${hold ? `. ${hold.text}` : `. Together ${count} pull ${mm(seat.magnet.total)} N: give load to have it checked`}`, hold && hold.level !== 'pass' ? `${label}: magnets weak` : null, { code: 'fit.magnet.load', refs, values: { pull: seat.magnet.pull, total: seat.magnet.total, ...(hold ? hold.values : {}) } });
190
+ const fits = [...new Set(found.map((k) => k.fit))],
191
+ seat = seats.find((st) => st.fit === fits[0]),
192
+ parts = [...new Set(found.map((k) => k.part))];
193
+ f(
194
+ 'pass',
195
+ `${label}: ${count} × ${m.name} in ${fits.join(' and ')} pockets drawn ${[...new Set(found.map((k) => mm(k.d)))].join(' and ')} mm (${seat.pocket.rules.join(', ')}) in ${parts.join(' and ')}, each deep enough for the magnet`,
196
+ null,
197
+ { code, refs, values: { count, pocket: seat.pocket.diameter, grams: m.grams } },
198
+ );
199
+ const pull = seat.findings.find((g) => g.code === 'magnet.pull'),
200
+ hold = seat.findings.find((g) => g.code === 'magnet.hold');
201
+ f(
202
+ hold ? hold.level : 'info',
203
+ `${label}: ${pull.text}${hold ? `. ${hold.text}` : `. Together ${count} pull ${mm(seat.magnet.total)} N: give load to have it checked`}`,
204
+ hold && hold.level !== 'pass' ? `${label}: magnets weak` : null,
205
+ {
206
+ code: 'fit.magnet.load',
207
+ refs,
208
+ values: { pull: seat.magnet.pull, total: seat.magnet.total, ...(hold ? hold.values : {}) },
209
+ },
210
+ );
93
211
  }