@rulecad/cad 0.8.0 → 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 (83) hide show
  1. package/AGENTS.md +24 -10
  2. package/CHANGELOG.md +404 -0
  3. package/EXAMPLES.md +40 -38
  4. package/README.md +193 -67
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2250 -512
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1847 -434
  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 +649 -121
  17. package/lib/drive.mjs +187 -63
  18. package/lib/edit.mjs +899 -191
  19. package/lib/errors.mjs +16 -3
  20. package/lib/explain.mjs +62 -13
  21. package/lib/fasten.mjs +2702 -507
  22. package/lib/fittings.mjs +715 -121
  23. package/lib/fixings.mjs +21 -9
  24. package/lib/framebuild.mjs +790 -175
  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 +123 -21
  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 +2 -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 +1021 -213
  55. package/lib/motorsvg.mjs +114 -32
  56. package/lib/names.mjs +3 -1
  57. package/lib/optimise.mjs +978 -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 +223 -35
  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 +15 -5
  68. package/lib/sandbox-hooks.mjs +77 -19
  69. package/lib/sandbox-run.mjs +117 -18
  70. package/lib/sandbox.mjs +509 -60
  71. package/lib/schema.mjs +42 -11
  72. package/lib/seats.mjs +227 -49
  73. package/lib/services.mjs +261 -52
  74. package/lib/sheet.mjs +851 -178
  75. package/lib/slotnuts.mjs +213 -34
  76. package/lib/targets/gist.mjs +4 -1
  77. package/lib/targets.mjs +22 -4
  78. package/lib/task.mjs +376 -79
  79. package/lib/versions.mjs +111 -28
  80. package/lib/wires.mjs +228 -45
  81. package/lib/workspace.mjs +197 -12
  82. package/package.json +19 -19
  83. package/schemas/assembly.schema.json +2 -2
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
  }
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);