@rulecad/fixings 0.1.8 → 0.2.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/CHANGELOG.md +36 -0
- package/README.md +1 -1
- package/bolts.d.mts +26 -18
- package/bolts.mjs +201 -49
- package/draw.d.mts +4 -2
- package/draw.mjs +58 -17
- package/fixings.d.mts +9 -6
- package/fixings.mjs +185 -26
- package/hardware.d.mts +42 -20
- package/hardware.mjs +388 -80
- package/holes.d.mts +32 -25
- package/holes.mjs +243 -61
- package/magnets.d.mts +45 -30
- package/magnets.mjs +521 -122
- package/model.d.mts +47 -29
- package/model.mjs +751 -134
- package/package.json +16 -30
- package/pins.d.mts +46 -32
- package/pins.mjs +480 -113
- package/seats.d.mts +61 -30
- package/seats.mjs +770 -144
- package/snaps.d.mts +32 -24
- package/snaps.mjs +770 -180
- package/strength.d.mts +65 -35
- package/strength.mjs +850 -181
- package/threads.d.mts +59 -42
- package/threads.mjs +910 -193
package/model.mjs
CHANGED
|
@@ -31,7 +31,32 @@ import engagementSet from '@rulecad/rules/engagement.json' with { type: 'json' }
|
|
|
31
31
|
import s275 from '@rulecad/rules/s275.json' with { type: 'json' };
|
|
32
32
|
import ss304 from '@rulecad/rules/ss304.json' with { type: 'json' };
|
|
33
33
|
|
|
34
|
-
const KIT = [
|
|
34
|
+
const KIT = [
|
|
35
|
+
m1_6,
|
|
36
|
+
m2,
|
|
37
|
+
m2_5,
|
|
38
|
+
m3,
|
|
39
|
+
m4,
|
|
40
|
+
m5,
|
|
41
|
+
m6,
|
|
42
|
+
m8,
|
|
43
|
+
m10,
|
|
44
|
+
m12,
|
|
45
|
+
unc_4_40,
|
|
46
|
+
unc_6_32,
|
|
47
|
+
unc_8_32,
|
|
48
|
+
unc_10_24,
|
|
49
|
+
unf_10_32,
|
|
50
|
+
unc_1_4_20,
|
|
51
|
+
unc_5_16_18,
|
|
52
|
+
unc_3_8_16,
|
|
53
|
+
pt_k22,
|
|
54
|
+
pt_k25,
|
|
55
|
+
pt_k30,
|
|
56
|
+
pt_k35,
|
|
57
|
+
pt_k40,
|
|
58
|
+
pt_k50,
|
|
59
|
+
];
|
|
35
60
|
const KIT_BY_NAME = Object.fromEntries(KIT.map((s) => [s.name, s]));
|
|
36
61
|
|
|
37
62
|
/**
|
|
@@ -39,50 +64,347 @@ const KIT_BY_NAME = Object.fromEntries(KIT.map((s) => [s.name, s]));
|
|
|
39
64
|
* @type {Record<string, { value: number, unit: string, rule: string, kind: string, source: string, checked: string }>}
|
|
40
65
|
*/
|
|
41
66
|
export const DEFAULTS = {
|
|
42
|
-
nut_sink: {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
67
|
+
nut_sink: {
|
|
68
|
+
value: 2,
|
|
69
|
+
unit: 'layers',
|
|
70
|
+
rule: 'A nut pocket is this many layers deeper than the nut is tall, so the nut sits under the face it went in by',
|
|
71
|
+
kind: 'community',
|
|
72
|
+
source: "How the kit's lever bench draws its M4 and M5 nut pockets (examples/bench: nut_height + layer * 2)",
|
|
73
|
+
checked: '2026-11-15',
|
|
74
|
+
},
|
|
75
|
+
nut_slack: {
|
|
76
|
+
value: 1,
|
|
77
|
+
unit: 'layers',
|
|
78
|
+
rule:
|
|
79
|
+
'A nut dropped in mid-print lies at least this far under the layer printed over it, so the nozzle clears ' +
|
|
80
|
+
'a nut that sits a little high',
|
|
81
|
+
kind: 'estimate',
|
|
82
|
+
source: 'A working margin of this package, to be measured on the fixings coupon',
|
|
83
|
+
checked: '2026-11-15',
|
|
84
|
+
},
|
|
85
|
+
pitch_diameter: {
|
|
86
|
+
value: (3 * Math.sqrt(3)) / 8,
|
|
87
|
+
unit: '× pitch',
|
|
88
|
+
rule:
|
|
89
|
+
"A 60° thread's pitch diameter is its major diameter less 0.6495 of its pitch: the diameter a threaded " +
|
|
90
|
+
'length weighs as',
|
|
91
|
+
kind: 'spec',
|
|
92
|
+
source:
|
|
93
|
+
'The ISO 68-1 basic profile, as worked at https://en.wikipedia.org/wiki/ISO_metric_screw_thread (D_p = ' +
|
|
94
|
+
'D_maj - 0.649519 P)',
|
|
95
|
+
checked: '2026-11-15',
|
|
96
|
+
},
|
|
97
|
+
steel_density: {
|
|
98
|
+
...s275.rules.density,
|
|
99
|
+
rule:
|
|
100
|
+
"Density of steel, for a screw's, nut's or washer's mass (the kit's S275 figure: carbon steels differ by " +
|
|
101
|
+
'under 1%)',
|
|
102
|
+
},
|
|
46
103
|
stainless_density: { ...ss304.rules.density, rule: 'Density of 304 stainless steel (A2), for stainless hardware' },
|
|
47
|
-
thread_layers: {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
104
|
+
thread_layers: {
|
|
105
|
+
value: 5,
|
|
106
|
+
unit: 'layers',
|
|
107
|
+
rule:
|
|
108
|
+
'A printed thread needs this many layers to a pitch: a layer or two on each flank and one on the crest. ' +
|
|
109
|
+
'Finer, and the layers no longer draw the tooth',
|
|
110
|
+
kind: 'estimate',
|
|
111
|
+
source:
|
|
112
|
+
'A working figure of this package: at a 0.2 mm layer it makes M6 (1 mm pitch) the smallest metric ' +
|
|
113
|
+
'thread drawn. To be measured on the fixings coupon',
|
|
114
|
+
checked: '2026-11-15',
|
|
115
|
+
},
|
|
116
|
+
thread_lines: {
|
|
117
|
+
value: 1,
|
|
118
|
+
unit: 'lines',
|
|
119
|
+
rule:
|
|
120
|
+
"A printed thread's tooth is at least this many print lines deep, measured on the standard's basic " +
|
|
121
|
+
"profile: a shallower tooth is lost in the wall's outer line",
|
|
122
|
+
kind: 'estimate',
|
|
123
|
+
source: 'A working figure of this package, to be measured on the fixings coupon',
|
|
124
|
+
checked: '2026-11-15',
|
|
125
|
+
},
|
|
126
|
+
thread_crest: {
|
|
127
|
+
value: 0.125,
|
|
128
|
+
unit: '× pitch',
|
|
129
|
+
rule:
|
|
130
|
+
'The least flat left on the crest of a printed tooth whose down-facing flank has been laid back: an ' +
|
|
131
|
+
'eighth of the pitch, the crest flat ISO 68-1 gives an external thread',
|
|
132
|
+
kind: 'default',
|
|
133
|
+
source:
|
|
134
|
+
"ISO 68-1 basic profile (the outermost eighth of the triangle's height is cut off), as worked at " +
|
|
135
|
+
'https://en.wikipedia.org/wiki/ISO_metric_screw_thread',
|
|
136
|
+
checked: '2026-11-15',
|
|
137
|
+
},
|
|
138
|
+
shear_ratio: {
|
|
139
|
+
value: 1 / Math.sqrt(3),
|
|
140
|
+
unit: '× strength',
|
|
141
|
+
rule:
|
|
142
|
+
"A material's shear strength is taken as its tensile strength (a plastic's) or yield strength (a " +
|
|
143
|
+
"metal's) over the square root of 3: an estimate. In a printed part the bond between layers and the " +
|
|
144
|
+
'walls round the hole decide the real figure',
|
|
145
|
+
kind: 'estimate',
|
|
146
|
+
source:
|
|
147
|
+
'The von Mises criterion: in pure shear a material yields at its tensile yield stress over √3, ' +
|
|
148
|
+
'https://en.wikipedia.org/wiki/Von_Mises_yield_criterion',
|
|
149
|
+
checked: '2026-11-15',
|
|
150
|
+
},
|
|
151
|
+
nut_factor: {
|
|
152
|
+
value: 0.2,
|
|
153
|
+
unit: 'T / (d F)',
|
|
154
|
+
rule:
|
|
155
|
+
'The nut factor K in T = K d F, for plain steel threads with a friction of about 0.15: an estimate. ' +
|
|
156
|
+
'Preload from a torque wrench scatters by 25% lubricated and 35% dry, and a lubricated or plated screw ' +
|
|
157
|
+
'has a lower K',
|
|
158
|
+
kind: 'estimate',
|
|
159
|
+
source:
|
|
160
|
+
'Bolted joint, https://en.wikipedia.org/wiki/Bolted_joint (K about 0.20 at a friction of 0.15; the ' +
|
|
161
|
+
'accuracy of each tightening method)',
|
|
162
|
+
checked: '2026-11-15',
|
|
163
|
+
},
|
|
164
|
+
preload_share: {
|
|
165
|
+
value: 0.75,
|
|
166
|
+
unit: '× proof load',
|
|
167
|
+
rule:
|
|
168
|
+
'A screw tightened fully carries about three quarters of its proof load: the preload taken where none is ' +
|
|
169
|
+
'given',
|
|
170
|
+
kind: 'community',
|
|
171
|
+
source:
|
|
172
|
+
'Bolted joint, https://en.wikipedia.org/wiki/Bolted_joint (a preload of at least 75% of the proof load ' +
|
|
173
|
+
'for structural joints, up to 90% for permanent ones)',
|
|
174
|
+
checked: '2026-11-15',
|
|
175
|
+
},
|
|
176
|
+
insert_engagement: {
|
|
177
|
+
value: 1,
|
|
178
|
+
unit: '× diameter',
|
|
179
|
+
rule:
|
|
180
|
+
'A screw engages a brass heat-set insert by at least its own diameter, or the whole insert where that is ' +
|
|
181
|
+
'shorter: an estimate, the figure the engagement rules give for steel',
|
|
182
|
+
kind: 'estimate',
|
|
183
|
+
source:
|
|
184
|
+
"A working figure of this package, after engagement.steel in @rulecad/rules; no insert maker's figure was found",
|
|
185
|
+
checked: '2026-11-15',
|
|
186
|
+
},
|
|
187
|
+
boss_diameter: {
|
|
188
|
+
value: 2,
|
|
189
|
+
unit: '× diameter',
|
|
190
|
+
rule:
|
|
191
|
+
'Plastic a screw forms or cuts its own thread in (a pilot, a tapped or a printed thread) stands at least ' +
|
|
192
|
+
"this many of the screw's diameters across round the hole, so it does not split as the thread is formed: a " +
|
|
193
|
+
"screw maker's guide for moulded bosses, taken for printed ones too",
|
|
194
|
+
kind: 'community',
|
|
195
|
+
source:
|
|
196
|
+
'TR Fastenings, Plas-Tech 30-20 technical information (a boss of at least twice the nominal diameter, ' +
|
|
197
|
+
'physical testing recommended), ' +
|
|
198
|
+
'https://trfastenings.com/knowledge-base/fasteners-for-plastic/plas-tech-30-20-technical-information',
|
|
199
|
+
checked: '2026-11-29',
|
|
200
|
+
},
|
|
201
|
+
tip_room: {
|
|
202
|
+
value: 1,
|
|
203
|
+
unit: 'mm',
|
|
204
|
+
rule:
|
|
205
|
+
"A screw's tip stops at least this far short of the bottom of a blind hole, so it tightens on its head " +
|
|
206
|
+
"and not on its tip: an estimate, the millimetre an insert's blind hole is given past the insert",
|
|
207
|
+
kind: 'estimate',
|
|
208
|
+
source:
|
|
209
|
+
"A working figure of this package, after insert_depth in the thread rule sets (the insert's length and 1 mm)",
|
|
210
|
+
checked: '2026-11-15',
|
|
211
|
+
},
|
|
212
|
+
load_margin: {
|
|
213
|
+
value: 2,
|
|
214
|
+
unit: '× load',
|
|
215
|
+
rule: 'A printed joint is sized to hold about twice the load it is given',
|
|
216
|
+
kind: 'default',
|
|
217
|
+
source: "The kit's standard for printed parts: sized from the load with about twice the margin",
|
|
218
|
+
checked: '2026-11-15',
|
|
219
|
+
},
|
|
220
|
+
clamp_friction: {
|
|
221
|
+
value: 0.3,
|
|
222
|
+
unit: '× clamp force',
|
|
223
|
+
rule:
|
|
224
|
+
'The friction between two printed parts clamped by screws, for whether a joint slips before its screws ' +
|
|
225
|
+
'bear on their holes: an estimate. No measured figure for PLA on PLA at rest was found; the figures ' +
|
|
226
|
+
'found are for printed PLA sliding on steel, about 0.38 to 0.57, and 0.3 is taken under them',
|
|
227
|
+
kind: 'estimate',
|
|
228
|
+
source:
|
|
229
|
+
'Sliding friction of printed PLA on steel: Pawlak, kinetic 0.49 ' +
|
|
230
|
+
'(https://yadda.icm.edu.pl/baztech/element/' +
|
|
231
|
+
'bwmeta1.element.baztech-f7c60c77-2f7e-4c4f-8d4e-f962d0c503e7/c/pawlak.pdf), ' +
|
|
232
|
+
'and Sirin et al., Rapid Prototyping Journal 2023, 0.38 to 0.57 pin on disc ' +
|
|
233
|
+
'(https://www.emerald.com/insight/content/doi/10.1108/RPJ-03-2022-0081/full/html); to be measured on ' +
|
|
234
|
+
'the fixings coupon',
|
|
235
|
+
checked: '2026-11-15',
|
|
236
|
+
},
|
|
237
|
+
snap_play: {
|
|
238
|
+
value: 0.5,
|
|
239
|
+
unit: 'mm',
|
|
240
|
+
rule:
|
|
241
|
+
"A snap pin's shaft may be at most this much longer than the parts it goes through: the barb clicks out " +
|
|
242
|
+
'that far past the far face. Longer, and the pin rattles in its hole',
|
|
243
|
+
kind: 'estimate',
|
|
244
|
+
source:
|
|
245
|
+
"A working figure of this package: the launcher's pins are drawn 0.2 mm (x1c.fit) longer than their " +
|
|
246
|
+
'parts. No coupon row measures it, so it stays an estimate: snap pins a step longer each through one ' +
|
|
247
|
+
'plate, the longest that does not rattle, would measure it',
|
|
248
|
+
checked: '2026-11-21',
|
|
249
|
+
},
|
|
250
|
+
pin_least: {
|
|
251
|
+
value: 2,
|
|
252
|
+
unit: 'mm',
|
|
253
|
+
rule:
|
|
254
|
+
'The thinnest snap pin or printed plain pin: under this a peg is a few print lines across and breaks as ' +
|
|
255
|
+
'it goes in. Thinner pins are filament or steel',
|
|
256
|
+
kind: 'estimate',
|
|
257
|
+
source:
|
|
258
|
+
'A working figure of this package: about five 0.42 mm lines across, the thinnest the kit draws a snap ' +
|
|
259
|
+
"pin (2.5 mm, the tensioner's) less a margin. No coupon row measures it (the holds coupon's thinnest " +
|
|
260
|
+
'snap pin is 3 mm), so it stays an estimate: snap pins of 1.5, 2 and 2.5 mm pushed through their ' +
|
|
261
|
+
'sockets, the thinnest that does not break, would measure it',
|
|
262
|
+
checked: '2026-11-21',
|
|
263
|
+
},
|
|
264
|
+
magnet_floor: {
|
|
265
|
+
value: 3,
|
|
266
|
+
unit: 'layers',
|
|
267
|
+
rule:
|
|
268
|
+
"The least floor left under a magnet's pocket: three solid layers, so the floor prints whole and the " +
|
|
269
|
+
'magnet cannot be pulled through it. A floor the magnet pulls through is also its gap, so thinner pulls ' +
|
|
270
|
+
'harder',
|
|
271
|
+
kind: 'estimate',
|
|
272
|
+
source:
|
|
273
|
+
"A working figure of this package, after Bambu Studio's default of three bottom shell layers. No " +
|
|
274
|
+
"coupon row measures it (the holds coupon's pockets have a hole under them), so it stays an estimate: " +
|
|
275
|
+
'pockets with floors of 2, 3 and 4 layers, the thinnest a magnet does not pull through or show ' +
|
|
276
|
+
'through, would measure it',
|
|
277
|
+
checked: '2026-11-21',
|
|
278
|
+
},
|
|
279
|
+
brass_density: {
|
|
280
|
+
value: 8.47,
|
|
281
|
+
unit: 'g/cm³',
|
|
282
|
+
rule:
|
|
283
|
+
"Density of free-machining brass CW614N (CuZn39Pb3), taken for a heat-set insert. The insert maker's " +
|
|
284
|
+
'sheet names no alloy, so the alloy is assumed',
|
|
285
|
+
kind: 'estimate',
|
|
286
|
+
source:
|
|
287
|
+
'Aalco, CW614N brass rod datasheet, ' +
|
|
288
|
+
'https://www.aalco.co.uk/datasheets/Copper-and-Copper-Alloys-CW614N-Brass-Rod_31.ashx',
|
|
289
|
+
checked: '2026-11-15',
|
|
290
|
+
},
|
|
291
|
+
snap_brief: {
|
|
292
|
+
value: 0.7,
|
|
293
|
+
unit: '× yield strain',
|
|
294
|
+
rule:
|
|
295
|
+
'An annular snap may strain a plastic to this share of its yield strain as it snaps: the guide gives ' +
|
|
296
|
+
'about 70% for an amorphous plastic in a single brief snap (a partly crystalline one may go almost to ' +
|
|
297
|
+
'yield). Printed PLA and PETG are amorphous and the guide lists neither, so it is applied to them ' +
|
|
298
|
+
'unchecked; the yield strain is taken as strength over modulus, which sits below the real one, on the ' +
|
|
299
|
+
'safe side',
|
|
300
|
+
kind: 'community',
|
|
301
|
+
source:
|
|
302
|
+
'Bayer MaterialScience, Snap-Fit Joints for Plastics: A Design Guide, copy at ' +
|
|
303
|
+
'https://fab.cba.mit.edu/classes/S62.12/people/vernelle.noel/Plastic_Snap_fit_design.pdf, page 11, ' +
|
|
304
|
+
'Permissible undercut',
|
|
305
|
+
checked: '2026-11-23',
|
|
306
|
+
},
|
|
307
|
+
snap_often: {
|
|
308
|
+
value: 0.6,
|
|
309
|
+
unit: '× brief strain',
|
|
310
|
+
rule: 'A snap opened and closed often, as a lid is, is held to this share of the brief-snap strain',
|
|
311
|
+
kind: 'community',
|
|
312
|
+
source:
|
|
313
|
+
'Bayer MaterialScience, Snap-Fit Joints for Plastics: A Design Guide, copy at ' +
|
|
314
|
+
'https://fab.cba.mit.edu/classes/S62.12/people/vernelle.noel/Plastic_Snap_fit_design.pdf, page 12, ' +
|
|
315
|
+
'Table 2: for frequent separation and rejoining, use about 60% of these values',
|
|
316
|
+
checked: '2026-11-23',
|
|
317
|
+
},
|
|
318
|
+
snap_remote: {
|
|
319
|
+
value: 3,
|
|
320
|
+
unit: '× near-end force',
|
|
321
|
+
rule:
|
|
322
|
+
"An annular snap at least 1.8 √(d t) in from its tube's free end is about this many times as stiff as " +
|
|
323
|
+
'one at the end: four times in theory, and tests rarely give more than three. Between the two the kit ' +
|
|
324
|
+
'takes a straight line, an estimate',
|
|
325
|
+
kind: 'community',
|
|
326
|
+
source:
|
|
327
|
+
'Bayer MaterialScience, Snap-Fit Joints for Plastics: A Design Guide, copy at ' +
|
|
328
|
+
'https://fab.cba.mit.edu/classes/S62.12/people/vernelle.noel/Plastic_Snap_fit_design.pdf, page 22',
|
|
329
|
+
checked: '2026-11-23',
|
|
330
|
+
},
|
|
331
|
+
snap_friction: {
|
|
332
|
+
value: 0.5,
|
|
333
|
+
unit: 'μ',
|
|
334
|
+
rule:
|
|
335
|
+
'Friction between two printed parts of one plastic sliding over each other as a snap goes home: an ' +
|
|
336
|
+
'estimate. The guide lists no PLA or PETG; for the glassy plastics nearest them it gives 0.40 to 0.60 on ' +
|
|
337
|
+
'steel (PS, SAN, PC, PMMA), and 1.2 times that for a plastic on itself',
|
|
338
|
+
kind: 'estimate',
|
|
339
|
+
source:
|
|
340
|
+
'Bayer MaterialScience, Snap-Fit Joints for Plastics: A Design Guide, copy at ' +
|
|
341
|
+
'https://fab.cba.mit.edu/classes/S62.12/people/vernelle.noel/Plastic_Snap_fit_design.pdf, page 15, ' +
|
|
342
|
+
'Table 3',
|
|
343
|
+
checked: '2026-11-23',
|
|
344
|
+
},
|
|
345
|
+
snap_least: {
|
|
346
|
+
value: 0.2,
|
|
347
|
+
unit: 'mm',
|
|
348
|
+
rule:
|
|
349
|
+
'The least radial undercut an annular snap is drawn with: twice x1c.hole_comp, the error a printed round ' +
|
|
350
|
+
"comes out with, so the print's own error cannot take the whole of it",
|
|
351
|
+
kind: 'estimate',
|
|
352
|
+
source:
|
|
353
|
+
'A working figure of this package, from x1c.hole_comp (0.1 mm); the fixings coupon has no snap ring ' +
|
|
354
|
+
'yet to measure it',
|
|
355
|
+
checked: '2026-11-23',
|
|
356
|
+
},
|
|
357
|
+
snap_slot: {
|
|
358
|
+
value: 1,
|
|
359
|
+
unit: 'mm',
|
|
360
|
+
rule:
|
|
361
|
+
"The width a slotted snap skirt's slots are drawn: wide enough to print apart with room to spare " +
|
|
362
|
+
'(x1c.print_gap, 0.3 mm, is the narrowest gap that does) and to keep clean, and narrow enough to leave ' +
|
|
363
|
+
'the fingers most of the skirt. An estimate',
|
|
364
|
+
kind: 'estimate',
|
|
365
|
+
source:
|
|
366
|
+
'A working figure of this package, over x1c.print_gap (0.3 mm); the fixings coupon has no slotted ' +
|
|
367
|
+
'skirt yet to measure it',
|
|
368
|
+
checked: '2026-11-23',
|
|
369
|
+
},
|
|
67
370
|
};
|
|
68
371
|
|
|
69
372
|
// ---------- small things every module here uses ----------
|
|
70
373
|
export const r3 = (x) => +x.toFixed(3);
|
|
71
374
|
export const mm = (x) => String(+Number(x).toFixed(3));
|
|
72
375
|
export const isNum = (x) => typeof x === 'number' && Number.isFinite(x);
|
|
73
|
-
export const shown = (v) => {
|
|
74
|
-
|
|
376
|
+
export const shown = (v) => {
|
|
377
|
+
const s = v === undefined ? 'nothing' : (JSON.stringify(v) ?? String(v));
|
|
378
|
+
return s.length > 80 ? `${s.slice(0, 77)}...` : s;
|
|
379
|
+
}; // (a whole rule set handed in by mistake is not printed back)
|
|
380
|
+
export const near = (a, b) => {
|
|
381
|
+
// edits between two names, to suggest the nearest
|
|
75
382
|
const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
|
|
76
383
|
for (let j = 1; j <= b.length; j++) d[0][j] = j;
|
|
77
|
-
for (let i = 1; i <= a.length; i++)
|
|
384
|
+
for (let i = 1; i <= a.length; i++)
|
|
385
|
+
for (let j = 1; j <= b.length; j++)
|
|
386
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
78
387
|
return d[a.length][b.length];
|
|
79
388
|
};
|
|
80
|
-
export function positive(what, k, v) {
|
|
81
|
-
|
|
389
|
+
export function positive(what, k, v) {
|
|
390
|
+
if (!(isNum(v) && v > 0)) throw new Error(`${what}: ${k} is a number above 0, in mm (got ${shown(v)})`);
|
|
391
|
+
return v;
|
|
392
|
+
}
|
|
393
|
+
export function oneOf(what, k, v, list) {
|
|
394
|
+
if (!list.includes(v))
|
|
395
|
+
throw new Error(`${what}: ${k} is ${list.slice(0, -1).join(', ')} or ${list.at(-1)} (got ${shown(v)})`);
|
|
396
|
+
return v;
|
|
397
|
+
}
|
|
82
398
|
/** A finding in the kit's one shape. */
|
|
83
399
|
export const finding = (level, text, code, extra = {}) => ({ level, text, code, ...extra });
|
|
84
400
|
/** What a result rests on: a figure's name, value, kind and source, without its whole rule. */
|
|
85
|
-
export const rest = (name, f) => ({
|
|
401
|
+
export const rest = (name, f) => ({
|
|
402
|
+
name,
|
|
403
|
+
value: f.value,
|
|
404
|
+
...(f.unit ? { unit: f.unit } : {}),
|
|
405
|
+
kind: f.kind,
|
|
406
|
+
source: f.source,
|
|
407
|
+
});
|
|
86
408
|
const article = (s) => (/^(M|8-)/.test(s) ? 'an' : 'a');
|
|
87
409
|
/** "an M3", "a 1/4-20 UNC", "a PT K30". */
|
|
88
410
|
export const a = (thread) => `${article(thread)} ${thread}`;
|
|
@@ -102,55 +424,132 @@ export const GRAMMAR = [
|
|
|
102
424
|
'word = "cap" | "button" | "countersunk" | "hex" | "set" | "pan" a screw\'s head form',
|
|
103
425
|
' | [ "hex" | "square" | "lock" ] "nut" a nut (hex if not said)',
|
|
104
426
|
' | "washer" | "insert" a plain washer, a heat-set insert',
|
|
105
|
-
|
|
106
|
-
' | "printed" a screw or nut printed with its
|
|
427
|
+
" | a variant of the thread's rule set torx, on a PT screw",
|
|
428
|
+
' | "printed" a screw or nut printed with its ' +
|
|
429
|
+
'thread, not bought',
|
|
107
430
|
].join('\n');
|
|
108
431
|
|
|
109
432
|
/** The head forms a screw's name can say. */
|
|
110
433
|
export const HEADS = ['cap', 'button', 'countersunk', 'hex', 'set', 'pan'];
|
|
111
434
|
/** The kinds of nut a name can say. */
|
|
112
435
|
export const NUTS = ['hex', 'square', 'lock'];
|
|
113
|
-
const HEAD_WORDS = {
|
|
436
|
+
const HEAD_WORDS = {
|
|
437
|
+
cap: 'cap',
|
|
438
|
+
socket: 'cap',
|
|
439
|
+
button: 'button',
|
|
440
|
+
countersunk: 'countersunk',
|
|
441
|
+
csk: 'countersunk',
|
|
442
|
+
flat: 'countersunk',
|
|
443
|
+
hex: 'hex',
|
|
444
|
+
set: 'set',
|
|
445
|
+
grub: 'set',
|
|
446
|
+
pan: 'pan',
|
|
447
|
+
};
|
|
114
448
|
const NUT_WORDS = { hex: 'hex', square: 'square', lock: 'lock', nyloc: 'lock' };
|
|
115
449
|
const NOISE = ['screw', 'bolt', 'head', 'plain', 'heat-set'];
|
|
116
450
|
const ITEMS = ['nut', 'washer', 'insert'];
|
|
117
451
|
const squash = (s) => String(s).toLowerCase().replace(/[\s#]/g, '');
|
|
118
|
-
const inches = (text) => {
|
|
452
|
+
const inches = (text) => {
|
|
453
|
+
const [whole, frac] = /\//.test(text)
|
|
454
|
+
? /\s/.test(text.trim())
|
|
455
|
+
? text.trim().split(/\s+/)
|
|
456
|
+
: ['0', text]
|
|
457
|
+
: [text, '0/1'],
|
|
458
|
+
[n, d] = frac.split('/').map(Number);
|
|
459
|
+
return Number(whole) + n / d;
|
|
460
|
+
};
|
|
119
461
|
|
|
120
462
|
/**
|
|
121
463
|
* A fixing's name read by the grammar, before any rule set is looked at.
|
|
122
464
|
* @param {string} name 'M3', 'M3x12 cap', '1/4-20 x 1/2 cap', 'M4 lock nut', 'K30x10 pan torx'
|
|
123
|
-
* @returns {{ thread: string, form: 'metric' | 'unified' | 'plastic', length: number | null, unit: 'mm' | 'in' | null,
|
|
124
|
-
*
|
|
465
|
+
* @returns {{ thread: string, form: 'metric' | 'unified' | 'plastic', length: number | null, unit: 'mm' | 'in' | null,
|
|
466
|
+
* item: 'thread' | 'screw' | 'nut' | 'washer' | 'insert', head: string | null, nut: string | null, extra: string[],
|
|
467
|
+
* printed?: boolean }}
|
|
468
|
+
* length: as written, in unit (null: the thread's own, mm or inches); extra: words left for the rule set's variants;
|
|
469
|
+
* printed: the word printed was said
|
|
125
470
|
*/
|
|
126
471
|
export function parseName(name) {
|
|
127
|
-
const what = 'fixing name',
|
|
472
|
+
const what = 'fixing name',
|
|
473
|
+
help =
|
|
474
|
+
`A name is a thread, then a length and what it is where it has them: 'M3', 'M3x12 cap', '1/4-20 x 1/2 ` +
|
|
475
|
+
`button', 'M4 lock nut', 'M3 insert'`;
|
|
128
476
|
if (typeof name !== 'string' || !name.trim()) throw new Error(`${what}: give a name, not ${shown(name)}. ${help}`);
|
|
129
477
|
const s = name.trim();
|
|
130
478
|
let t, thread, form;
|
|
131
|
-
if ((t = /^m\s*(\d+(?:\.\d+)?)/i.exec(s))) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
else
|
|
135
|
-
|
|
479
|
+
if ((t = /^m\s*(\d+(?:\.\d+)?)/i.exec(s))) {
|
|
480
|
+
thread = `M${t[1]}`;
|
|
481
|
+
form = 'metric';
|
|
482
|
+
} else if ((t = /^(?:pt\s*)?k\s*(\d+)/i.exec(s))) {
|
|
483
|
+
thread = `K${t[1]}`;
|
|
484
|
+
form = 'plastic';
|
|
485
|
+
} else if ((t = /^#?\s*(\d+\/\d+|\d+)\s*-\s*(\d+)(?:\s*(unc|unf)\b)?/i.exec(s))) {
|
|
486
|
+
thread = `${t[1]}-${t[2]}${t[3] ? ` ${t[3].toUpperCase()}` : ''}`;
|
|
487
|
+
form = 'unified';
|
|
488
|
+
} else throw new Error(`${what}: "${name}" does not start with a thread (M3, M2.5, 6-32, 1/4-20, K30). ${help}`);
|
|
489
|
+
let tail = s.slice(t[0].length),
|
|
490
|
+
length = null,
|
|
491
|
+
unit = null;
|
|
136
492
|
const l = /^\s*[x×*]\s*(\d+\s+\d+\/\d+|\d+\/\d+|\d*\.?\d+)\s*(mm\b|in\b|"|”)?/i.exec(tail);
|
|
137
|
-
if (l) {
|
|
138
|
-
|
|
139
|
-
|
|
493
|
+
if (l) {
|
|
494
|
+
length = inches(l[1]);
|
|
495
|
+
unit = l[2] ? (/mm/i.test(l[2]) ? 'mm' : 'in') : null;
|
|
496
|
+
tail = tail.slice(l[0].length);
|
|
497
|
+
if (!(length > 0)) throw new Error(`${what}: "${name}": a length is above 0 (got ${l[1]})`);
|
|
498
|
+
} else if (/^\s*[x×*]/.test(tail))
|
|
499
|
+
throw new Error(`${what}: "${name}": after the x comes a length (M3x12, 1/4-20 x 1/2). ${help}`);
|
|
500
|
+
const all = tail
|
|
501
|
+
.toLowerCase()
|
|
502
|
+
.split(/[\s,]+/)
|
|
503
|
+
.filter(Boolean)
|
|
504
|
+
.filter((w) => !NOISE.includes(w)),
|
|
505
|
+
printed = all.includes('printed'),
|
|
506
|
+
words = all.filter((w) => w !== 'printed');
|
|
140
507
|
const items = words.filter((w) => ITEMS.includes(w));
|
|
141
508
|
if (items.length > 1) throw new Error(`${what}: "${name}" names ${items.join(' and ')}: one thing at a time`);
|
|
142
|
-
const item = items[0] ?? null,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
509
|
+
const item = items[0] ?? null,
|
|
510
|
+
others = words.filter((w) => !ITEMS.includes(w)),
|
|
511
|
+
table = item === 'nut' ? NUT_WORDS : item ? {} : HEAD_WORDS;
|
|
512
|
+
const forms = [...new Set(others.filter((w) => w in table).map((w) => table[w]))],
|
|
513
|
+
extra = others.filter((w) => !(w in table));
|
|
514
|
+
if (forms.length > 1)
|
|
515
|
+
throw new Error(
|
|
516
|
+
`${what}: "${name}" says ${forms.join(' and ')}: ` +
|
|
517
|
+
(item === 'nut' ? 'a nut is one of' : 'a head is one of') +
|
|
518
|
+
` ${(item === 'nut' ? NUTS : HEADS).join(', ')}`,
|
|
519
|
+
);
|
|
520
|
+
if (item === 'nut' || item === 'washer') {
|
|
521
|
+
if (length != null) throw new Error(`${what}: "${name}": a ${item} has no length`);
|
|
522
|
+
}
|
|
523
|
+
if (printed && (item === 'washer' || item === 'insert' || form === 'plastic'))
|
|
524
|
+
throw new Error(
|
|
525
|
+
`${what}: "${name}": only a screw or a nut is printed with its thread` +
|
|
526
|
+
(form === 'plastic' ? ' (a screw for plastic forms its own thread, and is bought)' : ''),
|
|
527
|
+
);
|
|
528
|
+
return {
|
|
529
|
+
thread,
|
|
530
|
+
form,
|
|
531
|
+
length,
|
|
532
|
+
unit,
|
|
533
|
+
item: item ?? (length != null || forms[0] ? 'screw' : 'thread'),
|
|
534
|
+
head: item ? null : (forms[0] ?? null),
|
|
535
|
+
nut: item === 'nut' ? (forms[0] ?? 'hex') : null,
|
|
536
|
+
extra,
|
|
537
|
+
...(printed ? { printed: true } : {}),
|
|
538
|
+
};
|
|
148
539
|
}
|
|
149
540
|
|
|
150
541
|
// ---------- rule sets by thread ----------
|
|
151
|
-
// The keys a thread is found by: as written, and an inch thread without its series, a plastic screw without its maker's
|
|
152
|
-
|
|
153
|
-
const
|
|
542
|
+
// The keys a thread is found by: as written, and an inch thread without its series, a plastic screw without its maker's
|
|
543
|
+
// PT
|
|
544
|
+
const keysOf = (text) => {
|
|
545
|
+
const k = squash(text);
|
|
546
|
+
return [...new Set([k, k.replace(/un[cf]$/, ''), k.replace(/^pt/, '')])];
|
|
547
|
+
};
|
|
548
|
+
const index = (sets) => {
|
|
549
|
+
const out = new Map();
|
|
550
|
+
for (const s of sets) for (const k of keysOf(s.rules.thread.value)) if (!out.has(k)) out.set(k, s);
|
|
551
|
+
return out;
|
|
552
|
+
};
|
|
154
553
|
const KIT_INDEX = index(KIT);
|
|
155
554
|
const isSet = (s) => !!s && typeof s === 'object' && !!s.rules && typeof s.rules === 'object';
|
|
156
555
|
// A workspace's sets, by the thread each is for: the one its `thread` rule names, or (an older set, from before threads
|
|
@@ -158,12 +557,16 @@ const isSet = (s) => !!s && typeof s === 'object' && !!s.rules && typeof s.rules
|
|
|
158
557
|
const OWN = new WeakMap();
|
|
159
558
|
function ownIndex(what, sets) {
|
|
160
559
|
if (sets == null) return new Map();
|
|
161
|
-
if (typeof sets !== 'object')
|
|
560
|
+
if (typeof sets !== 'object')
|
|
561
|
+
throw new Error(
|
|
562
|
+
`${what}: sets is the workspace's rule sets, by name ({ m3: { name, rules } }) or as a list, not ${shown(sets)}`,
|
|
563
|
+
);
|
|
162
564
|
if (OWN.has(sets)) return OWN.get(sets);
|
|
163
565
|
const out = new Map();
|
|
164
566
|
for (const [n, s] of Array.isArray(sets) ? sets.map((x) => [x?.name, x]) : Object.entries(sets)) {
|
|
165
567
|
if (!isSet(s)) continue;
|
|
166
|
-
const text =
|
|
568
|
+
const text =
|
|
569
|
+
typeof s.rules.thread?.value === 'string' ? s.rules.thread.value : KIT_BY_NAME[s.name ?? n]?.rules.thread.value;
|
|
167
570
|
if (text) for (const k of keysOf(text)) if (!out.has(k)) out.set(k, { ...s, name: s.name ?? n });
|
|
168
571
|
}
|
|
169
572
|
OWN.set(sets, out);
|
|
@@ -175,23 +578,36 @@ function ownIndex(what, sets) {
|
|
|
175
578
|
* @param {{ sets?: object }} [opts] @returns {string[]}
|
|
176
579
|
*/
|
|
177
580
|
export function threads(opts = {}) {
|
|
178
|
-
const own = [...new Set([...ownIndex('threads', opts?.sets).values()])].map(
|
|
581
|
+
const own = [...new Set([...ownIndex('threads', opts?.sets).values()])].map(
|
|
582
|
+
(s) => s.rules.thread?.value ?? KIT_BY_NAME[s.name].rules.thread.value,
|
|
583
|
+
);
|
|
179
584
|
return [...new Set([...own, ...KIT.map((s) => s.rules.thread.value)])];
|
|
180
585
|
}
|
|
181
586
|
|
|
182
587
|
const HEAD_FIGURES = {
|
|
183
588
|
cap: { diameter: 'head', height: 'head_height', key: 'head_key' },
|
|
184
589
|
button: { diameter: 'button_head', height: 'button_head_height', key: 'button_key' },
|
|
185
|
-
countersunk: {
|
|
590
|
+
countersunk: {
|
|
591
|
+
diameter: 'countersunk_head',
|
|
592
|
+
height: 'countersunk_head_height',
|
|
593
|
+
key: 'countersunk_key',
|
|
594
|
+
angle: 'countersunk_angle',
|
|
595
|
+
},
|
|
186
596
|
hex: { flats: 'hex_head_flats', height: 'hex_head_height' },
|
|
187
597
|
set: { key: 'set_screw_key', point: 'set_screw_point' },
|
|
188
|
-
pan: { diameter: 'pan_head', height: 'pan_head_height', drive: 'pan_head_drive' }
|
|
598
|
+
pan: { diameter: 'pan_head', height: 'pan_head_height', drive: 'pan_head_drive' },
|
|
599
|
+
};
|
|
600
|
+
const NUT_FIGURES = {
|
|
601
|
+
hex: { flats: 'nut_flats', height: 'nut_height' },
|
|
602
|
+
square: { flats: 'square_nut_flats', height: 'square_nut_height' },
|
|
603
|
+
lock: { flats: 'lock_nut_flats', height: 'lock_nut_height' },
|
|
189
604
|
};
|
|
190
|
-
const NUT_FIGURES = { hex: { flats: 'nut_flats', height: 'nut_height' }, square: { flats: 'square_nut_flats', height: 'square_nut_height' }, lock: { flats: 'lock_nut_flats', height: 'lock_nut_height' } };
|
|
191
605
|
const first = (o) => Object.values(o)[0];
|
|
192
606
|
const madeOf = (figures) => ({
|
|
193
|
-
heads: HEADS.filter((h) => figures[first(HEAD_FIGURES[h])]),
|
|
194
|
-
|
|
607
|
+
heads: HEADS.filter((h) => figures[first(HEAD_FIGURES[h])]),
|
|
608
|
+
nuts: NUTS.filter((n) => figures[first(NUT_FIGURES[n])]),
|
|
609
|
+
washer: !!figures.washer_diameter,
|
|
610
|
+
insert: !!figures.insert_hole,
|
|
195
611
|
});
|
|
196
612
|
const list = (xs) => (xs.length ? xs.join(', ') : 'none');
|
|
197
613
|
|
|
@@ -205,7 +621,11 @@ export const cite = (m, n) => `${m.set}.${n}`;
|
|
|
205
621
|
export function figure(what, m, n) {
|
|
206
622
|
const f = m.figures[n];
|
|
207
623
|
if (!f) throw new Error(`${what}: ${m.thread} has no ${n} in its rule set (${m.set})`);
|
|
208
|
-
if (!isNum(f.value))
|
|
624
|
+
if (!isNum(f.value))
|
|
625
|
+
throw new Error(
|
|
626
|
+
`${what}: ${cite(m, n)} is written as ${shown(f.value)}: a number is needed here (hand in the rule set ` +
|
|
627
|
+
`with its values worked out)`,
|
|
628
|
+
);
|
|
209
629
|
return f.value;
|
|
210
630
|
}
|
|
211
631
|
|
|
@@ -216,9 +636,15 @@ export function figure(what, m, n) {
|
|
|
216
636
|
*/
|
|
217
637
|
export function headOf(what, m, form) {
|
|
218
638
|
oneOf(what, 'head', form, HEADS);
|
|
219
|
-
if (!m.made.heads.includes(form))
|
|
220
|
-
|
|
221
|
-
|
|
639
|
+
if (!m.made.heads.includes(form))
|
|
640
|
+
throw new Error(
|
|
641
|
+
`${what}: ${m.thread} is not made with a ${form} head, by its rule set (${m.set}): it comes as ` +
|
|
642
|
+
`${list(m.made.heads)}`,
|
|
643
|
+
);
|
|
644
|
+
const names = HEAD_FIGURES[form],
|
|
645
|
+
h = { form, diameter: 0, height: 0, across: 0, figures: Object.values(names).filter((n) => m.figures[n]) };
|
|
646
|
+
for (const [k, n] of Object.entries(names))
|
|
647
|
+
if (m.figures[n]) h[k] = k === 'drive' ? m.figures[n].value : figure(what, m, n);
|
|
222
648
|
h.across = h.flats ? r3(h.flats / Math.cos(Math.PI / 6)) : h.diameter; // (a hexagon's corners)
|
|
223
649
|
return h;
|
|
224
650
|
}
|
|
@@ -226,24 +652,43 @@ export function headOf(what, m, form) {
|
|
|
226
652
|
/**
|
|
227
653
|
* A nut on a thread, from its figures: refused by name where that size has none.
|
|
228
654
|
* @param {string} what @param {Fixing} m @param {string} kind hex, square or lock
|
|
229
|
-
* @returns {{ kind: string, flats: number, height: number, across: number, figures: string[] }} across: over the
|
|
655
|
+
* @returns {{ kind: string, flats: number, height: number, across: number, figures: string[] }} across: over the
|
|
656
|
+
* corners
|
|
230
657
|
*/
|
|
231
658
|
export function nutOf(what, m, kind) {
|
|
232
659
|
oneOf(what, 'kind', kind, NUTS);
|
|
233
|
-
if (!m.made.nuts.includes(kind))
|
|
234
|
-
|
|
235
|
-
|
|
660
|
+
if (!m.made.nuts.includes(kind))
|
|
661
|
+
throw new Error(
|
|
662
|
+
`${what}: ${m.thread} has no ${kind} nut in its rule set (${m.set}): its nuts are ${list(m.made.nuts)}`,
|
|
663
|
+
);
|
|
664
|
+
const names = NUT_FIGURES[kind],
|
|
665
|
+
flats = figure(what, m, names.flats);
|
|
666
|
+
return {
|
|
667
|
+
kind,
|
|
668
|
+
flats,
|
|
669
|
+
height: figure(what, m, names.height),
|
|
670
|
+
across: r3(kind === 'square' ? flats * Math.SQRT2 : flats / Math.cos(Math.PI / 6)),
|
|
671
|
+
figures: Object.values(names),
|
|
672
|
+
};
|
|
236
673
|
}
|
|
237
674
|
|
|
238
|
-
const INCH = 25.4,
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
675
|
+
const INCH = 25.4,
|
|
676
|
+
frac = (x) => {
|
|
677
|
+
// a length in inches as it is said: 1/2, 1 1/2, or a decimal where it is no 64th
|
|
678
|
+
const n = Math.round(x * 64);
|
|
679
|
+
if (Math.abs(n / 64 - x) > 1e-6) return String(+x.toFixed(4));
|
|
680
|
+
const g = (p, q) => (q ? g(q, p % q) : p),
|
|
681
|
+
whole = Math.floor(n / 64),
|
|
682
|
+
rem = n - whole * 64,
|
|
683
|
+
d = g(rem, 64);
|
|
684
|
+
return rem ? `${whole ? `${whole} ` : ''}${rem / d}/${64 / d}` : String(whole);
|
|
685
|
+
};
|
|
243
686
|
const said = (m, L) => (m.form === 'unified' ? `${frac(L / INCH)} in (${mm(L)} mm)` : `${mm(L)} mm`);
|
|
244
687
|
|
|
245
|
-
/** A screw's name in its one spelling: 'M3x12 cap', '1/4-20 UNC x 1/2
|
|
246
|
-
|
|
688
|
+
/** A screw's name in its one spelling: 'M3x12 cap', '1/4-20 UNC x 1/2
|
|
689
|
+
* cap'. @param {Fixing} m @param {number | null} length mm @param {string} form @returns {string} */
|
|
690
|
+
export const screwName = (m, length, form) =>
|
|
691
|
+
`${m.thread}${length == null ? '' : m.form === 'unified' ? ` x ${frac(length / INCH)}` : `x${mm(length)}`} ${form}`;
|
|
247
692
|
|
|
248
693
|
/**
|
|
249
694
|
* A screw's length held to the lengths it is sold in: refused, naming the nearest sold, unless the caller allows it.
|
|
@@ -254,142 +699,314 @@ export function lengthOf(what, m, length, allow = false) {
|
|
|
254
699
|
positive(what, 'length', length);
|
|
255
700
|
const f = m.figures.lengths;
|
|
256
701
|
if (!f) return { length, sold: null };
|
|
257
|
-
if (!Array.isArray(f.value) || !f.value.every(isNum))
|
|
702
|
+
if (!Array.isArray(f.value) || !f.value.every(isNum))
|
|
703
|
+
throw new Error(`${what}: ${cite(m, 'lengths')} is a list of numbers in mm (got ${shown(f.value)})`);
|
|
258
704
|
const hit = f.value.find((x) => Math.abs(x - length) < 1e-3);
|
|
259
705
|
if (hit != null) return { length: hit, sold: true };
|
|
260
706
|
if (allow) return { length, sold: false };
|
|
261
|
-
const below = f.value.filter((x) => x < length).at(-1),
|
|
262
|
-
|
|
707
|
+
const below = f.value.filter((x) => x < length).at(-1),
|
|
708
|
+
above = f.value.find((x) => x > length),
|
|
709
|
+
nearest = [below, above].filter((x) => x != null).map((x) => said(m, x));
|
|
710
|
+
throw new Error(
|
|
711
|
+
`${what}: ${a(m.thread)} screw is not sold ${said(m, length)} long: the nearest ` +
|
|
712
|
+
`${nearest.length > 1 ? 'are' : 'is'} ${nearest.join(' and ')} (${cite(m, 'lengths')}: ` +
|
|
713
|
+
`${f.value.map((x) => (m.form === 'unified' ? frac(x / INCH) : mm(x))).join(', ')}` +
|
|
714
|
+
`${m.form === 'unified' ? ' in' : ' mm'}, the socket cap screw's). For a length you know you can buy, ` +
|
|
715
|
+
`pass { anyLength: true }`,
|
|
716
|
+
);
|
|
263
717
|
}
|
|
264
718
|
|
|
265
719
|
/**
|
|
266
|
-
* @typedef {{ value: number | string | number[], unit?: string, kind: string, source: string, rule?: string,
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* @typedef {{
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
720
|
+
* @typedef {{ value: number | string | number[], unit?: string, kind: string, source: string, rule?: string,
|
|
721
|
+
* checked?: string, from?: 'workspace' | 'kit' }} Figure
|
|
722
|
+
* one figure of a thread: its value, how far to trust it (kind: spec, measured, community, estimate, default, derived
|
|
723
|
+
* or given) and where it comes from
|
|
724
|
+
* @typedef {{ form: string, diameter: number, height: number, across: number, key?: number, flats?: number,
|
|
725
|
+
* angle?: number, point?: number, drive?: string, figures: string[] }} Head
|
|
726
|
+
* a head: its diameter (0 for a hexagon head, which has flats, and for a set screw), its height (0 for a set screw),
|
|
727
|
+
* across: the widest it is; figures: the rules it was read from
|
|
728
|
+
* @typedef {{ kind: 'fixing', item: 'thread' | 'screw' | 'nut' | 'washer' | 'insert', name: string, said: string,
|
|
729
|
+
* set: string, thread: string,
|
|
730
|
+
* form: 'metric' | 'unified' | 'plastic', major: number, pitch: number, length: number | null, sold: boolean | null,
|
|
731
|
+
* head: Head | null, nut: { kind: string, flats: number, height: number, across: number, figures: string[] } | null,
|
|
732
|
+
* variant: string | null, made: { heads: string[], nuts: string[], washer: boolean, insert: boolean }, figures:
|
|
733
|
+
* Record<string, Figure> }} Fixing name: the name in its one spelling; said: as it was asked for; set: the rule set's
|
|
734
|
+
* name; major, pitch: the thread, mm; length: a screw's, from under its head (a countersunk screw's and a set
|
|
735
|
+
* screw's: overall), or an insert's; sold: whether that length is in the thread's list (null: it lists none); made:
|
|
736
|
+
* the head forms, nuts, washer and insert this size has figures for
|
|
277
737
|
*/
|
|
278
738
|
|
|
279
739
|
/**
|
|
280
740
|
* A fixing by name, from its thread's rule set: a thread ('M3', '1/4-20'), or a whole screw ('M3x12 cap'), nut
|
|
281
741
|
* ('M4 lock nut'), washer or insert on it. See GRAMMAR for what a name may say.
|
|
282
742
|
* @param {string} name
|
|
283
|
-
* @param {Record<string, number | string | number[] | Partial<Figure>>} [change] a different or measured figure, by its
|
|
743
|
+
* @param {Record<string, number | string | number[] | Partial<Figure>>} [change] a different or measured figure, by its
|
|
744
|
+
* rule's name:
|
|
284
745
|
* { clearance: 3.3 } or { pilot: { value: 2.8, kind: 'measured', source: 'my coupon, 2026-11-20' } }
|
|
285
|
-
* @param {{ sets?: object, anyLength?: boolean, variant?: string }} [opts] sets: the workspace's rule sets, by name or
|
|
746
|
+
* @param {{ sets?: object, anyLength?: boolean, variant?: string }} [opts] sets: the workspace's rule sets, by name or
|
|
747
|
+
* as a
|
|
286
748
|
* list, where a set for this thread is used before the kit's (its figures first, the kit's for what it lacks);
|
|
287
749
|
* anyLength: take a length the thread's list does not have; variant: a variant of the rule set, by its name
|
|
288
750
|
* @returns {Fixing}
|
|
289
751
|
*/
|
|
290
752
|
export function fixingModel(name, change = {}, opts = {}) {
|
|
291
|
-
const what = 'fixingModel',
|
|
292
|
-
|
|
293
|
-
|
|
753
|
+
const what = 'fixingModel',
|
|
754
|
+
p = parseName(name);
|
|
755
|
+
if (change == null || typeof change !== 'object')
|
|
756
|
+
throw new Error(`${what}: change is figures by their rules' names ({ clearance: 3.3 }), not ${shown(change)}`);
|
|
757
|
+
const key = squash(p.thread),
|
|
758
|
+
own = ownIndex(what, opts?.sets).get(key),
|
|
759
|
+
kit = KIT_INDEX.get(key);
|
|
294
760
|
if (!own && !kit) {
|
|
295
|
-
const all = threads(opts),
|
|
296
|
-
|
|
761
|
+
const all = threads(opts),
|
|
762
|
+
// (one near miss is a slip: 10-32 UNC; several, as round M7, are no help)
|
|
763
|
+
close = all.filter((t) => keysOf(t).some((k) => near(key, k) <= 1));
|
|
764
|
+
throw new Error(
|
|
765
|
+
`${what}: no thread "${p.thread}"` +
|
|
766
|
+
(close.length === 1 ? ` (did you mean ${close[0]}?)` : '') +
|
|
767
|
+
`. There are: ${all.join(', ')}. For another thread, hand in its rule set ({ sets })`,
|
|
768
|
+
);
|
|
297
769
|
}
|
|
298
|
-
const set = own ?? kit,
|
|
770
|
+
const set = own ?? kit,
|
|
771
|
+
figures = {};
|
|
299
772
|
if (own && kit && own !== kit) for (const [n, r] of Object.entries(kit.rules)) figures[n] = { ...r, from: 'kit' };
|
|
300
|
-
for (const [n, r] of Object.entries(set.rules))
|
|
301
|
-
|
|
302
|
-
const
|
|
773
|
+
for (const [n, r] of Object.entries(set.rules))
|
|
774
|
+
figures[n] = own && kit && own !== kit ? { ...r, from: 'workspace' } : { ...r };
|
|
775
|
+
const variants = { ...(own && kit ? kit.variants : {}), ...set.variants },
|
|
776
|
+
variantNames = Object.keys(variants);
|
|
777
|
+
const apply = (v) => {
|
|
778
|
+
for (const [n, value] of Object.entries(variants[v].values ?? {}))
|
|
779
|
+
figures[n] = {
|
|
780
|
+
...figures[n],
|
|
781
|
+
value,
|
|
782
|
+
kind: variants[v].kind ?? figures[n]?.kind,
|
|
783
|
+
source: variants[v].source ?? figures[n]?.source,
|
|
784
|
+
};
|
|
785
|
+
return v;
|
|
786
|
+
};
|
|
303
787
|
const find = (w) => variantNames.find((v) => squash(v) === squash(w));
|
|
304
788
|
let variant = null;
|
|
305
|
-
if (opts?.variant != null) {
|
|
789
|
+
if (opts?.variant != null) {
|
|
790
|
+
const v = find(opts.variant);
|
|
791
|
+
if (!v) throw new Error(`${what}: ${set.name} has no variant "${opts.variant}" (${list(variantNames)})`);
|
|
792
|
+
variant = apply(v);
|
|
793
|
+
}
|
|
306
794
|
for (const w of p.extra) {
|
|
307
795
|
const v = find(w);
|
|
308
|
-
if (!v)
|
|
796
|
+
if (!v)
|
|
797
|
+
throw new Error(
|
|
798
|
+
`${what}: "${name}": nothing is called "${w}". A head form is one of ${HEADS.join(', ')}; a nut is ` +
|
|
799
|
+
`'hex nut', 'square nut' or 'lock nut'; and there are 'washer' and 'insert'` +
|
|
800
|
+
(variantNames.length ? `. ${set.name}'s variants: ${list(variantNames)}` : ''),
|
|
801
|
+
);
|
|
309
802
|
variant = apply(v);
|
|
310
803
|
}
|
|
311
804
|
for (const [n, c] of Object.entries(change)) {
|
|
312
|
-
if (!figures[n]) {
|
|
313
|
-
|
|
805
|
+
if (!figures[n]) {
|
|
806
|
+
const guess = Object.keys(figures)
|
|
807
|
+
.map((k) => [near(n, k), k])
|
|
808
|
+
.sort((x, y) => x[0] - y[0])[0];
|
|
809
|
+
throw new Error(
|
|
810
|
+
`${what}: ${set.name} has no figure "${n}"${guess[0] <= 3 ? ` (did you mean ${guess[1]}?)` : ''}`,
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
figures[n] =
|
|
814
|
+
typeof c === 'object' && c && !Array.isArray(c)
|
|
815
|
+
? { ...figures[n], ...c }
|
|
816
|
+
: { ...figures[n], value: c, kind: 'given', source: 'given to fixingModel' };
|
|
314
817
|
if (figures[n].value == null) throw new Error(`${what}: ${n} needs a value (got ${shown(c)})`);
|
|
315
818
|
}
|
|
316
|
-
for (const n of ['thread', 'thread_form', 'major_diameter', 'pitch'])
|
|
317
|
-
|
|
819
|
+
for (const n of ['thread', 'thread_form', 'major_diameter', 'pitch'])
|
|
820
|
+
if (!figures[n])
|
|
821
|
+
throw new Error(
|
|
822
|
+
`${what}: ${set.name} is no thread's rule set: it has no ${n} (a thread needs thread, thread_form, ` +
|
|
823
|
+
`major_diameter and pitch)`,
|
|
824
|
+
);
|
|
825
|
+
const form = oneOf(`${what}: ${set.name}.thread_form`, 'the form', figures.thread_form.value, [
|
|
826
|
+
'metric',
|
|
827
|
+
'unified',
|
|
828
|
+
'plastic',
|
|
829
|
+
]);
|
|
318
830
|
/** @type {Fixing} */
|
|
319
|
-
const m = {
|
|
320
|
-
|
|
321
|
-
|
|
831
|
+
const m = {
|
|
832
|
+
kind: 'fixing',
|
|
833
|
+
item: p.item,
|
|
834
|
+
name: '',
|
|
835
|
+
said: name,
|
|
836
|
+
set: set.name,
|
|
837
|
+
thread: String(figures.thread.value),
|
|
838
|
+
form,
|
|
839
|
+
major: 0,
|
|
840
|
+
pitch: 0,
|
|
841
|
+
length: null,
|
|
842
|
+
sold: null,
|
|
843
|
+
head: null,
|
|
844
|
+
nut: null,
|
|
845
|
+
variant,
|
|
846
|
+
made: madeOf(figures),
|
|
847
|
+
figures,
|
|
848
|
+
};
|
|
849
|
+
m.major = positive(`${what}: ${set.name}`, 'major_diameter', figure(what, m, 'major_diameter'));
|
|
850
|
+
m.pitch = positive(`${what}: ${set.name}`, 'pitch', figure(what, m, 'pitch'));
|
|
851
|
+
const given =
|
|
852
|
+
p.length == null
|
|
853
|
+
? null
|
|
854
|
+
: p.item === 'insert'
|
|
855
|
+
? p.unit === 'in'
|
|
856
|
+
? p.length * INCH
|
|
857
|
+
: p.length
|
|
858
|
+
: (p.unit ?? (form === 'unified' ? 'in' : 'mm')) === 'in'
|
|
859
|
+
? p.length * INCH
|
|
860
|
+
: p.length;
|
|
322
861
|
if (p.item === 'screw') {
|
|
323
862
|
m.head = headOf(what, m, p.head ?? (m.made.heads.includes('cap') ? 'cap' : m.made.heads[0]));
|
|
324
863
|
// (the list of lengths is the socket cap screw's: a set screw is sold shorter, so its length is not held to it)
|
|
325
864
|
if (given != null && m.head.form === 'set') m.length = positive(what, 'length', given);
|
|
326
|
-
|
|
865
|
+
// (a printed screw is printed at any length)
|
|
866
|
+
else if (given != null) Object.assign(m, lengthOf(what, m, given, !!opts?.anyLength || !!p.printed));
|
|
327
867
|
} else if (p.item === 'nut') m.nut = nutOf(what, m, p.nut);
|
|
328
|
-
else if (p.item === 'washer') {
|
|
329
|
-
|
|
868
|
+
else if (p.item === 'washer') {
|
|
869
|
+
if (!m.made.washer) throw new Error(`${what}: ${m.thread} has no washer in its rule set (${set.name})`);
|
|
870
|
+
} else if (p.item === 'insert') {
|
|
330
871
|
if (!m.made.insert) throw new Error(`${what}: ${m.thread} has no heat-set insert in its rule set (${set.name})`);
|
|
331
872
|
if (given != null && Math.abs(given - figure(what, m, 'insert_length')) > 1e-3) {
|
|
332
873
|
const fits = variantNames.filter((v) => Math.abs((variants[v].values?.insert_length ?? NaN) - given) < 1e-3);
|
|
333
|
-
if (!fits.length)
|
|
874
|
+
if (!fits.length) {
|
|
875
|
+
const listed = [
|
|
876
|
+
set.rules.insert_length?.value ?? kit?.rules.insert_length?.value,
|
|
877
|
+
...variantNames.map((v) => variants[v].values?.insert_length),
|
|
878
|
+
]
|
|
879
|
+
.filter(isNum)
|
|
880
|
+
.map(mm)
|
|
881
|
+
.join(', ');
|
|
882
|
+
throw new Error(
|
|
883
|
+
`${what}: ${a(m.thread)} insert is not listed ${mm(given)} mm long: its rule set (${set.name}) has ` +
|
|
884
|
+
`${listed} mm`,
|
|
885
|
+
);
|
|
886
|
+
}
|
|
334
887
|
m.variant = apply(fits[0]);
|
|
335
888
|
}
|
|
336
889
|
m.length = figure(what, m, 'insert_length');
|
|
337
890
|
}
|
|
338
891
|
const also = `${m.variant ? ` ${m.variant}` : ''}${p.printed ? ' printed' : ''}`;
|
|
339
892
|
if (p.printed) m.printed = true;
|
|
340
|
-
m.name =
|
|
341
|
-
|
|
893
|
+
m.name =
|
|
894
|
+
m.item === 'thread'
|
|
895
|
+
? `${m.thread}${also}`
|
|
896
|
+
: m.item === 'screw'
|
|
897
|
+
? `${screwName(m, m.length, m.head.form)}${also}`
|
|
898
|
+
: m.item === 'nut'
|
|
899
|
+
? `${m.thread} ${m.nut.kind} nut${also}`
|
|
900
|
+
: m.item === 'washer'
|
|
901
|
+
? `${m.thread} washer`
|
|
902
|
+
: form === 'unified'
|
|
903
|
+
? `${m.thread} x ${mm(m.length)} mm insert`
|
|
904
|
+
: `${m.thread}x${mm(m.length)} insert`;
|
|
342
905
|
return m;
|
|
343
906
|
}
|
|
344
907
|
|
|
345
908
|
/**
|
|
346
909
|
* A fixing from what a function was handed: its model as it is, or a name read with the caller's rule sets.
|
|
347
|
-
* @param {string} what the function asking, for its
|
|
910
|
+
* @param {string} what the function asking, for its
|
|
911
|
+
* refusals @param {Fixing | string} m @param {{ sets?: object, anyLength?: boolean }} [o]
|
|
348
912
|
* @returns {Fixing}
|
|
349
913
|
*/
|
|
350
914
|
export function fixingOf(what, m, o = {}) {
|
|
351
915
|
if (typeof m === 'string') return fixingModel(m, {}, { sets: o?.sets, anyLength: o?.anyLength });
|
|
352
|
-
if (!m || m.kind !== 'fixing')
|
|
916
|
+
if (!m || m.kind !== 'fixing')
|
|
917
|
+
throw new Error(`${what}: give a fixing's model (fixingModel) or its name ('M3', 'M3x12 cap'), not ${shown(m)}`);
|
|
353
918
|
return m;
|
|
354
919
|
}
|
|
355
920
|
|
|
356
921
|
// ---------- the printer ----------
|
|
357
|
-
const BASICS = [
|
|
922
|
+
const BASICS = [
|
|
923
|
+
'hole_comp',
|
|
924
|
+
'fit',
|
|
925
|
+
'layer',
|
|
926
|
+
'line',
|
|
927
|
+
'chamfer',
|
|
928
|
+
'bridge',
|
|
929
|
+
'overhang',
|
|
930
|
+
'slip',
|
|
931
|
+
'press',
|
|
932
|
+
'thread_gap',
|
|
933
|
+
'snap_clearance',
|
|
934
|
+
'pin_press',
|
|
935
|
+
'pin_slip',
|
|
936
|
+
'magnet_press',
|
|
937
|
+
'magnet_glue',
|
|
938
|
+
'magnet_crush',
|
|
939
|
+
'print_gap',
|
|
940
|
+
];
|
|
358
941
|
/**
|
|
359
|
-
* @typedef {{ name: string, figure: (k: string) => (Figure & { name: string, borrowed?: boolean }) | null,
|
|
360
|
-
*
|
|
361
|
-
*
|
|
942
|
+
* @typedef {{ name: string, figure: (k: string) => (Figure & { name: string, borrowed?: boolean }) | null,
|
|
943
|
+
* n: (k: string) => number, cite: (k: string) => string, rest: (k: string) => object }} Printer
|
|
944
|
+
* figure: a rule of the printer's own (never borrowed); n: a basic clearance as a number, the printer's own or, where
|
|
945
|
+
* it has none, the X1C's; cite: the name that number is cited by
|
|
362
946
|
*/
|
|
363
947
|
/**
|
|
364
948
|
* The printer's rules as fixings reads them. Handed nothing, the kit's X1C. Handed a rule set ({ name, rules }), its
|
|
365
949
|
* figures with their kinds and sources. Handed plain numbers (a design's v.x1c), those, marked as given. A basic
|
|
366
|
-
* clearance the printer lacks (hole_comp, fit, layer, line, chamfer, bridge, overhang, slip, press, thread_gap,
|
|
367
|
-
*
|
|
950
|
+
* clearance the printer lacks (hole_comp, fit, layer, line, chamfer, bridge, overhang, slip, press, thread_gap,
|
|
951
|
+
* snap_clearance, pin_press, pin_slip, magnet_press, magnet_glue, magnet_crush, print_gap) is borrowed from the X1C and
|
|
952
|
+
* cited as the X1C's; a measured figure (pilot_m3) is never borrowed, since it was measured on that machine.
|
|
368
953
|
* @param {string} what @param {object} [p] @returns {Printer}
|
|
369
954
|
*/
|
|
370
955
|
export function printerOf(what, p) {
|
|
371
|
-
let name = x1c.name,
|
|
956
|
+
let name = x1c.name,
|
|
957
|
+
own = x1c.rules;
|
|
372
958
|
if (p != null) {
|
|
373
|
-
if (typeof p !== 'object' || Array.isArray(p))
|
|
374
|
-
|
|
375
|
-
|
|
959
|
+
if (typeof p !== 'object' || Array.isArray(p))
|
|
960
|
+
throw new Error(
|
|
961
|
+
`${what}: printer is the printer's rule set ({ name, rules }) or its values ({ hole_comp: 0.1, fit: ` +
|
|
962
|
+
`0.2, layer: 0.2 }), not ${shown(p)}`,
|
|
963
|
+
);
|
|
964
|
+
if (isSet(p)) {
|
|
965
|
+
name = p.name ?? 'the printer';
|
|
966
|
+
own = p.rules;
|
|
967
|
+
} else {
|
|
968
|
+
name = typeof p.name === 'string' ? p.name : 'the printer';
|
|
969
|
+
own = Object.fromEntries(
|
|
970
|
+
Object.entries(p)
|
|
971
|
+
.filter(([, v]) => isNum(v))
|
|
972
|
+
.map(([k, v]) => [
|
|
973
|
+
k,
|
|
974
|
+
{ value: v, kind: 'given', source: "the printer's values, handed in as plain numbers" },
|
|
975
|
+
]),
|
|
976
|
+
);
|
|
977
|
+
}
|
|
376
978
|
}
|
|
377
|
-
const sep = name === 'the printer' ? '
|
|
979
|
+
const sep = name === 'the printer' ? "'s " : '.';
|
|
378
980
|
const figure = (k) => (own[k] ? { ...own[k], name: `${name}${sep}${k}` } : null);
|
|
379
981
|
const basic = (k) => figure(k) ?? (BASICS.includes(k) ? { ...x1c.rules[k], name: `x1c.${k}`, borrowed: true } : null);
|
|
380
982
|
const n = (k) => {
|
|
381
983
|
const f = basic(k);
|
|
382
984
|
if (!f) throw new Error(`${what}: the printer (${name}) has no ${k}`);
|
|
383
|
-
if (!isNum(f.value) || f.value < 0)
|
|
985
|
+
if (!isNum(f.value) || f.value < 0)
|
|
986
|
+
throw new Error(
|
|
987
|
+
`${what}: ${f.name} is a number of 0 or more (got ${shown(f.value)}): hand in the printer's values worked out`,
|
|
988
|
+
);
|
|
384
989
|
return f.value;
|
|
385
990
|
};
|
|
386
|
-
return {
|
|
991
|
+
return {
|
|
992
|
+
name,
|
|
993
|
+
figure,
|
|
994
|
+
n,
|
|
995
|
+
cite: (k) => basic(k)?.name ?? `${name}${sep}${k}`,
|
|
996
|
+
rest: (k) => {
|
|
997
|
+
const f = basic(k);
|
|
998
|
+
return { ...rest(f.name, f), ...(f.borrowed ? { borrowed: true } : {}) };
|
|
999
|
+
},
|
|
1000
|
+
};
|
|
387
1001
|
}
|
|
388
1002
|
|
|
389
1003
|
/** How much thread a screw needs in a material, in diameters: the engagement rule set's figures. */
|
|
390
1004
|
export const ENGAGEMENT = engagementSet.rules;
|
|
391
1005
|
/** Which engagement rule a material reads: steel, aluminium or plastic (anything not named a metal). */
|
|
392
|
-
export const engagementOf = (material) => {
|
|
1006
|
+
export const engagementOf = (material) => {
|
|
1007
|
+
const k = String(material ?? 'plastic').toLowerCase();
|
|
1008
|
+
return /steel|stainless|iron/.test(k) ? 'steel' : /^alu/.test(k) ? 'aluminium' : 'plastic';
|
|
1009
|
+
};
|
|
393
1010
|
|
|
394
1011
|
/** A height rounded up to whole layers. */
|
|
395
1012
|
export const layers = (h, layer) => r3(Math.ceil(h / layer - 1e-6) * layer);
|