@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/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 ADDED
@@ -0,0 +1,41 @@
1
+ // Names that become parts of file paths: rule sets (rules/<name>.json, and the kit's), projects (projects/<name>/),
2
+ // objects (objects/<name>.js), layouts and problems. They are read from a workspace's own files (project.json's
3
+ // "printer", an object's "uses", cad.config.json) and from the command line, and a workspace can come from anyone, so a
4
+ // name is used only if it is plain: letters, digits, - and _, starting with a letter or digit. A name with a slash, a
5
+ // backslash or a dot could climb out of its folder (issue #342: "printer": "../../../A/B" made cad check copy a file
6
+ // to a new place outside the workspace), so it is refused, with the file and field it came from.
7
+
8
+ /** The characters a rule-set, project, object, layout or problem name may hold. */
9
+ export const PLAIN_NAME = /^[a-z0-9][a-z0-9_-]*$/i;
10
+
11
+ /** Whether `name` is a plain name (PLAIN_NAME), safe to join into a path. */
12
+ export const isPlainName = (name) => typeof name === 'string' && PLAIN_NAME.test(name);
13
+
14
+ /**
15
+ * `name` if it is plain, else an error saying so: which kind of name it is (`what`, as "rule set name") and, where it
16
+ * was read from a file, which file and field (`where`, as 'projects/p/project.json, its "printer"').
17
+ * @param {unknown} name
18
+ * @param {string} what
19
+ * @param {string} [where]
20
+ * @returns {string}
21
+ */
22
+ export function plainName(name, what, where) {
23
+ if (isPlainName(name)) return /** @type {string} */ (name);
24
+ const shown = typeof name === 'string' ? `"${name}"` : String(name);
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
+ );
28
+ }
29
+
30
+ // An object named in another object's "uses": <project>/<object>, then optionally :<variant> and @<version or range>
31
+ const OBJECT_REF = /^([^/:@]+)\/([^/:@]+)(?::([^/@]+))?(?:@([^/]+))?$/;
32
+
33
+ /**
34
+ * Whether an entry of "uses" naming another object (it holds a slash) has the form <project>/<object>, optionally
35
+ * with :<variant> and @<version>, both names plain. A path to a file is not accepted there.
36
+ * @param {unknown} ref
37
+ */
38
+ export function isObjectRefName(ref) {
39
+ const m = typeof ref === 'string' && OBJECT_REF.exec(ref);
40
+ return !!m && isPlainName(m[1]) && isPlainName(m[2]);
41
+ }