@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/list.mjs
CHANGED
|
@@ -7,7 +7,7 @@ import { placeItems, jointValues } from '@rulecad/model-link';
|
|
|
7
7
|
import { screwModel, pinModel, magnetModel } from '@rulecad/fixings';
|
|
8
8
|
import { peekRuleSet as loadRuleSet } from './rules.mjs';
|
|
9
9
|
import { motorScrews } from './motors.mjs';
|
|
10
|
-
import { paths } from './workspace.mjs';
|
|
10
|
+
import { paths, printerOf } from './workspace.mjs';
|
|
11
11
|
import { readJson } from './host.mjs';
|
|
12
12
|
import path from 'node:path';
|
|
13
13
|
import { volume } from './mesh.mjs';
|
|
@@ -20,117 +20,387 @@ const n = (x, d = 1) => Number(x.toFixed(d));
|
|
|
20
20
|
|
|
21
21
|
export function materialOf(project) {
|
|
22
22
|
let name = 'pla';
|
|
23
|
-
try {
|
|
24
|
-
|
|
23
|
+
try {
|
|
24
|
+
name = String(readJson(path.join(paths.project(project), 'project.json')).material || 'pla')
|
|
25
|
+
.toLowerCase()
|
|
26
|
+
.split(/\s/)[0];
|
|
27
|
+
} catch {
|
|
28
|
+
// (no project.json to read: PLA)
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const set = loadRuleSet(name);
|
|
32
|
+
return { name, density: set.rules.density.value, ...(set.rules.cost ? { cost: set.rules.cost } : {}) };
|
|
33
|
+
} catch {
|
|
34
|
+
return { name, density: 1.25, assumed: true };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// How fast the project's printer lays plastic, for the minutes: its rule set's print_flow (mm³/s), the printer named by
|
|
39
|
+
// project.json or cad.config.json (workspace.mjs printerOf), as cad check reads it. Where that rule set gives none (or
|
|
40
|
+
// cannot be read), 12 mm³/s, a usual print flow, marked assumed so the list says so.
|
|
41
|
+
export function flowOf(project) {
|
|
42
|
+
let printer = null;
|
|
43
|
+
try {
|
|
44
|
+
printer = printerOf(project);
|
|
45
|
+
const f = loadRuleSet(printer).rules.print_flow?.value;
|
|
46
|
+
if (typeof f === 'number' && f > 0) return { printer, flow: f };
|
|
47
|
+
} catch {
|
|
48
|
+
// (no printer named, or its rule set unreadable: the usual flow, said to be assumed)
|
|
49
|
+
}
|
|
50
|
+
return { printer, flow: 12, assumed: true };
|
|
25
51
|
}
|
|
26
52
|
|
|
27
53
|
// Every printed thing in the assembly, once for each copy: its volume, grams and minutes
|
|
28
54
|
export function printedParts(s) {
|
|
29
|
-
const mat = materialOf(s.project);
|
|
30
|
-
|
|
55
|
+
const mat = materialOf(s.project);
|
|
56
|
+
const { flow } = flowOf(s.project);
|
|
31
57
|
const by = new Map();
|
|
32
58
|
for (const p of s.parts) {
|
|
33
59
|
if (p.role !== 'part' && p.role != null) continue;
|
|
34
60
|
// (a printed beam's part is drawn solid and printed with walls and infill: its fill is the share of plastic laid)
|
|
35
|
-
const key = p.leaf ?? p.item,
|
|
36
|
-
|
|
61
|
+
const key = p.leaf ?? p.item,
|
|
62
|
+
mm3 = p.triangles.length ? volume(p.triangles) * (p.fill ?? 1) : 0, // (a part drawn empty weighs nothing)
|
|
63
|
+
o = by.get(key) ?? { id: key, object: p.object ?? null, volume: 0 };
|
|
64
|
+
o.volume += mm3;
|
|
65
|
+
by.set(key, o);
|
|
37
66
|
}
|
|
38
67
|
// and what comes with a placed part, printed beside it (a levelling foot's pad), by its model's grams
|
|
39
|
-
for (const c of companions(s))
|
|
40
|
-
|
|
68
|
+
for (const c of companions(s))
|
|
69
|
+
if (c.grams > 0) by.set(c.id, { id: c.id, object: null, volume: (c.grams / mat.density) * 1000, with: c.of });
|
|
70
|
+
return [...by.values()].map((o) => ({
|
|
71
|
+
...o,
|
|
72
|
+
volume: n(o.volume, 0),
|
|
73
|
+
grams: n((o.volume / 1000) * mat.density),
|
|
74
|
+
minutes: n(o.volume / flow / 60, 0),
|
|
75
|
+
}));
|
|
41
76
|
}
|
|
42
77
|
|
|
43
78
|
// The items printed together in place (the assembly's printInPlace) as the one print they are: one row, their grams and
|
|
44
79
|
// the minutes of the whole, in the place of the first of them
|
|
45
80
|
function inPlaceRow(s, printed) {
|
|
46
|
-
const ids = Array.isArray(s.a?.printInPlace) ? s.a.printInPlace : [],
|
|
81
|
+
const ids = Array.isArray(s.a?.printInPlace) ? s.a.printInPlace : [],
|
|
82
|
+
members = printed.filter((p) => ids.includes(p.id));
|
|
47
83
|
if (members.length < 2) return printed;
|
|
48
|
-
const mat = materialOf(s.project);
|
|
49
|
-
|
|
50
|
-
const volume = members.reduce((t, p) => t + p.volume, 0),
|
|
84
|
+
const mat = materialOf(s.project);
|
|
85
|
+
const { flow } = flowOf(s.project);
|
|
86
|
+
const volume = members.reduce((t, p) => t + p.volume, 0),
|
|
87
|
+
row = {
|
|
88
|
+
id: members.map((p) => p.id).join(', '),
|
|
89
|
+
object: null,
|
|
90
|
+
volume: n(volume, 0),
|
|
91
|
+
grams: n((volume / 1000) * mat.density),
|
|
92
|
+
minutes: n(volume / flow / 60, 0),
|
|
93
|
+
inPlace: members.map((p) => p.id),
|
|
94
|
+
};
|
|
51
95
|
const at = printed.indexOf(members[0]);
|
|
52
96
|
return [...printed.slice(0, at), row, ...printed.slice(at).filter((p) => !members.includes(p))];
|
|
53
97
|
}
|
|
54
98
|
|
|
55
99
|
/** The whole list. @returns {{ bought: object[], fasteners: object[], wires: object[], printed: object[], cut: object | null, totals: object }} */
|
|
56
100
|
export function shoppingList(s) {
|
|
57
|
-
const mat = materialOf(s.project),
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
101
|
+
const mat = materialOf(s.project),
|
|
102
|
+
printed = inPlaceRow(s, printedParts(s)),
|
|
103
|
+
bought = new Map(),
|
|
104
|
+
fasteners = new Map();
|
|
105
|
+
for (const o of s.objects)
|
|
106
|
+
if (o.bought && !o.unplaced) {
|
|
107
|
+
// (a beam by its profile and length: one line for each length cut, lib/beams.mjs)
|
|
108
|
+
// (a panel by its sheet and size: lib/panels.mjs)
|
|
109
|
+
const len = o.bought.length != null ? Number(o.bought.length.toFixed(2)) : null,
|
|
110
|
+
size = o.bought.size ?? null,
|
|
111
|
+
inHand = o.have === true, // ("have": in hand, not to buy; set on each copy and on a component's parts by lib/assembly.mjs)
|
|
112
|
+
key = `${o.bought.set}|${o.bought.variant ?? ''}|${len ?? ''}|${size ?? ''}|${inHand}`;
|
|
113
|
+
const line = bought.get(key) ?? {
|
|
114
|
+
set: o.bought.set,
|
|
115
|
+
variant: o.bought.variant,
|
|
116
|
+
part: o.bought.part,
|
|
117
|
+
...(len != null ? { length: len } : {}),
|
|
118
|
+
...(size ? { size } : {}),
|
|
119
|
+
...(inHand ? { have: true } : {}),
|
|
120
|
+
qty: 0,
|
|
121
|
+
grams: o.bought.mass != null ? o.bought.mass * 1000 : null,
|
|
122
|
+
...(o.bought.cost ? { cost: o.bought.cost.value, currency: o.bought.cost.unit } : {}),
|
|
123
|
+
items: [],
|
|
124
|
+
};
|
|
125
|
+
line.qty++;
|
|
126
|
+
line.items.push(o.id);
|
|
127
|
+
bought.set(key, line);
|
|
128
|
+
}
|
|
66
129
|
// fasten.count: the hardware a fasten placed (a component's too), one part a piece; a motor's mounting screws; and a fit
|
|
67
130
|
// that names its fastener by hand. Summed by name, which is the thread, length and head, or the nut's kind
|
|
68
|
-
const add = (name, n, from, more = {}) => {
|
|
131
|
+
const add = (name, n, from, more = {}) => {
|
|
132
|
+
const line = fasteners.get(name) ?? { fastener: name, qty: 0, from: [], ...more };
|
|
133
|
+
line.qty += n;
|
|
134
|
+
if (!line.from.includes(from)) line.from.push(from);
|
|
135
|
+
fasteners.set(name, line);
|
|
136
|
+
};
|
|
69
137
|
// (a printed bolt is no bought screw: it goes with the printed parts, by its name, with its grams and minutes)
|
|
70
138
|
const bolts = new Map();
|
|
71
|
-
for (const p of s.parts)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
139
|
+
for (const p of s.parts)
|
|
140
|
+
if (p.hardware?.printed) {
|
|
141
|
+
const h = p.hardware,
|
|
142
|
+
b = bolts.get(h.name) ?? { id: h.name, object: null, count: 0, grams: 0, bolt: true, from: [] };
|
|
143
|
+
b.count++;
|
|
144
|
+
b.grams += h.grams ?? 0;
|
|
145
|
+
if (!b.from.includes(h.from)) b.from.push(h.from);
|
|
146
|
+
bolts.set(h.name, b);
|
|
147
|
+
}
|
|
148
|
+
const printer = flowOf(s.project),
|
|
149
|
+
{ flow } = printer;
|
|
150
|
+
for (const b of bolts.values()) {
|
|
151
|
+
const mm3 = (b.grams / mat.density) * 1000;
|
|
152
|
+
printed.push({
|
|
153
|
+
id: b.id,
|
|
154
|
+
object: null,
|
|
155
|
+
volume: n(mm3, 0),
|
|
156
|
+
grams: n(b.grams),
|
|
157
|
+
minutes: n(mm3 / flow / 60, 0),
|
|
158
|
+
count: b.count,
|
|
159
|
+
bolt: true,
|
|
160
|
+
from: b.from,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
for (const p of s.parts)
|
|
164
|
+
if (p.hardware && !p.hardware.printed) {
|
|
165
|
+
const h = p.hardware;
|
|
166
|
+
add(h.name, 1, h.from ?? h.name, {
|
|
167
|
+
kind: h.kind,
|
|
168
|
+
thread: h.thread ?? null,
|
|
169
|
+
length: h.length ?? null,
|
|
170
|
+
head: h.head ?? null,
|
|
171
|
+
grams: h.grams ?? null,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
75
174
|
for (const m of motorScrews(s, { placeItems, jointValues })) {
|
|
76
|
-
let grams = null;
|
|
77
|
-
|
|
175
|
+
let grams = null;
|
|
176
|
+
try {
|
|
177
|
+
grams = screwModel(m.thread, { length: m.length, head: m.head, anyLength: true }).mass;
|
|
178
|
+
} catch {
|
|
179
|
+
// (not a screw the fixings can weigh: listed with no weight)
|
|
180
|
+
}
|
|
181
|
+
add(m.name, m.count, `${m.item} on ${m.other} (its mount, a fit that fastens)`, {
|
|
182
|
+
kind: 'screw',
|
|
183
|
+
thread: m.thread,
|
|
184
|
+
length: m.length,
|
|
185
|
+
head: m.head,
|
|
186
|
+
grams,
|
|
187
|
+
});
|
|
78
188
|
}
|
|
79
189
|
// what a printed fitting takes besides its screws: a hinge's pin and its nut, a catch's magnet and strike (lib/fittings.mjs)
|
|
80
190
|
// (a nut carrier's nuts that a fasten placed in it are listed with that fasten, not again)
|
|
81
|
-
const placedIn = (o, h) =>
|
|
82
|
-
|
|
191
|
+
const placedIn = (o, h) =>
|
|
192
|
+
o.fitting.fitting === 'nut-carrier'
|
|
193
|
+
? s.parts.filter((p) => p.hardware?.kind === h.kind && p.hardware.thread === h.thread && p.moves === o.id).length
|
|
194
|
+
: 0;
|
|
195
|
+
for (const o of s.objects)
|
|
196
|
+
if (o.fitting?.hardware && !o.unplaced)
|
|
197
|
+
for (const h of o.fitting.hardware) {
|
|
198
|
+
const n = (h.count ?? 1) - placedIn(o, h);
|
|
199
|
+
if (n > 0)
|
|
200
|
+
add(h.name, n, `${o.id} (${h.why ?? 'its fitting'})`, {
|
|
201
|
+
kind: h.kind,
|
|
202
|
+
thread: h.thread ?? null,
|
|
203
|
+
length: h.length ?? null,
|
|
204
|
+
head: h.head ?? null,
|
|
205
|
+
grams: null,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
83
208
|
// (pins and magnets a fit names by the schema's form: by name, with their mass and whether they are bought, cut from filament or printed)
|
|
84
|
-
for (const x of s.fits)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
209
|
+
for (const x of s.fits)
|
|
210
|
+
if (x.fastener && x.fasten == null) {
|
|
211
|
+
let more = {};
|
|
212
|
+
if (x.pin)
|
|
213
|
+
try {
|
|
214
|
+
const m = pinModel(x.fastener, { material: mat.name });
|
|
215
|
+
more = { kind: 'pin', made: m.made, thread: null, length: m.length, grams: m.grams };
|
|
216
|
+
} catch {
|
|
217
|
+
// (not a pin the fixings know: listed as it is named)
|
|
218
|
+
}
|
|
219
|
+
if (x.magnet)
|
|
220
|
+
try {
|
|
221
|
+
more = { kind: 'magnet', made: 'bought', thread: null, length: null, grams: magnetModel(x.fastener).grams };
|
|
222
|
+
} catch {
|
|
223
|
+
// (not a magnet the fixings know: listed as it is named)
|
|
224
|
+
}
|
|
225
|
+
add(x.fastener, x.count != null ? x.num(x.count) : 1, x.note || `${x.a} / ${x.b}`, more);
|
|
226
|
+
}
|
|
89
227
|
// Rounded up to whole packs (packs.pack), the spares said
|
|
90
|
-
let pack = null;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
228
|
+
let pack = null;
|
|
229
|
+
try {
|
|
230
|
+
const r = loadRuleSet('packs').rules.pack;
|
|
231
|
+
pack = { size: r.value, rule: 'packs.pack', kind: r.kind };
|
|
232
|
+
} catch {
|
|
233
|
+
// (no packs rule set: fixings are listed one by one)
|
|
234
|
+
}
|
|
235
|
+
const fixings = [...fasteners.values()]
|
|
236
|
+
.map((l) => ({
|
|
237
|
+
...l,
|
|
238
|
+
...(l.grams != null ? { grams: n(l.grams, 3), mass: n(l.grams * l.qty, 2) } : {}),
|
|
239
|
+
...(l.kind && pack && (l.kind !== 'pin' || l.made === 'bought')
|
|
240
|
+
? {
|
|
241
|
+
packs: Math.ceil(l.qty / pack.size),
|
|
242
|
+
buy: Math.ceil(l.qty / pack.size) * pack.size,
|
|
243
|
+
spare: Math.ceil(l.qty / pack.size) * pack.size - l.qty,
|
|
244
|
+
}
|
|
245
|
+
: {}),
|
|
246
|
+
}))
|
|
247
|
+
.sort(
|
|
248
|
+
(a, b) =>
|
|
249
|
+
(a.kind ? 0 : 1) - (b.kind ? 0 : 1) ||
|
|
250
|
+
['screw', 'nut', 'washer', 'insert', 'pin', 'magnet'].indexOf(a.kind) -
|
|
251
|
+
['screw', 'nut', 'washer', 'insert', 'pin', 'magnet'].indexOf(b.kind) ||
|
|
252
|
+
String(a.thread).localeCompare(String(b.thread)) ||
|
|
253
|
+
(a.length ?? 0) - (b.length ?? 0) ||
|
|
254
|
+
a.fastener.localeCompare(b.fastener),
|
|
255
|
+
);
|
|
256
|
+
const rows = s.wires?.length ? wiresFor(s) : [],
|
|
257
|
+
wires = new Map();
|
|
258
|
+
for (const w of rows) {
|
|
259
|
+
const key = w.gauge ?? 'gauge not given',
|
|
260
|
+
l = wires.get(key) ?? { gauge: w.gauge, conductors: 0, length: 0, runs: [] };
|
|
261
|
+
l.length += w.length * w.conductors;
|
|
262
|
+
l.conductors += w.conductors;
|
|
263
|
+
l.runs.push(w.id);
|
|
264
|
+
wires.set(key, l);
|
|
265
|
+
}
|
|
266
|
+
const boughtList = [...bought.values()]
|
|
267
|
+
.map((b) => ({ ...b, grams: b.grams != null ? n(b.grams) : null }))
|
|
268
|
+
.sort((a, b) => a.part.localeCompare(b.part) || a.set.localeCompare(b.set) || (b.length ?? 0) - (a.length ?? 0));
|
|
269
|
+
const printedGrams = printed.reduce((t, p) => t + p.grams, 0),
|
|
270
|
+
boughtGrams = boughtList.reduce((t, b) => t + (b.grams ?? 0) * b.qty, 0),
|
|
271
|
+
unknown = boughtList.filter((b) => b.grams == null).map((b) => b.set);
|
|
97
272
|
const cut = cutListData(cutListOf(s));
|
|
98
273
|
const fixingsGrams = fixings.reduce((t, f) => t + (f.mass ?? 0), 0);
|
|
99
274
|
// what it costs, where prices are given: the bought items' by their sets, the printed parts' plastic by the material's
|
|
100
275
|
// price a kilogram; fixings and bars are not priced, and the list says which lines have no price
|
|
101
|
-
const priced = boughtList.filter((b) => b.cost != null && !b.have),
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
276
|
+
const priced = boughtList.filter((b) => b.cost != null && !b.have),
|
|
277
|
+
currencies = [...new Set(priced.map((b) => b.currency))];
|
|
278
|
+
const plastic =
|
|
279
|
+
printedGrams > 0 ? printedCost(printedGrams, { name: mat.name, rules: mat.cost ? { cost: mat.cost } : {} }) : null;
|
|
280
|
+
const cost =
|
|
281
|
+
priced.length || plastic
|
|
282
|
+
? {
|
|
283
|
+
bought:
|
|
284
|
+
priced.length && currencies.length === 1
|
|
285
|
+
? n(
|
|
286
|
+
priced.reduce((t, b) => t + b.cost * b.qty, 0),
|
|
287
|
+
2,
|
|
288
|
+
)
|
|
289
|
+
: null,
|
|
290
|
+
currency: currencies.length === 1 ? currencies[0] : (plastic?.unit ?? null),
|
|
291
|
+
...(currencies.length > 1 ? { currencies } : {}),
|
|
292
|
+
unpriced: [...new Set(boughtList.filter((b) => b.cost == null && !b.have).map((b) => b.set))],
|
|
293
|
+
printed: plastic ? n(plastic.value, 2) : null,
|
|
294
|
+
...(plastic ? { printedUnit: plastic.unit, printedRule: plastic.rule } : {}),
|
|
295
|
+
}
|
|
296
|
+
: null;
|
|
297
|
+
return {
|
|
298
|
+
bought: boughtList,
|
|
299
|
+
fasteners: fixings,
|
|
300
|
+
wires: [...wires.values()],
|
|
301
|
+
printed,
|
|
302
|
+
cut,
|
|
303
|
+
...(cost ? { cost } : {}),
|
|
304
|
+
totals: {
|
|
305
|
+
printedGrams: n(printedGrams),
|
|
306
|
+
boughtGrams: n(boughtGrams),
|
|
307
|
+
fixingsGrams: n(fixingsGrams),
|
|
308
|
+
machineGrams: n(printedGrams + boughtGrams + fixingsGrams),
|
|
309
|
+
...(pack && fixings.some((f) => f.packs) ? { pack } : {}),
|
|
310
|
+
minutes: n(
|
|
311
|
+
printed.reduce((t, p) => t + p.minutes, 0),
|
|
312
|
+
0,
|
|
313
|
+
),
|
|
314
|
+
material: mat.name,
|
|
315
|
+
...(mat.assumed ? { densityAssumed: true, density: mat.density } : {}),
|
|
316
|
+
printer: printer.printer,
|
|
317
|
+
flow,
|
|
318
|
+
...(printer.assumed ? { flowAssumed: true } : {}),
|
|
319
|
+
...(unknown.length ? { noMassFor: unknown } : {}),
|
|
320
|
+
},
|
|
321
|
+
};
|
|
106
322
|
}
|
|
107
323
|
|
|
324
|
+
/** Whose flow a list's minutes are from, and what was assumed (a printer without a print_flow, a material without a density). @param {object} t a list's totals @returns {string} */
|
|
325
|
+
export const flowText = (t) =>
|
|
326
|
+
`${t.flowAssumed ? `the usual flow (${t.printer ? `${t.printer} gives no print_flow` : 'no printer named'}: ${t.flow} mm³/s assumed)` : `${t.printer ?? 'the printer'}'s flow, ${t.flow} mm³/s`}${t.densityAssumed ? `; no density known for ${t.material}: ${t.density} g/cm³ assumed for the grams` : ''}`;
|
|
327
|
+
|
|
108
328
|
export function listText(ref, l) {
|
|
109
329
|
const out = [`${ref}: what it takes to build`];
|
|
110
330
|
const row = (a) => out.push(a.join(' '));
|
|
111
331
|
// (with parts in hand, those to buy come first, then what is in hand: neither is left off the list)
|
|
112
|
-
const boughtRow = (b) =>
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
332
|
+
const boughtRow = (b) =>
|
|
333
|
+
row([
|
|
334
|
+
` ${String(b.qty).padStart(3)} ×`,
|
|
335
|
+
`${b.set}${b.variant ? ` (${b.variant})` : ''}${b.length != null ? ` x ${b.length} mm` : ''}${b.size ? ` cut ${b.size[0]} x ${b.size[1]} mm` : ''}`,
|
|
336
|
+
`a ${b.part}`,
|
|
337
|
+
b.grams != null ? `${b.grams} g each` : 'mass not given',
|
|
338
|
+
...(b.cost != null ? [`${b.cost} ${b.currency} each`] : []),
|
|
339
|
+
b.items.join(', '),
|
|
340
|
+
]);
|
|
341
|
+
const toBuy = l.bought.filter((b) => !b.have),
|
|
342
|
+
inHand = l.bought.filter((b) => b.have);
|
|
343
|
+
if (l.bought.length && !inHand.length) {
|
|
344
|
+
out.push('', 'Bought');
|
|
345
|
+
toBuy.forEach(boughtRow);
|
|
346
|
+
} else if (l.bought.length) {
|
|
347
|
+
out.push('', toBuy.length ? 'Bought, to buy' : 'Bought: nothing to buy, all in hand');
|
|
348
|
+
toBuy.forEach(boughtRow);
|
|
349
|
+
out.push('', 'Bought, in hand (marked "have" in the assembly)');
|
|
350
|
+
inHand.forEach(boughtRow);
|
|
351
|
+
}
|
|
352
|
+
if (l.cut) {
|
|
353
|
+
out.push(
|
|
354
|
+
'',
|
|
355
|
+
`Cut list (kerf ${l.cut.rules.kerf.value} mm, trim ${l.cut.rules.trim.value} mm; a sheet for each mark: cad beams ${ref})`,
|
|
356
|
+
...cutListText(l.cut),
|
|
357
|
+
);
|
|
118
358
|
}
|
|
119
|
-
if (l.cut) { out.push('', `Cut list (kerf ${l.cut.rules.kerf.value} mm, trim ${l.cut.rules.trim.value} mm; a sheet for each mark: cad beams ${ref})`, ...cutListText(l.cut)); }
|
|
120
359
|
if (l.fasteners.length) {
|
|
121
360
|
out.push('', 'Fasteners');
|
|
122
|
-
for (const f of l.fasteners)
|
|
123
|
-
|
|
361
|
+
for (const f of l.fasteners)
|
|
362
|
+
row([
|
|
363
|
+
` ${String(f.qty).padStart(3)} ×`,
|
|
364
|
+
f.fastener,
|
|
365
|
+
...(f.mass != null ? [`${f.grams} g each, ${f.mass} g`] : []),
|
|
366
|
+
...(f.packs ? [`${f.packs} pack${f.packs > 1 ? 's' : ''} of ${l.totals.pack.size}, ${f.spare} spare`] : []),
|
|
367
|
+
f.from.join('; '),
|
|
368
|
+
]);
|
|
369
|
+
if (l.totals.pack)
|
|
370
|
+
out.push(
|
|
371
|
+
` (packs of ${l.totals.pack.size}: ${l.totals.pack.rule}, ${/^[aeiou]/.test(l.totals.pack.kind) ? 'an' : 'a'} ${l.totals.pack.kind}; masses are estimates from each piece's outline)`,
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
if (l.wires.length) {
|
|
375
|
+
out.push('', 'Wire');
|
|
376
|
+
for (const w of l.wires) row([` ${w.length} mm of`, w.gauge ?? 'gauge not given', `in ${w.runs.join(', ')}`]);
|
|
377
|
+
}
|
|
378
|
+
if (l.printed.length) {
|
|
379
|
+
out.push('', `Printed (${l.totals.material})`);
|
|
380
|
+
for (const p of l.printed)
|
|
381
|
+
row([
|
|
382
|
+
` ${String(p.grams).padStart(7)} g`,
|
|
383
|
+
`about ${p.minutes} min`,
|
|
384
|
+
`${p.id}${p.object && p.object !== p.id ? ` (${p.object})` : ''}${p.inPlace ? ' (printed together in place: one print)' : ''}${p.with ? ` (comes with ${p.with}: printed beside it, screwed into it by hand)` : ''}${p.bolt ? ` (${p.count} printed bolt${p.count > 1 ? 's' : ''}, standing on ${p.count > 1 ? 'their heads' : 'its head'}: ${p.from.join('; ')})` : ''}`,
|
|
385
|
+
]);
|
|
124
386
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
387
|
+
out.push(
|
|
388
|
+
'',
|
|
389
|
+
`Total: ${l.totals.printedGrams} g printed, about ${l.totals.minutes} min of printing (an estimate from volume and ${flowText(l.totals)}), ${l.totals.fixingsGrams ? `${l.totals.fixingsGrams} g of fixings, ` : ''}${l.totals.machineGrams} g as a machine${l.totals.noMassFor ? ` (no mass given for ${l.totals.noMassFor.join(', ')})` : ''}`,
|
|
390
|
+
);
|
|
128
391
|
if (l.cost) {
|
|
129
|
-
const c = l.cost,
|
|
130
|
-
|
|
392
|
+
const c = l.cost,
|
|
393
|
+
parts = [];
|
|
394
|
+
if (c.bought != null)
|
|
395
|
+
parts.push(
|
|
396
|
+
`${c.bought} ${c.currency} bought${c.unpriced.length ? ` (no price given for ${c.unpriced.join(', ')})` : ''}`,
|
|
397
|
+
);
|
|
131
398
|
else if (c.currencies) parts.push(`bought items priced in ${c.currencies.join(' and ')}, not added together`);
|
|
132
|
-
if (c.printed != null)
|
|
133
|
-
|
|
399
|
+
if (c.printed != null)
|
|
400
|
+
parts.push(`${c.printed} ${c.printedUnit} of ${l.totals.material} printed (the plastic alone)`);
|
|
401
|
+
out.push(
|
|
402
|
+
`Cost: ${parts.join(', ')}; fixings and bars are not priced. Prices are each set's, on the date it was checked`,
|
|
403
|
+
);
|
|
134
404
|
}
|
|
135
405
|
return out.join('\n');
|
|
136
406
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Who may use a live view (live.mjs, the optimiser's; live-shape.mjs, a flexure being shaped). Both are small web
|
|
2
|
+
// servers on this computer, and any web page open in the browser can send requests to them. Two checks keep those pages
|
|
3
|
+
// out:
|
|
4
|
+
// - The Host the request names must be this computer: 127.0.0.1, localhost or [::1], the address the server was told
|
|
5
|
+
// to listen on, any other written-out address (192.168.1.5, 100.101.102.103, [fd7a::1]), or a name the person asked
|
|
6
|
+
// for (allowHosts). A page on someone's site can point its own name at 127.0.0.1 (DNS rebinding) and then read the
|
|
7
|
+
// view as if it were its own; its requests still carry its own name as the Host, and are refused. A written-out
|
|
8
|
+
// address can't be pointed anywhere, so accepting every one is as safe as accepting this computer's own, and it lets
|
|
9
|
+
// in a phone on Tailscale: in Tailscale's userspace mode the machine has no 100.x address of its own, and tailscaled
|
|
10
|
+
// passes the phone's requests to 127.0.0.1 with the Host the phone used.
|
|
11
|
+
// - A POST (the stop button) that says which page sent it (Origin) must come from the view's own page: the Origin must
|
|
12
|
+
// be http:// and the Host. A page on any other site that posts here names its own site, and is refused.
|
|
13
|
+
// - No other page may show the view inside itself (a frame): a page that frames it, nearly invisible, under a button of
|
|
14
|
+
// its own gets the person's click on the view's stop button, which then comes from the view's own page. Every answer
|
|
15
|
+
// says it may not be framed.
|
|
16
|
+
// The port is not checked: the name is what a page can't fake, and a view reached through a forwarded port (ssh -L
|
|
17
|
+
// 9000:localhost:8090) arrives with another port in its Host.
|
|
18
|
+
import net from 'node:net';
|
|
19
|
+
|
|
20
|
+
const LOCAL = ['127.0.0.1', 'localhost', '::1'];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The name in a Host header, without its port or an IPv6 address's brackets, in lower case ('' if there is none).
|
|
24
|
+
* @param {string | undefined} host
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
export function hostName(host) {
|
|
28
|
+
const h = String(host ?? '')
|
|
29
|
+
.trim()
|
|
30
|
+
.toLowerCase();
|
|
31
|
+
if (h.startsWith('[')) return h.slice(1, h.indexOf(']') < 0 ? undefined : h.indexOf(']'));
|
|
32
|
+
const name = h.split(':').length === 2 ? h.slice(0, h.lastIndexOf(':')) : h;
|
|
33
|
+
return name.endsWith('.') ? name.slice(0, -1) : name;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Makes the check a live view's server runs on each request. It answers a refused request itself (421 for a Host
|
|
38
|
+
* that isn't this computer's, 403 for another page's POST) and says whether to go on. Every answer refuses to be shown
|
|
39
|
+
* in another page's frame.
|
|
40
|
+
* @param {{ host?: string, allowHosts?: string[] }} [o] host: the address the server listens on; allowHosts: more
|
|
41
|
+
* names to accept, such as a Tailscale MagicDNS name (my-laptop)
|
|
42
|
+
* @returns {(req: import('node:http').IncomingMessage, res: import('node:http').ServerResponse) => boolean} true when
|
|
43
|
+
* the request may go on
|
|
44
|
+
*/
|
|
45
|
+
export function liveGuard({ host, allowHosts = [] } = {}) {
|
|
46
|
+
const names = new Set([...LOCAL, ...[host, ...allowHosts].filter(Boolean).map(hostName)]);
|
|
47
|
+
const accepted = (name) => names.has(name) || net.isIP(name) !== 0;
|
|
48
|
+
return (req, res) => {
|
|
49
|
+
res.setHeader('content-security-policy', "frame-ancestors 'none'");
|
|
50
|
+
res.setHeader('x-frame-options', 'DENY');
|
|
51
|
+
const given = req.headers.host;
|
|
52
|
+
if (!given || !accepted(hostName(given))) {
|
|
53
|
+
res.writeHead(421, { 'content-type': 'text/plain; charset=utf-8' });
|
|
54
|
+
res.end(
|
|
55
|
+
`This live view answers only to this computer's own names and addresses, not "${given ?? ''}". ` +
|
|
56
|
+
'To open it by another name (a Tailscale name, say), give that name with --allow-host.\n',
|
|
57
|
+
);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const origin = req.headers.origin;
|
|
61
|
+
if (req.method === 'POST' && origin !== undefined && origin.toLowerCase() !== `http://${given.toLowerCase()}`) {
|
|
62
|
+
res.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' });
|
|
63
|
+
res.end('Only the live view itself can stop the run: this request came from another page.\n');
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
};
|
|
68
|
+
}
|