@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/errors.mjs CHANGED
@@ -22,12 +22,25 @@ export function located(e, file, code) {
22
22
  }
23
23
 
24
24
  /** An error as plain data: { code, message, file?, line?, path? }. @param {any} e */
25
- export const errorData = (e) => ({ code: e?.code || 'error', message: e?.message || String(e), ...(e?.file ? { file: e.file } : {}), ...(e?.line ? { line: e.line } : {}), ...(e?.path ? { path: e.path } : {}), ...(e?.refs ? { refs: e.refs } : {}) });
25
+ export const errorData = (e) => ({
26
+ code: e?.code || 'error',
27
+ message: e?.message || String(e),
28
+ ...(e?.file ? { file: e.file } : {}),
29
+ ...(e?.line ? { line: e.line } : {}),
30
+ ...(e?.path ? { path: e.path } : {}),
31
+ ...(e?.refs ? { refs: e.refs } : {}),
32
+ });
26
33
 
27
34
  /** The closest name to a typo, one or two letters out, or null. @param {string} word @param {string[]} names */
28
35
  export function near(word, names) {
29
- const d = (a, b) => { const m = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]); for (let j = 1; j <= b.length; j++) m[0][j] = j;
30
- for (let i = 1; i <= a.length; i++) for (let j = 1; j <= b.length; j++) m[i][j] = Math.min(m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1)); return m[a.length][b.length]; };
36
+ const d = (a, b) => {
37
+ const m = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
38
+ for (let j = 1; j <= b.length; j++) m[0][j] = j;
39
+ for (let i = 1; i <= a.length; i++)
40
+ for (let j = 1; j <= b.length; j++)
41
+ m[i][j] = Math.min(m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
42
+ return m[a.length][b.length];
43
+ };
31
44
  const best = names.map((n) => [n, d(word, n)]).sort((x, y) => x[1] - y[1])[0];
32
45
  return best && best[1] <= 2 ? best[0] : null;
33
46
  }
@@ -0,0 +1,51 @@
1
+ // What a design rests on that is only an estimate, said as one warning (rules.estimate) by an object's check and an
2
+ // assembly's alike: AGENTS.md promises that a figure nobody has measured is flagged where it is used. No files are read
3
+ // here (an assembly hands in what its own rule names are, kindOf), so a seeded defect can run it as it is.
4
+
5
+ /**
6
+ * The warning that a design rests on estimates (rules.estimate), for an object's check and an assembly's alike: the
7
+ * rule-set entries that are only estimates (those the design read, and the object's own variables marked so), the
8
+ * assembly's own rule names that are (seen), and the seats v.shapes drew to an estimated fit (lib/designshapes.mjs).
9
+ * Null where nothing rests on an estimate.
10
+ * @param {{ entries?: object[], seats?: object[] | null }[]} builds each object's build (an assembly's every item's)
11
+ * @param {{ ref: string, seen?: Iterable<string>, kindOf?: (name: string) => string | undefined, assembly?: boolean }} o
12
+ * ref: the object or assembly, project/name; seen: rule names (set.rule) the assembly itself read, with kindOf to
13
+ * give each one's kind; assembly: whether ref is an assembly, which cad measured takes
14
+ * @returns {{ level: 'warn', text: string, short: string, code: 'rules.estimate', refs: { rules: string[],
15
+ * seats?: { helper: string, rule: string, fit: any }[] } } | null}
16
+ */
17
+ export function estimatesFinding(builds, { ref, seen = [], kindOf = () => undefined, assembly = false }) {
18
+ const est = new Set();
19
+ for (const b of builds)
20
+ for (const e of b?.entries ?? []) if (e.kind === 'estimate' && (e.used || !e.name.includes('.'))) est.add(e.name);
21
+ for (const n of seen) if (kindOf(n) === 'estimate') est.add(n);
22
+ const seats = new Map();
23
+ for (const b of builds)
24
+ for (const s of Array.isArray(b?.seats) ? b.seats : [])
25
+ if (s?.refs?.kind === 'estimate' && typeof s.refs.rule === 'string')
26
+ seats.set(`${s.refs.helper} ${s.refs.rule}`, { helper: s.refs.helper, rule: s.refs.rule, fit: s.refs.fit });
27
+ if (!est.size && !seats.size) return null; // (a seat drawn to an estimated fit always names that fit's rule)
28
+ // (a seat drawn to a rule-set entry, x1c.press, is named once, as that rule, even where the design passed the
29
+ // printer's figures as a copy and never read the rule itself: cad measured settles it; one drawn to a fit of
30
+ // @rulecad/shapes' own, SHAFT_FITS.press, is settled by giving the fit that works, in mm each side)
31
+ const seated = [...seats.values()],
32
+ own = seated.filter((s) => s.rule.startsWith('SHAFT_FITS.'));
33
+ for (const s of seated) if (!s.rule.startsWith('SHAFT_FITS.')) est.add(s.rule);
34
+ const rules = [...est],
35
+ named = [...rules, ...own.map((s) => `${s.helper}'s ${s.rule}`)];
36
+ const settle = assembly
37
+ ? `cad measured ${ref} <name>=<value>`
38
+ : `cad measured <project>/<assembly> <name>=<value>, from an assembly that holds ${ref}`;
39
+ return {
40
+ level: 'warn',
41
+ text:
42
+ `uses estimates, not measurements: ${named.join(', ')}` +
43
+ (rules.length ? `. Measure each and settle it with ${settle}` : '') +
44
+ (own.length
45
+ ? `. A seat drawn to an estimated fit is settled by pressing its part into a test print and giving the fit that works, in mm each side (fit: <mm>)`
46
+ : ''),
47
+ short: `Estimate: ${named.join(', ')}`,
48
+ code: 'rules.estimate',
49
+ refs: { rules, ...(seated.length ? { seats: seated } : {}) },
50
+ };
51
+ }
package/lib/explain.mjs CHANGED
@@ -13,10 +13,18 @@ import { host } from './host.mjs';
13
13
  * @param {string} project @param {string} ref the problem, name or name:variant
14
14
  */
15
15
  export async function problemInputs(project, ref) {
16
- const { readProblem, densitiesKey } = await import('./optimise.mjs'), r = await readProblem(project, ref);
16
+ const { readProblem, densitiesKey } = await import('./optimise.mjs'),
17
+ r = await readProblem(project, ref);
17
18
  const kept = paths.cache('densities', `${densitiesKey(r.problem)}.json`);
18
- return { problem: ref, file: path.relative(WS, r.file).split(path.sep).join('/'), title: r.title, material: r.material.name, ...r.inputs,
19
- densities: path.relative(WS, kept).split(path.sep).join('/'), made: host.exists(kept) };
19
+ return {
20
+ problem: ref,
21
+ file: path.relative(WS, r.file).split(path.sep).join('/'),
22
+ title: r.title,
23
+ material: r.material.name,
24
+ ...r.inputs,
25
+ densities: path.relative(WS, kept).split(path.sep).join('/'),
26
+ made: host.exists(kept),
27
+ };
20
28
  }
21
29
 
22
30
  /**
@@ -25,8 +33,25 @@ export async function problemInputs(project, ref) {
25
33
  */
26
34
  export function valuesOf(b, names = []) {
27
35
  const own = b.entries.filter((e) => !e.name.includes('.'));
28
- const pick = names.length ? names.map((n) => own.find((e) => e.name === n) || (() => { throw Object.assign(new Error(`no variable "${n}" (variables: ${own.map((e) => e.name).join(', ')})`), { code: 'var.unknown' }); })()) : own;
29
- return pick.map((e) => ({ name: e.name, value: e.value, unit: e.unit || '', ...(e.expr ? { expr: e.expr } : {}), ...(e.rule ? { rule: e.rule } : {}), ...(e.label ? { label: e.label } : {}) }));
36
+ const pick = names.length
37
+ ? names.map(
38
+ (n) =>
39
+ own.find((e) => e.name === n) ||
40
+ (() => {
41
+ throw Object.assign(new Error(`no variable "${n}" (variables: ${own.map((e) => e.name).join(', ')})`), {
42
+ code: 'var.unknown',
43
+ });
44
+ })(),
45
+ )
46
+ : own;
47
+ return pick.map((e) => ({
48
+ name: e.name,
49
+ value: e.value,
50
+ unit: e.unit || '',
51
+ ...(e.expr ? { expr: e.expr } : {}),
52
+ ...(e.rule ? { rule: e.rule } : {}),
53
+ ...(e.label ? { label: e.label } : {}),
54
+ }));
30
55
  }
31
56
 
32
57
  /**
@@ -35,28 +60,52 @@ export function valuesOf(b, names = []) {
35
60
  * @param {string} ref project/object @param {string} name a variable, or a rule (x1c.line) @param {{ depth?: number }} [options]
36
61
  */
37
62
  export async function why(ref, name, { depth = 12 } = {}) {
38
- const builds = new Map(), seen = new Set();
63
+ const builds = new Map(),
64
+ seen = new Set();
39
65
  const get = async (r) => builds.get(r) || (builds.set(r, await build(locate(r))), builds.get(r));
40
66
  // A rule as an expression writes it (pla_prusament.strength_z) is the entry of the set named with a hyphen
41
67
  // (pla-prusament), as variables reads it: a - is a minus in an expression
42
- const entry = (b, n) => b.entries.find((x) => x.name === n) || (n.includes('.') ? b.entries.find((x) => x.name === n.slice(0, n.indexOf('.')).replace(/_/g, '-') + n.slice(n.indexOf('.'))) : undefined);
68
+ const entry = (b, n) =>
69
+ b.entries.find((x) => x.name === n) ||
70
+ (n.includes('.')
71
+ ? b.entries.find((x) => x.name === n.slice(0, n.indexOf('.')).replace(/_/g, '-') + n.slice(n.indexOf('.')))
72
+ : undefined);
43
73
  const node = async (r, n, d) => {
44
- const b = await get(r), end = /^[^.]+\.[^.]+\.(low|high)$/.exec(n)?.[1], base = end && entry(b, n.slice(0, -end.length - 1));
74
+ const b = await get(r),
75
+ end = /^[^.]+\.[^.]+\.(low|high)$/.exec(n)?.[1],
76
+ base = end && entry(b, n.slice(0, -end.length - 1));
45
77
  // An end of a rule's range (pa_cf.strength.low): the rule's own entry, its value that end, reading the rule
46
- const e = base ? { ...base, value: base[end], expr: undefined, rule: `the ${end} end of the range ${base.name} is found in (${base.rule})` } : entry(b, n);
78
+ const e = base
79
+ ? {
80
+ ...base,
81
+ value: base[end],
82
+ expr: undefined,
83
+ rule: `the ${end} end of the range ${base.name} is found in (${base.rule})`,
84
+ }
85
+ : entry(b, n);
47
86
  if (!e) throw Object.assign(new Error(`${r}: no variable or rule "${n}"`), { code: 'var.unknown' });
48
- const out = { name: n, value: e.value, unit: e.unit || '', object: r, ...(e.expr ? { expr: e.expr } : {}), ...(e.rule ? { rule: e.rule } : {}),
49
- ...(e.kind ? { kind: e.kind } : {}), ...(e.source ? { source: e.source } : {}) };
87
+ const out = {
88
+ name: n,
89
+ value: e.value,
90
+ unit: e.unit || '',
91
+ object: r,
92
+ ...(e.expr ? { expr: e.expr } : {}),
93
+ ...(e.rule ? { rule: e.rule } : {}),
94
+ ...(e.kind ? { kind: e.kind } : {}),
95
+ ...(e.source ? { source: e.source } : {}),
96
+ };
50
97
  const key = `${r}:${n}`;
51
98
  if (seen.has(key)) return { ...out, seen: true };
52
99
  seen.add(key);
53
100
  // Another object's value (keyboard.link_l in the link): explained in that object's own design
54
101
  const [set, rest] = n.includes('.') ? [n.slice(0, n.indexOf('.')), n.slice(n.indexOf('.') + 1)] : [null, null];
55
- const from = set && (b.object.uses || []).find((u) => isObjectRef(u) && u.split('/').pop().split(/[:@]/)[0] === set);
102
+ const from =
103
+ set && (b.object.uses || []).find((u) => isObjectRef(u) && u.split('/').pop().split(/[:@]/)[0] === set);
56
104
  if (from && d > 0) return { ...(await node(from.split(/[:@]/)[0], rest, d - 1)), via: `${r}:${n}` };
57
105
  out.reads = d > 0 ? await Promise.all((b.deps?.[n] || []).map((m) => node(r, m, d - 1))) : [];
58
106
  // A problem variable: the optimised result it picks, and what that is made from
59
- if (b.object.variables?.[n]?.type === 'problem' && e.value && e.value !== 'none') out.problem = await problemInputs(r.split('/')[0], e.value);
107
+ if (b.object.variables?.[n]?.type === 'problem' && e.value && e.value !== 'none')
108
+ out.problem = await problemInputs(r.split('/')[0], e.value);
60
109
  return out;
61
110
  };
62
111
  return node(ref, name, depth);