@rulecad/cad 0.10.0 → 0.11.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 +8 -4
- package/CHANGELOG.md +285 -1
- package/EXAMPLES.md +101 -89
- package/README.md +104 -27
- package/bin/cad.mjs +41 -13
- package/lib/assembly.mjs +418 -101
- package/lib/bays.mjs +124 -0
- package/lib/cache-file.mjs +66 -23
- package/lib/clone.mjs +93 -11
- package/lib/design.mjs +40 -4
- package/lib/edit.mjs +5 -3
- package/lib/framebuild.mjs +72 -91
- package/lib/framecheck.mjs +154 -77
- package/lib/framesize.mjs +37 -1
- package/lib/gait.mjs +17 -7
- package/lib/guides.mjs +84 -50
- package/lib/live-guard.mjs +18 -8
- package/lib/measured.mjs +2 -1
- package/lib/mesh.mjs +156 -56
- package/lib/motors.mjs +17 -4
- package/lib/node-version.mjs +52 -0
- package/lib/order.mjs +1 -2
- package/lib/picture-servers.mjs +66 -0
- package/lib/power.mjs +14 -7
- package/lib/preview.mjs +12 -25
- package/lib/programs.mjs +113 -0
- package/lib/sandbox-guard.mjs +43 -0
- package/lib/sandbox.mjs +172 -12
- package/lib/targets/gist.mjs +9 -6
- package/lib/versions.mjs +106 -10
- package/lib/workspace.mjs +3 -1
- package/package.json +13 -13
- package/schemas/assembly.schema.json +5 -4
- package/schemas/object.schema.json +1 -1
package/lib/guides.mjs
CHANGED
|
@@ -66,7 +66,6 @@ const { cylinder, cuboid, polygon } = jscad.primitives,
|
|
|
66
66
|
{ subtract, union } = jscad.booleans,
|
|
67
67
|
{ extrudeRotate } = jscad.extrusions;
|
|
68
68
|
const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
|
|
69
|
-
const up2 = (x, step) => Math.ceil(x / step - 1e-9) * step;
|
|
70
69
|
const mm = (x) => String(+Number(x).toFixed(2)),
|
|
71
70
|
r3 = (x) => +x.toFixed(3),
|
|
72
71
|
n3 = (x, d = 2) => String(Number(Number(x).toFixed(d)));
|
|
@@ -426,14 +425,14 @@ export function expandGuides(a, num, context = framesContext(), material = 'pla'
|
|
|
426
425
|
// the rods pressed into the holders, run through the bearings; the bearings pressed into the carriage
|
|
427
426
|
for (const r of rods)
|
|
428
427
|
fits.push(
|
|
429
|
-
{ a: r, b: ha, fastens: true,
|
|
430
|
-
{ a: r, b: hb, fastens: true,
|
|
431
|
-
{ a: r, b: cid,
|
|
428
|
+
{ a: r, b: ha, fastens: true, overlap: 'allowed', note: 'The rod pressed through its holder' },
|
|
429
|
+
{ a: r, b: hb, fastens: true, overlap: 'allowed', note: 'The rod pressed through its holder' },
|
|
430
|
+
{ a: r, b: cid, overlap: 'allowed', note: "The rod runs through the carriage's bearing" },
|
|
432
431
|
);
|
|
433
432
|
lms.forEach((x, k) =>
|
|
434
433
|
fits.push(
|
|
435
|
-
{ a: x, b: cid, fastens: true,
|
|
436
|
-
{ a: rods[k], b: x,
|
|
434
|
+
{ a: x, b: cid, fastens: true, overlap: 'allowed', note: 'The bearing pressed into its pocket' },
|
|
435
|
+
{ a: rods[k], b: x, overlap: 'allowed', note: 'Rod in bearing' },
|
|
437
436
|
),
|
|
438
437
|
);
|
|
439
438
|
const record = {
|
|
@@ -548,16 +547,22 @@ export function expandGuides(a, num, context = framesContext(), material = 'pla'
|
|
|
548
547
|
: [];
|
|
549
548
|
fits.push(
|
|
550
549
|
{ a: mo, b: plate, fastens: true, min: -0.1, note: "The motor's face on its plate" },
|
|
551
|
-
{ a: mo, b: cp,
|
|
552
|
-
{ a: sw, b: cp,
|
|
553
|
-
{ a: sw, b: nt,
|
|
554
|
-
{ a: sw, b: cid,
|
|
555
|
-
{ a: nt, b: cid, fastens: true,
|
|
556
|
-
{ a: sw, b: ha,
|
|
557
|
-
{ a: sw, b: hb,
|
|
550
|
+
{ a: mo, b: cp, overlap: 'allowed', note: "The motor's shaft in the coupler" },
|
|
551
|
+
{ a: sw, b: cp, overlap: 'allowed', note: 'The screw in the coupler' },
|
|
552
|
+
{ a: sw, b: nt, overlap: 'allowed', note: 'The screw through its nut' },
|
|
553
|
+
{ a: sw, b: cid, overlap: 'allowed', note: 'The screw through the carriage' },
|
|
554
|
+
{ a: nt, b: cid, fastens: true, overlap: 'allowed', note: 'The nut in its bore, its flange screwed on' },
|
|
555
|
+
{ a: sw, b: ha, overlap: 'allowed', note: 'The screw through holder a' },
|
|
556
|
+
{ a: sw, b: hb, overlap: 'allowed', note: 'The screw into holder b' },
|
|
558
557
|
...sup.flatMap((x, k) => [
|
|
559
|
-
{
|
|
560
|
-
|
|
558
|
+
{
|
|
559
|
+
a: x,
|
|
560
|
+
b: k ? hb : ha,
|
|
561
|
+
fastens: true,
|
|
562
|
+
overlap: 'allowed',
|
|
563
|
+
note: "The screw's bearing pressed into its holder",
|
|
564
|
+
},
|
|
565
|
+
{ a: sw, b: x, overlap: 'allowed', note: 'The screw in its bearing' },
|
|
561
566
|
]),
|
|
562
567
|
);
|
|
563
568
|
legFastens(plate, pm, `${sc.id}: the motor plate's leg`, sc.id);
|
|
@@ -588,7 +593,7 @@ export function expandGuides(a, num, context = framesContext(), material = 'pla'
|
|
|
588
593
|
added.push({ id: es.sw, bought: es.set, at: `${es.mount}.switch`, guideOf: id });
|
|
589
594
|
fits.push(
|
|
590
595
|
{ a: es.sw, b: es.mount, fastens: true, min: -0.1, note: 'The switch screwed to its mount' },
|
|
591
|
-
{ a: es.sw, b: cid,
|
|
596
|
+
{ a: es.sw, b: cid, overlap: 'allowed', note: 'The flag on the lever, at home' },
|
|
592
597
|
);
|
|
593
598
|
es.model.screws.forEach((x, k) =>
|
|
594
599
|
fasten.push({
|
|
@@ -752,7 +757,7 @@ export function expandGuides(a, num, context = framesContext(), material = 'pla'
|
|
|
752
757
|
a: nid,
|
|
753
758
|
b: bid,
|
|
754
759
|
fastens: true,
|
|
755
|
-
|
|
760
|
+
overlap: 'allowed',
|
|
756
761
|
note: `${nid} in ${bid}'s ${q.name} slot, held there by the rail's screws`,
|
|
757
762
|
});
|
|
758
763
|
}
|
|
@@ -991,7 +996,7 @@ export function expandGuides(a, num, context = framesContext(), material = 'pla'
|
|
|
991
996
|
});
|
|
992
997
|
fits.push(
|
|
993
998
|
{ a: mo, b: plate, fastens: true, min: -0.1, note: "The motor's face under its plate" },
|
|
994
|
-
{ a: mo, b: pu,
|
|
999
|
+
{ a: mo, b: pu, overlap: 'allowed', note: "The motor's shaft in the pulley" },
|
|
995
1000
|
{ a: il, b: blk, fastens: true, min: -0.1, note: "The idler on its axle, on the block's ring" },
|
|
996
1001
|
);
|
|
997
1002
|
const spec = joints.find((x) => x.id === jid);
|
|
@@ -1255,10 +1260,56 @@ export function guideBuilt(it, id) {
|
|
|
1255
1260
|
});
|
|
1256
1261
|
}
|
|
1257
1262
|
|
|
1263
|
+
/**
|
|
1264
|
+
* What each rail guide adds to an error budget (guide.budget), said once the frame is solved (after frameChecks), so
|
|
1265
|
+
* that the beam's sag under the guide is the frame's own figure, the one frame.deflection gives (s.frameGuideSag), and
|
|
1266
|
+
* the rail's estimate only where the frame is not solved. @param {object} s a built assembly @param {Function} f the
|
|
1267
|
+
* findings' sink
|
|
1268
|
+
*/
|
|
1269
|
+
export function guideBudgetFindings(s, f) {
|
|
1270
|
+
for (const r of s.guides ?? []) {
|
|
1271
|
+
if (['rods', 'belt', 'screw', 'wheels', 'slider'].includes(r.kind) || !r.terms?.length) continue;
|
|
1272
|
+
const solved = s.frameGuideSag?.get(r.id),
|
|
1273
|
+
t = r.terms.map((x) =>
|
|
1274
|
+
x.term === 'beam sag' && solved
|
|
1275
|
+
? {
|
|
1276
|
+
term: 'beam sag',
|
|
1277
|
+
value: solved.sag,
|
|
1278
|
+
kind: solved.restsOn === 'beams' ? 'derived' : solved.restsOn,
|
|
1279
|
+
note: `${solved.beam} under ${r.id}, solved with the frame${solved.pose ? `, ${solved.pose}` : ''}: ${n3(solved.rigid, 3)} mm with its joins rigid`,
|
|
1280
|
+
}
|
|
1281
|
+
: x,
|
|
1282
|
+
),
|
|
1283
|
+
sag = t.find((x) => x.term === 'beam sag');
|
|
1284
|
+
const worst = t.reduce((a, x) => a + x.value, 0);
|
|
1285
|
+
const said = !sag
|
|
1286
|
+
? ''
|
|
1287
|
+
: solved
|
|
1288
|
+
? ` The beam's sag is the frame's own, as frame.deflection gives it, with the joins' springs (${solved.restsOn === 'estimate' ? 'their stiffness an estimate' : solved.restsOn === 'measured' ? 'their stiffness measured' : 'no joins'})`
|
|
1289
|
+
: ` The beam's sag is an estimate (${GUIDE_DEFAULTS.sag.rule.split(':')[0].toLowerCase()}): the frame is not solved, so frame.deflection gives none`;
|
|
1290
|
+
f(
|
|
1291
|
+
'info',
|
|
1292
|
+
`${r.id}: along its travel it adds ${t.map((x) => `${x.term} ${n3(x.value, 3)} mm (${x.kind})`).join(', ')} to the error budget: ${n3(worst, 3)} mm at worst.${said}`,
|
|
1293
|
+
null,
|
|
1294
|
+
{
|
|
1295
|
+
code: 'guide.budget',
|
|
1296
|
+
values: { terms: t, worst },
|
|
1297
|
+
refs: {
|
|
1298
|
+
guide: r.id,
|
|
1299
|
+
beam: r.beam,
|
|
1300
|
+
rail: r.model.set,
|
|
1301
|
+
carriages: r.carriages,
|
|
1302
|
+
...(sag ? { sag: solved ? 'frame' : 'estimate' } : {}),
|
|
1303
|
+
},
|
|
1304
|
+
},
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1258
1309
|
/**
|
|
1259
1310
|
* A built assembly's guide findings: guide.holes, guide.end and guide.travel from the rail's model, guide.fit (on the
|
|
1260
|
-
* face, its nuts, its ends)
|
|
1261
|
-
*
|
|
1311
|
+
* face, its nuts, its ends) and guide.jig (the printed jig that centres it, and how to print one); guide.budget comes
|
|
1312
|
+
* after the frame's solve (guideBudgetFindings). @param {object} s a built assembly @param {Function} f the findings' sink
|
|
1262
1313
|
*/
|
|
1263
1314
|
export function guideFindings(s, f) {
|
|
1264
1315
|
for (const r of s.guides ?? []) {
|
|
@@ -1354,17 +1405,6 @@ export function guideFindings(s, f) {
|
|
|
1354
1405
|
values: x.values ?? {},
|
|
1355
1406
|
refs: { ...refs, ...(x.refs ?? {}) },
|
|
1356
1407
|
});
|
|
1357
|
-
const t = r.terms ?? [];
|
|
1358
|
-
if (t.length)
|
|
1359
|
-
f(
|
|
1360
|
-
'info',
|
|
1361
|
-
`${r.id}: along its travel it adds ${t.map((x) => `${x.term} ${n3(x.value, 3)} mm (${x.kind})`).join(', ')} to the error budget: ${n3(
|
|
1362
|
-
t.reduce((a, x) => a + x.value, 0),
|
|
1363
|
-
3,
|
|
1364
|
-
)} mm at worst. The beam's sag is an estimate (${GUIDE_DEFAULTS.sag.rule.split(':')[0].toLowerCase()})`,
|
|
1365
|
-
null,
|
|
1366
|
-
{ code: 'guide.budget', values: { terms: t, worst: t.reduce((a, x) => a + x.value, 0) }, refs },
|
|
1367
|
-
);
|
|
1368
1408
|
if (r.jig?.error) f('info', `${r.id}: no printed jig: ${r.jig.error}`, null, { code: 'guide.jig', refs });
|
|
1369
1409
|
else if (r.jig)
|
|
1370
1410
|
f(
|
|
@@ -1540,7 +1580,9 @@ function beltFindings(r, f) {
|
|
|
1540
1580
|
);
|
|
1541
1581
|
// (the belt carries the push and, off level, the weight's part along the travel: the two together at their worst.
|
|
1542
1582
|
// The motor holds the pulley, so the spans share it by their lengths, the short one taking the most: beltSpans walks
|
|
1543
|
-
// the travel for where the spans differ most, which is at one of its ends
|
|
1583
|
+
// the travel for where the spans differ most, which is at one of its ends. The layout's own belt.tension holds the
|
|
1584
|
+
// tension with no push; this one, with the push on it, takes that case in and fails past the rated tension, so it
|
|
1585
|
+
// stands in its place)
|
|
1544
1586
|
const push = r.force ?? 0,
|
|
1545
1587
|
W = r.weight ?? 0,
|
|
1546
1588
|
F = push + W,
|
|
@@ -1600,24 +1642,16 @@ function beltFindings(r, f) {
|
|
|
1600
1642
|
refs: { ...refs, ...(carried.length ? { carried } : {}) },
|
|
1601
1643
|
},
|
|
1602
1644
|
);
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
`${r.id}:
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
way = d >= 0 ? "toward the beam's end b" : 'toward the rail',
|
|
1614
|
-
fits = Math.abs(d) <= room + 1e-9;
|
|
1615
|
-
f(
|
|
1616
|
-
fits ? 'pass' : 'fail',
|
|
1617
|
-
`${r.id}: cut ${mm(L.cut)} mm, the belt sets its tension with the idler's block ${mm(Math.abs(d))} mm ${way} from where it is drawn, along the slot, which leaves it ${mm(room)} mm that way${fits ? `: ${mm(room - Math.abs(d))} mm of the tensioner's travel left` : `: the tensioner's travel is used up; cut the belt ${mm(up2(2 * (Math.abs(d) - room), B.pitch))} mm ${d >= 0 ? 'shorter' : 'longer'}, or move the block`}`,
|
|
1618
|
-
fits ? null : `${r.id}: tensioner used up`,
|
|
1619
|
-
{ code: 'belt.tensioner', values: { offset: d, room: L.idler.room, left: r3(room - Math.abs(d)) }, refs },
|
|
1620
|
-
);
|
|
1645
|
+
// (the layout's own checks, as @rulecad/frames gives them: the working run's line and the tensioner's room)
|
|
1646
|
+
for (const code of ['belt.line', 'belt.tensioner']) {
|
|
1647
|
+
const x = L.findings.find((q) => q.code === code),
|
|
1648
|
+
title = { 'belt.line': 'belt off square', 'belt.tensioner': 'tensioner used up' }[code];
|
|
1649
|
+
f(x.level, `${r.id}: ${x.text}`, x.level === 'fail' ? `${r.id}: ${title}` : null, {
|
|
1650
|
+
code,
|
|
1651
|
+
values: x.values,
|
|
1652
|
+
refs,
|
|
1653
|
+
});
|
|
1654
|
+
}
|
|
1621
1655
|
f(
|
|
1622
1656
|
'info',
|
|
1623
1657
|
`${r.id}: the belt holds the carriage on two spans, the pulley's side a and the idler's side b; at its least stiff they act as ${mm(L.span)} mm of belt (a·b/(a+b), the most of it through the travel); at ${n3(B.k, 0)} N a unit of strain (${B.set}'s stiffness, ${B.kKind ?? 'its kind not given'}), ${n3(F, 2)} N${W ? ` (the push and the weight along the travel)` : ''} stretches it ${n3((F * L.span) / B.k, 4)} mm: the belt stretch term of the error budget where a task is given`,
|
package/lib/live-guard.mjs
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
// says it may not be framed.
|
|
16
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
17
|
// 9000:localhost:8090) arrives with another port in its Host.
|
|
18
|
+
// The two servers a picture is taken from (picture-servers.mjs) run the same checks, for the few seconds they are up,
|
|
19
|
+
// and tell a refused request what they are in words of their own (says).
|
|
18
20
|
import net from 'node:net';
|
|
19
21
|
|
|
20
22
|
const LOCAL = ['127.0.0.1', 'localhost', '::1'];
|
|
@@ -33,16 +35,27 @@ export function hostName(host) {
|
|
|
33
35
|
return name.endsWith('.') ? name.slice(0, -1) : name;
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
// What a refused request is told, unless the server gives words of its own (says): a live view's person can ask for
|
|
39
|
+
// another name, and its page has a stop button
|
|
40
|
+
const LIVE_VIEW = {
|
|
41
|
+
host: (given) =>
|
|
42
|
+
`This live view answers only to this computer's own names and addresses, not "${given}". ` +
|
|
43
|
+
'To open it by another name (a Tailscale name, say), give that name with --allow-host.\n',
|
|
44
|
+
post: 'Only the live view itself can stop the run: this request came from another page.\n',
|
|
45
|
+
};
|
|
46
|
+
|
|
36
47
|
/**
|
|
37
48
|
* Makes the check a live view's server runs on each request. It answers a refused request itself (421 for a Host
|
|
38
49
|
* 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
50
|
* in another page's frame.
|
|
40
|
-
* @param {{ host?: string, allowHosts?: string[]
|
|
41
|
-
* names to accept, such as a Tailscale MagicDNS name
|
|
51
|
+
* @param {{ host?: string, allowHosts?: string[], says?: { host: (given: string) => string, post: string } }} [o]
|
|
52
|
+
* host: the address the server listens on; allowHosts: more names to accept, such as a Tailscale MagicDNS name
|
|
53
|
+
* (my-laptop); says: what a refused request is told, for a server that is not a live view (host: for a Host that
|
|
54
|
+
* isn't this computer's, given the Host it named; post: for another page's POST), each ending in a new line
|
|
42
55
|
* @returns {(req: import('node:http').IncomingMessage, res: import('node:http').ServerResponse) => boolean} true when
|
|
43
56
|
* the request may go on
|
|
44
57
|
*/
|
|
45
|
-
export function liveGuard({ host, allowHosts = [] } = {}) {
|
|
58
|
+
export function liveGuard({ host, allowHosts = [], says = LIVE_VIEW } = {}) {
|
|
46
59
|
const names = new Set([...LOCAL, ...[host, ...allowHosts].filter(Boolean).map(hostName)]);
|
|
47
60
|
const accepted = (name) => names.has(name) || net.isIP(name) !== 0;
|
|
48
61
|
return (req, res) => {
|
|
@@ -51,16 +64,13 @@ export function liveGuard({ host, allowHosts = [] } = {}) {
|
|
|
51
64
|
const given = req.headers.host;
|
|
52
65
|
if (!given || !accepted(hostName(given))) {
|
|
53
66
|
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
|
-
);
|
|
67
|
+
res.end(says.host(given ?? ''));
|
|
58
68
|
return false;
|
|
59
69
|
}
|
|
60
70
|
const origin = req.headers.origin;
|
|
61
71
|
if (req.method === 'POST' && origin !== undefined && origin.toLowerCase() !== `http://${given.toLowerCase()}`) {
|
|
62
72
|
res.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' });
|
|
63
|
-
res.end(
|
|
73
|
+
res.end(says.post);
|
|
64
74
|
return false;
|
|
65
75
|
}
|
|
66
76
|
return true;
|
package/lib/measured.mjs
CHANGED
|
@@ -461,7 +461,8 @@ const HOLDS = {
|
|
|
461
461
|
};
|
|
462
462
|
|
|
463
463
|
// The pull bolt on the holds coupon (row 46): an M8 printed bolt whose waist breaks across its layers, so it measures
|
|
464
|
-
// how strongly a plastic holds across its layers
|
|
464
|
+
// how strongly a plastic holds across its layers on the user's printer, where the data sheet's figure comes from test
|
|
465
|
+
// bars printed at 100% infill (or a plastic of their own gives none). It goes in the plastic's set
|
|
465
466
|
// as strength_z, which a printed bolt standing on its head, and every part pulled across its layers, then reads
|
|
466
467
|
function boltRule(project, set, key, data) {
|
|
467
468
|
if (key !== 'strength_z' || data.rules?.density == null || typeof data.rules?.strength?.value !== 'number')
|
package/lib/mesh.mjs
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
// align(moving, fixed) -> the turn and shift that best lays moving over fixed
|
|
8
8
|
// deviation(from, to) -> how far points on one surface are from the other, signed (+ outside, - inside)
|
|
9
9
|
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
10
11
|
import zlib from 'node:zlib';
|
|
12
|
+
import { constants as bufferLimits } from 'node:buffer';
|
|
11
13
|
|
|
12
14
|
export const AXES = ['x', 'y', 'z'];
|
|
13
15
|
export const axisOf = (a) => {
|
|
@@ -21,22 +23,66 @@ export const planeAxes = (axis) => (axis === 2 ? [0, 1] : axis === 1 ? [0, 2] :
|
|
|
21
23
|
// ---- reading ----
|
|
22
24
|
|
|
23
25
|
export function readMesh(file) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
let buf;
|
|
27
|
+
try {
|
|
28
|
+
buf = fs.readFileSync(file);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
// (what Node can't read at once is said as what it is: its own words name neither the file nor a way on)
|
|
31
|
+
const why =
|
|
32
|
+
e.code === 'ERR_FS_FILE_TOO_LARGE'
|
|
33
|
+
? 'more than 2 GB, the most cad reads of a file'
|
|
34
|
+
: e.code === 'EISDIR'
|
|
35
|
+
? 'a folder, not an STL or 3MF file'
|
|
36
|
+
: null;
|
|
37
|
+
if (!why) throw e;
|
|
38
|
+
throw new Error(`${path.basename(file)}: ${why}`, { cause: e });
|
|
39
|
+
}
|
|
40
|
+
// (what is wrong with the file is said with its name, as `cad measure a.3mf b.stl` reads two)
|
|
41
|
+
const named = (read) => {
|
|
42
|
+
try {
|
|
43
|
+
return read(buf);
|
|
44
|
+
} catch (e) {
|
|
45
|
+
throw new Error(`${path.basename(file)}: ${e.message}`, { cause: e });
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
if (/\.3mf$/i.test(file)) return named(read3mf);
|
|
49
|
+
if (/\.stl$/i.test(file)) return named(readStl);
|
|
27
50
|
throw new Error(`${file}: an STL or 3MF, please (export STEP files from a CAD program as STL first)`);
|
|
28
51
|
}
|
|
29
52
|
|
|
30
53
|
// What cad reads from a file it did not make (an STL or 3MF from the internet): past these, the file is refused with
|
|
31
54
|
// the limit named, rather than left to fill memory. Two million triangles is about 660 MB as cad holds them, and more
|
|
32
55
|
// than a detailed print-ready model has; a 3MF that places its objects more than a hundred thousand times is made to
|
|
33
|
-
// hurt, not to print; 512 MB is what its zip may unpack to
|
|
34
|
-
|
|
56
|
+
// hurt, not to print; 512 MB is what its zip may unpack to; 16 is how deep a 3MF's objects may sit inside each other
|
|
57
|
+
// (a deeper one, or one that holds itself, is refused rather than read with its deeper parts left out); 64 MB is the
|
|
58
|
+
// most `cad clone` downloads or reads for a board or one of its parts' models (KiCad's largest are a few MB); text is
|
|
59
|
+
// the longest text Node holds (a little under 512 MB of characters): a 3MF's model file or a text STL past it is
|
|
60
|
+
// refused in cad's words, since Node's own ("Cannot create a string longer than 0x1fffffe8 characters") name nothing.
|
|
61
|
+
export const MESH_LIMITS = {
|
|
62
|
+
triangles: 2_000_000,
|
|
63
|
+
places: 100_000,
|
|
64
|
+
unzipped: 512 * 1024 * 1024,
|
|
65
|
+
depth: 16,
|
|
66
|
+
download: 64 * 1024 * 1024,
|
|
67
|
+
text: bufferLimits.MAX_STRING_LENGTH,
|
|
68
|
+
};
|
|
69
|
+
// A file's bytes as text, refused if they are more than Node holds as one text (what: the file, for the message)
|
|
70
|
+
function asText(buf, what, longest = MESH_LIMITS.text) {
|
|
71
|
+
if (buf.length > longest)
|
|
72
|
+
throw new Error(`${what} is more than ${Math.floor(longest / (1024 * 1024))} MB of text, the most cad reads`);
|
|
73
|
+
return buf.toString('utf8');
|
|
74
|
+
}
|
|
35
75
|
const grouped = (n) => n.toLocaleString('en-GB');
|
|
76
|
+
// A name out of a file from elsewhere, fit to say in a message: a damaged or hostile file can hold anything there, so
|
|
77
|
+
// a character that is not print (one that could move a terminal's cursor, say) is shown as "?", and a long name is cut.
|
|
78
|
+
const sayable = (name) => {
|
|
79
|
+
const s = String(name).replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu, '?');
|
|
80
|
+
return s.length > 80 ? `${s.slice(0, 80)}...` : s;
|
|
81
|
+
};
|
|
36
82
|
const finite = (v) => v.length === 3 && v.every(Number.isFinite);
|
|
37
83
|
|
|
38
|
-
// readStl(buf, { triangles }) -> triangles; refuses a corner that is not three numbers, and a file past
|
|
39
|
-
export function readStl(buf, { triangles: most = MESH_LIMITS.triangles } = {}) {
|
|
84
|
+
// readStl(buf, { triangles, text }) -> triangles; refuses a corner that is not three numbers, and a file past a limit
|
|
85
|
+
export function readStl(buf, { triangles: most = MESH_LIMITS.triangles, text: longest = MESH_LIMITS.text } = {}) {
|
|
40
86
|
const n = buf.length >= 84 ? buf.readUInt32LE(80) : -1;
|
|
41
87
|
if (n >= 0 && 84 + n * 50 === buf.length) {
|
|
42
88
|
// binary
|
|
@@ -53,7 +99,7 @@ export function readStl(buf, { triangles: most = MESH_LIMITS.triangles } = {}) {
|
|
|
53
99
|
return tris;
|
|
54
100
|
}
|
|
55
101
|
const pts = [];
|
|
56
|
-
for (const m of buf
|
|
102
|
+
for (const m of asText(buf, 'the STL', longest).matchAll(/vertex\s+(\S+)\s+(\S+)\s+(\S+)/g)) {
|
|
57
103
|
const p = [+m[1], +m[2], +m[3]];
|
|
58
104
|
if (!finite(p)) {
|
|
59
105
|
const k = pts.length;
|
|
@@ -70,49 +116,16 @@ export function readStl(buf, { triangles: most = MESH_LIMITS.triangles } = {}) {
|
|
|
70
116
|
}
|
|
71
117
|
|
|
72
118
|
// A zip's files by name (stored or deflated), from its central directory. What it unpacks, all entries together, may
|
|
73
|
-
// come to `max` bytes (512 MB): a zip made to unpack far past its size is refused, not unpacked into memory.
|
|
119
|
+
// come to `max` bytes (512 MB): a zip made to unpack far past its size is refused, not unpacked into memory. Every
|
|
120
|
+
// place and size the file gives is held against its length before it is read: one that points past the end is a
|
|
121
|
+
// damaged zip file, said with the entry, not Node's own "out of range".
|
|
74
122
|
export function unzip(buf, { max = MESH_LIMITS.unzipped } = {}) {
|
|
75
|
-
let e = buf.length - 22;
|
|
76
|
-
while (e >= 0 && buf.readUInt32LE(e) !== 0x06054b50) e--;
|
|
77
|
-
if (e < 0) throw new Error('not a zip file');
|
|
78
|
-
let count = buf.readUInt16LE(e + 10),
|
|
79
|
-
p = buf.readUInt32LE(e + 16);
|
|
80
|
-
if (p === 0xffffffff && e >= 20 && buf.readUInt32LE(e - 20) === 0x07064b50) {
|
|
81
|
-
// Zip64: the real numbers are in its own record
|
|
82
|
-
const z = Number(buf.readBigUInt64LE(e - 12));
|
|
83
|
-
count = Number(buf.readBigUInt64LE(z + 32));
|
|
84
|
-
p = Number(buf.readBigUInt64LE(z + 48));
|
|
85
|
-
}
|
|
86
123
|
const files = {},
|
|
87
124
|
tooBig = () => new Error(`the zip unpacks to more than ${max / (1024 * 1024)} MB, the most cad reads`);
|
|
88
|
-
let
|
|
125
|
+
let [count, p] = zipList(buf),
|
|
126
|
+
left = max;
|
|
89
127
|
for (let i = 0; i < count; i++) {
|
|
90
|
-
|
|
91
|
-
const method = buf.readUInt16LE(p + 10),
|
|
92
|
-
nameLen = buf.readUInt16LE(p + 28);
|
|
93
|
-
const extraLen = buf.readUInt16LE(p + 30),
|
|
94
|
-
noteLen = buf.readUInt16LE(p + 32);
|
|
95
|
-
let local = buf.readUInt32LE(p + 42),
|
|
96
|
-
csize = buf.readUInt32LE(p + 20);
|
|
97
|
-
const name = buf.toString('utf8', p + 46, p + 46 + nameLen);
|
|
98
|
-
// Zip64: sizes and offset in the extra field when the 32-bit ones are 0xffffffff
|
|
99
|
-
const ext = buf.subarray(p + 46 + nameLen, p + 46 + nameLen + extraLen);
|
|
100
|
-
for (let q = 0; q + 4 <= ext.length;) {
|
|
101
|
-
const id = ext.readUInt16LE(q),
|
|
102
|
-
len = ext.readUInt16LE(q + 2);
|
|
103
|
-
if (id === 1) {
|
|
104
|
-
let r = q + 4;
|
|
105
|
-
if (buf.readUInt32LE(p + 24) === 0xffffffff) r += 8; // uncompressed size
|
|
106
|
-
if (buf.readUInt32LE(p + 20) === 0xffffffff) {
|
|
107
|
-
csize = Number(ext.readBigUInt64LE(r));
|
|
108
|
-
r += 8;
|
|
109
|
-
}
|
|
110
|
-
if (local === 0xffffffff) local = Number(ext.readBigUInt64LE(r));
|
|
111
|
-
}
|
|
112
|
-
q += 4 + len;
|
|
113
|
-
}
|
|
114
|
-
const start = local + 30 + buf.readUInt16LE(local + 26) + buf.readUInt16LE(local + 28);
|
|
115
|
-
const data = buf.subarray(start, start + csize);
|
|
128
|
+
const { name, entry, method, data, next } = zipEntry(buf, p, i);
|
|
116
129
|
let file = null;
|
|
117
130
|
if (method === 8)
|
|
118
131
|
try {
|
|
@@ -120,26 +133,103 @@ export function unzip(buf, { max = MESH_LIMITS.unzipped } = {}) {
|
|
|
120
133
|
file = zlib.inflateRawSync(data, { maxOutputLength: left + 1 });
|
|
121
134
|
} catch (err) {
|
|
122
135
|
if (err.code === 'ERR_BUFFER_TOO_LARGE') throw tooBig();
|
|
136
|
+
if (/^Z_/.test(err.code || '')) throw damagedZip(`${entry} doesn't unpack (${err.message})`);
|
|
123
137
|
throw err;
|
|
124
138
|
}
|
|
125
139
|
else if (method === 0) file = data;
|
|
126
140
|
if (file && (left -= file.length) < 0) throw tooBig();
|
|
127
141
|
files[name] = file;
|
|
128
|
-
p
|
|
142
|
+
p = next;
|
|
129
143
|
}
|
|
130
144
|
return files;
|
|
131
145
|
}
|
|
146
|
+
const damagedZip = (what) => new Error(`a damaged zip file: ${what}`);
|
|
147
|
+
// Whether the `len` bytes at `at` are all in the file (a place past what a number holds exactly is not)
|
|
148
|
+
const inZip = (buf, at, len) => Number.isSafeInteger(at) && at >= 0 && at + len <= buf.length;
|
|
149
|
+
|
|
150
|
+
// How many files a zip lists and where its list (the central directory) starts: [count, place], from the record at
|
|
151
|
+
// the zip's end, or from the Zip64 record before it when the file is written with 64-bit numbers
|
|
152
|
+
function zipList(buf) {
|
|
153
|
+
let e = buf.length - 22;
|
|
154
|
+
while (e >= 0 && buf.readUInt32LE(e) !== 0x06054b50) e--;
|
|
155
|
+
if (e < 0) throw new Error('not a zip file');
|
|
156
|
+
let count = buf.readUInt16LE(e + 10),
|
|
157
|
+
p = buf.readUInt32LE(e + 16);
|
|
158
|
+
if (p === 0xffffffff && e >= 20 && buf.readUInt32LE(e - 20) === 0x07064b50) {
|
|
159
|
+
// Zip64: the real numbers are in its own record
|
|
160
|
+
const z = Number(buf.readBigUInt64LE(e - 12));
|
|
161
|
+
if (!inZip(buf, z, 56)) throw damagedZip('its record of large sizes is past its end');
|
|
162
|
+
count = Number(buf.readBigUInt64LE(z + 32));
|
|
163
|
+
p = Number(buf.readBigUInt64LE(z + 48));
|
|
164
|
+
}
|
|
165
|
+
if (count && !inZip(buf, p, 46)) throw damagedZip('its list of files starts past its end');
|
|
166
|
+
return [count, p];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Entry i of a zip's list of files, at p: { name, entry (as a message names it), method (0 stored, 8 deflated), data
|
|
170
|
+
// (its bytes as packed), next (where the list's next entry is) }, each place and size checked before it is read
|
|
171
|
+
function zipEntry(buf, p, i) {
|
|
172
|
+
if (!inZip(buf, p, 46)) throw damagedZip(`its list of files runs past its end at entry ${i + 1}`);
|
|
173
|
+
if (buf.readUInt32LE(p) !== 0x02014b50) throw damagedZip(`entry ${i + 1} of its list of files is not an entry`);
|
|
174
|
+
const nameLen = buf.readUInt16LE(p + 28),
|
|
175
|
+
extraLen = buf.readUInt16LE(p + 30),
|
|
176
|
+
noteLen = buf.readUInt16LE(p + 32);
|
|
177
|
+
if (!inZip(buf, p + 46, nameLen + extraLen + noteLen))
|
|
178
|
+
throw damagedZip(`its list of files runs past its end at entry ${i + 1}`);
|
|
179
|
+
const name = buf.toString('utf8', p + 46, p + 46 + nameLen),
|
|
180
|
+
entry = `entry ${i + 1} (${sayable(name)})`;
|
|
181
|
+
let local = buf.readUInt32LE(p + 42),
|
|
182
|
+
csize = buf.readUInt32LE(p + 20);
|
|
183
|
+
// Zip64: sizes and offset in the extra field when the 32-bit ones are 0xffffffff
|
|
184
|
+
const ext = buf.subarray(p + 46 + nameLen, p + 46 + nameLen + extraLen);
|
|
185
|
+
for (let q = 0; q + 4 <= ext.length;) {
|
|
186
|
+
const id = ext.readUInt16LE(q),
|
|
187
|
+
len = ext.readUInt16LE(q + 2);
|
|
188
|
+
if (id === 1) {
|
|
189
|
+
let r = q + 4;
|
|
190
|
+
const big = () => {
|
|
191
|
+
if (r + 8 > Math.min(ext.length, q + 4 + len))
|
|
192
|
+
throw damagedZip(`${entry} lacks the large sizes it says it has`);
|
|
193
|
+
return Number(ext.readBigUInt64LE(r));
|
|
194
|
+
};
|
|
195
|
+
if (buf.readUInt32LE(p + 24) === 0xffffffff) r += 8; // uncompressed size
|
|
196
|
+
if (buf.readUInt32LE(p + 20) === 0xffffffff) {
|
|
197
|
+
csize = big();
|
|
198
|
+
r += 8;
|
|
199
|
+
}
|
|
200
|
+
if (local === 0xffffffff) local = big();
|
|
201
|
+
}
|
|
202
|
+
q += 4 + len;
|
|
203
|
+
}
|
|
204
|
+
if (!inZip(buf, local, 30)) throw damagedZip(`${entry} starts past the end of the file`);
|
|
205
|
+
if (buf.readUInt32LE(local) !== 0x04034b50) throw damagedZip(`${entry} is not where its list of files says`);
|
|
206
|
+
const start = local + 30 + buf.readUInt16LE(local + 26) + buf.readUInt16LE(local + 28);
|
|
207
|
+
if (!inZip(buf, start, csize)) throw damagedZip(`${entry} runs past the end of the file`);
|
|
208
|
+
return {
|
|
209
|
+
name,
|
|
210
|
+
entry,
|
|
211
|
+
method: buf.readUInt16LE(p + 10),
|
|
212
|
+
data: buf.subarray(start, start + csize),
|
|
213
|
+
next: p + 46 + nameLen + extraLen + noteLen,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
132
216
|
|
|
133
217
|
// A 3MF: every build item, with its transform, and the components it is made of (in this model file or another).
|
|
134
|
-
// limits: { triangles, places, max } (MESH_LIMITS): what it places is counted first, and a file past a limit is
|
|
135
|
-
// before anything is placed, as is a corner that is not three numbers
|
|
218
|
+
// limits: { triangles, places, depth, max } (MESH_LIMITS): what it places is counted first, and a file past a limit is
|
|
219
|
+
// refused before anything is placed, as is a corner that is not three numbers, a triangle naming a corner its object
|
|
220
|
+
// hasn't, an object more than `depth` (16) levels deep inside others, and an object that holds itself.
|
|
136
221
|
export function read3mf(buf, limits = {}) {
|
|
137
|
-
const {
|
|
222
|
+
const {
|
|
223
|
+
triangles: most = MESH_LIMITS.triangles,
|
|
224
|
+
places: follows = MESH_LIMITS.places,
|
|
225
|
+
depth: deepest = MESH_LIMITS.depth,
|
|
226
|
+
text: longest = MESH_LIMITS.text,
|
|
227
|
+
} = limits;
|
|
138
228
|
const files = unzip(buf, limits);
|
|
139
229
|
const models = Object.fromEntries(
|
|
140
230
|
Object.entries(files)
|
|
141
231
|
.filter(([n, d]) => d && /\.model$/i.test(n))
|
|
142
|
-
.map(([n, d]) => ['/' + n.replace(/^\//, ''), d
|
|
232
|
+
.map(([n, d]) => ['/' + n.replace(/^\//, ''), asText(d, `the 3MF's model file ${sayable(n)}`, longest)]),
|
|
143
233
|
);
|
|
144
234
|
const root = Object.keys(models).find((n) => /^\/3D\/3dmodel\.model$/i.test(n)) || Object.keys(models)[0];
|
|
145
235
|
if (!root) throw new Error('no 3D model in the 3MF');
|
|
@@ -158,19 +248,29 @@ export function read3mf(buf, limits = {}) {
|
|
|
158
248
|
: Object.keys(objects)
|
|
159
249
|
.filter((k) => k.startsWith(root))
|
|
160
250
|
.map((ref) => ({ ref }));
|
|
161
|
-
// How many triangles and placings each object brings, with all it holds (by depth
|
|
162
|
-
|
|
251
|
+
// How many triangles and placings each object brings, with all it holds (by depth). An object deeper than cad follows
|
|
252
|
+
// is refused, not left out: the model would look whole and not be; so is one that holds itself.
|
|
253
|
+
const memo = new Map(),
|
|
254
|
+
holding = new Set(),
|
|
255
|
+
named = (key) => `object ${sayable(key.split('#')[1])} (${sayable(key.split('#')[0].replace(/^\//, ''))})`;
|
|
163
256
|
const tally = (key, depth) => {
|
|
164
257
|
const o = objects[key];
|
|
165
|
-
if (!o
|
|
258
|
+
if (!o) return [0, 0];
|
|
259
|
+
if (holding.has(key)) throw new Error(`the 3MF's ${named(key)} holds itself, so it has no end`);
|
|
260
|
+
if (depth > deepest)
|
|
261
|
+
throw new Error(
|
|
262
|
+
`the 3MF holds ${named(key)} more than ${deepest} levels deep inside others, the most cad follows`,
|
|
263
|
+
);
|
|
166
264
|
const k = `${key}@${depth}`;
|
|
167
265
|
if (!memo.has(k)) {
|
|
168
266
|
const sum = [o.tris.length, 1];
|
|
267
|
+
holding.add(key);
|
|
169
268
|
for (const c of o.components) {
|
|
170
269
|
const [t, n] = tally(c.ref, depth + 1);
|
|
171
270
|
sum[0] += t;
|
|
172
271
|
sum[1] += n;
|
|
173
272
|
}
|
|
273
|
+
holding.delete(key);
|
|
174
274
|
memo.set(k, sum);
|
|
175
275
|
}
|
|
176
276
|
return memo.get(k);
|
|
@@ -187,7 +287,7 @@ export function read3mf(buf, limits = {}) {
|
|
|
187
287
|
const out = [];
|
|
188
288
|
const place = (key, M, depth = 0) => {
|
|
189
289
|
const o = objects[key];
|
|
190
|
-
if (!o || depth >
|
|
290
|
+
if (!o || depth > deepest) return; // (refused above)
|
|
191
291
|
for (const t of o.tris) {
|
|
192
292
|
const placed = t.map((v) => apply3mf(M, v));
|
|
193
293
|
// (finite corners and finite transforms can still multiply past what a number holds: 1e200 placed at 1e200)
|
package/lib/motors.mjs
CHANGED
|
@@ -172,7 +172,12 @@ function slideDrive(spec, what, j, num) {
|
|
|
172
172
|
export const shaftFits = (drives, joints, ids) =>
|
|
173
173
|
drives
|
|
174
174
|
.filter((d) => d.joint && d.model.shaft.length)
|
|
175
|
-
.map((d) => ({
|
|
175
|
+
.map((d) => ({
|
|
176
|
+
a: `${d.id}.shaft`,
|
|
177
|
+
b: ids[joints.find((j) => j.id === d.joint).child],
|
|
178
|
+
overlap: 'allowed',
|
|
179
|
+
silent: true,
|
|
180
|
+
}));
|
|
176
181
|
|
|
177
182
|
// "loads": [{ "joint": "elbow", "torque": 0.5 }, { "joint": "slide", "force": 12 }, { "mass": 0.2, "at": "forearm.tip" }]
|
|
178
183
|
export function loadsOf(a, anchor, num, joints) {
|
|
@@ -291,14 +296,19 @@ export const mountTorque = (d) =>
|
|
|
291
296
|
);
|
|
292
297
|
|
|
293
298
|
// A motor's speed if the assembly doesn't say: its corner speed, its rated speed, or half its free speed (a stepper's
|
|
294
|
-
// at its driver's supply, volts: the assembly's, else the package's default)
|
|
299
|
+
// at its driver's supply, volts: the assembly's, else the package's default). A geared stepper works at the step rate
|
|
300
|
+
// its pull-in torque is measured at, where that torque is its own (past it @rulecad/motors lets the torque fall)
|
|
295
301
|
export function workingSpeed(model, spec, supply) {
|
|
296
302
|
if (spec.speed != null) return spec.speed;
|
|
297
303
|
const p = performance(model, { supply });
|
|
304
|
+
const tested = model.figures.pull_in_rate?.value,
|
|
305
|
+
starts = model.figures.step_rate?.value;
|
|
298
306
|
return model.type === 'dc'
|
|
299
307
|
? p.limit / 2
|
|
300
308
|
: model.figures.pull_in_torque
|
|
301
|
-
?
|
|
309
|
+
? tested && starts && tested < starts
|
|
310
|
+
? (p.limit * tested) / starts
|
|
311
|
+
: p.limit
|
|
302
312
|
: model.type === 'servo'
|
|
303
313
|
? p.limit
|
|
304
314
|
: model.type === 'brushless'
|
|
@@ -751,7 +761,10 @@ function mountCheck(s, d, f, W, avail, materialName, frame) {
|
|
|
751
761
|
each = Math.max(...screwGroup(m.mount.holes, { moment: [0, 0, avail] }).screws.map((x) => x.shear)),
|
|
752
762
|
bearing = each / (hd * t),
|
|
753
763
|
margin = mat.strength / bearing;
|
|
754
|
-
|
|
764
|
+
// mount.printable is left out: it is about the solid mount() cuts for a pose, and neither is known here (the part
|
|
765
|
+
// may be drawn for the other pose, or by hand), so it would say what is not true of this part. How the part prints
|
|
766
|
+
// is read from its own mesh, by the print checks
|
|
767
|
+
for (const x of here.findings.filter((y) => y.code !== 'mount.printable'))
|
|
755
768
|
f(x.level, `${d.id} on ${other}: ${x.text}`, x.level === 'pass' ? null : `${d.id}: ${x.code.split('.')[1]}`, {
|
|
756
769
|
code: 'motor.mount',
|
|
757
770
|
...(x.values ? { values: x.values } : {}),
|