@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.
- package/AGENTS.md +45 -16
- package/CHANGELOG.md +724 -0
- package/EXAMPLES.md +61 -54
- package/README.md +376 -98
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2664 -541
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1883 -435
- package/lib/beams.mjs +256 -52
- package/lib/belts.mjs +66 -15
- package/lib/board-work.mjs +12 -0
- package/lib/bought.mjs +309 -53
- package/lib/buildpage.mjs +179 -29
- package/lib/cache-file.mjs +105 -0
- package/lib/carried.mjs +201 -0
- package/lib/clone.mjs +194 -42
- package/lib/code-text.mjs +28 -0
- package/lib/design-imports.mjs +127 -0
- package/lib/design.mjs +871 -147
- package/lib/designshapes.mjs +142 -0
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +922 -192
- package/lib/errors.mjs +16 -3
- package/lib/estimates.mjs +51 -0
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2708 -507
- package/lib/fe-key.mjs +29 -0
- package/lib/fittings.mjs +721 -122
- package/lib/fixings.mjs +38 -13
- package/lib/framebuild.mjs +790 -175
- package/lib/framecheck.mjs +1901 -368
- package/lib/framegen.mjs +108 -22
- package/lib/frameheat.mjs +225 -42
- package/lib/frameprint.mjs +513 -108
- package/lib/frames.mjs +86 -18
- package/lib/framesize.mjs +719 -120
- package/lib/frametip.mjs +235 -0
- package/lib/gait.mjs +135 -23
- package/lib/grip.mjs +62 -13
- package/lib/guides.mjs +1403 -230
- package/lib/host.mjs +129 -23
- package/lib/inplace.mjs +171 -36
- package/lib/jigs.mjs +360 -78
- package/lib/joins.mjs +697 -118
- package/lib/jsonedit.mjs +88 -26
- package/lib/kept.mjs +84 -0
- package/lib/kit.mjs +439 -69
- package/lib/layout.mjs +2 -1
- package/lib/links.mjs +149 -42
- package/lib/list.mjs +334 -64
- package/lib/live-guard.mjs +68 -0
- package/lib/live-shape.mjs +96 -24
- package/lib/live.mjs +189 -59
- package/lib/loads.mjs +597 -136
- package/lib/machine.mjs +372 -74
- package/lib/magnets.mjs +162 -44
- package/lib/measured.mjs +1080 -117
- package/lib/mesh.mjs +526 -119
- package/lib/meshcmd.mjs +215 -60
- package/lib/motors.mjs +1002 -214
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +18 -1
- package/lib/optimise.mjs +991 -287
- package/lib/order.mjs +515 -114
- package/lib/panels.mjs +482 -94
- package/lib/parts.mjs +81 -23
- package/lib/pins.mjs +163 -44
- package/lib/plates.mjs +223 -35
- package/lib/player.mjs +37 -9
- package/lib/power.mjs +64 -13
- package/lib/preview.mjs +83 -16
- package/lib/profilemeasure.mjs +572 -129
- package/lib/rules.mjs +17 -6
- package/lib/sandbox-hooks.mjs +179 -27
- package/lib/sandbox-lock.mjs +225 -0
- package/lib/sandbox-preload.mjs +2 -2
- package/lib/sandbox-run.mjs +122 -25
- package/lib/sandbox.mjs +667 -75
- package/lib/schema.mjs +42 -11
- package/lib/seats.mjs +227 -49
- package/lib/services.mjs +261 -52
- package/lib/sheet.mjs +963 -179
- package/lib/slotnuts.mjs +213 -34
- package/lib/targets/gist.mjs +4 -1
- package/lib/targets.mjs +22 -4
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +285 -39
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +200 -14
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
- package/schemas/project.schema.json +6 -0
- package/templates/coupons/MEASURE.md +109 -0
- package/templates/coupons/assemblies/fixings-test.json +20 -0
- package/templates/coupons/assemblies/slot-test.json +11 -0
- package/templates/coupons/objects/fixings-coupon.js +88 -0
- package/templates/coupons/objects/fixings-coupon.json +26 -0
- package/templates/coupons/objects/holds-coupon.js +130 -0
- package/templates/coupons/objects/holds-coupon.json +42 -0
- package/templates/coupons/objects/slot-coupon.js +89 -0
- package/templates/coupons/objects/slot-coupon.json +29 -0
- package/templates/coupons/project.json +10 -0
package/lib/plates.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import path from 'node:path';
|
|
|
6
6
|
import { bounds, check as printCheck } from '@rulecad/printcheck';
|
|
7
7
|
import { locateIn, build } from './design.mjs';
|
|
8
8
|
import { paths, printerOf } from './workspace.mjs';
|
|
9
|
+
import { readJson } from './host.mjs';
|
|
9
10
|
import { peekRuleSet } from './rules.mjs';
|
|
10
11
|
import { CadError } from './errors.mjs';
|
|
11
12
|
import { assemblyPath, buildAssembly, checkAssembly } from './assembly.mjs';
|
|
@@ -18,7 +19,14 @@ const n3 = (x) => +x.toFixed(3);
|
|
|
18
19
|
// The gap between parts on a plate: what two parts need to print apart at the first layer (print_gap, plus the first
|
|
19
20
|
// layer's spread each side), up to whole lines of the nozzle
|
|
20
21
|
function rulesOf(project, gap) {
|
|
21
|
-
const r = peekRuleSet(printerOf(project)).rules,
|
|
22
|
+
const r = peekRuleSet(printerOf(project)).rules,
|
|
23
|
+
num = (k, f) => {
|
|
24
|
+
const v = r[k]?.value;
|
|
25
|
+
return typeof v === 'number' ? v : f;
|
|
26
|
+
},
|
|
27
|
+
bed = num('bed', 256),
|
|
28
|
+
line = num('line', 0.42),
|
|
29
|
+
need = num('print_gap', 0.3) + 2 * num('foot', 0.15);
|
|
22
30
|
return { bed, gap: gap ?? n3(Math.ceil((need - 1e-6) / line) * line), need };
|
|
23
31
|
}
|
|
24
32
|
|
|
@@ -26,43 +34,105 @@ function rulesOf(project, gap) {
|
|
|
26
34
|
// (a printed fitting, a join's, a guide's or an item's own, goes on as its item's fitting laid as it prints: "pose": "print")
|
|
27
35
|
// (each with its material: the project's, or a printed beam's own "material", so a plate is one plastic)
|
|
28
36
|
async function plateParts(s) {
|
|
29
|
-
const out = [],
|
|
37
|
+
const out = [],
|
|
38
|
+
items = new Map((s.items ?? []).map((it) => [it.id, it])),
|
|
39
|
+
own = materialOf(s.project).name;
|
|
30
40
|
for (const o of s.objects) {
|
|
31
41
|
if (o.unplaced || o.role !== 'part' || !o.objName) continue;
|
|
32
|
-
if (o.beam?.printed) {
|
|
42
|
+
if (o.beam?.printed) {
|
|
43
|
+
// a printed beam, laid as it prints (lib/beams.mjs)
|
|
33
44
|
const bi = (s.a.items || []).find((x) => x.id === o.id); // (the item as written: the built one holds the beam's model)
|
|
34
45
|
if (!bi?.beam?.profile) continue;
|
|
35
|
-
const beamSpec = laidSpec(bi, s.num, o.beam.length),
|
|
36
|
-
|
|
46
|
+
const beamSpec = laidSpec(bi, s.num, o.beam.length),
|
|
47
|
+
b = beamBuilt({ id: o.id, beam: beamSpec }, s.num, o.id).b,
|
|
48
|
+
tris = b.parts.flatMap((p) => p.triangles),
|
|
49
|
+
{ lo, hi } = bounds(tris);
|
|
50
|
+
out.push({
|
|
51
|
+
id: o.id.replace(/\./g, '_'),
|
|
52
|
+
source: o.id,
|
|
53
|
+
object: o.objName,
|
|
54
|
+
beam: beamSpec,
|
|
55
|
+
set: {},
|
|
56
|
+
filament: 'base',
|
|
57
|
+
material: String(bi.beam.material ?? own).toLowerCase(),
|
|
58
|
+
hash: b.hash,
|
|
59
|
+
lo,
|
|
60
|
+
size: [hi[0] - lo[0], hi[1] - lo[1]],
|
|
61
|
+
height: hi[2] - lo[2],
|
|
62
|
+
});
|
|
37
63
|
continue;
|
|
38
64
|
}
|
|
39
65
|
const it = o.fitting ? items.get(o.id) : null;
|
|
40
66
|
if (o.fitting && !it?.fitting) continue;
|
|
41
67
|
const fitting = it ? { ...it.fitting, pose: 'print' } : null;
|
|
42
|
-
const b = fitting
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
68
|
+
const b = fitting
|
|
69
|
+
? (await fittingBuilt({ ...it, fitting }, o.id, framesContext(), plasticOf(s.project))).b
|
|
70
|
+
: await build({ ...locateIn(s.project, o.objName), ...(o.variant ? { variant: o.variant } : {}) }, o.set || {});
|
|
71
|
+
const tris = b.parts.flatMap((p) => p.triangles),
|
|
72
|
+
{ lo, hi } = bounds(tris);
|
|
73
|
+
const fil =
|
|
74
|
+
[...new Set(b.parts.filter((p) => p.role !== 'support').map((p) => p.filament || 'base'))].sort().join('+') ||
|
|
75
|
+
'base';
|
|
76
|
+
out.push({
|
|
77
|
+
id: o.id.replace(/\./g, '_'),
|
|
78
|
+
source: o.id,
|
|
79
|
+
object: o.objName,
|
|
80
|
+
...(fitting ? { fitting } : {}),
|
|
81
|
+
variant: o.variant,
|
|
82
|
+
set: o.set || {},
|
|
83
|
+
filament: fil,
|
|
84
|
+
material: own,
|
|
85
|
+
hash: b.hash,
|
|
86
|
+
lo,
|
|
87
|
+
size: [hi[0] - lo[0], hi[1] - lo[1]],
|
|
88
|
+
height: hi[2] - lo[2],
|
|
89
|
+
});
|
|
46
90
|
}
|
|
47
91
|
// what comes with a placed part and is printed beside it (a levelling foot's pad: lib/fittings.mjs companions)
|
|
48
92
|
for (const c of companions(s)) {
|
|
49
|
-
const fitting = { ...c.fitting, pose: 'print' },
|
|
50
|
-
|
|
93
|
+
const fitting = { ...c.fitting, pose: 'print' },
|
|
94
|
+
b = (await fittingBuilt({ id: c.id, fitting }, c.id, framesContext(), plasticOf(s.project))).b,
|
|
95
|
+
tris = b.parts.flatMap((p) => p.triangles),
|
|
96
|
+
{ lo, hi } = bounds(tris);
|
|
97
|
+
out.push({
|
|
98
|
+
id: c.id.replace(/\./g, '_'),
|
|
99
|
+
source: c.of,
|
|
100
|
+
object: b.object.name,
|
|
101
|
+
fitting,
|
|
102
|
+
set: {},
|
|
103
|
+
filament: 'base',
|
|
104
|
+
material: own,
|
|
105
|
+
hash: b.hash,
|
|
106
|
+
lo,
|
|
107
|
+
size: [hi[0] - lo[0], hi[1] - lo[1]],
|
|
108
|
+
height: hi[2] - lo[2],
|
|
109
|
+
});
|
|
51
110
|
}
|
|
52
111
|
return out;
|
|
53
112
|
}
|
|
54
113
|
|
|
55
114
|
// Shelves on a bed, largest first: as many plates as it takes
|
|
56
115
|
function pack(parts, { bed, gap }) {
|
|
57
|
-
const sorted = [...parts].sort((a, b) => b.size[1] - a.size[1] || b.size[0] - a.size[0] || a.id.localeCompare(b.id)),
|
|
116
|
+
const sorted = [...parts].sort((a, b) => b.size[1] - a.size[1] || b.size[0] - a.size[0] || a.id.localeCompare(b.id)),
|
|
117
|
+
beds = [];
|
|
58
118
|
const room = bed - 2 * gap;
|
|
59
119
|
for (const p of sorted) {
|
|
60
120
|
let done = false;
|
|
61
121
|
for (const b of beds) {
|
|
62
122
|
const shelf = b.shelves.at(-1);
|
|
63
|
-
if (shelf.x + p.size[0] <= room && p.size[1] <= shelf.h) {
|
|
123
|
+
if (shelf.x + p.size[0] <= room && p.size[1] <= shelf.h) {
|
|
124
|
+
b.items.push({ p, x: shelf.x, y: shelf.y });
|
|
125
|
+
shelf.x += p.size[0] + gap;
|
|
126
|
+
done = true;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
64
129
|
const y = shelf.y + shelf.h + gap;
|
|
65
|
-
if (y + p.size[1] <= room && p.size[0] <= room) {
|
|
130
|
+
if (y + p.size[1] <= room && p.size[0] <= room) {
|
|
131
|
+
b.shelves.push({ y, h: p.size[1], x: p.size[0] + gap });
|
|
132
|
+
b.items.push({ p, x: 0, y });
|
|
133
|
+
done = true;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
66
136
|
}
|
|
67
137
|
if (!done) beds.push({ shelves: [{ y: 0, h: p.size[1], x: p.size[0] + gap }], items: [{ p, x: 0, y: 0 }] });
|
|
68
138
|
}
|
|
@@ -71,18 +141,45 @@ function pack(parts, { bed, gap }) {
|
|
|
71
141
|
|
|
72
142
|
const plateFile = (project, name) => assemblyPath(`${project}/${name}`);
|
|
73
143
|
function existing(s) {
|
|
74
|
-
const dir = path.join(paths.project(s.project), 'assemblies'),
|
|
75
|
-
|
|
144
|
+
const dir = path.join(paths.project(s.project), 'assemblies'),
|
|
145
|
+
out = [];
|
|
146
|
+
for (const f of fs.existsSync(dir) ? fs.readdirSync(dir) : []) {
|
|
147
|
+
if (!f.endsWith('.json')) continue;
|
|
148
|
+
let a;
|
|
149
|
+
try {
|
|
150
|
+
a = readJson(path.join(dir, f));
|
|
151
|
+
} catch {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (a.packed?.of === s.name) out.push({ name: f.slice(0, -5), a });
|
|
155
|
+
}
|
|
76
156
|
return out.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }));
|
|
77
157
|
}
|
|
78
158
|
// <assembly>-<filament>-<n>; a plate in another material than the project's has it in its name too
|
|
79
|
-
const nameOf = (s, filament, k, material, own) =>
|
|
159
|
+
const nameOf = (s, filament, k, material, own) =>
|
|
160
|
+
`${s.name}-${material && material !== own ? `${material}-` : ''}${filament.replace(/\+/g, '-')}-${k}`;
|
|
80
161
|
|
|
81
162
|
function plateJson(s, plate, { bed, gap }) {
|
|
82
163
|
const origin = (bed - 2 * gap) / 2;
|
|
83
|
-
return {
|
|
84
|
-
|
|
85
|
-
|
|
164
|
+
return {
|
|
165
|
+
title: `${s.a.title || s.name}: ${plate.filament}, plate ${plate.n}`,
|
|
166
|
+
kind: 'plate',
|
|
167
|
+
note: `Generated by cad plates from ${s.name}. Hand edits are replaced when a part on it changes.`,
|
|
168
|
+
packed: {
|
|
169
|
+
of: s.name,
|
|
170
|
+
filament: plate.filament,
|
|
171
|
+
material: plate.material,
|
|
172
|
+
gap,
|
|
173
|
+
parts: Object.fromEntries(plate.items.map(({ p }) => [p.id, p.hash])),
|
|
174
|
+
},
|
|
175
|
+
items: plate.items.map(({ p, x, y }) => ({
|
|
176
|
+
id: p.id,
|
|
177
|
+
...(p.fitting ? { fitting: p.fitting } : p.beam ? { beam: p.beam } : { object: p.object }),
|
|
178
|
+
...(p.variant ? { variant: p.variant } : {}),
|
|
179
|
+
...(Object.keys(p.set).length ? { set: p.set } : {}),
|
|
180
|
+
at: [n3(x - origin - p.lo[0]), n3(y - origin - p.lo[1]), n3(-p.lo[2])],
|
|
181
|
+
})),
|
|
182
|
+
};
|
|
86
183
|
}
|
|
87
184
|
|
|
88
185
|
/**
|
|
@@ -91,39 +188,130 @@ function plateJson(s, plate, { bed, gap }) {
|
|
|
91
188
|
* @returns {Promise<{ plates: object[], touched: string[], removed: string[], gap: number, bed: number, findings: object[] }>}
|
|
92
189
|
*/
|
|
93
190
|
export async function platesOf(s, { write = false, gap: asked } = {}) {
|
|
94
|
-
const parts = await plateParts(s),
|
|
191
|
+
const parts = await plateParts(s),
|
|
192
|
+
{ bed, gap } = rulesOf(s.project, asked),
|
|
193
|
+
findings = [],
|
|
194
|
+
plates = [],
|
|
195
|
+
touched = [],
|
|
196
|
+
removed = [],
|
|
197
|
+
own = materialOf(s.project).name;
|
|
95
198
|
if (!parts.length) throw new CadError('plates.empty', `${s.project}/${s.name} has nothing printed to put on a plate`);
|
|
96
199
|
const big = parts.filter((p) => p.size[0] > bed - 2 * gap || p.size[1] > bed - 2 * gap);
|
|
97
|
-
for (const p of big)
|
|
98
|
-
|
|
200
|
+
for (const p of big)
|
|
201
|
+
findings.push({
|
|
202
|
+
level: 'fail',
|
|
203
|
+
text: `${p.id} (${p.object}) is ${Number(p.size[0].toFixed(1))} × ${Number(p.size[1].toFixed(1))} mm: bigger than the ${bed} mm bed, so it has no plate: split it or turn it`,
|
|
204
|
+
code: 'plate.bed',
|
|
205
|
+
refs: { item: p.id },
|
|
206
|
+
values: { size: p.size, bed },
|
|
207
|
+
});
|
|
208
|
+
const by = new Map(parts.filter((p) => !big.includes(p)).map((p) => [p.id, p])),
|
|
209
|
+
had = existing(s),
|
|
210
|
+
kept = new Set(),
|
|
211
|
+
used = new Set();
|
|
99
212
|
// A plate stands if everything on it is still there and unchanged
|
|
100
213
|
for (const e of had) {
|
|
101
|
-
const on = Object.entries(e.a.packed.parts || {}),
|
|
214
|
+
const on = Object.entries(e.a.packed.parts || {}),
|
|
215
|
+
ok = on.length && on.every(([id, h]) => by.get(id)?.hash === h) && e.a.packed.gap === gap;
|
|
102
216
|
if (!ok) continue;
|
|
103
|
-
kept.add(e.name);
|
|
217
|
+
kept.add(e.name);
|
|
218
|
+
on.forEach(([id]) => used.add(id));
|
|
219
|
+
plates.push({
|
|
220
|
+
name: e.name,
|
|
221
|
+
filament: e.a.packed.filament,
|
|
222
|
+
material: e.a.packed.material ?? own,
|
|
223
|
+
a: e.a,
|
|
224
|
+
items: e.a.items.map((it) => {
|
|
225
|
+
const p = by.get(it.id),
|
|
226
|
+
origin = (bed - 2 * gap) / 2;
|
|
227
|
+
return { p, x: it.at[0] + p.lo[0] + origin, y: it.at[1] + p.lo[1] + origin };
|
|
228
|
+
}),
|
|
229
|
+
stood: true,
|
|
230
|
+
});
|
|
104
231
|
}
|
|
105
232
|
// Everything else, packed by material and filament; names reused from the plates that are going, so a changed part keeps its name
|
|
106
|
-
const free = [...by.values()].filter((p) => !used.has(p.id)),
|
|
107
|
-
|
|
233
|
+
const free = [...by.values()].filter((p) => !used.has(p.id)),
|
|
234
|
+
byFilament = new Map();
|
|
235
|
+
for (const p of free) {
|
|
236
|
+
const key = `${p.material}|${p.filament}`;
|
|
237
|
+
if (!byFilament.has(key)) byFilament.set(key, []);
|
|
238
|
+
byFilament.get(key).push(p);
|
|
239
|
+
}
|
|
108
240
|
const goingNames = had.filter((e) => !kept.has(e.name)).map((e) => e.name);
|
|
109
241
|
for (const [key, list] of byFilament) {
|
|
110
|
-
const [material, filament] = [key.slice(0, key.indexOf('|')), key.slice(key.indexOf('|') + 1)],
|
|
111
|
-
|
|
112
|
-
|
|
242
|
+
const [material, filament] = [key.slice(0, key.indexOf('|')), key.slice(key.indexOf('|') + 1)],
|
|
243
|
+
beds = pack(list, { bed, gap });
|
|
244
|
+
let k = 0;
|
|
245
|
+
const next = () => {
|
|
246
|
+
const reuse = goingNames.find((n) => {
|
|
247
|
+
const e = had.find((x) => x.name === n);
|
|
248
|
+
return e?.a.packed.filament === filament && (e.a.packed.material ?? own) === material;
|
|
249
|
+
});
|
|
250
|
+
if (reuse) {
|
|
251
|
+
goingNames.splice(goingNames.indexOf(reuse), 1);
|
|
252
|
+
return reuse;
|
|
253
|
+
}
|
|
254
|
+
let name;
|
|
255
|
+
do name = nameOf(s, filament, ++k, material, own);
|
|
256
|
+
while (kept.has(name) || plates.some((x) => x.name === name));
|
|
257
|
+
return name;
|
|
258
|
+
};
|
|
259
|
+
for (const b of beds) {
|
|
260
|
+
const name = next(),
|
|
261
|
+
n = Number(name.match(/-(\d+)$/)?.[1]) || 1,
|
|
262
|
+
plate = { name, filament, material, n, items: b.items };
|
|
263
|
+
plate.a = plateJson(s, plate, { bed, gap });
|
|
264
|
+
plates.push(plate);
|
|
265
|
+
touched.push(name);
|
|
266
|
+
}
|
|
113
267
|
}
|
|
114
268
|
for (const n of goingNames) removed.push(n);
|
|
115
269
|
plates.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }));
|
|
116
270
|
if (write) {
|
|
117
|
-
const dir = path.join(paths.project(s.project), 'assemblies');
|
|
118
|
-
|
|
271
|
+
const dir = path.join(paths.project(s.project), 'assemblies');
|
|
272
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
273
|
+
for (const p of plates)
|
|
274
|
+
if (!p.stood) fs.writeFileSync(plateFile(s.project, p.name), `${JSON.stringify(p.a, null, 2)}\n`);
|
|
119
275
|
for (const n of removed) fs.rmSync(plateFile(s.project, n), { force: true });
|
|
120
276
|
}
|
|
121
277
|
const grams = new Map(printedParts(s).map((x) => [x.id.replace(/\./g, '_'), x]));
|
|
122
|
-
return {
|
|
278
|
+
return {
|
|
279
|
+
bed,
|
|
280
|
+
gap,
|
|
281
|
+
touched,
|
|
282
|
+
removed,
|
|
283
|
+
findings,
|
|
284
|
+
plates: plates.map((p) => ({
|
|
285
|
+
name: p.name,
|
|
286
|
+
filament: p.filament,
|
|
287
|
+
material: p.material,
|
|
288
|
+
parts: p.items.map(({ p: q, x, y }) => ({
|
|
289
|
+
id: q.id,
|
|
290
|
+
object: q.object,
|
|
291
|
+
x: n3(x + gap),
|
|
292
|
+
y: n3(y + gap),
|
|
293
|
+
w: n3(q.size[0]),
|
|
294
|
+
d: n3(q.size[1]),
|
|
295
|
+
})),
|
|
296
|
+
grams: n3(p.items.reduce((t, { p: q }) => t + (grams.get(q.id)?.grams ?? 0), 0)),
|
|
297
|
+
minutes: p.items.reduce((t, { p: q }) => t + (grams.get(q.id)?.minutes ?? 0), 0),
|
|
298
|
+
stood: !!p.stood,
|
|
299
|
+
})),
|
|
300
|
+
};
|
|
123
301
|
}
|
|
124
302
|
|
|
125
303
|
/** Each plate's checks: what check says of a plate, the print check over the whole bed included. */
|
|
126
304
|
export async function checkPlate(ref) {
|
|
127
|
-
const s = await buildAssembly(ref),
|
|
128
|
-
|
|
305
|
+
const s = await buildAssembly(ref),
|
|
306
|
+
printer = printerOf(s.project),
|
|
307
|
+
rules = Object.fromEntries(Object.keys(peekRuleSet(printer).rules).map((k) => [k, s.num(`${printer}.${k}`)])),
|
|
308
|
+
printed = s.parts.filter((p) => p.role !== 'reference');
|
|
309
|
+
return [
|
|
310
|
+
...checkAssembly(s),
|
|
311
|
+
...printCheck(
|
|
312
|
+
printed.flatMap((p) => p.triangles),
|
|
313
|
+
rules,
|
|
314
|
+
{ parts: printed },
|
|
315
|
+
),
|
|
316
|
+
];
|
|
129
317
|
}
|
package/lib/player.mjs
CHANGED
|
@@ -17,18 +17,45 @@ export function playerData(mechanism, { title = '', version = '', object = '' }
|
|
|
17
17
|
const frames = mechanism.frames || [];
|
|
18
18
|
if (!frames.length) throw new Error('the mechanism has no sequence to play: give its lamina a sequence of states');
|
|
19
19
|
const flexures = new Set(Object.keys(mechanism.joints || {}));
|
|
20
|
-
const kinds = Object.fromEntries(
|
|
20
|
+
const kinds = Object.fromEntries(
|
|
21
|
+
[...new Set(frames.flatMap((fr) => Object.keys(fr.parts)))].map((n) => [
|
|
22
|
+
n,
|
|
23
|
+
n === 'ground' ? 'ground' : flexures.has(n) ? 'flexure' : 'body',
|
|
24
|
+
]),
|
|
25
|
+
);
|
|
21
26
|
return {
|
|
22
|
-
title,
|
|
27
|
+
title,
|
|
28
|
+
version,
|
|
29
|
+
object,
|
|
30
|
+
kinds,
|
|
23
31
|
ground: frames[0].parts.ground ? loopsOf(frames[0].parts.ground) : [],
|
|
24
32
|
frames: frames.map((fr) => ({
|
|
25
|
-
label: fr.label || (fr.from === fr.to ? fr.from : `${fr.from} to ${fr.to}`),
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
label: fr.label || (fr.from === fr.to ? fr.from : `${fr.from} to ${fr.to}`),
|
|
34
|
+
from: fr.from,
|
|
35
|
+
to: fr.to,
|
|
36
|
+
t: fr.t,
|
|
37
|
+
parts: Object.fromEntries(
|
|
38
|
+
Object.entries(fr.parts)
|
|
39
|
+
.filter(([n]) => n !== 'ground')
|
|
40
|
+
.map(([n, o]) => [n, loopsOf(o)]),
|
|
41
|
+
),
|
|
42
|
+
...(fr.forces ? { forces: fr.forces } : {}),
|
|
43
|
+
...(fr.energy ? { energy: fr.energy } : {}),
|
|
44
|
+
...(fr.strain ? { strain: fr.strain } : {}),
|
|
28
45
|
})),
|
|
29
|
-
curves: (mechanism.curves || []).map((c) => ({
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
curves: (mechanism.curves || []).map((c) => ({
|
|
47
|
+
from: c.from,
|
|
48
|
+
to: c.to,
|
|
49
|
+
curve: c.curve,
|
|
50
|
+
events: c.events || [],
|
|
51
|
+
...(c.holding ? { holding: c.holding } : {}),
|
|
52
|
+
})),
|
|
53
|
+
sequence: mechanism.sequence || [],
|
|
54
|
+
limit: mechanism.limit ?? null,
|
|
55
|
+
friction: mechanism.friction ?? 0,
|
|
56
|
+
joints: mechanism.joints || {},
|
|
57
|
+
trip: mechanism.trip || null,
|
|
58
|
+
shot: mechanism.shot || null,
|
|
32
59
|
findings: (mechanism.findings || []).map((f) => ({ level: f.level, code: f.code, text: f.text })),
|
|
33
60
|
};
|
|
34
61
|
}
|
|
@@ -40,7 +67,8 @@ export function playerData(mechanism, { title = '', version = '', object = '' }
|
|
|
40
67
|
*/
|
|
41
68
|
export function playerHtml(data) {
|
|
42
69
|
const json = JSON.stringify(data).replace(/</g, '\\u003c');
|
|
43
|
-
const esc = (s) =>
|
|
70
|
+
const esc = (s) =>
|
|
71
|
+
String(s).replace(/[<>&"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', '"': '"' })[c]);
|
|
44
72
|
return `<!doctype html>
|
|
45
73
|
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
|
46
74
|
<title>${esc(`${data.title} ${data.version}`.trim())}</title>
|
package/lib/power.mjs
CHANGED
|
@@ -4,28 +4,79 @@ import { driver as driverModel, setting, powerBudget } from '@rulecad/motors';
|
|
|
4
4
|
|
|
5
5
|
const n3 = (x, d = 2) => String(Number(x.toFixed(d)));
|
|
6
6
|
|
|
7
|
-
function itemById(s, id) {
|
|
7
|
+
function itemById(s, id) {
|
|
8
|
+
return s.objects.find((o) => o.id === id);
|
|
9
|
+
}
|
|
8
10
|
|
|
9
11
|
export function powerChecks(s, f) {
|
|
10
|
-
const drives = s.motion?.drives ?? [],
|
|
11
|
-
|
|
12
|
+
const drives = s.motion?.drives ?? [],
|
|
13
|
+
supplies = s.objects.filter((o) => o.bought?.part === 'supply');
|
|
14
|
+
const psu = supplies[0]?.built.values,
|
|
15
|
+
voltage = psu?.output_voltage,
|
|
16
|
+
draws = [];
|
|
12
17
|
for (const d of drives) {
|
|
13
|
-
const m = d.model,
|
|
18
|
+
const m = d.model,
|
|
19
|
+
refs = { item: d.id, ...(d.joint ? { joint: d.joint } : {}) };
|
|
14
20
|
if (m.type === 'stepper') {
|
|
15
|
-
const rated = m.figures.rated_current?.value,
|
|
21
|
+
const rated = m.figures.rated_current?.value,
|
|
22
|
+
current = d.spec.current ?? rated,
|
|
23
|
+
R = m.figures.resistance?.value;
|
|
16
24
|
draws.push({ name: d.id, current, resistance: R, ...(d.spec.duty != null ? { duty: d.spec.duty } : {}) });
|
|
17
|
-
if (!d.spec.driver) {
|
|
25
|
+
if (!d.spec.driver) {
|
|
26
|
+
if (s.task)
|
|
27
|
+
f(
|
|
28
|
+
'warn',
|
|
29
|
+
`${d.id} has no "driver": nothing checks the current it's set to (name a bought driver item)`,
|
|
30
|
+
`${d.id}: no driver`,
|
|
31
|
+
{ code: 'motor.driver', refs },
|
|
32
|
+
);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
18
35
|
const o = itemById(s, d.spec.driver);
|
|
19
|
-
if (!o || o.bought?.part !== 'driver') {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
36
|
+
if (!o || o.bought?.part !== 'driver') {
|
|
37
|
+
f('fail', `${d.id}: driver "${d.spec.driver}" isn't a bought driver item`, `${d.id}: no such driver`, {
|
|
38
|
+
code: 'motor.driver',
|
|
39
|
+
refs,
|
|
40
|
+
});
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const model = driverModel(o.bought.set, { variant: o.bought.variant ?? undefined }),
|
|
44
|
+
{ vref, findings } = setting(model, current, {
|
|
45
|
+
cooled: !!d.spec.cooled,
|
|
46
|
+
supply: voltage,
|
|
47
|
+
microsteps: d.spec.microsteps,
|
|
48
|
+
});
|
|
49
|
+
for (const x of findings)
|
|
50
|
+
f(x.level, `${d.id}: ${x.text}`, `${d.id}: driver`, { code: x.code, values: x.values, refs });
|
|
51
|
+
if (!findings.some((x) => x.level === 'fail'))
|
|
52
|
+
f(
|
|
53
|
+
'pass',
|
|
54
|
+
`${d.id} on ${model.name}: set the trimmer to ${n3(vref, 3)} V for ${n3(current, 2)} A per coil (${model.formula}${model.uart ? `, or set it over UART` : ''}), ${model.sense} ohm sense resistors: read yours before trusting this`,
|
|
55
|
+
null,
|
|
56
|
+
{ code: 'motor.driver', refs, values: { vref, current, sense: model.sense } },
|
|
57
|
+
);
|
|
23
58
|
} else if (m.type === 'servo') {
|
|
24
59
|
draws.push({ name: d.id, current: 0.7, voltage: m.figures.rated_voltage?.value ?? 5, duty: d.spec.duty ?? 0.2 });
|
|
25
60
|
}
|
|
26
61
|
}
|
|
27
|
-
if (!draws.length || !supplies.length) {
|
|
28
|
-
|
|
62
|
+
if (!draws.length || !supplies.length) {
|
|
63
|
+
if (draws.length && s.task)
|
|
64
|
+
f('warn', 'there is no "supply" among the bought items: nothing checks the power', 'no supply', {
|
|
65
|
+
code: 'power.supply',
|
|
66
|
+
});
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const o = supplies[0],
|
|
70
|
+
I = psu.output_current,
|
|
71
|
+
extra = s.objects
|
|
72
|
+
.filter((x) => x.bought?.part === 'board')
|
|
73
|
+
.map((x) => ({ name: x.id, current: 0.1, voltage: 5, duty: 1 }));
|
|
29
74
|
const r = powerBudget([...draws, ...extra], { voltage, current: I, margin: s.a.power?.margin ?? 1.25 });
|
|
30
|
-
for (const x of r.findings)
|
|
75
|
+
for (const x of r.findings)
|
|
76
|
+
f(
|
|
77
|
+
x.level,
|
|
78
|
+
`${o.id} (${o.bought.variant ?? o.bought.set}): ${x.text}`,
|
|
79
|
+
x.level === 'pass' ? null : `${o.id}: too small`,
|
|
80
|
+
{ code: x.code, values: x.values, refs: { item: o.id } },
|
|
81
|
+
);
|
|
31
82
|
}
|
package/lib/preview.mjs
CHANGED
|
@@ -13,31 +13,62 @@ import { createRequire } from 'node:module';
|
|
|
13
13
|
import { TOOL } from './workspace.mjs';
|
|
14
14
|
|
|
15
15
|
// Any Chrome or Chromium: $CAD_CHROME, else the usual places on macOS, Linux and Windows
|
|
16
|
-
const CHROME =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
const CHROME =
|
|
17
|
+
process.env.CAD_CHROME ||
|
|
18
|
+
[
|
|
19
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
20
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
21
|
+
'/usr/bin/google-chrome',
|
|
22
|
+
'/usr/bin/google-chrome-stable',
|
|
23
|
+
'/usr/bin/chromium',
|
|
24
|
+
'/usr/bin/chromium-browser',
|
|
25
|
+
'/opt/pw-browsers/chromium',
|
|
26
|
+
'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
|
|
27
|
+
].find((f) => fs.existsSync(f));
|
|
21
28
|
const THREE = path.dirname(createRequire(import.meta.url).resolve('three')); // wherever npm put it
|
|
22
29
|
|
|
23
30
|
export async function preview(parts, { title = '', meta = '', out, explode = false, marks = null, built = false }) {
|
|
24
|
-
const flat = (tris) => {
|
|
25
|
-
|
|
31
|
+
const flat = (tris) => {
|
|
32
|
+
const positions = [];
|
|
33
|
+
for (const t of tris) for (let i = 1; i < t.length - 1; i++) positions.push(...t[0], ...t[i], ...t[i + 1]);
|
|
34
|
+
return positions;
|
|
35
|
+
};
|
|
36
|
+
const data = JSON.stringify({
|
|
37
|
+
title,
|
|
38
|
+
meta,
|
|
39
|
+
explode,
|
|
40
|
+
marks,
|
|
41
|
+
parts: parts.map((p) => ({
|
|
42
|
+
name: p.name,
|
|
43
|
+
role: built && p.role === 'reference' ? 'part' : p.role,
|
|
44
|
+
label: p.label,
|
|
45
|
+
item: p.object || p.item,
|
|
46
|
+
...(p.lift ? { lift: p.lift, with: p.with } : {}),
|
|
47
|
+
positions: flat(p.triangles),
|
|
48
|
+
})),
|
|
49
|
+
});
|
|
26
50
|
const server = http.createServer((req, res) => {
|
|
27
51
|
const u = new URL(req.url, 'http://x').pathname;
|
|
28
|
-
const send = (type, body) => {
|
|
52
|
+
const send = (type, body) => {
|
|
53
|
+
res.writeHead(200, { 'Content-Type': type });
|
|
54
|
+
res.end(body);
|
|
55
|
+
};
|
|
29
56
|
if (u === '/') return send('text/html', fs.readFileSync(path.join(TOOL, 'lib/preview/page.html')));
|
|
30
57
|
if (u === '/data.json') return send('application/json', data);
|
|
31
58
|
const f = path.join(THREE, path.basename(u)); // only three's build files
|
|
32
59
|
if (u.startsWith('/three/') && fs.existsSync(f)) return send('text/javascript', fs.readFileSync(f));
|
|
33
|
-
res.writeHead(404);
|
|
60
|
+
res.writeHead(404);
|
|
61
|
+
res.end();
|
|
34
62
|
});
|
|
35
63
|
return shoot(server, out);
|
|
36
64
|
}
|
|
37
65
|
|
|
38
66
|
// Any page as an image: html served on this Mac for the few seconds Chrome takes (an SVG drawing, a report)
|
|
39
67
|
export async function picture(html, out, { width = 1800, height = 1200 } = {}) {
|
|
40
|
-
const server = http.createServer((req, res) => {
|
|
68
|
+
const server = http.createServer((req, res) => {
|
|
69
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
70
|
+
res.end(html);
|
|
71
|
+
});
|
|
41
72
|
return shoot(server, out, { width, height });
|
|
42
73
|
}
|
|
43
74
|
|
|
@@ -49,17 +80,53 @@ async function shoot(server, out, { width = 1800, height = 1200 } = {}) {
|
|
|
49
80
|
try {
|
|
50
81
|
await new Promise((resolve, reject) => {
|
|
51
82
|
if (!CHROME) return reject(new Error('no Chrome or Chromium found for previews: set CAD_CHROME to one'));
|
|
52
|
-
const p = spawn(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
83
|
+
const p = spawn(
|
|
84
|
+
CHROME,
|
|
85
|
+
[
|
|
86
|
+
'--headless=new',
|
|
87
|
+
`--user-data-dir=${profile}`,
|
|
88
|
+
'--no-first-run',
|
|
89
|
+
'--hide-scrollbars',
|
|
90
|
+
'--force-device-scale-factor=1',
|
|
91
|
+
`--window-size=${width},${height}`,
|
|
92
|
+
'--virtual-time-budget=8000',
|
|
93
|
+
`--screenshot=${out}`,
|
|
94
|
+
`http://127.0.0.1:${server.address().port}/`,
|
|
95
|
+
],
|
|
96
|
+
{ stdio: 'ignore' },
|
|
97
|
+
);
|
|
98
|
+
const t = setTimeout(() => {
|
|
99
|
+
p.kill();
|
|
100
|
+
reject(new Error('Chrome took too long to render the preview'));
|
|
101
|
+
}, 60000);
|
|
102
|
+
p.on('error', (e) => {
|
|
103
|
+
clearTimeout(t);
|
|
104
|
+
reject(
|
|
105
|
+
new Error(
|
|
106
|
+
`couldn't start Chrome for the preview (${CHROME}): ${e.message}. Set CAD_CHROME to a Chrome or Chromium`,
|
|
107
|
+
),
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
p.on('exit', () => {
|
|
111
|
+
clearTimeout(t);
|
|
112
|
+
fs.existsSync(out)
|
|
113
|
+
? resolve()
|
|
114
|
+
: reject(
|
|
115
|
+
new Error(
|
|
116
|
+
`Chrome made no screenshot (${CHROME}): set CAD_CHROME to a Chrome or Chromium that runs headless`,
|
|
117
|
+
),
|
|
118
|
+
);
|
|
119
|
+
});
|
|
57
120
|
});
|
|
58
121
|
} finally {
|
|
59
122
|
server.close();
|
|
60
123
|
// Chrome's helpers can still be writing to its profile as it exits (macOS: "Directory not empty"): try a few
|
|
61
124
|
// times, and leave it in the temporary folder rather than fail a picture that was made
|
|
62
|
-
try {
|
|
125
|
+
try {
|
|
126
|
+
fs.rmSync(profile, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 });
|
|
127
|
+
} catch {
|
|
128
|
+
/* the OS clears it */
|
|
129
|
+
}
|
|
63
130
|
}
|
|
64
131
|
return out;
|
|
65
132
|
}
|