@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/measured.mjs
CHANGED
|
@@ -11,24 +11,182 @@
|
|
|
11
11
|
// to its plastic's set (beam_modulus, beam_strength; _z standing up). The holds coupon's fits (a snap pin's socket, a
|
|
12
12
|
// plain pin's press and slip, a magnet's press, glue and crush ribs, and the snap pins' barb as a record) need no --rule
|
|
13
13
|
// either, and each is refused outside what a fit of its kind can be; its pull bolt writes the plastic's strength_z.
|
|
14
|
+
// A rule measured on a coupon needs no assembly: written the same, its row goes in the workspace's own ledger, with
|
|
15
|
+
// nothing said to have moved. The coupons come into a maker's workspace with cad coupons (packages/cad/templates/
|
|
16
|
+
// coupons), and each source names the coupon's MEASURE.md there. exportMeasured gives what a workspace measured that
|
|
17
|
+
// the kit lacks, as a diff of the kit's rule sets, to send upstream.
|
|
14
18
|
import fs from 'node:fs';
|
|
15
19
|
import path from 'node:path';
|
|
16
20
|
import { setAt } from './jsonedit.mjs';
|
|
17
21
|
import { CadError } from './errors.mjs';
|
|
18
22
|
import { paths, WS, RULE_DIRS, printerOf } from './workspace.mjs';
|
|
19
|
-
import {
|
|
23
|
+
import { peekRuleSet, insideWorkspace } from './rules.mjs';
|
|
24
|
+
import { readOwnText } from './host.mjs';
|
|
20
25
|
import { buildAssembly, checkAssembly, assemblyPath } from './assembly.mjs';
|
|
21
26
|
import { slotModel, JOIN_SET, joinRuleSet } from '@rulecad/frames';
|
|
22
27
|
import { fixingsContext } from './design.mjs';
|
|
23
28
|
import { fixingsFor, threads } from '@rulecad/fixings';
|
|
24
29
|
import { couponFigure } from './frameprint.mjs';
|
|
30
|
+
import { springFor } from './joins.mjs';
|
|
25
31
|
|
|
26
|
-
const today = () => new Date().toISOString().slice(0, 10),
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
const today = () => new Date().toISOString().slice(0, 10),
|
|
33
|
+
n = (x) => String(Number(Number(x).toFixed(4)));
|
|
34
|
+
/**
|
|
35
|
+
* The ledger a measurement is kept in: its project's, beside its assemblies, or with no project (a figure measured on
|
|
36
|
+
* a coupon and written back without an assembly) the workspace's own, ledger.jsonl beside cad.config.json.
|
|
37
|
+
* @param {string | null} project
|
|
38
|
+
*/
|
|
39
|
+
export const ledgerFile = (project) =>
|
|
40
|
+
project ? path.join(paths.project(project), 'ledger.jsonl') : path.join(WS, 'ledger.jsonl');
|
|
41
|
+
// a project's folder, or with no project the workspace (whose printer is cad.config.json's, and which has no plastic)
|
|
42
|
+
const projectDir = (project) => (project ? paths.project(project) : WS);
|
|
43
|
+
|
|
44
|
+
// The coupons that settle a figure, each as a source names it: where it is in this workspace (the MEASURE.md of the
|
|
45
|
+
// project that holds it: the project measured first, then the one cad coupons adds, then any other), so a rule set
|
|
46
|
+
// names a file the maker has, never a path in the kit's repository. One that no project here holds is said by how to
|
|
47
|
+
// get it: cad coupons for the three it adds, the kit's bench for the rig and the beam coupon (the beam coupon is in the
|
|
48
|
+
// assembly measured, so only that project is looked in).
|
|
49
|
+
const COUPON_AT = {
|
|
50
|
+
fixings: { said: 'the fixings coupon', object: 'fixings-coupon', from: 'cad coupons adds it to your workspace' },
|
|
51
|
+
holds: { said: 'the holds coupon', object: 'holds-coupon', from: 'cad coupons adds it to your workspace' },
|
|
52
|
+
slot: { said: 'the slot coupon', object: 'slot-coupon', from: 'cad coupons adds it to your workspace' },
|
|
53
|
+
rig: {
|
|
54
|
+
said: 'the joint rig',
|
|
55
|
+
object: 'joint-hanger',
|
|
56
|
+
from: "the kit's bench, examples/bench/projects/benchmarks/MEASURE.md in the rulecad repository",
|
|
57
|
+
},
|
|
58
|
+
beam: {
|
|
59
|
+
said: 'the beam coupon',
|
|
60
|
+
object: null,
|
|
61
|
+
from: "the kit's bench, examples/bench/projects/benchmarks/MEASURE.md in the rulecad repository",
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
function couponAt(which, project) {
|
|
65
|
+
const { said, object, from } = COUPON_AT[which];
|
|
66
|
+
let all = [];
|
|
67
|
+
try {
|
|
68
|
+
all = object ? fs.readdirSync(paths.projects).sort() : [];
|
|
69
|
+
} catch {
|
|
70
|
+
// (no projects folder: none holds it)
|
|
71
|
+
}
|
|
72
|
+
const at = [...new Set([project, ...(object ? ['coupons', ...all] : [])])].filter(Boolean).find((p) => {
|
|
73
|
+
const dir = path.join(paths.projects, p);
|
|
74
|
+
return (
|
|
75
|
+
fs.existsSync(path.join(dir, 'MEASURE.md')) &&
|
|
76
|
+
(!object || fs.existsSync(path.join(dir, 'objects', `${object}.json`)))
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
return `${said} (${at ? `projects/${at}/MEASURE.md` : from})`;
|
|
80
|
+
}
|
|
81
|
+
export const readLedger = (project) => {
|
|
82
|
+
const f = ledgerFile(project);
|
|
83
|
+
return fs.existsSync(f)
|
|
84
|
+
? readOwnText(f)
|
|
85
|
+
.split('\n')
|
|
86
|
+
.filter(Boolean)
|
|
87
|
+
.map((l) => JSON.parse(l))
|
|
88
|
+
: [];
|
|
89
|
+
};
|
|
29
90
|
|
|
30
91
|
const findingsOf = async (ref) => checkAssembly(await buildAssembly(ref));
|
|
31
|
-
|
|
92
|
+
|
|
93
|
+
// A finding's identity, to pair it with itself on the other side of a measurement: its code and the refs that name
|
|
94
|
+
// what it is about (a fasten's index, a fit's a and b, a join, a beam, a part), never the refs that hold its results.
|
|
95
|
+
// Those move with the measurement: a fasten's list of screws comes in another order once its holes change size, a
|
|
96
|
+
// fit's outOfRange and a deflection's springs change with the figures. A finding about one screw of several names it.
|
|
97
|
+
const NAMING = [
|
|
98
|
+
'fasten',
|
|
99
|
+
'fit',
|
|
100
|
+
'join',
|
|
101
|
+
'joint',
|
|
102
|
+
'item',
|
|
103
|
+
'items',
|
|
104
|
+
'beam',
|
|
105
|
+
'part',
|
|
106
|
+
'a',
|
|
107
|
+
'b',
|
|
108
|
+
'through',
|
|
109
|
+
'into',
|
|
110
|
+
'screw',
|
|
111
|
+
'where',
|
|
112
|
+
'pose',
|
|
113
|
+
'place',
|
|
114
|
+
'side',
|
|
115
|
+
'fitting',
|
|
116
|
+
'set',
|
|
117
|
+
'profile',
|
|
118
|
+
'rule',
|
|
119
|
+
'guide',
|
|
120
|
+
'motor',
|
|
121
|
+
'wire',
|
|
122
|
+
'panel',
|
|
123
|
+
'mount',
|
|
124
|
+
'object',
|
|
125
|
+
'nut',
|
|
126
|
+
'kind',
|
|
127
|
+
'name',
|
|
128
|
+
];
|
|
129
|
+
const naming = (v) =>
|
|
130
|
+
typeof v === 'string' ||
|
|
131
|
+
Number.isInteger(v) ||
|
|
132
|
+
(Array.isArray(v) && v.every((x) => typeof x === 'string' || Number.isInteger(x)));
|
|
133
|
+
/**
|
|
134
|
+
* A finding's identity for matching before and after: its code and what it is about.
|
|
135
|
+
* @param {{ code?: string, refs?: Record<string, any> }} x a finding
|
|
136
|
+
* @returns {string}
|
|
137
|
+
*/
|
|
138
|
+
export function identityOf(x) {
|
|
139
|
+
const r = x.refs ?? {},
|
|
140
|
+
own = NAMING.filter((k) => naming(r[k])).map((k) => [k, r[k]]);
|
|
141
|
+
if (Array.isArray(r.screws) && r.screws.length === 1) own.push(['screws', r.screws]);
|
|
142
|
+
return `${x.code}|${JSON.stringify(own)}`;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The findings before and after a change, paired like with like: by identity, and where several share one (a fasten's
|
|
146
|
+
* two clearance findings, its hole's and its slot's), in the order the checks gave them, which is the same each run.
|
|
147
|
+
* @param {object[]} before
|
|
148
|
+
* @param {object[]} after
|
|
149
|
+
* @returns {[object, object][]} each finding after with its own before (findings on one side only are left out)
|
|
150
|
+
*/
|
|
151
|
+
export function pairFindings(before, after) {
|
|
152
|
+
const was = new Map();
|
|
153
|
+
for (const x of before) {
|
|
154
|
+
const k = identityOf(x);
|
|
155
|
+
was.set(k, [...(was.get(k) ?? []), x]);
|
|
156
|
+
}
|
|
157
|
+
const taken = new Map(),
|
|
158
|
+
pairs = [];
|
|
159
|
+
for (const x of after) {
|
|
160
|
+
const k = identityOf(x),
|
|
161
|
+
i = taken.get(k) ?? 0,
|
|
162
|
+
y = was.get(k)?.[i];
|
|
163
|
+
taken.set(k, i + 1);
|
|
164
|
+
if (y) pairs.push([y, x]);
|
|
165
|
+
}
|
|
166
|
+
return pairs;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* What moved between two runs of an assembly's checks: each prediction whose figure changed, and each other finding
|
|
170
|
+
* whose words changed.
|
|
171
|
+
* @param {object[]} before
|
|
172
|
+
* @param {object[]} after
|
|
173
|
+
*/
|
|
174
|
+
export function movedBetween(before, after) {
|
|
175
|
+
const moved = [];
|
|
176
|
+
for (const [y, x] of pairFindings(before, after)) {
|
|
177
|
+
if (x.predict && y.predict) {
|
|
178
|
+
if (Math.abs(x.predict.value - y.predict.value) > 1e-9)
|
|
179
|
+
moved.push({
|
|
180
|
+
code: x.code,
|
|
181
|
+
figure: x.predict.figure,
|
|
182
|
+
unit: x.predict.unit,
|
|
183
|
+
from: y.predict.value,
|
|
184
|
+
to: x.predict.value,
|
|
185
|
+
});
|
|
186
|
+
} else if (y.text !== x.text) moved.push({ code: x.code, text: x.text, was: y.text });
|
|
187
|
+
}
|
|
188
|
+
return moved;
|
|
189
|
+
}
|
|
32
190
|
// A prediction's name, as cad measured takes it: the check's code, and the part or joint it's about if there are several
|
|
33
191
|
export const predictionsOf = (findings) => findings.filter((x) => x.predict);
|
|
34
192
|
|
|
@@ -36,90 +194,183 @@ export const predictionsOf = (findings) => findings.filter((x) => x.predict);
|
|
|
36
194
|
// set @rulecad/frames' estimates), so nothing is written until every value has been checked
|
|
37
195
|
function ruleFile(name) {
|
|
38
196
|
const dot = name.indexOf('.');
|
|
39
|
-
if (dot < 1)
|
|
40
|
-
|
|
41
|
-
|
|
197
|
+
if (dot < 1)
|
|
198
|
+
throw new CadError(
|
|
199
|
+
'measured.name',
|
|
200
|
+
`"${name}": name a rule as <set>.<rule> (x1c.press), or a prediction by its check's code (task.accuracy)`,
|
|
201
|
+
);
|
|
202
|
+
const set = name.slice(0, dot),
|
|
203
|
+
rule = name.slice(dot + 1);
|
|
204
|
+
const file = path.join(WS, 'rules', `${set}.json`),
|
|
205
|
+
from = RULE_DIRS.map((d) => path.join(d, `${set}.json`)).find((f) => fs.existsSync(f));
|
|
42
206
|
// the joins set has no file in the kit: the workspace's copy starts from @rulecad/frames' estimates
|
|
43
|
-
if (set === JOIN_SET && !from) {
|
|
207
|
+
if (set === JOIN_SET && !from) {
|
|
208
|
+
const text = JSON.stringify(joinRuleSet(), null, 2) + '\n';
|
|
209
|
+
return {
|
|
210
|
+
set,
|
|
211
|
+
rule,
|
|
212
|
+
data: JSON.parse(text),
|
|
213
|
+
file,
|
|
214
|
+
text,
|
|
215
|
+
copied: `started rules/${set}.json from @rulecad/frames' estimates: yours to keep and edit`,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
44
218
|
const data = peekRuleSet(set); // (the measurement goes in your copy: the tool's set is copied there when it is written)
|
|
45
|
-
return {
|
|
219
|
+
return {
|
|
220
|
+
set,
|
|
221
|
+
rule,
|
|
222
|
+
data,
|
|
223
|
+
file,
|
|
224
|
+
text: readOwnText(from),
|
|
225
|
+
copied: from === file ? null : `copied the ${set} rule set into rules/${set}.json: yours to keep and edit`,
|
|
226
|
+
};
|
|
46
227
|
}
|
|
47
228
|
|
|
48
229
|
// The slot coupon (examples/bench/projects/benchmarks/MEASURE.md, rows 29 to 33): the two fits it measures, which a
|
|
49
230
|
// printer's set may not hold yet, each with what it is and what the kit drew before (the printer's fit), so the ledger
|
|
50
231
|
// shows predicted against measured; and a slot's own figures, measured by caliper on the person's beam
|
|
51
|
-
const SLOT_COUPON = 'the slot coupon (examples/bench/projects/benchmarks/MEASURE.md)';
|
|
52
232
|
function slotRule(project, set, key, data) {
|
|
233
|
+
const slotAt = couponAt('slot', project);
|
|
53
234
|
if (set === printerOf(project) && ['slot_fit', 'printed_slot_fit'].includes(key)) {
|
|
54
|
-
const fit = data.rules?.fit,
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
235
|
+
const fit = data.rules?.fit,
|
|
236
|
+
was = typeof fit?.value === 'number' ? fit.value : null;
|
|
237
|
+
return {
|
|
238
|
+
unit: 'mm',
|
|
239
|
+
predicted: was,
|
|
240
|
+
was: `${set}.fit, ${fit?.kind ?? 'none'}`,
|
|
241
|
+
source: slotAt,
|
|
242
|
+
rule:
|
|
243
|
+
key === 'slot_fit'
|
|
244
|
+
? `Clearance each side between a printed part and a bought beam's slot it slides in (a tongue in the opening, a carrier in the cavity): the tightest on ${slotAt} that slid the beam's length by hand without rattling. Printed carriers are drawn with it (@rulecad/frames carrierModel) before the general fit`
|
|
245
|
+
: `Clearance each side a slot printed in a part is opened by, so a bought slot nut slides in it: the tightest printed slot on ${slotAt} a slide-in nut ran the length of by hand without catching. For printed beams and plates with slots`,
|
|
246
|
+
kind: 'measured',
|
|
247
|
+
};
|
|
58
248
|
}
|
|
59
|
-
if (data.part === 'slot' && data.rules?.[key]) return { source: `calipers on your own beam (${
|
|
249
|
+
if (data.part === 'slot' && data.rules?.[key]) return { source: `calipers on your own beam (${slotAt}, row 32)` };
|
|
60
250
|
return null;
|
|
61
251
|
}
|
|
62
252
|
|
|
63
253
|
// The joint rig (examples/bench/projects/benchmarks/MEASURE.md, rows 34 to 37): a join's bending spring, written to the
|
|
64
254
|
// joins set or a bought fitting's set, and a slot nut's hold along its slot, which no maker publishes, so a nut's set
|
|
65
255
|
// has no rule for it until it is measured
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (data.part === 'slot_nut' && key === 'hold_along')
|
|
69
|
-
|
|
256
|
+
function rigRule(project, set, key, data, frame = null) {
|
|
257
|
+
const rigAt = couponAt('rig', project);
|
|
258
|
+
if (data.part === 'slot_nut' && key === 'hold_along')
|
|
259
|
+
return {
|
|
260
|
+
unit: 'N',
|
|
261
|
+
predicted: null,
|
|
262
|
+
was: 'no figure: no maker publishes it',
|
|
263
|
+
source: `${rigAt}, a luggage scale`,
|
|
264
|
+
rule: `The pull along its slot at which this nut slips, its screw tightened as its maker says (or firmly by hand with a hex key where it says nothing): the reading on ${rigAt}'s luggage scale as the nut moved`,
|
|
265
|
+
};
|
|
70
266
|
// what a spring measured on the rig is, in place of the estimate's sentence ("a working guess ... until the joint rig
|
|
71
267
|
// measures it"); the drop it came from goes in its source
|
|
72
|
-
|
|
73
|
-
const
|
|
268
|
+
// (or on a frame of the maker's own: the spring that gives the drop read in that frame's solve)
|
|
269
|
+
const RIG = frame ? `the frame ${frame}, a dial on a beam held out by the join` : rigAt,
|
|
270
|
+
how = frame
|
|
271
|
+
? `measured on the frame ${frame}: a beam held out by the join, the drop of its end under a hung mass, and the spring that gives that drop in the frame's own solve`
|
|
272
|
+
: `measured on ${RIG}: a beam held out by the join, the drop of its end under a hung mass less the beam's own bending (F L² / that)`;
|
|
273
|
+
const turn = (who) => ({
|
|
274
|
+
bending: `How stiffly ${who} holds the end of a beam against turning across its axis`,
|
|
275
|
+
twist: `How stiffly ${who} holds the end of a beam against twisting about its own axis`,
|
|
276
|
+
});
|
|
74
277
|
if (set === JOIN_SET) {
|
|
75
|
-
const j = key.match(/^(.+)_(bending|twist|printed_share)$/),
|
|
278
|
+
const j = key.match(/^(.+)_(bending|twist|printed_share)$/),
|
|
279
|
+
kind = j?.[1].replace(/_/g, '-');
|
|
76
280
|
if (!j) return { source: RIG };
|
|
77
|
-
return {
|
|
78
|
-
:
|
|
281
|
+
return {
|
|
282
|
+
source: RIG,
|
|
283
|
+
measuredRule:
|
|
284
|
+
j[2] === 'printed_share'
|
|
285
|
+
? `A ${kind} join with a printed fitting, as a share of the fitting's own bending stiffness by finite elements: the join's spring ${how}, over the fitting's figure`
|
|
286
|
+
: `${turn(`a ${kind} join`)[j[2]]}, for a 20 mm series profile: ${how}`,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
if (data.part === 'fitting' && /_stiffness$/.test(key)) {
|
|
290
|
+
const k = key.replace(/_stiffness$/, '');
|
|
291
|
+
return {
|
|
292
|
+
source: RIG,
|
|
293
|
+
...(turn('this fitting')[k] ? { measuredRule: `${turn('this fitting')[k]}, its screws tight: ${how}` } : {}),
|
|
294
|
+
};
|
|
79
295
|
}
|
|
80
|
-
if (data.part === 'fitting' && /_stiffness$/.test(key)) { const k = key.replace(/_stiffness$/, ''); return { source: RIG, ...(turn('this fitting')[k] ? { measuredRule: `${turn('this fitting')[k]}, its screws tight: ${how}` } : {}) }; }
|
|
81
296
|
return null;
|
|
82
297
|
}
|
|
83
298
|
|
|
84
299
|
// The beam coupon (examples/bench/projects/benchmarks/MEASURE.md, rows 38 and 39): the modulus a printed beam bends at
|
|
85
300
|
// and the stress it breaks at, which no plastic's data sheet gives for a beam of walls and infill, so a plastic's set has
|
|
86
301
|
// no rule for them until the coupon is measured
|
|
87
|
-
|
|
88
|
-
function couponRule(set, key, data) {
|
|
302
|
+
function couponRule(project, set, key, data) {
|
|
89
303
|
const m = key.match(/^beam_(modulus|strength)(_z)?$/);
|
|
90
304
|
if (!m || data.rules?.density == null) return null;
|
|
305
|
+
const beamAt = couponAt('beam', project);
|
|
91
306
|
const how = m[2] ? 'printed standing up, bent across its layers' : 'printed lying down, its layers along it';
|
|
92
|
-
return {
|
|
93
|
-
|
|
94
|
-
|
|
307
|
+
return {
|
|
308
|
+
unit: 'MPa',
|
|
309
|
+
source: beamAt,
|
|
310
|
+
predicted: data.rules?.[m[2] && data.rules[`${m[1]}_z`] ? `${m[1]}_z` : m[1]]?.value ?? null,
|
|
311
|
+
was: `${set}.${m[1]}${m[2] && data.rules[`${m[1]}_z`] ? '_z' : ''}, the data sheet's`,
|
|
312
|
+
measuredRule:
|
|
313
|
+
m[1] === 'modulus'
|
|
314
|
+
? `The modulus a printed beam of ${set.toUpperCase()} bends at, ${how}, with its section counted from its walls and infill (@rulecad/frames printedSection): the sag of ${beamAt} under a hung mass, measured with a dial, and the solve's sag scaled to it. A printed beam takes it before the plastic's own modulus`
|
|
315
|
+
: `The stress a printed beam of ${set.toUpperCase()} breaks at in bending, ${how}, its section counted from its walls and infill: the mass ${beamAt} broke at, as the stress the solve gives under it. A printed beam takes it before the plastic's own strength, and its creep limit follows it`,
|
|
316
|
+
};
|
|
95
317
|
}
|
|
96
318
|
|
|
97
319
|
// The printer's rules the fixings read that a printer's set may not hold yet, each measured on the fixings coupon
|
|
98
320
|
// (examples/bench/projects/benchmarks/MEASURE.md): what it is, its unit, and what the kit drew before it was measured,
|
|
99
321
|
// so the ledger shows predicted against measured. A pilot or an insert hole is per thread, and may name a plastic
|
|
100
322
|
// (x1c.pilot_m4, x1c.pilot_m4_petg); the rest are one per printer.
|
|
101
|
-
const COUPON = 'the fixings coupon (examples/bench/projects/benchmarks/MEASURE.md)';
|
|
102
323
|
function fixingsRule(project, set, key) {
|
|
103
324
|
if (set !== printerName(project)) return null;
|
|
104
|
-
|
|
105
|
-
|
|
325
|
+
const fixAt = couponAt('fixings', project),
|
|
326
|
+
holdsAt = couponAt('holds', project);
|
|
327
|
+
let fx;
|
|
328
|
+
const f = () =>
|
|
329
|
+
(fx ||= (() => {
|
|
330
|
+
const { context } = fixingsContext({ project, dir: projectDir(project) });
|
|
331
|
+
return { ...fixingsFor({ sets: context.sets, printer: context.printer }), material: context.material };
|
|
332
|
+
})());
|
|
333
|
+
const plastic = (p) => (p ?? f().material)?.toUpperCase() ?? 'the plastic it was printed in';
|
|
106
334
|
const hole = key.match(/^(pilot|insert)_(.+)$/);
|
|
107
335
|
if (hole) {
|
|
108
336
|
const bySet = new Map(threads().map((t) => [fixingsFor().fixingModel(t).set, t]));
|
|
109
|
-
const [setName, thread] =
|
|
337
|
+
const [setName, thread] =
|
|
338
|
+
[...bySet]
|
|
339
|
+
.filter(([k]) => hole[2] === k || hole[2].startsWith(`${k}_`))
|
|
340
|
+
.sort((a, b) => b[0].length - a[0].length)[0] ?? [];
|
|
110
341
|
if (!thread) return null;
|
|
111
342
|
const own = hole[2].slice(setName.length + 1) || null;
|
|
112
343
|
if (own && !/^[a-z][a-z0-9]*$/.test(own)) return null;
|
|
113
344
|
const h = f().holeModel(thread, { kind: hole[1], ...(own ? { material: own } : {}) });
|
|
114
|
-
return {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
345
|
+
return {
|
|
346
|
+
unit: 'mm',
|
|
347
|
+
predicted: h.nominal,
|
|
348
|
+
was: `${h.basis.rule}, ${h.basis.kind}`,
|
|
349
|
+
rule:
|
|
350
|
+
hole[1] === 'pilot'
|
|
351
|
+
? `Hole to draw for ${/^(M|8|11|18)/.test(thread) ? 'an' : 'a'} ${thread} screw to form its own thread in ${plastic(own)}, standing upright: the one on ${fixAt} the screw went into straight and held in, turned by hand. Drawn as it is: no hole compensation`
|
|
352
|
+
: `Hole to draw for ${/^(M|8|11|18)/.test(thread) ? 'an' : 'a'} ${thread} heat-set insert in ${plastic(own)}: the one on ${fixAt} the insert went into straight and flush, and held when a screw was tightened in it. Drawn as it is: no hole compensation`,
|
|
353
|
+
source: fixAt,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
if (key === 'nut_fit')
|
|
357
|
+
return {
|
|
358
|
+
unit: 'mm',
|
|
359
|
+
predicted: f().nutPocketModel('M3').clearance,
|
|
360
|
+
was: 'snug, no clearance',
|
|
361
|
+
source: fixAt,
|
|
362
|
+
rule: `Clearance across the flats of a nut pocket let in from a face, on top of hole_comp: the tightest on ${fixAt} a hex nut dropped into by hand and did not turn in as its screw was tightened. A nut pocket's snug fit`,
|
|
363
|
+
};
|
|
364
|
+
if (key === 'nut_slip') {
|
|
365
|
+
const t = f().nutTrapModel('M3', { depth: 1 });
|
|
366
|
+
return {
|
|
367
|
+
unit: 'mm',
|
|
368
|
+
predicted: t.clearance,
|
|
369
|
+
was: t.rests.find((r) => /slip$/.test(r.name))?.name ?? 'slip',
|
|
370
|
+
source: fixAt,
|
|
371
|
+
rule: `Clearance across the flats of a nut slot a nut slides into from the side, on top of hole_comp: the tightest on ${fixAt} a hex nut slid into by hand, stayed in turned over, and did not turn as its screw was tightened. A nut slot's slip fit`,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
123
374
|
// The holds coupon's fits (rows 40 to 45): each the printer's rule a pin, a snap pin's socket or a magnet's pocket is
|
|
124
375
|
// drawn from, read through the model that draws it, so the ledger's prediction is what the kit drew; and the snap
|
|
125
376
|
// pins' barb, a record as the flank form is. Each is held to what a fit of its kind can be, so a slip of the pen
|
|
@@ -127,134 +378,846 @@ function fixingsRule(project, set, key) {
|
|
|
127
378
|
const fit = HOLDS[key];
|
|
128
379
|
if (fit) {
|
|
129
380
|
const r = fit.model(f()).rests.find((x) => x.name.endsWith(`.${key}`) || x.name.endsWith(`'s ${key}`));
|
|
130
|
-
return {
|
|
131
|
-
|
|
381
|
+
return {
|
|
382
|
+
unit: 'mm',
|
|
383
|
+
predicted: r?.value ?? fit.kit,
|
|
384
|
+
was: r ? `${r.name}, ${r.kind}` : fit.was,
|
|
385
|
+
source: holdsAt,
|
|
386
|
+
measuredRule: fit.rule(holdsAt),
|
|
387
|
+
check: (x) =>
|
|
388
|
+
x < fit.range[0] || x > fit.range[1] || (fit.above && x === 0)
|
|
389
|
+
? `is ${fit.above ? 'above' : 'from'} ${fit.range[0]}${fit.above ? ' and up' : ''} to ${fit.range[1]} mm: ${fit.why}`
|
|
390
|
+
: null,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
if (key === 'thread_flank') {
|
|
394
|
+
const t = f().threadModel('M8', { length: f().holeModel('M8', { kind: 'thread' }).depth, internal: true });
|
|
395
|
+
return {
|
|
396
|
+
unit: '°',
|
|
397
|
+
predicted: t.overhang.drawn,
|
|
398
|
+
was: 'flanks: printed, the default',
|
|
399
|
+
source: fixAt,
|
|
400
|
+
allowed: [t.overhang.drawn, t.overhang.standard],
|
|
401
|
+
rule: `The down-facing flank of a printed thread standing upright, as an overhang from upright: ${t.overhang.drawn} is the form laid back to the printer's overhang (flanks: 'printed', the kit's default), ${t.overhang.standard} the standard's own form (flanks: 'standard'). The one on ${fixAt} that held, turned smoothly and printed clean. A record for choosing the default: designs still draw flanks: 'printed' unless they say otherwise`,
|
|
402
|
+
};
|
|
132
403
|
}
|
|
133
|
-
if (key === 'thread_flank') { const t = f().threadModel('M8', { length: f().holeModel('M8', { kind: 'thread' }).depth, internal: true }); return { unit: '°', predicted: t.overhang.drawn, was: 'flanks: printed, the default', source: COUPON, allowed: [t.overhang.drawn, t.overhang.standard],
|
|
134
|
-
rule: `The down-facing flank of a printed thread standing upright, as an overhang from upright: ${t.overhang.drawn} is the form laid back to the printer's overhang (flanks: 'printed', the kit's default), ${t.overhang.standard} the standard's own form (flanks: 'standard'). The one on ${COUPON} that held, turned smoothly and printed clean. A record for choosing the default: designs still draw flanks: 'printed' unless they say otherwise` }; }
|
|
135
404
|
return null;
|
|
136
405
|
}
|
|
137
406
|
// The holds coupon's fits: the rule each writes once measured, what bounds it, and the model that draws from it
|
|
138
|
-
const HOLDS_COUPON = 'the holds coupon (examples/bench/projects/benchmarks/MEASURE.md)';
|
|
139
407
|
const HOLDS = {
|
|
140
|
-
snap_clearance: {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
rule:
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
408
|
+
snap_clearance: {
|
|
409
|
+
range: [0, 0.5],
|
|
410
|
+
above: true,
|
|
411
|
+
model: (f) => f.pinModel('snap pin 4x6'),
|
|
412
|
+
why: 'its gap is drawn with no hole compensation, so at 0 the printed socket comes out smaller than the peg and pinches it: a press, not a snap. Past half a millimetre a pin rocks; the coupon prints 0.1 to 0.3, and never a socket at 0',
|
|
413
|
+
rule: (holdsAt) =>
|
|
414
|
+
`Gap round a printed snap pin's peg in its socket, each side, as drawn (no hole compensation): the socket on ${holdsAt} a snap pin clicked through by hand and then held without rocking`,
|
|
415
|
+
},
|
|
416
|
+
pin_press: {
|
|
417
|
+
range: [0, 0.3],
|
|
418
|
+
model: (f) => f.pinModel('dowel 3x12', { fit: 'press' }),
|
|
419
|
+
why: 'how much smaller a hole is drawn than the pin it grips, past hole_comp, and more than 0.3 mm splits a printed boss; the coupon prints 0 to 0.1',
|
|
420
|
+
rule: (holdsAt) =>
|
|
421
|
+
`How much smaller across a printed hole is drawn than a plain pin pressed into it, past hole_comp: the hole on ${holdsAt} a 3 mm dowel (or 1.75 mm filament) pressed into by hand or in a vice, stood straight, and could not be pulled out by hand`,
|
|
422
|
+
},
|
|
423
|
+
pin_slip: {
|
|
424
|
+
range: [0, 0.6],
|
|
425
|
+
model: (f) => f.pinModel('dowel 3x12', { fit: 'slip' }),
|
|
426
|
+
why: "how much larger a hole is drawn than the pin that turns in it, past hole_comp, and past 0.6 mm it is a loose hole. At 0 the hole is the pin and hole_comp, the pin's own size once printed: a close slip; the coupon prints 0.1 to 0.3, and 0 once it is centred on 0.1",
|
|
427
|
+
rule: (holdsAt) =>
|
|
428
|
+
`How much larger across a printed hole is drawn than a plain pin that turns in it, past hole_comp: the tightest hole on ${holdsAt} a 3 mm dowel (or 1.75 mm filament) slid into by hand and turned freely in`,
|
|
429
|
+
},
|
|
430
|
+
magnet_press: {
|
|
431
|
+
range: [0, 0.4],
|
|
432
|
+
model: (f) => f.magnetSeatModel('disc magnet 8x3', { fit: 'press' }),
|
|
433
|
+
why: 'how much smaller a pocket is drawn than the magnet it grips, past hole_comp, and more than 0.4 mm splits its wall; the coupon prints 0 to 0.2',
|
|
434
|
+
rule: (holdsAt) =>
|
|
435
|
+
`How much smaller across a printed pocket is drawn than a disc magnet pressed into it, past hole_comp: the pocket on ${holdsAt} a 6x3 and an 8x3 magnet went into with a thumb or a vice, flush, without splitting the wall, and stayed in when the plate was turned over and tapped`,
|
|
436
|
+
},
|
|
437
|
+
magnet_glue: {
|
|
438
|
+
range: [0, 0.6],
|
|
439
|
+
model: (f) => f.magnetSeatModel('disc magnet 8x3', { fit: 'glue' }),
|
|
440
|
+
why: "room for a film of glue across the pocket, past hole_comp, and past 0.6 mm the magnet sits off centre; the coupon's press row shows where a magnet drops in",
|
|
441
|
+
rule: (holdsAt) =>
|
|
442
|
+
`How much larger across a printed pocket is drawn than a magnet glued into it, past hole_comp: the least on ${holdsAt} a magnet dropped into freely, which leaves room for glue`,
|
|
443
|
+
},
|
|
444
|
+
magnet_crush: {
|
|
445
|
+
range: [0, 0.6],
|
|
446
|
+
model: (f) => f.magnetSeatModel('disc magnet 8x3', { fit: 'ribs' }),
|
|
447
|
+
why: "how far inside the magnet the crush ribs' tips stand, past hole_comp, and past 0.6 mm a rib no longer crushes but splits. At 0 the ribs' tips stand at the magnet and hole_comp, a light crush once printed; the coupon prints 0.1 to 0.3, and 0 once it is centred on 0.1",
|
|
448
|
+
rule: (holdsAt) =>
|
|
449
|
+
`With crush ribs, how much smaller across the ribs' tips stand than the magnet, past hole_comp: the ribbed pocket on ${holdsAt} an 8x3 magnet pressed into by hand, flush, and held when the plate was turned over and tapped`,
|
|
450
|
+
},
|
|
451
|
+
snap_barb: {
|
|
452
|
+
range: [0, 1],
|
|
453
|
+
above: true,
|
|
454
|
+
kit: 0.4,
|
|
455
|
+
was: 'barb 0.4, the default',
|
|
456
|
+
model: () => ({ rests: [] }),
|
|
457
|
+
why: "a snap pin's barb past its peg, and past a millimetre the halves cannot close enough to let it through; the coupon prints 0.3 to 0.5",
|
|
458
|
+
rule: (holdsAt) =>
|
|
459
|
+
`A snap pin's barb past its peg: the one on ${holdsAt} that clicked through its socket by hand and held when pulled. A record for choosing the default: pins are still drawn with a 0.4 mm barb unless their name says barb N`,
|
|
460
|
+
},
|
|
154
461
|
};
|
|
155
462
|
|
|
156
463
|
// The pull bolt on the holds coupon (row 46): an M8 printed bolt whose waist breaks across its layers, so it measures
|
|
157
464
|
// how strongly a plastic holds across its layers, which PLA's data sheet does not give. It goes in the plastic's set
|
|
158
465
|
// as strength_z, which a printed bolt standing on its head, and every part pulled across its layers, then reads
|
|
159
|
-
function boltRule(set, key, data) {
|
|
160
|
-
if (key !== 'strength_z' || data.rules?.density == null || typeof data.rules?.strength?.value !== 'number')
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
466
|
+
function boltRule(project, set, key, data) {
|
|
467
|
+
if (key !== 'strength_z' || data.rules?.density == null || typeof data.rules?.strength?.value !== 'number')
|
|
468
|
+
return null;
|
|
469
|
+
const holdsAt = couponAt('holds', project);
|
|
470
|
+
const along = data.rules.strength.value,
|
|
471
|
+
z = data.rules.strength_z;
|
|
472
|
+
return {
|
|
473
|
+
unit: 'MPa',
|
|
474
|
+
predicted: z?.value ?? along,
|
|
475
|
+
was: z ? `${set}.strength_z, ${z.kind}` : `${set}.strength, along the layers: no figure across them`,
|
|
476
|
+
source: `${holdsAt}, the pull bolt broken on a luggage scale`,
|
|
477
|
+
measuredRule: `Tensile strength of ${set.toUpperCase()} across its printed layers: the pull bolt on ${holdsAt}, printed standing, broken on a luggage scale at its 3.5 mm waist (the kilograms × 9.81 over 9.62 mm²). A printed bolt standing on its head, and any part pulled across its layers, takes it before the along-layer strength`,
|
|
478
|
+
check: (x) =>
|
|
479
|
+
!(x > 0) || x > along
|
|
480
|
+
? `is above 0 and no more than ${set}.strength, ${along} MPa: a print holds less across its layers than along them, so read the scale's peak again, and work it as kilograms × 9.81 over the waist's 9.62 mm²`
|
|
481
|
+
: null,
|
|
482
|
+
};
|
|
165
483
|
}
|
|
166
|
-
const printerName = (project) => {
|
|
484
|
+
const printerName = (project) => {
|
|
485
|
+
try {
|
|
486
|
+
return fixingsContext({ project, dir: projectDir(project) }).context.printer?.name ?? 'x1c';
|
|
487
|
+
} catch {
|
|
488
|
+
return 'x1c';
|
|
489
|
+
}
|
|
490
|
+
};
|
|
167
491
|
// A rule the workspace's copy lacks and the kit's own set of that name holds (a set copied before the rule was added)
|
|
168
|
-
function kitRule(set, key) {
|
|
492
|
+
function kitRule(set, key) {
|
|
493
|
+
try {
|
|
494
|
+
return JSON.parse(fs.readFileSync(path.join(RULE_DIRS.at(-1), `${set}.json`), 'utf8')).rules?.[key] ?? null;
|
|
495
|
+
} catch {
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// A typed figure that is most likely a slip: of the other sign from the one it replaces, or under half or over twice it.
|
|
501
|
+
// An estimate is a guess the measurement is there to correct, so it may move further: a tenth to ten times, which
|
|
502
|
+
// still catches a figure in the wrong unit or a diameter for a clearance. Nothing is said where there is no figure to
|
|
503
|
+
// compare with (a new rule, a fit the kit drew at 0)
|
|
504
|
+
const RATIO = { settled: [0.5, 2, 'half to twice it'], estimate: [0.1, 10, 'a tenth to ten times it, as an estimate'] };
|
|
505
|
+
function slipOf(value, prior, unit, kind) {
|
|
506
|
+
if (typeof prior !== 'number' || !Number.isFinite(prior) || prior === 0) return null;
|
|
507
|
+
const was = `${n(prior)}${unit ? ` ${unit}` : ''}`;
|
|
508
|
+
if (value !== 0 && Math.sign(value) !== Math.sign(prior)) return `is of the other sign from the ${was} it replaces`;
|
|
509
|
+
const r = value / prior,
|
|
510
|
+
[least, most, said] = kind === 'estimate' ? RATIO.estimate : RATIO.settled;
|
|
511
|
+
if (r < least || r > most) return `is ${n(r)} times the ${was} it replaces, outside ${said}`;
|
|
512
|
+
return null;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// The assembly a measurement is for, from <project>/<assembly>; or with no ref, none: a coupon's figure, kept in the
|
|
516
|
+
// workspace's ledger. `what` says what the assembly is for, where it is not there
|
|
517
|
+
function assemblyOf(ref, what) {
|
|
518
|
+
if (ref == null) return { project: null, assembly: null, shown: 'ledger.jsonl', history: 'cad history' };
|
|
519
|
+
const [project, assembly] = String(ref).split('/');
|
|
520
|
+
if (!project || !assembly || !fs.existsSync(assemblyPath(ref)))
|
|
521
|
+
throw new CadError('measured.assembly', `no assembly "${ref}": give <project>/<assembly>, ${what}`);
|
|
522
|
+
return { project, assembly, shown: ref, history: `cad history ${ref}` };
|
|
523
|
+
}
|
|
169
524
|
|
|
170
525
|
/**
|
|
171
526
|
* Write measurements back.
|
|
172
|
-
* @param {string} ref project/assembly
|
|
527
|
+
* @param {string | null} ref project/assembly, the one whose predictions it moves and whose checks are run either
|
|
528
|
+
* side; or null for a rule measured on a coupon with no assembly to run: written the same, kept in the workspace's
|
|
529
|
+
* ledger, with nothing said to have moved
|
|
173
530
|
* @param {Record<string, number>} values name → measured value
|
|
174
|
-
* @param {{ unit?: string, source?: string, rule?: string, date?: string }} [options]
|
|
531
|
+
* @param {{ unit?: string, source?: string, rule?: string, date?: string, force?: boolean }} [options] force: write
|
|
532
|
+
* a figure under half or over twice the one it replaces, or of the other sign, which is otherwise refused as a slip
|
|
175
533
|
* @returns {Promise<{ entries: object[], moved: { from: string, to: string }[] }>}
|
|
176
534
|
*/
|
|
177
|
-
export async function recordMeasured(ref, values, { unit, source, rule, date = today() } = {}) {
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
535
|
+
export async function recordMeasured(ref, values, { unit, source, rule, date = today(), force = false } = {}) {
|
|
536
|
+
const { project, assembly: name } = assemblyOf(
|
|
537
|
+
ref,
|
|
538
|
+
'the one whose predictions it moves, or leave it out to write a rule measured on a coupon (cad measured x1c.pilot_m3=2.9)',
|
|
539
|
+
);
|
|
540
|
+
if (!Object.keys(values).length)
|
|
541
|
+
throw new CadError(
|
|
542
|
+
'measured.usage',
|
|
543
|
+
'usage: cad measured [<project>/<assembly>] <name>=<value> … [--unit mm] [--source "how it was measured"]',
|
|
544
|
+
);
|
|
545
|
+
const before = ref == null ? [] : await findingsOf(ref),
|
|
546
|
+
entries = [],
|
|
547
|
+
predictions = predictionsOf(before);
|
|
548
|
+
const writes = [],
|
|
549
|
+
slots = new Map(), // (every value is checked before any is written, so a refusal leaves the rule sets as they were)
|
|
550
|
+
slips = [];
|
|
183
551
|
// a rule's new value, checked before anything is written (a refusal leaves the rule sets as they were)
|
|
184
|
-
const writeRule = (k, value, { from = null, unit: u = unit } = {}) => {
|
|
552
|
+
const writeRule = (k, value, { from = null, unit: u = unit, frame = null } = {}) => {
|
|
185
553
|
const { set, rule: key, data, file, text, copied } = ruleFile(k);
|
|
186
|
-
const old = data.rules?.[key],
|
|
187
|
-
|
|
554
|
+
const old = data.rules?.[key],
|
|
555
|
+
known =
|
|
556
|
+
slotRule(project, set, key, data) ??
|
|
557
|
+
rigRule(project, set, key, data, frame) ??
|
|
558
|
+
couponRule(project, set, key, data) ??
|
|
559
|
+
boltRule(project, set, key, data) ??
|
|
560
|
+
fixingsRule(project, set, key) ??
|
|
561
|
+
(old ? null : kitRule(set, key));
|
|
562
|
+
if (known?.allowed && !known.allowed.includes(value))
|
|
563
|
+
throw new CadError(
|
|
564
|
+
'measured.value',
|
|
565
|
+
`${k} is ${known.allowed.join(' or ')}: the flank forms the coupon prints (got ${value})`,
|
|
566
|
+
);
|
|
188
567
|
const wrong = known?.check?.(value);
|
|
189
568
|
if (wrong) throw new CadError('measured.value', `${k} ${wrong} (got ${value})`);
|
|
190
|
-
if (!old && !rule && !known)
|
|
191
|
-
|
|
192
|
-
|
|
569
|
+
if (!old && !rule && !known)
|
|
570
|
+
throw new CadError(
|
|
571
|
+
'measured.rule',
|
|
572
|
+
ref == null
|
|
573
|
+
? `${set} has no rule "${key}": --rule "what it is" to add it, or give the assembly first to measure one of its checks' predictions (cad measured <project>/<assembly> ${k}=${value})`
|
|
574
|
+
: `${set} has no rule "${key}" (and no check here predicts "${k}"): --rule "what it is" to add it (checks: ${[...new Set(predictions.map((x) => x.code))].join(', ') || 'none'})`,
|
|
575
|
+
);
|
|
576
|
+
const had = old ?? known,
|
|
577
|
+
prior = typeof had?.value === 'number' ? had.value : (known?.predicted ?? null);
|
|
578
|
+
if (u && had?.unit && u !== had.unit)
|
|
579
|
+
throw new CadError('measured.unit', `${k} is in ${had.unit}, and this is in ${u}: give it in ${had.unit}`);
|
|
580
|
+
// a figure typed for a rule, held to the one it replaces (a figure worked out from a drop or a sag is not: the
|
|
581
|
+
// estimate it replaces may be far out, and the drop or sag is checked where it is read; nor is a fit the coupon
|
|
582
|
+
// bounds by what a fit of its kind can be, where 0 may be right). Said once every firmer check has had its say
|
|
583
|
+
if (!from && !force && !known?.check && !known?.allowed) {
|
|
584
|
+
// (a 0 where a coupon in the kit measures the figure is one it prints: it steps either side of the kit's figure,
|
|
585
|
+
// and the fixings coupon's nut slots start at 0. An empty figure, which reads as 0, is refused at the command line)
|
|
586
|
+
const kind = old?.kind ?? (/\bestimate\b/.test(known?.was ?? '') ? 'estimate' : null),
|
|
587
|
+
slip =
|
|
588
|
+
value === 0 && couponOf(set, key, data, printerName(project))
|
|
589
|
+
? null
|
|
590
|
+
: slipOf(value, prior, had?.unit ?? u ?? '', kind);
|
|
591
|
+
if (slip)
|
|
592
|
+
slips.push(
|
|
593
|
+
`${k}=${value} ${slip} (${old ? old.kind : (known?.was ?? 'the kit')}). That is most often a slip of the pen: a diameter for a clearance, a point in the wrong place. Measure it again; if it is right, give --force to write it as it is`,
|
|
594
|
+
);
|
|
595
|
+
}
|
|
193
596
|
// its sentence: what was measured, never the estimate's "until it is measured": the one given, else the rig's, else the rule's own
|
|
194
|
-
const entry = {
|
|
597
|
+
const entry = {
|
|
598
|
+
value,
|
|
599
|
+
unit: u ?? had?.unit ?? '',
|
|
600
|
+
rule: (from ? null : rule) ?? known?.measuredRule ?? had.rule,
|
|
601
|
+
kind: 'measured',
|
|
602
|
+
source: [source || known?.source || 'measured on the bench', from].filter(Boolean).join(': '),
|
|
603
|
+
checked: date,
|
|
604
|
+
};
|
|
195
605
|
if (data.part === 'slot') slots.set(set, { data, change: { ...slots.get(set)?.change, [key]: value } });
|
|
196
606
|
writes.push({ file, key, entry, text, copied, set });
|
|
197
|
-
entries.push({
|
|
607
|
+
entries.push({
|
|
608
|
+
date,
|
|
609
|
+
assembly: name,
|
|
610
|
+
name: k,
|
|
611
|
+
figure: entry.rule,
|
|
612
|
+
unit: entry.unit,
|
|
613
|
+
predicted: prior,
|
|
614
|
+
was: old ? old.kind : (known?.was ?? known?.kind ?? null),
|
|
615
|
+
measured: value,
|
|
616
|
+
ratio: prior ? value / prior : null,
|
|
617
|
+
...(source ? { source } : {}),
|
|
618
|
+
});
|
|
198
619
|
};
|
|
199
620
|
for (const [k, value] of Object.entries(values)) {
|
|
200
621
|
if (!Number.isFinite(value)) throw new CadError('measured.value', `${k}: a measurement is a number (got ${value})`);
|
|
201
622
|
const named = predictions.filter((x) => x.code === k || x.predict.name === k);
|
|
202
623
|
if (named.length) {
|
|
203
624
|
const p = named[0].predict;
|
|
204
|
-
entries.push({
|
|
625
|
+
entries.push({
|
|
626
|
+
date,
|
|
627
|
+
assembly: name,
|
|
628
|
+
name: k,
|
|
629
|
+
code: named[0].code,
|
|
630
|
+
figure: p.figure,
|
|
631
|
+
unit: p.unit,
|
|
632
|
+
predicted: p.value,
|
|
633
|
+
measured: value,
|
|
634
|
+
ratio: p.value ? value / p.value : null,
|
|
635
|
+
...(source ? { source } : {}),
|
|
636
|
+
});
|
|
205
637
|
// a drop on the joint rig: what the beam bends of it is the beam's own; the rest is the join turning, so its spring
|
|
206
638
|
if (p.rig) {
|
|
207
|
-
const r = p.rig,
|
|
208
|
-
|
|
209
|
-
const
|
|
210
|
-
|
|
639
|
+
const r = p.rig,
|
|
640
|
+
n = (x) => String(Number(Number(x).toFixed(3)));
|
|
641
|
+
const frame = r.from === 'frame',
|
|
642
|
+
rigidSaid = frame
|
|
643
|
+
? `the ${n(r.rigid)} mm the frame gives under ${n(r.force)} N at ${n(r.arm)} mm with the join${r.others ? ` and the ${r.others} other${r.others > 1 ? 's' : ''} on the same rule` : ''} held rigid`
|
|
644
|
+
: `the ${n(r.rigid)} mm the beam bends by itself under ${n(r.force)} N at ${n(r.arm)} mm`;
|
|
645
|
+
// the spring that gives the drop read, from the same model the prediction came from: the frame solved again at
|
|
646
|
+
// each step of a bounded search, or the rig's closed form
|
|
647
|
+
const found = typeof r.dropAt === 'function' ? springFor(r, value) : { refused: 'unsolved' };
|
|
648
|
+
if (found.refused === 'stiff')
|
|
649
|
+
throw new CadError(
|
|
650
|
+
'measured.value',
|
|
651
|
+
`${k}: ${n(value)} mm is no more than ${rigidSaid}, so the join would be stiffer than solid metal: read the drop again, from the dial's reading with the mass hung less its reading without`,
|
|
652
|
+
);
|
|
653
|
+
if (found.refused === 'soft')
|
|
654
|
+
throw new CadError(
|
|
655
|
+
'measured.value',
|
|
656
|
+
`${k}: ${n(value)} mm is more than the join can give: even a million times softer than its ${(r.spring / 1e6).toFixed(3)} N·m/mrad it drops ${n(found.most)} mm, so something else gives (a support or another join loose, the dial's arm moving): look at what holds the frame and the dial, and read the drop again`,
|
|
657
|
+
);
|
|
658
|
+
if (found.refused)
|
|
659
|
+
throw new CadError(
|
|
660
|
+
'measured.value',
|
|
661
|
+
`${k}: no join spring was found that gives ${n(value)} mm${frame ? " in the frame's solve" : ''}, so nothing is written: read the drop again, and check the frame's supports`,
|
|
662
|
+
);
|
|
663
|
+
const spring = found.spring,
|
|
664
|
+
figure = Number((spring * r.per).toPrecision(4));
|
|
665
|
+
writeRule(r.rule, figure, {
|
|
666
|
+
unit: r.unit,
|
|
667
|
+
frame: frame ? `${project}/${name}` : null,
|
|
668
|
+
from: frame
|
|
669
|
+
? `${n(value)} mm drop of ${k.replace(/\.drop$/, '')} under ${n(r.force)} N at ${n(r.arm)} mm (${r.join}), in the frame's solve where ${n(r.rigid)} mm of it is the frame with the join${r.others ? ` and the ${r.others} other${r.others > 1 ? 's' : ''} on the same rule` : ''} held rigid, is a spring of ${(spring / 1e6).toFixed(3)} N·m/mrad`
|
|
670
|
+
: `${n(value)} mm drop of ${k.replace(/\.drop$/, '')} under ${n(r.force)} N at ${n(r.arm)} mm (${r.join}), less the beam's own ${n(r.beam)} mm, is a spring of ${(spring / 1e6).toFixed(3)} N·m/mrad`,
|
|
671
|
+
});
|
|
211
672
|
}
|
|
212
673
|
// the beam coupon: the modulus from its sag, the strength from the mass it broke at
|
|
213
674
|
if (p.coupon) {
|
|
214
|
-
const c = p.coupon,
|
|
215
|
-
|
|
216
|
-
|
|
675
|
+
const c = p.coupon,
|
|
676
|
+
figure = couponFigure(c, p.value, value);
|
|
677
|
+
if (!(value > 0))
|
|
678
|
+
throw new CadError(
|
|
679
|
+
'measured.value',
|
|
680
|
+
`${k}: a ${c.kind === 'modulus' ? 'sag' : 'mass'} is above 0 (got ${value})`,
|
|
681
|
+
);
|
|
682
|
+
writeRule(c.rule, figure, {
|
|
683
|
+
unit: c.unit,
|
|
684
|
+
from:
|
|
685
|
+
c.kind === 'modulus'
|
|
686
|
+
? `${n(value)} mm sag of ${k.replace(/\.sag$/, '')} under ${c.what}, against ${n(p.value)} mm at ${n(c.base)} MPa`
|
|
687
|
+
: `broke at ${n(value)} kg ${c.what}, ${n(c.perKg)} MPa a kilogram on its own weight's ${n(c.self)} MPa`,
|
|
688
|
+
});
|
|
217
689
|
}
|
|
218
690
|
continue;
|
|
219
691
|
}
|
|
220
692
|
writeRule(k, value);
|
|
221
693
|
}
|
|
222
694
|
// a slot's figures together still make a slot (an opening narrower than its cavity, lips thinner than it is deep)
|
|
223
|
-
for (const [set, { data, change }] of slots) {
|
|
695
|
+
for (const [set, { data, change }] of slots) {
|
|
696
|
+
try {
|
|
697
|
+
slotModel(data, change);
|
|
698
|
+
} catch (e) {
|
|
699
|
+
throw new CadError('measured.value', `${set}: ${e.message.replace(/^slotModel: /, '')}`);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
if (slips.length) throw new CadError('measured.value', slips.join('\n'));
|
|
224
703
|
// every value checked: now each set is written, the workspace's copy made from where it started where there is none
|
|
225
704
|
const texts = new Map();
|
|
226
|
-
for (const w of writes)
|
|
705
|
+
for (const w of writes)
|
|
706
|
+
texts.set(w.file, { ...w, text: setAt(texts.get(w.file)?.text ?? w.text, ['rules', w.key], w.entry) });
|
|
227
707
|
for (const [file, w] of texts) {
|
|
228
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
708
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
709
|
+
fs.writeFileSync(file, w.text);
|
|
229
710
|
if (w.copied) console.error(w.copied);
|
|
230
711
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
712
|
+
// (with no assembly there are no checks to run again: nothing is said to have moved)
|
|
713
|
+
const after = ref == null ? [] : await findingsOf(ref),
|
|
714
|
+
moved = movedBetween(before, after);
|
|
715
|
+
fs.appendFileSync(
|
|
716
|
+
insideWorkspace(ledgerFile(project)), // (never through a link: rules.mjs)
|
|
717
|
+
entries.map((e) => JSON.stringify({ ...e, moved: moved.filter((m) => m.figure) })).join('\n') + '\n',
|
|
718
|
+
);
|
|
238
719
|
return { entries, moved, findings: after };
|
|
239
720
|
}
|
|
240
721
|
|
|
241
|
-
//
|
|
722
|
+
// The coupons that settle a printer's or a plastic's figures (examples/bench/projects/benchmarks/MEASURE.md): the
|
|
723
|
+
// object printed, its rows there, and the rules it measures (a pattern for one per thread). `names` are the ones it
|
|
724
|
+
// settles that a set may not hold yet, listed for an assembly the coupon is in until each is measured
|
|
725
|
+
const COUPONS = [
|
|
726
|
+
{ object: 'fit-bar', said: 'the fit bar', rows: '4', printer: ['hole_comp'] },
|
|
727
|
+
{ object: 'seats-coupon', said: 'the seats coupon', rows: '21', printer: ['press', 'slip'] },
|
|
728
|
+
{
|
|
729
|
+
object: 'fixings-coupon',
|
|
730
|
+
said: 'the fixings coupon',
|
|
731
|
+
rows: '22 to 28',
|
|
732
|
+
printer: [
|
|
733
|
+
/^pilot_m[\d_]+(_[a-z][a-z0-9]*)?$/,
|
|
734
|
+
/^insert_m[\d_]+(_[a-z][a-z0-9]*)?$/,
|
|
735
|
+
'nut_fit',
|
|
736
|
+
'nut_slip',
|
|
737
|
+
'thread_gap',
|
|
738
|
+
'thread_flank',
|
|
739
|
+
],
|
|
740
|
+
names: [
|
|
741
|
+
'pilot_m2',
|
|
742
|
+
'pilot_m2_5',
|
|
743
|
+
'pilot_m3',
|
|
744
|
+
'pilot_m4',
|
|
745
|
+
'pilot_m5',
|
|
746
|
+
'insert_m2',
|
|
747
|
+
'insert_m3',
|
|
748
|
+
'nut_fit',
|
|
749
|
+
'nut_slip',
|
|
750
|
+
'thread_gap',
|
|
751
|
+
'thread_flank',
|
|
752
|
+
],
|
|
753
|
+
// (a thread set's pilot and insert hole are the starting figures the coupon's x1c.pilot_m3 and the like replace)
|
|
754
|
+
other: (set, key) => /^m\d[\d_]*$/.test(set) && ['pilot', 'insert_hole'].includes(key),
|
|
755
|
+
},
|
|
756
|
+
{
|
|
757
|
+
object: 'holds-coupon',
|
|
758
|
+
said: 'the holds coupon',
|
|
759
|
+
rows: '40 to 46',
|
|
760
|
+
printer: ['snap_barb', 'snap_clearance', 'pin_press', 'pin_slip', 'magnet_press', 'magnet_crush'],
|
|
761
|
+
plastic: ['strength_z'],
|
|
762
|
+
names: ['snap_barb', 'snap_clearance', 'pin_press', 'pin_slip', 'magnet_press', 'magnet_crush'],
|
|
763
|
+
},
|
|
764
|
+
{
|
|
765
|
+
object: 'slot-coupon',
|
|
766
|
+
said: 'the slot coupon',
|
|
767
|
+
rows: '29 to 33',
|
|
768
|
+
printer: ['slot_fit', 'printed_slot_fit'],
|
|
769
|
+
names: ['slot_fit', 'printed_slot_fit'],
|
|
770
|
+
},
|
|
771
|
+
// (these two are settled through the drops and sags their assemblies predict: cad measured <ref> c1.drop=<mm>)
|
|
772
|
+
{
|
|
773
|
+
object: 'joint-hanger',
|
|
774
|
+
said: 'the joint rig',
|
|
775
|
+
rows: '34 to 37',
|
|
776
|
+
other: (set, key, data) =>
|
|
777
|
+
set === JOIN_SET || (data?.part === 'fitting' && /_stiffness$/.test(key)) || key === 'hold_along',
|
|
778
|
+
},
|
|
779
|
+
{
|
|
780
|
+
said: 'the beam coupon',
|
|
781
|
+
rows: '38 and 39',
|
|
782
|
+
plastic: [/^beam_(modulus|strength)(_z)?$/],
|
|
783
|
+
here: (findings) => findings.some((x) => x.code === 'beam.coupon'),
|
|
784
|
+
},
|
|
785
|
+
];
|
|
786
|
+
const SETTLED = new Set(['measured', 'spec', 'derived']); // (a derived rule follows others, which are listed themselves)
|
|
787
|
+
const UNSETTLED = ['estimate', 'community', 'default'];
|
|
788
|
+
// the coupon that measures a rule, if one in the kit does
|
|
789
|
+
function couponOf(set, key, data, printer) {
|
|
790
|
+
const is = (list) => (list ?? []).some((p) => (typeof p === 'string' ? p === key : p.test(key)));
|
|
791
|
+
return (
|
|
792
|
+
COUPONS.find(
|
|
793
|
+
(c) =>
|
|
794
|
+
(set === printer && is(c.printer)) ||
|
|
795
|
+
(data?.rules?.density != null && is(c.plastic)) ||
|
|
796
|
+
c.other?.(set, key, data),
|
|
797
|
+
) ?? null
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
// whether an assembly is that coupon: it prints the coupon's object, or makes its predictions
|
|
801
|
+
const isHere = (c, objects, findings) => (c.object ? objects.has(c.object) : !!c.here?.(findings));
|
|
802
|
+
// every rule an assembly's build and checks read: its objects' own (estimates among their variables too), the rules
|
|
803
|
+
// its positions and checks named, the holes v.fixings drew (and the hole compensation added to them), and what its
|
|
804
|
+
// findings say they rest on
|
|
805
|
+
function rulesUsed(s, findings, printer) {
|
|
806
|
+
const names = new Set();
|
|
807
|
+
for (const o of s.objects) {
|
|
808
|
+
for (const e of o.built?.entries ?? [])
|
|
809
|
+
if (e.used || (!e.name.includes('.') && e.kind === 'estimate')) names.add(e.name);
|
|
810
|
+
for (const h of o.built?.fixings ?? []) {
|
|
811
|
+
if (typeof h.refs?.rule === 'string') names.add(h.refs.rule);
|
|
812
|
+
if (h.values?.comp > 0) names.add(`${printer}.hole_comp`);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
for (const x of s.seen ?? []) names.add(x);
|
|
816
|
+
for (const x of findings) {
|
|
817
|
+
const r = x.refs ?? {};
|
|
818
|
+
for (const v of [
|
|
819
|
+
r.rule,
|
|
820
|
+
...(Array.isArray(r.rests) ? r.rests : []),
|
|
821
|
+
...(x.code === 'rules.estimate' ? (r.rules ?? []) : []),
|
|
822
|
+
])
|
|
823
|
+
if (typeof v === 'string') names.add(v);
|
|
824
|
+
}
|
|
825
|
+
return names;
|
|
826
|
+
}
|
|
827
|
+
// a rule set read and not written: the workspace's copy, else the kit's, else for the joins set @rulecad/frames' estimates
|
|
828
|
+
const setOf = (name) => {
|
|
829
|
+
try {
|
|
830
|
+
return peekRuleSet(name);
|
|
831
|
+
} catch (e) {
|
|
832
|
+
if (name === JOIN_SET) return joinRuleSet();
|
|
833
|
+
throw e;
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
const ruleAt = (name) => {
|
|
837
|
+
const dot = name.indexOf('.');
|
|
838
|
+
if (dot < 1) return null;
|
|
839
|
+
try {
|
|
840
|
+
const set = setOf(name.slice(0, dot));
|
|
841
|
+
return {
|
|
842
|
+
set: name.slice(0, dot),
|
|
843
|
+
key: name.slice(dot + 1),
|
|
844
|
+
data: set,
|
|
845
|
+
rule: set.rules?.[name.slice(dot + 1)] ?? null,
|
|
846
|
+
};
|
|
847
|
+
} catch {
|
|
848
|
+
return null; // (an item's or another object's value, not a rule set's)
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* What an assembly still assumes: every rule it reads that is an estimate, a community figure or a default (its
|
|
854
|
+
* figure, what it is, the coupon in the kit that measures it, and whether this assembly is that coupon), each figure
|
|
855
|
+
* a coupon in it measures that its set does not hold yet, and each prediction its checks make.
|
|
856
|
+
* @param {string} ref project/assembly
|
|
857
|
+
* @returns {Promise<{ assembly: string, rows: { name: string, value: any, unit: string, rule: string, trust: string,
|
|
858
|
+
* coupon: string | null, here: boolean, settle: string, was?: string, code?: string }[] }>} trust is the rule's kind
|
|
859
|
+
* (estimate, community, default), "none yet" for a coupon's figure the set lacks, or "prediction" (code: the check's)
|
|
860
|
+
*/
|
|
861
|
+
export async function assumptions(ref) {
|
|
862
|
+
const [project] = String(ref).split('/');
|
|
863
|
+
if (!fs.existsSync(assemblyPath(ref)))
|
|
864
|
+
throw new CadError(
|
|
865
|
+
'measured.assembly',
|
|
866
|
+
`no assembly "${ref}": give <project>/<assembly>, the one whose figures it lists`,
|
|
867
|
+
);
|
|
868
|
+
const s = await buildAssembly(ref),
|
|
869
|
+
findings = checkAssembly(s),
|
|
870
|
+
printer = printerName(project),
|
|
871
|
+
objects = new Set(s.objects.map((o) => o.objName)),
|
|
872
|
+
settle = (name) => `cad measured ${ref} ${name}=<value>`,
|
|
873
|
+
rows = [],
|
|
874
|
+
listed = new Set();
|
|
875
|
+
const est = new Set(findings.find((x) => x.code === 'rules.estimate')?.refs?.rules ?? []);
|
|
876
|
+
const row = (name, { set, key, data, was, ...shown }) => {
|
|
877
|
+
const c = set ? couponOf(set, key, data, printer) : null;
|
|
878
|
+
listed.add(name);
|
|
879
|
+
rows.push({
|
|
880
|
+
name,
|
|
881
|
+
...shown,
|
|
882
|
+
coupon: c ? `${c.said} (MEASURE.md ${c.rows.includes(' ') ? 'rows' : 'row'} ${c.rows})` : null,
|
|
883
|
+
here: !!c && isHere(c, objects, findings),
|
|
884
|
+
settle: settle(name),
|
|
885
|
+
...(was ? { was } : {}),
|
|
886
|
+
});
|
|
887
|
+
};
|
|
888
|
+
for (const name of rulesUsed(s, findings, printer)) {
|
|
889
|
+
const r = unsettledRow(name, est);
|
|
890
|
+
if (r) row(name, r);
|
|
891
|
+
}
|
|
892
|
+
// what a coupon in this assembly measures that the printer's set does not hold as measured yet
|
|
893
|
+
for (const c of COUPONS.filter((x) => x.names && isHere(x, objects, findings)))
|
|
894
|
+
for (const key of c.names) {
|
|
895
|
+
const r = listed.has(`${printer}.${key}`) ? null : couponFigureRow(project, printer, key);
|
|
896
|
+
if (r) row(`${printer}.${key}`, r);
|
|
897
|
+
}
|
|
898
|
+
// the least settled first: what the set has no figure for, then estimates, community figures and defaults
|
|
899
|
+
const order = ['none yet', ...UNSETTLED];
|
|
900
|
+
rows.sort((a, b) => order.indexOf(a.trust) - order.indexOf(b.trust));
|
|
901
|
+
for (const x of predictionsOf(findings))
|
|
902
|
+
rows.push({
|
|
903
|
+
name: x.predict.name ?? x.code,
|
|
904
|
+
value: x.predict.value,
|
|
905
|
+
unit: x.predict.unit ?? '',
|
|
906
|
+
rule: x.predict.figure,
|
|
907
|
+
trust: 'prediction',
|
|
908
|
+
code: x.code,
|
|
909
|
+
coupon: null,
|
|
910
|
+
here: true,
|
|
911
|
+
settle: settle(x.predict.name ?? x.code),
|
|
912
|
+
});
|
|
913
|
+
return { assembly: ref, rows };
|
|
914
|
+
}
|
|
915
|
+
// a rule the assembly reads, as a row of the table if it is not settled (an estimate, a community figure, a default)
|
|
916
|
+
function unsettledRow(name, est) {
|
|
917
|
+
const at = ruleAt(name);
|
|
918
|
+
// (an object's own estimate, or a rule that could not be read)
|
|
919
|
+
if (!at?.rule) return est.has(name) ? { value: null, unit: '', rule: '', trust: 'estimate' } : null;
|
|
920
|
+
const kind = at.rule.kind ?? (est.has(name) ? 'estimate' : null);
|
|
921
|
+
if (!UNSETTLED.includes(kind) && !est.has(name)) return null;
|
|
922
|
+
return {
|
|
923
|
+
value: at.rule.value,
|
|
924
|
+
unit: at.rule.unit ?? '',
|
|
925
|
+
rule: at.rule.rule ?? '',
|
|
926
|
+
trust: kind ?? 'estimate',
|
|
927
|
+
set: at.set,
|
|
928
|
+
key: at.key,
|
|
929
|
+
data: at.data,
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
// a figure a coupon measures, as a row of the table unless the printer's set holds it settled: what the set holds, else
|
|
933
|
+
// what the kit draws in its place (the model's figure, or the kit's own set's where the workspace's copy is older)
|
|
934
|
+
function couponFigureRow(project, printer, key) {
|
|
935
|
+
const name = `${printer}.${key}`,
|
|
936
|
+
at = ruleAt(name);
|
|
937
|
+
if (at?.rule) {
|
|
938
|
+
if (SETTLED.has(at.rule.kind)) return null;
|
|
939
|
+
const { value, unit = '', rule = '', kind } = at.rule;
|
|
940
|
+
return { value, unit, rule, trust: kind ?? 'estimate', set: printer, key, data: at.data };
|
|
941
|
+
}
|
|
942
|
+
let known = null;
|
|
943
|
+
try {
|
|
944
|
+
known = at ? (slotRule(project, printer, key, at.data) ?? fixingsRule(project, printer, key)) : null;
|
|
945
|
+
} catch {
|
|
946
|
+
// (a figure the fixings cannot work out here: listed without what it was drawn with)
|
|
947
|
+
}
|
|
948
|
+
const kit = known ? null : kitRule(printer, key); // (a set copied before the kit had the rule)
|
|
949
|
+
const from = known
|
|
950
|
+
? { value: known.predicted, unit: known.unit, rule: known.rule ?? known.measuredRule, was: known.was }
|
|
951
|
+
: { value: kit?.value, unit: kit?.unit, rule: kit?.rule, was: kit ? `the kit's ${name}, ${kit.kind}` : undefined };
|
|
952
|
+
return {
|
|
953
|
+
value: from.value ?? null,
|
|
954
|
+
unit: from.unit ?? '',
|
|
955
|
+
rule: from.rule ?? '',
|
|
956
|
+
trust: 'none yet',
|
|
957
|
+
set: printer,
|
|
958
|
+
key,
|
|
959
|
+
data: at?.data,
|
|
960
|
+
was: from.was,
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
// What the kit assumes, as a Markdown table: each rule used that is not yet settled, its trust, what measures it (this
|
|
965
|
+
// assembly, where it is that coupon), and each prediction to measure
|
|
242
966
|
export async function assumptionsTable(ref) {
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
967
|
+
const { rows } = await assumptions(ref),
|
|
968
|
+
figure = (r) =>
|
|
969
|
+
r.value == null ? '' : `${typeof r.value === 'number' ? n(r.value) : r.value}${r.unit ? ` ${r.unit}` : ''}`;
|
|
970
|
+
const cell = (t) => String(t ?? '').replace(/\|/g, '\\|');
|
|
971
|
+
return [
|
|
972
|
+
'| What the kit assumes | What it is | Trust | Measured by | How to settle it |',
|
|
973
|
+
'|---|---|---|---|---|',
|
|
974
|
+
...rows.map((r) =>
|
|
975
|
+
r.trust === 'prediction'
|
|
976
|
+
? `| ${cell(r.rule)} | ${figure(r)}, predicted by \`${r.code}\` | prediction | this assembly | \`${r.settle}\` |`
|
|
977
|
+
: `| \`${r.name}\` | ${[figure(r), cell(r.rule)].filter(Boolean).join(': ')}${r.was ? ` (drawn with ${cell(r.was)})` : ''} | ${r.trust} | ${r.here ? `this assembly, ${r.coupon}` : (r.coupon ?? 'no coupon in the kit: a test print of your own')} | \`${r.settle}\` |`,
|
|
978
|
+
),
|
|
979
|
+
].join('\n');
|
|
251
980
|
}
|
|
981
|
+
/**
|
|
982
|
+
* Take a row out of an assembly's ledger: a figure written by mistake. The rule it went into is in a file of its own
|
|
983
|
+
* (rules/<set>.json), which this leaves as it is: what that file still holds for the name is returned, so the person
|
|
984
|
+
* can set it back there.
|
|
985
|
+
* @param {string | null} ref project/assembly, or null for a row written with no assembly (the workspace's ledger)
|
|
986
|
+
* @param {string} date the row's date, YYYY-MM-DD, as cad history shows it
|
|
987
|
+
* @param {string} which the row's name (x1c.fit, c1.drop), or its name and figure (x1c.fit=0.9) where the ledger holds
|
|
988
|
+
* that name more than once that day
|
|
989
|
+
* @returns {{ struck: object[], lines: string[], rule: { file: string, name: string, value: any, kind: string | null,
|
|
990
|
+
* same: boolean, said: string } | null }}
|
|
991
|
+
*/
|
|
992
|
+
export function strikeLedger(ref, date, which) {
|
|
993
|
+
// (rows written with no assembly: the workspace's ledger, which cad history alone shows)
|
|
994
|
+
const { project, assembly, shown, history } = assemblyOf(ref, 'the one the row is for');
|
|
995
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(String(date)))
|
|
996
|
+
throw new CadError(
|
|
997
|
+
'measured.usage',
|
|
998
|
+
`--strike takes the row's date, as ${history} shows it (2026-12-01), then its name (got "${date}")`,
|
|
999
|
+
);
|
|
1000
|
+
const eq = String(which).indexOf('='),
|
|
1001
|
+
name = eq > 0 ? which.slice(0, eq) : String(which),
|
|
1002
|
+
figure = eq > 0 ? Number(which.slice(eq + 1)) : null;
|
|
1003
|
+
if (eq > 0 && (!which.slice(eq + 1).trim() || !Number.isFinite(figure)))
|
|
1004
|
+
throw new CadError('measured.usage', `${which}: give the row's figure after the "=", as cad history shows it`);
|
|
1005
|
+
const rows = readLedger(project),
|
|
1006
|
+
hit = (e) =>
|
|
1007
|
+
(e.assembly ?? null) === assembly &&
|
|
1008
|
+
e.date === date &&
|
|
1009
|
+
e.name === name &&
|
|
1010
|
+
(figure == null || Math.abs(e.measured - figure) < 1e-9);
|
|
1011
|
+
const struck = rows.filter(hit);
|
|
1012
|
+
if (!struck.length) {
|
|
1013
|
+
const dates = rows.filter((e) => (e.assembly ?? null) === assembly && e.name === name).map((e) => e.date);
|
|
1014
|
+
throw new CadError(
|
|
1015
|
+
'measured.strike',
|
|
1016
|
+
`${shown}: no row for ${which} on ${date} in the ledger` +
|
|
1017
|
+
(dates.length ? ` (it has ${name} on ${[...new Set(dates)].join(', ')})` : ` (${history} lists its rows)`),
|
|
1018
|
+
);
|
|
1019
|
+
}
|
|
1020
|
+
// two measurements of one figure on one day: which is the slip is the person's to say
|
|
1021
|
+
if (figure == null && new Set(struck.map((e) => e.measured)).size > 1)
|
|
1022
|
+
throw new CadError(
|
|
1023
|
+
'measured.strike',
|
|
1024
|
+
`${shown}: the ledger has ${name} ${struck.length} times on ${date} (${struck.map((e) => n(e.measured)).join(', ')}): name the one to strike with its figure, ${name}=${n(struck.at(-1).measured)}`,
|
|
1025
|
+
);
|
|
1026
|
+
const kept = rows.filter((e) => !hit(e));
|
|
1027
|
+
fs.writeFileSync(
|
|
1028
|
+
insideWorkspace(ledgerFile(project)), // (never through a link: rules.mjs)
|
|
1029
|
+
kept.map((e) => JSON.stringify(e)).join('\n') + (kept.length ? '\n' : ''),
|
|
1030
|
+
);
|
|
1031
|
+
// the rule set the figure went into: the workspace's copy, if the name is a rule's
|
|
1032
|
+
let rule = null;
|
|
1033
|
+
const dot = name.indexOf('.'),
|
|
1034
|
+
file = dot > 0 ? path.join(WS, 'rules', `${name.slice(0, dot)}.json`) : null;
|
|
1035
|
+
if (file && fs.existsSync(file)) {
|
|
1036
|
+
let held;
|
|
1037
|
+
try {
|
|
1038
|
+
held = JSON.parse(readOwnText(file)).rules?.[name.slice(dot + 1)];
|
|
1039
|
+
} catch {
|
|
1040
|
+
held = undefined; // (a set that does not read: nothing to say of it)
|
|
1041
|
+
}
|
|
1042
|
+
if (held) {
|
|
1043
|
+
const rel = path.relative(WS, file).split(path.sep).join('/'),
|
|
1044
|
+
same = struck.some((e) => e.measured === held.value);
|
|
1045
|
+
rule = {
|
|
1046
|
+
file: rel,
|
|
1047
|
+
name,
|
|
1048
|
+
value: held.value,
|
|
1049
|
+
kind: held.kind ?? null,
|
|
1050
|
+
same,
|
|
1051
|
+
said: same
|
|
1052
|
+
? `${rel} still holds ${name} = ${held.value} (${held.kind ?? 'no kind'}): the ledger and the rule set are kept apart, so set it back there too (git checkout ${rel}, or cad measured with the right figure)`
|
|
1053
|
+
: `${rel} holds ${name} = ${held.value} (${held.kind ?? 'no kind'}), not the struck figure: the rule set is as it was`,
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
return { struck, lines: ledgerLines(struck), rule };
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/**
|
|
1061
|
+
* What this workspace measured that the kit does not hold yet, to send to the kit: each rule written as measured in a
|
|
1062
|
+
* rule set of the workspace's (rules/<set>.json) whose figure the kit's own set lacks, or holds as anything but a
|
|
1063
|
+
* measurement checked that day or since, as a change to the kit's file (a unified diff against
|
|
1064
|
+
* packages/rules/<set>.json, its layout kept, the rest of the file as the kit has it). A set the kit has no file for
|
|
1065
|
+
* (the joins set, a part of the maker's own) cannot be a change to one: its measured rules are listed apart, to send as
|
|
1066
|
+
* they are.
|
|
1067
|
+
* @returns {{ sets: { set: string, file: string, rules: { name: string, value: any, unit: string, checked: string |
|
|
1068
|
+
* null, source: string, kit: { value: any, kind: string | null } | null }[] }[], alone: { set: string, file: string,
|
|
1069
|
+
* rules: { name: string, value: any, unit: string, checked: string | null, source: string }[] }[], diff: string }}
|
|
1070
|
+
*/
|
|
1071
|
+
export function exportMeasured() {
|
|
1072
|
+
const own = path.join(WS, 'rules'),
|
|
1073
|
+
kitDir = RULE_DIRS.at(-1),
|
|
1074
|
+
sets = [],
|
|
1075
|
+
alone = [],
|
|
1076
|
+
diffs = [];
|
|
1077
|
+
let files = [];
|
|
1078
|
+
try {
|
|
1079
|
+
files =
|
|
1080
|
+
own === kitDir
|
|
1081
|
+
? []
|
|
1082
|
+
: fs
|
|
1083
|
+
.readdirSync(own)
|
|
1084
|
+
.filter((f) => f.endsWith('.json'))
|
|
1085
|
+
.sort();
|
|
1086
|
+
} catch {
|
|
1087
|
+
// (no rules folder: nothing measured here)
|
|
1088
|
+
}
|
|
1089
|
+
for (const f of files) {
|
|
1090
|
+
let data;
|
|
1091
|
+
try {
|
|
1092
|
+
data = JSON.parse(readOwnText(path.join(own, f)));
|
|
1093
|
+
} catch {
|
|
1094
|
+
continue; // (a set that does not read is cad rules check's to name)
|
|
1095
|
+
}
|
|
1096
|
+
const set = f.slice(0, -5),
|
|
1097
|
+
kitFile = path.join(kitDir, f),
|
|
1098
|
+
kitText = fs.existsSync(kitFile) ? fs.readFileSync(kitFile, 'utf8') : null,
|
|
1099
|
+
kit = kitText ? (JSON.parse(kitText).rules ?? {}) : null;
|
|
1100
|
+
const row = (key, r) => ({
|
|
1101
|
+
name: `${set}.${key}`,
|
|
1102
|
+
value: r.value,
|
|
1103
|
+
unit: r.unit ?? '',
|
|
1104
|
+
checked: r.checked ?? null,
|
|
1105
|
+
source: r.source ?? '',
|
|
1106
|
+
});
|
|
1107
|
+
// a measured figure the kit has as well as this, or measured since, is the kit's already
|
|
1108
|
+
const theirs = (key, r) =>
|
|
1109
|
+
kit?.[key]?.kind === 'measured' &&
|
|
1110
|
+
(JSON.stringify(kit[key]) === JSON.stringify(r) || String(kit[key].checked ?? '') >= String(r.checked ?? ''));
|
|
1111
|
+
const mine = Object.entries(data.rules ?? {}).filter(([key, r]) => r?.kind === 'measured' && !theirs(key, r));
|
|
1112
|
+
if (!mine.length) continue;
|
|
1113
|
+
if (!kitText) {
|
|
1114
|
+
alone.push({ set, file: `rules/${f}`, rules: mine.map(([key, r]) => row(key, r)) });
|
|
1115
|
+
continue;
|
|
1116
|
+
}
|
|
1117
|
+
let text = kitText;
|
|
1118
|
+
for (const [key, r] of mine) text = setAt(text, ['rules', key], r);
|
|
1119
|
+
sets.push({
|
|
1120
|
+
set,
|
|
1121
|
+
file: `packages/rules/${f}`,
|
|
1122
|
+
rules: mine.map(([key, r]) => ({
|
|
1123
|
+
...row(key, r),
|
|
1124
|
+
kit: kit[key] ? { value: kit[key].value, kind: kit[key].kind ?? null } : null,
|
|
1125
|
+
})),
|
|
1126
|
+
});
|
|
1127
|
+
diffs.push(unifiedDiff(`packages/rules/${f}`, kitText, text));
|
|
1128
|
+
}
|
|
1129
|
+
return { sets, alone, diff: diffs.join('') };
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
// The lines of two texts as a shortest list of steps, each [' ', line] kept, ['-', line] gone or ['+', line] new:
|
|
1133
|
+
// Myers' method, quick where two texts differ in a few places, as a rule set with a few figures changed does
|
|
1134
|
+
function editScript(a, b) {
|
|
1135
|
+
const trace = [];
|
|
1136
|
+
let v = new Map([[1, 0]]);
|
|
1137
|
+
search: for (let d = 0; d <= a.length + b.length; d++) {
|
|
1138
|
+
trace.push(v);
|
|
1139
|
+
v = new Map(v);
|
|
1140
|
+
for (let k = -d; k <= d; k += 2) {
|
|
1141
|
+
let x = k === -d || (k !== d && v.get(k - 1) < v.get(k + 1)) ? v.get(k + 1) : v.get(k - 1) + 1,
|
|
1142
|
+
y = x - k;
|
|
1143
|
+
while (x < a.length && y < b.length && a[x] === b[y]) (x++, y++);
|
|
1144
|
+
v.set(k, x);
|
|
1145
|
+
if (x >= a.length && y >= b.length) break search;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
const steps = [];
|
|
1149
|
+
let x = a.length,
|
|
1150
|
+
y = b.length;
|
|
1151
|
+
for (let d = trace.length - 1; d >= 0; d--) {
|
|
1152
|
+
const t = trace[d],
|
|
1153
|
+
k = x - y,
|
|
1154
|
+
back = k === -d || (k !== d && t.get(k - 1) < t.get(k + 1)) ? k + 1 : k - 1,
|
|
1155
|
+
px = t.get(back),
|
|
1156
|
+
py = px - back;
|
|
1157
|
+
for (; x > px && y > py; x--, y--) steps.push([' ', a[x - 1]]);
|
|
1158
|
+
if (d > 0) steps.push(x === px ? ['+', b[py]] : ['-', a[px]]);
|
|
1159
|
+
x = px;
|
|
1160
|
+
y = py;
|
|
1161
|
+
}
|
|
1162
|
+
return steps.reverse();
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
// Two texts' difference as a unified diff of one file, three lines kept round each change (what git apply and patch read)
|
|
1166
|
+
function unifiedDiff(file, before, after) {
|
|
1167
|
+
const lines = (t) => (t.endsWith('\n') ? t.slice(0, -1) : t).split('\n'),
|
|
1168
|
+
steps = editScript(lines(before), lines(after)),
|
|
1169
|
+
at = [], // each step's line in the old text and in the new, counted from 0
|
|
1170
|
+
C = 3;
|
|
1171
|
+
let i = 0,
|
|
1172
|
+
j = 0;
|
|
1173
|
+
for (const [op] of steps) {
|
|
1174
|
+
at.push([i, j]);
|
|
1175
|
+
if (op !== '+') i++;
|
|
1176
|
+
if (op !== '-') j++;
|
|
1177
|
+
}
|
|
1178
|
+
const changed = steps.flatMap(([op], s) => (op === ' ' ? [] : [s]));
|
|
1179
|
+
if (!changed.length) return '';
|
|
1180
|
+
const hunks = [];
|
|
1181
|
+
for (const s of changed) {
|
|
1182
|
+
const last = hunks.at(-1);
|
|
1183
|
+
if (last && s - last[1] <= 2 * C) last[1] = s;
|
|
1184
|
+
else hunks.push([s, s]);
|
|
1185
|
+
}
|
|
1186
|
+
const out = [`--- a/${file}`, `+++ b/${file}`];
|
|
1187
|
+
for (const [s, e] of hunks) {
|
|
1188
|
+
const from = Math.max(0, s - C),
|
|
1189
|
+
to = Math.min(steps.length - 1, e + C),
|
|
1190
|
+
part = steps.slice(from, to + 1),
|
|
1191
|
+
olds = part.filter(([op]) => op !== '+').length,
|
|
1192
|
+
news = part.filter(([op]) => op !== '-').length;
|
|
1193
|
+
const [oi, nj] = at[from];
|
|
1194
|
+
out.push(`@@ -${olds ? oi + 1 : oi},${olds} +${news ? nj + 1 : nj},${news} @@`);
|
|
1195
|
+
for (const [op, line] of part) out.push(`${op}${line}`);
|
|
1196
|
+
}
|
|
1197
|
+
return out.join('\n') + '\n';
|
|
1198
|
+
}
|
|
1199
|
+
|
|
252
1200
|
// A table kept in a file, between two markers: the rest of the file stays as written
|
|
253
1201
|
export const MARK = ['<!-- cad measured: start -->', '<!-- cad measured: end -->'];
|
|
254
1202
|
export function writeTable(file, table) {
|
|
255
|
-
const text = fs.existsSync(file) ?
|
|
1203
|
+
const text = fs.existsSync(file) ? readOwnText(file) : '',
|
|
1204
|
+
a = text.indexOf(MARK[0]),
|
|
1205
|
+
b = text.indexOf(MARK[1]);
|
|
256
1206
|
const block = `${MARK[0]}\n${table}\n${MARK[1]}`;
|
|
257
|
-
fs.writeFileSync(
|
|
1207
|
+
fs.writeFileSync(
|
|
1208
|
+
file,
|
|
1209
|
+
a >= 0 && b > a
|
|
1210
|
+
? text.slice(0, a) + block + text.slice(b + MARK[1].length)
|
|
1211
|
+
: `${text}${text && !text.endsWith('\n') ? '\n' : ''}\n${block}\n`,
|
|
1212
|
+
);
|
|
258
1213
|
}
|
|
259
1214
|
|
|
260
|
-
export const ledgerLines = (entries) =>
|
|
1215
|
+
export const ledgerLines = (entries) =>
|
|
1216
|
+
entries.map(
|
|
1217
|
+
(e) =>
|
|
1218
|
+
`${e.date} ${e.assembly ? `${e.assembly} ` : ''}${e.name}: measured ${n(e.measured)} ${e.unit || ''}`.trimEnd() +
|
|
1219
|
+
(e.predicted != null
|
|
1220
|
+
? `, predicted ${n(e.predicted)}${e.was ? ` (${e.was})` : ''}${e.ratio != null ? `, ratio ${n(e.ratio)}` : ''}`
|
|
1221
|
+
: '') +
|
|
1222
|
+
(e.source ? ` [${e.source}]` : ''),
|
|
1223
|
+
);
|