@rulecad/cad 0.8.0 → 0.10.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 (102) hide show
  1. package/AGENTS.md +45 -16
  2. package/CHANGELOG.md +724 -0
  3. package/EXAMPLES.md +61 -54
  4. package/README.md +376 -98
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2664 -541
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1883 -435
  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/cache-file.mjs +105 -0
  15. package/lib/carried.mjs +201 -0
  16. package/lib/clone.mjs +194 -42
  17. package/lib/code-text.mjs +28 -0
  18. package/lib/design-imports.mjs +127 -0
  19. package/lib/design.mjs +871 -147
  20. package/lib/designshapes.mjs +142 -0
  21. package/lib/drive.mjs +187 -63
  22. package/lib/edit.mjs +922 -192
  23. package/lib/errors.mjs +16 -3
  24. package/lib/estimates.mjs +51 -0
  25. package/lib/explain.mjs +62 -13
  26. package/lib/fasten.mjs +2708 -507
  27. package/lib/fe-key.mjs +29 -0
  28. package/lib/fittings.mjs +721 -122
  29. package/lib/fixings.mjs +38 -13
  30. package/lib/framebuild.mjs +790 -175
  31. package/lib/framecheck.mjs +1901 -368
  32. package/lib/framegen.mjs +108 -22
  33. package/lib/frameheat.mjs +225 -42
  34. package/lib/frameprint.mjs +513 -108
  35. package/lib/frames.mjs +86 -18
  36. package/lib/framesize.mjs +719 -120
  37. package/lib/frametip.mjs +235 -0
  38. package/lib/gait.mjs +135 -23
  39. package/lib/grip.mjs +62 -13
  40. package/lib/guides.mjs +1403 -230
  41. package/lib/host.mjs +129 -23
  42. package/lib/inplace.mjs +171 -36
  43. package/lib/jigs.mjs +360 -78
  44. package/lib/joins.mjs +697 -118
  45. package/lib/jsonedit.mjs +88 -26
  46. package/lib/kept.mjs +84 -0
  47. package/lib/kit.mjs +439 -69
  48. package/lib/layout.mjs +2 -1
  49. package/lib/links.mjs +149 -42
  50. package/lib/list.mjs +334 -64
  51. package/lib/live-guard.mjs +68 -0
  52. package/lib/live-shape.mjs +96 -24
  53. package/lib/live.mjs +189 -59
  54. package/lib/loads.mjs +597 -136
  55. package/lib/machine.mjs +372 -74
  56. package/lib/magnets.mjs +162 -44
  57. package/lib/measured.mjs +1080 -117
  58. package/lib/mesh.mjs +526 -119
  59. package/lib/meshcmd.mjs +215 -60
  60. package/lib/motors.mjs +1002 -214
  61. package/lib/motorsvg.mjs +114 -32
  62. package/lib/names.mjs +18 -1
  63. package/lib/optimise.mjs +991 -287
  64. package/lib/order.mjs +515 -114
  65. package/lib/panels.mjs +482 -94
  66. package/lib/parts.mjs +81 -23
  67. package/lib/pins.mjs +163 -44
  68. package/lib/plates.mjs +223 -35
  69. package/lib/player.mjs +37 -9
  70. package/lib/power.mjs +64 -13
  71. package/lib/preview.mjs +83 -16
  72. package/lib/profilemeasure.mjs +572 -129
  73. package/lib/rules.mjs +17 -6
  74. package/lib/sandbox-hooks.mjs +179 -27
  75. package/lib/sandbox-lock.mjs +225 -0
  76. package/lib/sandbox-preload.mjs +2 -2
  77. package/lib/sandbox-run.mjs +122 -25
  78. package/lib/sandbox.mjs +667 -75
  79. package/lib/schema.mjs +42 -11
  80. package/lib/seats.mjs +227 -49
  81. package/lib/services.mjs +261 -52
  82. package/lib/sheet.mjs +963 -179
  83. package/lib/slotnuts.mjs +213 -34
  84. package/lib/targets/gist.mjs +4 -1
  85. package/lib/targets.mjs +22 -4
  86. package/lib/task.mjs +376 -79
  87. package/lib/versions.mjs +285 -39
  88. package/lib/wires.mjs +228 -45
  89. package/lib/workspace.mjs +200 -14
  90. package/package.json +19 -19
  91. package/schemas/assembly.schema.json +2 -2
  92. package/schemas/project.schema.json +6 -0
  93. package/templates/coupons/MEASURE.md +109 -0
  94. package/templates/coupons/assemblies/fixings-test.json +20 -0
  95. package/templates/coupons/assemblies/slot-test.json +11 -0
  96. package/templates/coupons/objects/fixings-coupon.js +88 -0
  97. package/templates/coupons/objects/fixings-coupon.json +26 -0
  98. package/templates/coupons/objects/holds-coupon.js +130 -0
  99. package/templates/coupons/objects/holds-coupon.json +42 -0
  100. package/templates/coupons/objects/slot-coupon.js +89 -0
  101. package/templates/coupons/objects/slot-coupon.json +29 -0
  102. package/templates/coupons/project.json +10 -0
package/lib/motorsvg.mjs CHANGED
@@ -8,64 +8,129 @@ import { materialOf } from './list.mjs';
8
8
  const esc = (s) => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;');
9
9
  const fig = (x, d = 3) => String(+Number(x).toPrecision(d));
10
10
  const FONT = 'font-family="system-ui, sans-serif"';
11
- const wrap = (text, n) => text.split(' ').reduce((lines, word) => { const last = lines[lines.length - 1]; if (last && (last + ' ' + word).length <= n) lines[lines.length - 1] = `${last} ${word}`; else lines.push(word); return lines; }, []);
11
+ const wrap = (text, n) =>
12
+ text.split(' ').reduce((lines, word) => {
13
+ const last = lines[lines.length - 1];
14
+ if (last && (last + ' ' + word).length <= n) lines[lines.length - 1] = `${last} ${word}`;
15
+ else lines.push(word);
16
+ return lines;
17
+ }, []);
12
18
 
13
19
  // What each motor does, from the check: its joint, the train to the load, the torque there, its speed, whether it holds
14
20
  export function motorWork(s) {
15
- const found = [], helpers = { placeItems, jointValues, mul };
16
- motorChecks(s, (level, text, card, extra = {}) => found.push({ level, text, ...extra }), helpers, materialOf(s.project));
21
+ const found = [],
22
+ helpers = { placeItems, jointValues, mul };
23
+ motorChecks(
24
+ s,
25
+ (level, text, card, extra = {}) => found.push({ level, text, ...extra }),
26
+ helpers,
27
+ materialOf(s.project),
28
+ );
17
29
  const out = [];
18
30
  let mine = null;
19
31
  for (const x of found) {
20
- if (x.code === 'motor.model') { mine = { id: x.refs?.item, findings: [] }; out.push(mine); continue; }
32
+ if (x.code === 'motor.model') {
33
+ mine = { id: x.refs?.item, findings: [] };
34
+ out.push(mine);
35
+ continue;
36
+ }
21
37
  if (mine) mine.findings.push(x);
22
38
  }
23
- return out.map(({ id, findings }) => {
24
- const d = (s.motion?.drives || []).find((x) => x.id === id), torque = findings.find((x) => x.code === 'motor.torque' && x.values);
25
- if (!d || !torque) return null;
26
- const joint = torque.refs.joint, t = trainFrom(s.motion, joint), speed = findings.find((x) => x.code === 'motor.speed' && x.values);
27
- return { id, drive: d, joint, train: t, ...torque.values, level: torque.level, rpm: speed?.values.rpm ?? null,
28
- holds: findings.filter((x) => x.code === 'motor.holds').map((x) => ({ level: x.level, text: x.text })) };
29
- }).filter(Boolean);
39
+ return out
40
+ .map(({ id, findings }) => {
41
+ const d = (s.motion?.drives || []).find((x) => x.id === id),
42
+ // (the finding with what reaches the joint: not the one that says nothing it drives is loaded)
43
+ torque = findings.find((x) => x.code === 'motor.torque' && x.values?.available != null);
44
+ if (!d || !torque) return null;
45
+ const joint = torque.refs.joint,
46
+ t = trainFrom(s.motion, joint),
47
+ speed = findings.find((x) => x.code === 'motor.speed' && x.values);
48
+ return {
49
+ id,
50
+ drive: d,
51
+ joint,
52
+ train: t,
53
+ ...torque.values,
54
+ level: torque.level,
55
+ rpm: speed?.values.rpm ?? null,
56
+ holds: findings.filter((x) => x.code === 'motor.holds').map((x) => ({ level: x.level, text: x.text })),
57
+ };
58
+ })
59
+ .filter(Boolean);
30
60
  }
31
61
 
32
62
  // The motor's curve at its output, the load and the working speed marked
33
63
  export function workCurve(w) {
34
64
  const slide = w.train.slide != null;
35
- return curve(w.drive.model, { ratio: w.train.ratio, efficiency: w.train.efficiency, ...(w.drive.supply ? { supply: w.drive.supply } : {}), ...(w.rpm != null ? { at: { speed: w.rpm, ...(slide ? {} : { load: w.needed }) } } : {}) });
65
+ return curve(w.drive.model, {
66
+ ratio: w.train.ratio,
67
+ efficiency: w.train.efficiency,
68
+ ...(w.drive.supply ? { supply: w.drive.supply } : {}),
69
+ ...(w.rpm != null ? { at: { speed: w.rpm, ...(slide ? {} : { load: w.needed }) } } : {}),
70
+ });
36
71
  }
37
72
 
38
73
  // The torque through the train: from the motor's shaft, stage by stage, to the joint with the load; the load's need
39
74
  // carried back through the same stages, so each bar shows the margin where it is
40
75
  export function trainSvg(w, { title = '' } = {}) {
41
- const stages = [...w.train.stages].reverse(), slide = w.train.slide != null; // trainFrom walks from the load back to the motor
42
- const atMotor = w.available / (w.train.ratio * w.train.efficiency), rows = [{ name: `${w.id}'s shaft`, have: atMotor, kind: 'the motor' }];
76
+ const stages = [...w.train.stages].reverse(),
77
+ slide = w.train.slide != null; // trainFrom walks from the load back to the motor
78
+ const atMotor = w.shaft ?? w.available / (w.train.ratio * w.train.efficiency),
79
+ rows = [{ name: `${w.id}'s shaft`, have: atMotor, kind: 'the motor' }];
43
80
  let have = atMotor;
44
81
  for (const [k, st] of stages.entries()) {
45
82
  const last = k === stages.length - 1;
46
- if (slide && last) rows.push({ name: st.joint, have: w.available, kind: `${st.kind}, ${fig(st.efficiency * 100, 2)}%`, unit: 'N' });
47
- else { have *= (1 / Math.abs(st.ratio)) * st.efficiency; rows.push({ name: st.joint, have, kind: `${st.kind} ${fig(1 / Math.abs(st.ratio))}:1, ${fig(st.efficiency * 100, 2)}%` }); }
83
+ if (slide && last)
84
+ rows.push({ name: st.joint, have: w.available, kind: `${st.kind}, ${fig(st.efficiency * 100, 2)}%`, unit: 'N' });
85
+ else {
86
+ have *= (1 / Math.abs(st.ratio)) * st.efficiency;
87
+ rows.push({
88
+ name: st.joint,
89
+ have,
90
+ kind: `${st.kind} ${fig(1 / Math.abs(st.ratio))}:1, ${fig(st.efficiency * 100, 2)}%`,
91
+ });
92
+ }
48
93
  }
49
94
  // the need, carried back from the load through the same stages: the same margin at every one
50
95
  for (const r of rows) r.need = w.margin > 0 && Number.isFinite(w.margin) ? r.have / w.margin : null;
51
- const width = 720, rowH = 46, top = 64, L = 200, R = width - 150;
96
+ const width = 720,
97
+ rowH = 46,
98
+ top = 64,
99
+ L = 200,
100
+ R = width - 150;
52
101
  const scale = (r) => (R - L) / Math.max(r.have, r.need ?? 0, 1e-9) / 1.08; // each bar on its own scale: the margin is what reads
53
- const out = [`<rect width="100%" height="100%" fill="#fff"/>`,
102
+ const out = [
103
+ `<rect width="100%" height="100%" fill="#fff"/>`,
54
104
  `<text x="24" y="28" font-size="15" ${FONT} fill="#1a1a1a">${esc(title || `${w.id} into ${w.joint}: the torque through the train`)}</text>`,
55
- `<text x="24" y="48" font-size="11" ${FONT} fill="#888">each stage: what reaches it (blue) against what the load needs there (red), with the margin</text>`];
105
+ `<text x="24" y="48" font-size="11" ${FONT} fill="#888">each stage: what reaches it (blue) against what the load needs there (red), with the margin</text>`,
106
+ ];
56
107
  rows.forEach((r, i) => {
57
- const y = top + i * rowH, k = scale(r), u = r.unit || 'N m';
58
- out.push(`<text x="24" y="${y + 18}" font-size="13" ${FONT} fill="#1a1a1a">${esc(r.name)}</text><text x="24" y="${y + 33}" font-size="11" ${FONT} fill="#888">${esc(r.kind)}</text>`);
108
+ const y = top + i * rowH,
109
+ k = scale(r),
110
+ u = r.unit || 'N m';
111
+ out.push(
112
+ `<text x="24" y="${y + 18}" font-size="13" ${FONT} fill="#1a1a1a">${esc(r.name)}</text><text x="24" y="${y + 33}" font-size="11" ${FONT} fill="#888">${esc(r.kind)}</text>`,
113
+ );
59
114
  out.push(`<rect x="${L}" y="${y + 8}" width="${fig(r.have * k, 4)}" height="20" fill="#2266cc"/>`);
60
- if (r.need != null) out.push(`<line x1="${fig(L + r.need * k, 4)}" x2="${fig(L + r.need * k, 4)}" y1="${y + 2}" y2="${y + 34}" stroke="#cc3322" stroke-width="2.4"/>`);
115
+ if (r.need != null)
116
+ out.push(
117
+ `<line x1="${fig(L + r.need * k, 4)}" x2="${fig(L + r.need * k, 4)}" y1="${y + 2}" y2="${y + 34}" stroke="#cc3322" stroke-width="2.4"/>`,
118
+ );
61
119
  out.push(`<text x="${R + 10}" y="${y + 18}" font-size="12" ${FONT} fill="#1a1a1a">${fig(r.have)} ${u}</text>`);
62
- if (r.need) out.push(`<text x="${R + 10}" y="${y + 33}" font-size="11" ${FONT} fill="${r.have / r.need < 1 ? '#cc3322' : '#555'}">${fig(r.have / r.need, 3)}× the ${fig(r.need)}</text>`);
120
+ if (r.need)
121
+ out.push(
122
+ `<text x="${R + 10}" y="${y + 33}" font-size="11" ${FONT} fill="${r.have / r.need < 1 ? '#cc3322' : '#555'}">${fig(r.have / r.need, 3)}× the ${fig(r.need)}</text>`,
123
+ );
63
124
  });
64
125
  // whether it holds, power on and off, each line wrapped to the drawing's width
65
126
  let y = top + rows.length * rowH + 18;
66
127
  for (const h of w.holds) {
67
- const colour = h.level === 'pass' ? '#2a7a2a' : h.level === 'fail' ? '#cc3322' : '#b06a00', lines = wrap(`${h.level === 'pass' ? '✓' : h.level === 'fail' ? '✗' : '!'} ${h.text}`, 104);
68
- for (const l of lines) { out.push(`<text x="24" y="${y}" font-size="12" ${FONT} fill="${colour}">${esc(l)}</text>`); y += 17; }
128
+ const colour = h.level === 'pass' ? '#2a7a2a' : h.level === 'fail' ? '#cc3322' : '#b06a00',
129
+ lines = wrap(`${h.level === 'pass' ? '✓' : h.level === 'fail' ? '✗' : '!'} ${h.text}`, 104);
130
+ for (const l of lines) {
131
+ out.push(`<text x="24" y="${y}" font-size="12" ${FONT} fill="${colour}">${esc(l)}</text>`);
132
+ y += 17;
133
+ }
69
134
  y += 4;
70
135
  }
71
136
  const height = y + 8;
@@ -75,22 +140,39 @@ export function trainSvg(w, { title = '' } = {}) {
75
140
  // Labels for a check view: at each joint the motor's torque passes through, what reaches it and the margin; at the
76
141
  // joint with the load, whether it holds with the power off. Never drawn on the rest view.
77
142
  export function workMarks(s, works = motorWork(s)) {
78
- const motion = s.motion, worlds = placeItems(motion, s.jointValues ?? jointValues(motion, motion.pose ?? undefined)), out = [];
143
+ const motion = s.motion,
144
+ worlds = placeItems(motion, s.jointValues ?? jointValues(motion, motion.pose ?? undefined)),
145
+ out = [];
79
146
  for (const w of works) {
80
- const stages = [...w.train.stages].reverse(), atMotor = w.available / (w.train.ratio * w.train.efficiency);
81
- const where = (id) => { const j = motion.joints.find((x) => x.id === id); return j ? axisOf(motion, worlds, j, mul).O : null; };
147
+ const stages = [...w.train.stages].reverse(),
148
+ atMotor = w.shaft ?? w.available / (w.train.ratio * w.train.efficiency);
149
+ const where = (id) => {
150
+ const j = motion.joints.find((x) => x.id === id);
151
+ return j ? axisOf(motion, worlds, j, mul).O : null;
152
+ };
82
153
  const first = where(w.drive.joint);
83
- if (first && stages.length) out.push({ at: first, text: `${w.id}: ${fig(atMotor)} N m at the shaft`, level: 'info' });
154
+ if (first && stages.length)
155
+ out.push({ at: first, text: `${w.id}: ${fig(atMotor)} N m at the shaft`, level: 'info' });
84
156
  let have = atMotor;
85
157
  for (const [k, st] of stages.entries()) {
86
158
  const slide = w.train.slide != null && k === stages.length - 1;
87
159
  have = slide ? w.available : have * (1 / Math.abs(st.ratio)) * st.efficiency;
88
- const at = where(st.joint), last = k === stages.length - 1;
160
+ const at = where(st.joint),
161
+ last = k === stages.length - 1;
89
162
  if (!at) continue;
90
163
  const off = last ? w.holds.find((h) => /power off|no figure/.test(h.text)) : null;
91
- out.push({ at, text: `${st.joint}: ${fig(have)} ${slide ? 'N' : 'N m'}${last ? `, ${fig(w.margin)}× the load${off ? `; unpowered ${off.level === 'pass' ? 'holds' : off.level === 'fail' ? 'slips' : 'unknown'}` : ''}` : ''}`, level: last ? w.level : 'info' });
164
+ out.push({
165
+ at,
166
+ text: `${st.joint}: ${fig(have)} ${slide ? 'N' : 'N m'}${last ? `, ${fig(w.margin)}× the load${off ? `; unpowered ${off.level === 'pass' ? 'holds' : off.level === 'fail' ? 'slips' : 'unknown'}` : ''}` : ''}`,
167
+ level: last ? w.level : 'info',
168
+ });
92
169
  }
93
- if (!stages.length && first) out.push({ at: first, text: `${w.joint}: ${fig(w.available)} N m, ${fig(w.margin)}× the load`, level: w.level });
170
+ if (!stages.length && first)
171
+ out.push({
172
+ at: first,
173
+ text: `${w.joint}: ${fig(w.available)} ${w.unit ?? 'N m'}, ${fig(w.margin)}× the load`,
174
+ level: w.level,
175
+ });
94
176
  }
95
177
  return out;
96
178
  }
package/lib/names.mjs CHANGED
@@ -22,7 +22,9 @@ export const isPlainName = (name) => typeof name === 'string' && PLAIN_NAME.test
22
22
  export function plainName(name, what, where) {
23
23
  if (isPlainName(name)) return /** @type {string} */ (name);
24
24
  const shown = typeof name === 'string' ? `"${name}"` : String(name);
25
- throw new Error(`${where ? `${where}: ` : ''}the ${what} ${shown} is not a plain name. Use only letters, digits, - and _, starting with a letter or digit (no spaces, dots, slashes or backslashes), as x1c or keycap-2`);
25
+ throw new Error(
26
+ `${where ? `${where}: ` : ''}the ${what} ${shown} is not a plain name. Use only letters, digits, - and _, starting with a letter or digit (no spaces, dots, slashes or backslashes), as x1c or keycap-2`,
27
+ );
26
28
  }
27
29
 
28
30
  // An object named in another object's "uses": <project>/<object>, then optionally :<variant> and @<version or range>
@@ -37,3 +39,18 @@ export function isObjectRefName(ref) {
37
39
  const m = typeof ref === 'string' && OBJECT_REF.exec(ref);
38
40
  return !!m && isPlainName(m[1]) && isPlainName(m[2]);
39
41
  }
42
+
43
+ /**
44
+ * Whether `f` is a path to a file inside a workspace as the tool writes one down (a build's imports, a release's
45
+ * list of what it read): relative, with forward slashes, never climbing out (..), never absolute or with a drive
46
+ * letter, and never into a node_modules folder. Such lists can come from a workspace's own .cache/ or a release's tag,
47
+ * which a cloned workspace brings, so a path that is not like this is never read.
48
+ * @param {unknown} f
49
+ * @returns {boolean}
50
+ */
51
+ export const isWorkspacePath = (f) =>
52
+ typeof f === 'string' &&
53
+ f !== '' &&
54
+ !f.startsWith('/') &&
55
+ !/^[a-zA-Z]:|\\/.test(f) &&
56
+ f.split('/').every((s) => s !== '' && s !== '.' && s !== '..' && s.toLowerCase() !== 'node_modules');