@rulecad/cad 0.7.97 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +26 -10
- package/CHANGELOG.md +442 -2
- package/EXAMPLES.md +48 -42
- package/README.md +226 -73
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2253 -514
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1854 -437
- 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/carried.mjs +201 -0
- package/lib/clone.mjs +194 -42
- package/lib/design.mjs +734 -113
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +900 -191
- package/lib/errors.mjs +16 -3
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2710 -502
- package/lib/fittings.mjs +715 -121
- package/lib/fixings.mjs +21 -9
- package/lib/framebuild.mjs +792 -176
- package/lib/framecheck.mjs +1901 -368
- package/lib/framegen.mjs +100 -22
- package/lib/frameheat.mjs +225 -42
- package/lib/frameprint.mjs +513 -108
- package/lib/frames.mjs +81 -17
- package/lib/framesize.mjs +792 -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 +149 -18
- 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 +438 -68
- package/lib/layout.mjs +3 -1
- package/lib/links.mjs +149 -42
- package/lib/list.mjs +326 -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 +579 -130
- package/lib/machine.mjs +372 -74
- package/lib/magnets.mjs +162 -44
- package/lib/measured.mjs +417 -94
- package/lib/mesh.mjs +526 -119
- package/lib/meshcmd.mjs +215 -60
- package/lib/motors.mjs +1022 -212
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +41 -0
- package/lib/optimise.mjs +979 -283
- package/lib/order.mjs +519 -112
- package/lib/panels.mjs +482 -94
- package/lib/parts.mjs +78 -23
- package/lib/pins.mjs +163 -44
- package/lib/plates.mjs +224 -36
- 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 +38 -4
- package/lib/sandbox-hooks.mjs +154 -12
- package/lib/sandbox-preload.mjs +2 -2
- package/lib/sandbox-run.mjs +127 -12
- package/lib/sandbox.mjs +632 -35
- package/lib/schema.mjs +43 -11
- package/lib/seats.mjs +227 -49
- package/lib/services.mjs +261 -52
- package/lib/sheet.mjs +851 -178
- package/lib/slotnuts.mjs +213 -34
- package/lib/targets/gist.mjs +4 -1
- package/lib/targets.mjs +42 -6
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +113 -28
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +216 -17
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
- package/schemas/object.schema.json +1 -1
- package/schemas/project.schema.json +1 -1
package/lib/magnets.mjs
CHANGED
|
@@ -9,8 +9,12 @@ import { profile, holesIn, fixingsOf } from './fasten.mjs';
|
|
|
9
9
|
|
|
10
10
|
const { vec3 } = jscad.maths;
|
|
11
11
|
|
|
12
|
-
const mm = (n) => String(Number(Number(n).toFixed(3))),
|
|
13
|
-
|
|
12
|
+
const mm = (n) => String(Number(Number(n).toFixed(3))),
|
|
13
|
+
r3 = (x) => +x.toFixed(3);
|
|
14
|
+
const unit = (v) => {
|
|
15
|
+
const l = Math.hypot(...v);
|
|
16
|
+
return v.map((c) => c / l);
|
|
17
|
+
};
|
|
14
18
|
const top = (n) => String(n).split(/[.@]/)[0];
|
|
15
19
|
/** How far a pocket as drawn may be from one the magnet's fits draw and still be taken for it, mm across. */
|
|
16
20
|
const TOL = 0.06;
|
|
@@ -26,15 +30,34 @@ const FITS = ['press', 'glue', 'ribs'];
|
|
|
26
30
|
* @returns {object} the fit, with magnet and a fastener's name
|
|
27
31
|
*/
|
|
28
32
|
export function magnetOfFit(x, F) {
|
|
29
|
-
const p = x.fastener,
|
|
30
|
-
|
|
33
|
+
const p = x.fastener,
|
|
34
|
+
label = x.note || x.label || `${x.a} / ${x.b}`;
|
|
35
|
+
if (typeof p.magnet !== 'string' || !p.magnet.trim())
|
|
36
|
+
throw new Error(
|
|
37
|
+
`fit ${label}: fastener as an object names magnets: { "magnet": "disc magnet 8x3", "count": 2 } (got ${JSON.stringify(p)})`,
|
|
38
|
+
);
|
|
31
39
|
const stray = Object.keys(p).filter((k) => !MAGNET_KEYS.includes(k));
|
|
32
|
-
if (stray.length)
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
if (stray.length)
|
|
41
|
+
throw new Error(
|
|
42
|
+
`fit ${label}: magnets have no "${stray.join('", "')}": a fastener naming magnets takes ${MAGNET_KEYS.join(', ')} (the assembly schema's magnets)`,
|
|
43
|
+
);
|
|
44
|
+
if (x.count != null && p.count != null)
|
|
45
|
+
throw new Error(`fit ${label}: give the magnets' count once, in the fastener ({ "magnet": ..., "count": 2 })`);
|
|
46
|
+
if (p.fit != null && !FITS.includes(p.fit))
|
|
47
|
+
throw new Error(`fit ${label}: a magnet's fit is ${FITS.join(', ')} (got ${JSON.stringify(p.fit)})`);
|
|
35
48
|
let name;
|
|
36
|
-
try {
|
|
37
|
-
|
|
49
|
+
try {
|
|
50
|
+
name = F.magnetModel(p.magnet.trim()).name;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
throw new Error(`fit ${label}: ${e.message.replace(/^magnetModel: /, '')}`);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
fastens: true,
|
|
56
|
+
...x,
|
|
57
|
+
magnet: { ...p, magnet: p.magnet.trim() },
|
|
58
|
+
fastener: name,
|
|
59
|
+
...(p.count != null || x.count != null ? { count: p.count ?? x.count } : {}),
|
|
60
|
+
};
|
|
38
61
|
}
|
|
39
62
|
|
|
40
63
|
/**
|
|
@@ -42,52 +65,147 @@ export function magnetOfFit(x, F) {
|
|
|
42
65
|
* @param {object} x the fit, with magnet (magnetOfFit) @param {object} s the built assembly @param {Function} f the findings' sink, as checkAssembly's
|
|
43
66
|
*/
|
|
44
67
|
export function magnetFit(x, s, f) {
|
|
45
|
-
const label = x.note || x.label || `${x.a} to ${x.b}`,
|
|
46
|
-
|
|
68
|
+
const label = x.note || x.label || `${x.a} to ${x.b}`,
|
|
69
|
+
code = 'fit.magnet',
|
|
70
|
+
p = x.magnet,
|
|
71
|
+
refs = { a: x.a, b: x.b, magnet: x.fastener };
|
|
72
|
+
const fail = (text, short, more = {}) =>
|
|
73
|
+
f('fail', `${label}: ${text}`, short ?? `${label}: magnets`, { code, refs, ...more });
|
|
47
74
|
const { F, material } = fixingsOf(s.project);
|
|
48
|
-
const count = x.count != null ? x.num(x.count) : 1,
|
|
49
|
-
|
|
75
|
+
const count = x.count != null ? x.num(x.count) : 1,
|
|
76
|
+
load = p.load != null ? x.num(p.load) : null,
|
|
77
|
+
gap = p.gap != null ? x.num(p.gap) : 0;
|
|
78
|
+
if (!(Number.isInteger(count) && count >= 1))
|
|
79
|
+
return fail(`count is how many magnets, a whole number from 1 (got ${count})`);
|
|
50
80
|
let seats;
|
|
51
|
-
try {
|
|
81
|
+
try {
|
|
82
|
+
seats = (p.fit ? [p.fit] : FITS).map((fit) =>
|
|
83
|
+
F.magnetSeatModel(p.magnet, { fit, material, gap, count, ...(load != null ? { load } : {}) }),
|
|
84
|
+
);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
return fail(e.message.replace(/^magnet\w*: /, ''));
|
|
87
|
+
}
|
|
52
88
|
const m = seats[0].magnet;
|
|
53
|
-
if (m.shape !== 'disc')
|
|
89
|
+
if (m.shape !== 'disc')
|
|
90
|
+
return fail(
|
|
91
|
+
`${m.name}: a block magnet's pocket is not round, and only round pockets are found in the parts here; check its pocket with v.fixings.magnetSeatModel`,
|
|
92
|
+
`${label}: block magnet`,
|
|
93
|
+
);
|
|
54
94
|
const named = (q, n) => q.name === n || q.leaf === n || q.item === n || q.name.startsWith(`${n}.`);
|
|
55
95
|
const sides = [x.a, x.b].map((n) => ({ name: n, parts: s.parts.filter((q) => !q.hardware && named(q, top(n))) }));
|
|
56
96
|
const missing = sides.find((sd) => !sd.parts.length);
|
|
57
97
|
if (missing) return fail(`no part "${missing.name}"`, `${label}: no part`);
|
|
58
|
-
const R = m.diameter / 2 + 2,
|
|
98
|
+
const R = m.diameter / 2 + 2,
|
|
99
|
+
wants = seats.map((st) => ({ fit: st.fit, d: st.pocket.diameter, rules: st.pocket.rules }));
|
|
59
100
|
// The pockets: round holes about a fit's pocket, measured along their axis: the hole open at one end, its floor at the other
|
|
60
|
-
const found = [],
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
101
|
+
const found = [],
|
|
102
|
+
odd = [];
|
|
103
|
+
for (const sd of sides)
|
|
104
|
+
for (const q of sd.parts)
|
|
105
|
+
for (const h of holesIn(q)) {
|
|
106
|
+
const want = wants.find((w) => Math.abs(h.d - w.d) <= TOL),
|
|
107
|
+
near = !want && Math.abs(h.d - m.diameter) <= 0.6;
|
|
108
|
+
if (!want && !near) continue;
|
|
109
|
+
const O = q.m ? vec3.transform([0, 0, 0], h.at, q.m) : h.at,
|
|
110
|
+
e = q.m
|
|
111
|
+
? vec3.transform(
|
|
112
|
+
[0, 0, 0],
|
|
113
|
+
h.at.map((c, i) => c + h.axis[i]),
|
|
114
|
+
q.m,
|
|
115
|
+
)
|
|
116
|
+
: h.at,
|
|
117
|
+
D = unit(e.map((c, i) => c - O[i]));
|
|
118
|
+
const segs = profile(q.triangles, O, D, R).segs.filter((g) => g.solid || g.hole);
|
|
119
|
+
const holes = segs.filter((g) => g.hole);
|
|
120
|
+
if (!holes.length) continue;
|
|
121
|
+
// the pocket's run: its hole segments side by side (a lead-in's cone is one of its own), and the floor past one end
|
|
122
|
+
const hole = holes.reduce((a, b) => (b.z1 - b.z0 > a.z1 - a.z0 ? b : a));
|
|
123
|
+
let i0 = segs.indexOf(hole),
|
|
124
|
+
i1 = i0;
|
|
125
|
+
while (segs[i0 - 1]?.hole && Math.abs(segs[i0 - 1].z1 - segs[i0].z0) < 1e-4) i0--;
|
|
126
|
+
while (segs[i1 + 1]?.hole && Math.abs(segs[i1 + 1].z0 - segs[i1].z1) < 1e-4) i1++;
|
|
127
|
+
const before = segs[i0 - 1],
|
|
128
|
+
after = segs[i1 + 1],
|
|
129
|
+
run = { z0: segs[i0].z0, z1: segs[i1].z1 };
|
|
130
|
+
const floorSeg = before?.solid && !after ? before : after?.solid && !before ? after : null; // (blind: material on one side only)
|
|
131
|
+
const where = O.map(r3);
|
|
132
|
+
if (near) {
|
|
133
|
+
odd.push({ part: sd.name, d: r3(h.d), at: where });
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
found.push({
|
|
137
|
+
part: sd.name,
|
|
138
|
+
fit: want.fit,
|
|
139
|
+
rules: want.rules,
|
|
140
|
+
d: r3(h.d),
|
|
141
|
+
depth: r3(run.z1 - run.z0),
|
|
142
|
+
floor: floorSeg ? r3(floorSeg.z1 - floorSeg.z0) : null,
|
|
143
|
+
at: where,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
for (const o of odd)
|
|
147
|
+
fail(
|
|
148
|
+
`${o.part}'s pocket at ${o.at.map(mm).join(', ')} is drawn ${mm(o.d)} mm, and ${m.name} wants ${wants.map((w) => `${mm(w.d)} mm ${w.fit === 'ribs' ? 'with ribs' : `for a ${w.fit} fit`} (${w.rules.join(', ')})`).join(' or ')}: ${o.d < Math.min(...wants.map((w) => w.d)) ? 'too small, it will not go in' : 'too big, it will be loose'}`,
|
|
149
|
+
`${label}: pocket ${o.d < Math.min(...wants.map((w) => w.d)) ? 'small' : 'big'} in ${o.part}`,
|
|
150
|
+
{ at: o.at, values: { part: o.part, drawn: o.d } },
|
|
151
|
+
);
|
|
80
152
|
if (odd.length) return;
|
|
81
|
-
if (found.length < count)
|
|
153
|
+
if (found.length < count)
|
|
154
|
+
return fail(
|
|
155
|
+
`${count} × ${m.name} wanted, and ${found.length ? `only ${found.length} pocket${found.length > 1 ? 's' : ''} about its ${wants.map((w) => mm(w.d)).join(' or ')} mm ${found.length > 1 ? 'are' : 'is'}` : 'no pocket about its size is'} drawn in ${x.a} or ${x.b}: draw them with v.fixings.magnetSeat`,
|
|
156
|
+
`${label}: pockets missing`,
|
|
157
|
+
{ values: { count, found: found.length } },
|
|
158
|
+
);
|
|
82
159
|
let bad = 0;
|
|
83
160
|
for (const k of found) {
|
|
84
|
-
if (k.depth < m.thickness - TOL) {
|
|
85
|
-
|
|
86
|
-
|
|
161
|
+
if (k.depth < m.thickness - TOL) {
|
|
162
|
+
bad++;
|
|
163
|
+
fail(
|
|
164
|
+
`${k.part}'s pocket at ${k.at.map(mm).join(', ')} is ${mm(k.depth)} mm deep, and ${m.name} is ${mm(m.thickness)} mm thick: it stands ${mm(r3(m.thickness - k.depth))} mm proud of the face`,
|
|
165
|
+
`${label}: pocket shallow in ${k.part}`,
|
|
166
|
+
{ at: k.at, values: { depth: k.depth, thickness: m.thickness } },
|
|
167
|
+
);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (k.floor != null) {
|
|
171
|
+
try {
|
|
172
|
+
// (the floor held to fixings.magnet_floor by the seat's own refusal)
|
|
173
|
+
F.magnetSeatModel(p.magnet, {
|
|
174
|
+
fit: k.fit,
|
|
175
|
+
material,
|
|
176
|
+
thickness: r3(k.depth + k.floor),
|
|
177
|
+
recess: Math.max(0, r3(k.depth - m.thickness)),
|
|
178
|
+
});
|
|
179
|
+
} catch (e) {
|
|
180
|
+
bad++;
|
|
181
|
+
fail(
|
|
182
|
+
`${k.part}'s pocket at ${k.at.map(mm).join(', ')}: ${e.message.replace(/^magnetSeatModel: /, '')}`,
|
|
183
|
+
`${label}: floor thin in ${k.part}`,
|
|
184
|
+
{ at: k.at, values: { floor: k.floor } },
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
87
188
|
}
|
|
88
189
|
if (bad) return;
|
|
89
|
-
const fits = [...new Set(found.map((k) => k.fit))],
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
f(
|
|
190
|
+
const fits = [...new Set(found.map((k) => k.fit))],
|
|
191
|
+
seat = seats.find((st) => st.fit === fits[0]),
|
|
192
|
+
parts = [...new Set(found.map((k) => k.part))];
|
|
193
|
+
f(
|
|
194
|
+
'pass',
|
|
195
|
+
`${label}: ${count} × ${m.name} in ${fits.join(' and ')} pockets drawn ${[...new Set(found.map((k) => mm(k.d)))].join(' and ')} mm (${seat.pocket.rules.join(', ')}) in ${parts.join(' and ')}, each deep enough for the magnet`,
|
|
196
|
+
null,
|
|
197
|
+
{ code, refs, values: { count, pocket: seat.pocket.diameter, grams: m.grams } },
|
|
198
|
+
);
|
|
199
|
+
const pull = seat.findings.find((g) => g.code === 'magnet.pull'),
|
|
200
|
+
hold = seat.findings.find((g) => g.code === 'magnet.hold');
|
|
201
|
+
f(
|
|
202
|
+
hold ? hold.level : 'info',
|
|
203
|
+
`${label}: ${pull.text}${hold ? `. ${hold.text}` : `. Together ${count} pull ${mm(seat.magnet.total)} N: give load to have it checked`}`,
|
|
204
|
+
hold && hold.level !== 'pass' ? `${label}: magnets weak` : null,
|
|
205
|
+
{
|
|
206
|
+
code: 'fit.magnet.load',
|
|
207
|
+
refs,
|
|
208
|
+
values: { pull: seat.magnet.pull, total: seat.magnet.total, ...(hold ? hold.values : {}) },
|
|
209
|
+
},
|
|
210
|
+
);
|
|
93
211
|
}
|