@rulecad/fixings 0.1.9 → 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/hardware.mjs CHANGED
@@ -3,7 +3,24 @@
3
3
  // diameter, a head is its outline with no socket, and a bore is drawn just clear of the thread it takes. Each has a
4
4
  // twin that gives its numbers: the anchors an assembly places it by and its mass. All stand on the z axis.
5
5
  import jscad from '@jscad/modeling';
6
- import { DEFAULTS, fixingOf, headOf, nutOf, lengthOf, screwName, figure, cite, rest, finding, positive, oneOf, shown, mm, r3, a } from './model.mjs';
6
+ import {
7
+ DEFAULTS,
8
+ fixingOf,
9
+ headOf,
10
+ nutOf,
11
+ lengthOf,
12
+ screwName,
13
+ figure,
14
+ cite,
15
+ rest,
16
+ finding,
17
+ positive,
18
+ oneOf,
19
+ shown,
20
+ mm,
21
+ r3,
22
+ a,
23
+ } from './model.mjs';
7
24
  import { optionsOf } from './holes.mjs';
8
25
  import { lathe, turnedVolume, booleans, LIGHT } from './draw.mjs';
9
26
  import { threadModel, threadedScrew, threadedNut } from './threads.mjs';
@@ -13,29 +30,60 @@ import { materialOf } from './strength.mjs';
13
30
  * @typedef {import('./model.mjs').Fixing} Fixing a fixing's model, as fixingModel gives it
14
31
  */
15
32
 
16
- const { polygon, circle } = jscad.primitives, { extrudeLinear } = jscad.extrusions, { translate } = jscad.transforms;
17
- const COS30 = Math.cos(Math.PI / 6), CLEAR = 1 / Math.cos(Math.PI / LIGHT); // a bore's corners lie this far out, so its flats clear the thread
33
+ const { polygon, circle } = jscad.primitives,
34
+ { extrudeLinear } = jscad.extrusions,
35
+ { translate } = jscad.transforms;
36
+ const CLEAR = 1 / Math.cos(Math.PI / LIGHT); // a bore's corners lie this far out, so its flats clear the thread
18
37
  const anchor = (z, up = 1) => ({ origin: [0, 0, z], axis: [0, 0, up] });
19
38
  const MATERIALS = { steel: 'steel_density', stainless: 'stainless_density' };
20
39
  // A mass in grams from a volume in mm³, with the density it used
21
- function massOf(what, volume, material, density = MATERIALS[oneOf(what, 'material', material, Object.keys(MATERIALS))]) {
22
- return { mass: r3((volume * DEFAULTS[density].value) / 1000), density: rest(`fixings.${density}`, DEFAULTS[density]) };
40
+ function massOf(
41
+ what,
42
+ volume,
43
+ material,
44
+ density = MATERIALS[oneOf(what, 'material', material, Object.keys(MATERIALS))],
45
+ ) {
46
+ return {
47
+ mass: r3((volume * DEFAULTS[density].value) / 1000),
48
+ density: rest(`fixings.${density}`, DEFAULTS[density]),
49
+ };
23
50
  }
24
- // The area a threaded length weighs as: a 60° thread's, at its pitch diameter; a plastic screw's, midway between its root and its crests
51
+ // The area a threaded length weighs as: a 60° thread's, at its pitch diameter; a plastic screw's, midway between its
52
+ // root and its crests
25
53
  function threadArea(what, fx) {
26
- const d = fx.form === 'plastic' && fx.figures.root_diameter ? (fx.major + figure(what, fx, 'root_diameter')) / 2 : fx.major - DEFAULTS.pitch_diameter.value * fx.pitch;
54
+ const d =
55
+ fx.form === 'plastic' && fx.figures.root_diameter
56
+ ? (fx.major + figure(what, fx, 'root_diameter')) / 2
57
+ : fx.major - DEFAULTS.pitch_diameter.value * fx.pitch;
27
58
  return (Math.PI / 4) * d * d;
28
59
  }
29
60
  // The thread of a piece of hardware drawn for printing (display: 'thread'): its model, or null for the light solid
30
61
  const DISPLAYS = ['plain', 'thread'];
31
62
  function drawnThread(what, fx, o, extra) {
32
63
  if (oneOf(what, 'display', o.display ?? 'plain', DISPLAYS) === 'plain') return null;
33
- return threadModel(fx, { what, printer: o.printer, sets: o.sets, mate: o.mate, gap: o.gap, flanks: o.flanks, hand: o.hand, starts: o.starts, pitch: o.pitch, segments: o.segments, ...extra });
64
+ return threadModel(fx, {
65
+ what,
66
+ printer: o.printer,
67
+ sets: o.sets,
68
+ mate: o.mate,
69
+ gap: o.gap,
70
+ flanks: o.flanks,
71
+ hand: o.hand,
72
+ starts: o.starts,
73
+ pitch: o.pitch,
74
+ segments: o.segments,
75
+ ...extra,
76
+ });
34
77
  }
35
- const weighed = (name, mass, material, how) => finding('info', `${name}: ${mm(mass)} g of ${material}, an estimate from its outline (${how})`, 'fixing.mass', { values: { mass }, refs: { material } });
78
+ const weighed = (name, mass, material, how) =>
79
+ finding('info', `${name}: ${mm(mass)} g of ${material}, an estimate from its outline (${how})`, 'fixing.mass', {
80
+ values: { mass },
81
+ refs: { material },
82
+ });
36
83
 
37
84
  /**
38
- * @typedef {{ origin: number[], axis: number[] }} Anchor a point and a direction an assembly places a part by, as a motor's are
85
+ * @typedef {{ origin: number[], axis: number[] }} Anchor a point and a direction an assembly places a part by, as a
86
+ * motor's are
39
87
  */
40
88
 
41
89
  /**
@@ -45,53 +93,164 @@ const weighed = (name, mass, material, how) => finding('info', `${name}: ${mm(ma
45
93
  * A printed bolt ('M8x20 hex printed') keeps the word printed in its name and is weighed in its plastic; its strength
46
94
  * is printedBoltModel's.
47
95
  * @param {Fixing | string} m a screw's model or name ('M3x12 cap'), or a thread with { length, head }
48
- * @param {{ length?: number, head?: string, material?: string | object, anyLength?: boolean, sets?: object, display?: 'plain' | 'thread', printer?: object,
49
- * mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' | 'standard', hand?: 'right' | 'left', segments?: number }} [o]
50
- * length, head: where the model has none, or to change them; material: for its mass, steel (the default) or
51
- * stainless, or for a printed bolt its plastic (default PLA); display: plain
96
+ * @param {{ length?: number, head?: string, material?: string | object, anyLength?: boolean, sets?: object,
97
+ * display?: 'plain' | 'thread', printer?: object,
98
+ * mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' | 'standard', hand?: 'right' | 'left', segments?:
99
+ * number }} [o] length, head: where the model has none, or to change them; material: for its mass, steel (the
100
+ * default) or stainless, or for a printed bolt its plastic (default PLA); display: plain
52
101
  * (the light solid, the default) or thread (its thread drawn, to print it standing on its head: then printer, mate,
53
102
  * gap, flanks, hand and segments are threadModel's)
54
- * @returns {{ item: 'screw', name: string, printed?: boolean, thread: string, head: string, length: number, sold: boolean | null, major: number, headDiameter: number, headHeight: number, sink: number, top: number, key: number | null,
55
- * mass: number, material: string, display: 'plain' | 'thread', threaded: object | null, anchors: { seat: Anchor, tip: Anchor, axis: Anchor, top: Anchor }, profile: number[][], rests: object[], findings: object[] }}
56
- * sink: how deep a countersunk head's cone goes; top: the z of the head's top; mass: grams; threaded: threadModel's numbers where its thread is drawn;
57
- * anchors: seat (under the head, or the flush face of a countersunk screw), tip, axis (the thread's, at the seat) and top (where a key goes in)
103
+ * @returns {{ item: 'screw', name: string, printed?: boolean, thread: string, head: string, length: number,
104
+ * sold: boolean | null, major: number, headDiameter: number, headHeight: number, sink: number, top: number, key: number
105
+ * | null,
106
+ * mass: number, material: string, display: 'plain' | 'thread', threaded: object | null, anchors:
107
+ * { seat: Anchor, tip: Anchor, axis: Anchor, top: Anchor }, profile: number[][], rests: object[], findings: object[]
108
+ * }} sink: how deep a countersunk head's cone goes; top: the z of the head's top; mass: grams; threaded:
109
+ * threadModel's numbers where its thread is drawn; anchors: seat (under the head, or the flush face of a countersunk
110
+ * screw), tip, axis (the thread's, at the seat) and top (where a key goes in)
58
111
  */
59
112
  export function screwModel(m, o = {}) {
60
113
  const what = 'screw';
61
- optionsOf(what, o, '{ length: 12, head: \'cap\' }');
114
+ optionsOf(what, o, "{ length: 12, head: 'cap' }");
62
115
  const fx = fixingOf(what, m, o);
63
- if (!['screw', 'thread'].includes(fx.item)) throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not a screw: use ${fx.item}()`);
64
- const form = o.head ?? fx.head?.form ?? (fx.made.heads.includes('cap') ? 'cap' : fx.made.heads[0]), head = headOf(what, fx, form);
65
- if (o.length == null && fx.length == null) throw new Error(`${what}: give a length, in the name ('${fx.thread}x12 ${form}') or as { length: 12 }`);
66
- const { length, sold } = o.length == null ? fx : form === 'set' ? { length: positive(what, 'length', o.length), sold: null } : lengthOf(what, fx, o.length, !!o.anyLength || !!fx.printed); // (a printed screw is printed at any length)
67
- const r = fx.major / 2, R = head.across / 2, k = head.height, L = length;
116
+ if (!['screw', 'thread'].includes(fx.item))
117
+ throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not a screw: use ${fx.item}()`);
118
+ const form = o.head ?? fx.head?.form ?? (fx.made.heads.includes('cap') ? 'cap' : fx.made.heads[0]),
119
+ head = headOf(what, fx, form);
120
+ if (o.length == null && fx.length == null)
121
+ throw new Error(`${what}: give a length, in the name ('${fx.thread}x12 ${form}') or as { length: 12 }`);
122
+ const { length, sold } =
123
+ o.length == null
124
+ ? fx
125
+ : form === 'set'
126
+ ? { length: positive(what, 'length', o.length), sold: null }
127
+ : lengthOf(what, fx, o.length, !!o.anyLength || !!fx.printed); // (a printed screw is printed at any length)
128
+ const r = fx.major / 2,
129
+ R = head.across / 2,
130
+ k = head.height,
131
+ L = length;
68
132
  // The outline, turned about z: from the tip up the thread, out under the head and over it
69
- const sink = form === 'countersunk' ? Math.min(r3((R - r) / Math.tan((((head.angle ?? 90) / 2) * Math.PI) / 180)), L) : 0;
70
- const profile = form === 'set' ? [[0, -L], [r, -L], [r, 0], [0, 0]]
71
- : form === 'countersunk' ? [[0, -L], [r, -L], [r, -sink], [R, 0], [0, 0]]
72
- : form === 'button' ? [[0, -L], [r, -L], [r, 0], [R, 0], [R, 0.3 * k], [0.55 * R, k], [0, k]]
73
- : [[0, -L], [r, -L], [r, 0], [R, 0], [R, k], [0, k]]; // (a hexagon head's is its corners: the solid has six of them)
74
- const top = form === 'set' || form === 'countersunk' ? 0 : k, shank = L - sink;
75
- const headProfile = form === 'countersunk' ? [[0, -sink], [r, -sink], [R, 0], [0, 0]] : profile.filter(([, z]) => z >= 0).map((p, i) => (i === 0 ? [0, 0] : p));
76
- const headVolume = form === 'set' ? 0 : form === 'hex' ? (Math.sqrt(3) / 2) * head.flats ** 2 * k : turnedVolume(headProfile);
77
- // a printed bolt ('M8x20 hex printed') is named as printed and weighed in its plastic (PLA unless material names another): its strength is printedBoltModel's
78
- const volume = threadArea(what, fx) * shank + headVolume, plastic = fx.printed ? materialOf(what, o.material ?? 'pla', o.sets) : null;
79
- if (plastic && plastic.cls !== 'plastic') throw new Error(`${what}: ${fx.name} is printed, and ${plastic.name} is no plastic: give a plastic as its material (pla, petg), or name a bought screw ('${screwName(fx, L, form)}'). printedBoltModel('${fx.name}') gives its strength`);
80
- if (plastic && !plastic.density) throw new Error(`${what}: ${plastic.name} has no density in its rule set, so ${fx.name} cannot be weighed`);
81
- const material = plastic ? plastic.name : o.material ?? 'steel', w = plastic ? { mass: r3((volume * plastic.density.value) / 1000), density: rest(plastic.density.name, plastic.density) } : massOf(what, volume, material), name = `${screwName(fx, L, form)}${fx.printed ? ' printed' : ''}`;
82
- const threaded = drawnThread(what, fx, o, { length: shank, top: '-z' }); // (it prints standing on its head, so its tip is up)
83
- return { item: 'screw', name, ...(fx.printed ? { printed: true } : {}), thread: fx.thread, head: form, length: L, sold, major: fx.major, headDiameter: head.across, headHeight: k, sink, top, key: head.key ?? null, mass: w.mass, material,
84
- display: threaded ? 'thread' : 'plain', threaded, anchors: { seat: anchor(0), tip: anchor(-L, -1), axis: anchor(0), top: anchor(top) }, profile,
85
- rests: [rest(cite(fx, 'major_diameter'), fx.figures.major_diameter), rest(cite(fx, 'pitch'), fx.figures.pitch), ...head.figures.map((n) => rest(cite(fx, n), fx.figures[n])), w.density, ...(threaded ? threaded.rests : [])],
86
- findings: [weighed(name, w.mass, material, 'the thread at its pitch diameter, the head with no socket'), ...(fx.printed ? [finding('info', `${name} is printed in ${material}, not bought: printedBoltModel('${name}', { material: '${material}' }) gives its strength`, 'fixing.printed', { values: { grams: w.mass }, refs: { material } })] : []), ...(threaded ? threaded.findings : [])] };
133
+ const sink =
134
+ form === 'countersunk' ? Math.min(r3((R - r) / Math.tan((((head.angle ?? 90) / 2) * Math.PI) / 180)), L) : 0;
135
+ const profile =
136
+ form === 'set'
137
+ ? [
138
+ [0, -L],
139
+ [r, -L],
140
+ [r, 0],
141
+ [0, 0],
142
+ ]
143
+ : form === 'countersunk'
144
+ ? [
145
+ [0, -L],
146
+ [r, -L],
147
+ [r, -sink],
148
+ [R, 0],
149
+ [0, 0],
150
+ ]
151
+ : form === 'button'
152
+ ? [
153
+ [0, -L],
154
+ [r, -L],
155
+ [r, 0],
156
+ [R, 0],
157
+ [R, 0.3 * k],
158
+ [0.55 * R, k],
159
+ [0, k],
160
+ ]
161
+ : [
162
+ [0, -L],
163
+ [r, -L],
164
+ [r, 0],
165
+ [R, 0],
166
+ [R, k],
167
+ [0, k],
168
+ ]; // (a hexagon head's is its corners: the solid has six of them)
169
+ const top = form === 'set' || form === 'countersunk' ? 0 : k,
170
+ shank = L - sink;
171
+ const headProfile =
172
+ form === 'countersunk'
173
+ ? [
174
+ [0, -sink],
175
+ [r, -sink],
176
+ [R, 0],
177
+ [0, 0],
178
+ ]
179
+ : profile.filter(([, z]) => z >= 0).map((p, i) => (i === 0 ? [0, 0] : p));
180
+ const headVolume =
181
+ form === 'set' ? 0 : form === 'hex' ? (Math.sqrt(3) / 2) * head.flats ** 2 * k : turnedVolume(headProfile);
182
+ // a printed bolt ('M8x20 hex printed') is named as printed and weighed in its plastic (PLA unless material names
183
+ // another): its strength is printedBoltModel's
184
+ const volume = threadArea(what, fx) * shank + headVolume,
185
+ plastic = fx.printed ? materialOf(what, o.material ?? 'pla', o.sets) : null;
186
+ if (plastic && plastic.cls !== 'plastic')
187
+ throw new Error(
188
+ `${what}: ${fx.name} is printed, and ${plastic.name} is no plastic: give a plastic as its material ` +
189
+ `(pla, petg), or name a bought screw ('${screwName(fx, L, form)}'). printedBoltModel('${fx.name}') ` +
190
+ `gives its strength`,
191
+ );
192
+ if (plastic && !plastic.density)
193
+ throw new Error(`${what}: ${plastic.name} has no density in its rule set, so ${fx.name} cannot be weighed`);
194
+ const material = plastic ? plastic.name : (o.material ?? 'steel'),
195
+ w = plastic
196
+ ? { mass: r3((volume * plastic.density.value) / 1000), density: rest(plastic.density.name, plastic.density) }
197
+ : massOf(what, volume, material),
198
+ name = `${screwName(fx, L, form)}${fx.printed ? ' printed' : ''}`;
199
+ // (it prints standing on its head, so its tip is up)
200
+ const threaded = drawnThread(what, fx, o, { length: shank, top: '-z' });
201
+ return {
202
+ item: 'screw',
203
+ name,
204
+ ...(fx.printed ? { printed: true } : {}),
205
+ thread: fx.thread,
206
+ head: form,
207
+ length: L,
208
+ sold,
209
+ major: fx.major,
210
+ headDiameter: head.across,
211
+ headHeight: k,
212
+ sink,
213
+ top,
214
+ key: head.key ?? null,
215
+ mass: w.mass,
216
+ material,
217
+ display: threaded ? 'thread' : 'plain',
218
+ threaded,
219
+ anchors: { seat: anchor(0), tip: anchor(-L, -1), axis: anchor(0), top: anchor(top) },
220
+ profile,
221
+ rests: [
222
+ rest(cite(fx, 'major_diameter'), fx.figures.major_diameter),
223
+ rest(cite(fx, 'pitch'), fx.figures.pitch),
224
+ ...head.figures.map((n) => rest(cite(fx, n), fx.figures[n])),
225
+ w.density,
226
+ ...(threaded ? threaded.rests : []),
227
+ ],
228
+ findings: [
229
+ weighed(name, w.mass, material, 'the thread at its pitch diameter, the head with no socket'),
230
+ ...(fx.printed
231
+ ? [
232
+ finding(
233
+ 'info',
234
+ `${name} is printed in ${material}, not bought: printedBoltModel('${name}', { material: '` +
235
+ `${material}' }) gives its strength`,
236
+ 'fixing.printed',
237
+ { values: { grams: w.mass }, refs: { material } },
238
+ ),
239
+ ]
240
+ : []),
241
+ ...(threaded ? threaded.findings : []),
242
+ ],
243
+ };
87
244
  }
88
245
 
89
246
  /**
90
247
  * A screw as a light solid: its thread a plain cylinder at the major diameter, its head an outline with no socket.
91
248
  * An M3x12 cap screw is 96 triangles. With display: 'thread' its thread is drawn as threadModel gives it, in one
92
249
  * closed solid that prints standing on its head: thousands of triangles, so only where it is asked for.
93
- * @param {Fixing | string} m @param {{ length?: number, head?: string, material?: 'steel' | 'stainless', anyLength?: boolean, sets?: object, display?: 'plain' | 'thread', printer?: object,
94
- * mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' | 'standard', hand?: 'right' | 'left', segments?: number }} [o] as screwModel
250
+ * @param {Fixing | string} m @param {{ length?: number, head?: string, material?: 'steel' | 'stainless',
251
+ * anyLength?: boolean, sets?: object, display?: 'plain' | 'thread', printer?: object,
252
+ * mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' | 'standard', hand?: 'right' | 'left', segments?:
253
+ * number }} [o] as screwModel
95
254
  * @param {{ union: Function }} [ops] the design's v.ops (a hexagon head is joined to its thread)
96
255
  * @returns {object} a JSCAD geom3, with .model holding screwModel's numbers (its anchors and mass)
97
256
  */
@@ -99,66 +258,177 @@ export function screw(m, o = {}, ops = booleans) {
99
258
  const s = screwModel(m, o);
100
259
  if (s.threaded) return Object.assign(threadedScrew(s, s.threaded), { model: s });
101
260
  if (s.head !== 'hex') return Object.assign(lathe(s.profile, LIGHT), { model: s });
102
- const r = s.major / 2, R = s.headDiameter / 2, k = s.headHeight;
103
- return Object.assign(ops.union(lathe([[0, 0], [R, 0], [R, k], [0, k]], 6), lathe([[0, -s.length], [r, -s.length], [r, k / 2], [0, k / 2]], LIGHT)), { model: s });
261
+ const r = s.major / 2,
262
+ R = s.headDiameter / 2,
263
+ k = s.headHeight;
264
+ return Object.assign(
265
+ ops.union(
266
+ lathe(
267
+ [
268
+ [0, 0],
269
+ [R, 0],
270
+ [R, k],
271
+ [0, k],
272
+ ],
273
+ 6,
274
+ ),
275
+ lathe(
276
+ [
277
+ [0, -s.length],
278
+ [r, -s.length],
279
+ [r, k / 2],
280
+ [0, k / 2],
281
+ ],
282
+ LIGHT,
283
+ ),
284
+ ),
285
+ { model: s },
286
+ );
104
287
  }
105
288
 
106
289
  /**
107
290
  * A nut as numbers. It stands on the z axis, the face it bears on at z = 0 and its top at z = its height.
108
291
  * @param {Fixing | string} m a nut's model or name ('M3 lock nut'), or a thread with { kind }
109
- * @param {{ kind?: string, material?: 'steel' | 'stainless', sets?: object, display?: 'plain' | 'thread', printer?: object, mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' | 'standard', hand?: 'right' | 'left', segments?: number }} [o]
110
- * kind: hex, square or lock (default the model's, else hex); display: plain (the light solid) or thread (its thread drawn, to print it as it stands)
111
- * @returns {{ item: 'nut', name: string, thread: string, kind: string, flats: number, across: number, height: number, bore: number, mass: number, material: string, display: 'plain' | 'thread', threaded: object | null,
292
+ * @param {{ kind?: string, material?: 'steel' | 'stainless', sets?: object, display?: 'plain' | 'thread',
293
+ * printer?: object, mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' | 'standard', hand?: 'right' |
294
+ * 'left', segments?: number }} [o]
295
+ * kind: hex, square or lock (default the model's, else hex); display: plain (the light solid) or thread (its thread
296
+ * drawn, to print it as it stands)
297
+ * @returns {{ item: 'nut', name: string, thread: string, kind: string, flats: number, across: number, height: number,
298
+ * bore: number, mass: number, material: string, display: 'plain' | 'thread', threaded: object | null,
112
299
  * anchors: { seat: Anchor, top: Anchor, axis: Anchor }, rests: object[], findings: object[] }}
113
300
  */
114
301
  export function nutModel(m, o = {}) {
115
302
  const what = 'nut';
116
- optionsOf(what, o, '{ kind: \'lock\' }');
303
+ optionsOf(what, o, "{ kind: 'lock' }");
117
304
  const fx = fixingOf(what, m, o);
118
- if (!['nut', 'thread'].includes(fx.item)) throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not a nut: use ${fx.item}()`);
119
- const n = nutOf(what, fx, o.kind ?? fx.nut?.kind ?? 'hex'), area = n.kind === 'square' ? n.flats ** 2 : (Math.sqrt(3) / 2) * n.flats ** 2, material = o.material ?? 'steel';
120
- const w = massOf(what, (area - threadArea(what, fx)) * n.height, material), name = `${fx.thread} ${n.kind} nut`, threaded = drawnThread(what, fx, o, { length: n.height, internal: true });
121
- return { item: 'nut', name, thread: fx.thread, kind: n.kind, flats: n.flats, across: n.across, height: n.height, bore: fx.major, mass: w.mass, material, display: threaded ? 'thread' : 'plain', threaded,
122
- anchors: { seat: anchor(0), top: anchor(n.height), axis: anchor(0) }, rests: [...n.figures.map((k) => rest(cite(fx, k), fx.figures[k])), rest(cite(fx, 'major_diameter'), fx.figures.major_diameter), w.density, ...(threaded ? threaded.rests : [])],
123
- findings: [weighed(name, w.mass, material, n.kind === 'lock' ? 'a plain hexagon as tall as the nut with its collar' : 'its thread at the pitch diameter'), ...(threaded ? threaded.findings : [])] };
305
+ if (!['nut', 'thread'].includes(fx.item))
306
+ throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not a nut: use ${fx.item}()`);
307
+ const n = nutOf(what, fx, o.kind ?? fx.nut?.kind ?? 'hex'),
308
+ area = n.kind === 'square' ? n.flats ** 2 : (Math.sqrt(3) / 2) * n.flats ** 2,
309
+ material = o.material ?? 'steel';
310
+ const w = massOf(what, (area - threadArea(what, fx)) * n.height, material),
311
+ name = `${fx.thread} ${n.kind} nut`,
312
+ threaded = drawnThread(what, fx, o, { length: n.height, internal: true });
313
+ return {
314
+ item: 'nut',
315
+ name,
316
+ thread: fx.thread,
317
+ kind: n.kind,
318
+ flats: n.flats,
319
+ across: n.across,
320
+ height: n.height,
321
+ bore: fx.major,
322
+ mass: w.mass,
323
+ material,
324
+ display: threaded ? 'thread' : 'plain',
325
+ threaded,
326
+ anchors: { seat: anchor(0), top: anchor(n.height), axis: anchor(0) },
327
+ rests: [
328
+ ...n.figures.map((k) => rest(cite(fx, k), fx.figures[k])),
329
+ rest(cite(fx, 'major_diameter'), fx.figures.major_diameter),
330
+ w.density,
331
+ ...(threaded ? threaded.rests : []),
332
+ ],
333
+ findings: [
334
+ weighed(
335
+ name,
336
+ w.mass,
337
+ material,
338
+ n.kind === 'lock' ? 'a plain hexagon as tall as the nut with its collar' : 'its thread at the pitch diameter',
339
+ ),
340
+ ...(threaded ? threaded.findings : []),
341
+ ],
342
+ };
124
343
  }
125
344
 
126
345
  /**
127
346
  * A nut as a light solid: a hexagon or a square with a plain bore just clear of the thread. With display: 'thread'
128
347
  * its thread is drawn as threadModel gives it, in one closed solid that prints as it stands.
129
- * @param {Fixing | string} m @param {{ kind?: string, material?: 'steel' | 'stainless', sets?: object, display?: 'plain' | 'thread', printer?: object, mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' | 'standard', hand?: 'right' | 'left', segments?: number }} [o] as nutModel
348
+ * @param {Fixing | string} m @param {{ kind?: string, material?: 'steel' | 'stainless', sets?: object,
349
+ * display?: 'plain' | 'thread', printer?: object, mate?: 'printed' | 'bought', gap?: number, flanks?: 'printed' |
350
+ * 'standard', hand?: 'right' | 'left', segments?: number }} [o] as nutModel
130
351
  * @returns {object} a JSCAD geom3, with .model
131
352
  */
132
353
  export function nut(m, o = {}) {
133
- const n = nutModel(m, o), sides = n.kind === 'square' ? 4 : 6, R = n.across / 2;
354
+ const n = nutModel(m, o),
355
+ sides = n.kind === 'square' ? 4 : 6,
356
+ R = n.across / 2;
134
357
  if (n.threaded) return Object.assign(threadedNut(n, n.threaded), { model: n });
135
358
  // a hexagon's corner on x, as the pockets of @rulecad/shapes have it; a square's flats square to x and y
136
- const outline = polygon({ points: Array.from({ length: sides }, (_, i) => { const t = (2 * Math.PI * (i + (sides === 4 ? 0.5 : 0))) / sides; return [R * Math.cos(t), R * Math.sin(t)]; }) });
137
- return Object.assign(extrudeLinear({ height: n.height }, jscad.booleans.subtract(outline, circle({ radius: (n.bore / 2) * CLEAR, segments: LIGHT }))), { model: n });
359
+ const outline = polygon({
360
+ points: Array.from({ length: sides }, (_, i) => {
361
+ const t = (2 * Math.PI * (i + (sides === 4 ? 0.5 : 0))) / sides;
362
+ return [R * Math.cos(t), R * Math.sin(t)];
363
+ }),
364
+ });
365
+ return Object.assign(
366
+ extrudeLinear(
367
+ { height: n.height },
368
+ jscad.booleans.subtract(outline, circle({ radius: (n.bore / 2) * CLEAR, segments: LIGHT })),
369
+ ),
370
+ { model: n },
371
+ );
138
372
  }
139
373
 
140
374
  /**
141
375
  * A plain washer as numbers. It lies on z = 0, its top at z = its thickness.
142
376
  * @param {Fixing | string} m a washer's model or name ('M3 washer'), or a thread
143
377
  * @param {{ material?: 'steel' | 'stainless', sets?: object }} [o]
144
- * @returns {{ item: 'washer', name: string, thread: string, bore: number, diameter: number, thickness: number, mass: number, material: string, anchors: { seat: Anchor, top: Anchor, axis: Anchor }, rests: object[], findings: object[] }}
378
+ * @returns {{ item: 'washer', name: string, thread: string, bore: number, diameter: number, thickness: number,
379
+ * mass: number, material: string, anchors: { seat: Anchor, top: Anchor, axis: Anchor }, rests: object[], findings:
380
+ * object[] }}
145
381
  */
146
382
  export function washerModel(m, o = {}) {
147
383
  const what = 'washer';
148
- optionsOf(what, o, '{ material: \'stainless\' }');
384
+ optionsOf(what, o, "{ material: 'stainless' }");
149
385
  const fx = fixingOf(what, m, o);
150
- if (!['washer', 'thread'].includes(fx.item)) throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not a washer: use ${fx.item}()`);
386
+ if (!['washer', 'thread'].includes(fx.item))
387
+ throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not a washer: use ${fx.item}()`);
151
388
  if (!fx.made.washer) throw new Error(`${what}: ${fx.thread} has no washer in its rule set (${fx.set})`);
152
- const bore = figure(what, fx, 'washer_bore'), diameter = figure(what, fx, 'washer_diameter'), thickness = figure(what, fx, 'washer_thickness'), material = o.material ?? 'steel';
153
- const w = massOf(what, (Math.PI / 4) * (diameter ** 2 - bore ** 2) * thickness, material), name = `${fx.thread} washer`;
154
- return { item: 'washer', name, thread: fx.thread, bore, diameter, thickness, mass: w.mass, material, anchors: { seat: anchor(0), top: anchor(thickness), axis: anchor(0) },
155
- rests: [...['washer_bore', 'washer_diameter', 'washer_thickness'].map((k) => rest(cite(fx, k), fx.figures[k])), w.density], findings: [weighed(name, w.mass, material, 'a flat ring')] };
389
+ const bore = figure(what, fx, 'washer_bore'),
390
+ diameter = figure(what, fx, 'washer_diameter'),
391
+ thickness = figure(what, fx, 'washer_thickness'),
392
+ material = o.material ?? 'steel';
393
+ const w = massOf(what, (Math.PI / 4) * (diameter ** 2 - bore ** 2) * thickness, material),
394
+ name = `${fx.thread} washer`;
395
+ return {
396
+ item: 'washer',
397
+ name,
398
+ thread: fx.thread,
399
+ bore,
400
+ diameter,
401
+ thickness,
402
+ mass: w.mass,
403
+ material,
404
+ anchors: { seat: anchor(0), top: anchor(thickness), axis: anchor(0) },
405
+ rests: [
406
+ ...['washer_bore', 'washer_diameter', 'washer_thickness'].map((k) => rest(cite(fx, k), fx.figures[k])),
407
+ w.density,
408
+ ],
409
+ findings: [weighed(name, w.mass, material, 'a flat ring')],
410
+ };
156
411
  }
157
412
 
158
- /** A plain washer as a light solid: a flat ring. @param {Fixing | string} m @param {{ material?: 'steel' | 'stainless', sets?: object }} [o] @returns {object} a JSCAD geom3, with .model */
413
+ /** A plain washer as a light solid: a flat
414
+ * ring. @param {Fixing | string} m @param {{ material?: 'steel' | 'stainless', sets?: object }} [o] @returns {object} a
415
+ * JSCAD geom3, with .model */
159
416
  export function washer(m, o = {}) {
160
- const w = washerModel(m, o), r = (w.bore / 2) * CLEAR, R = w.diameter / 2;
161
- return Object.assign(lathe([[r, 0], [R, 0], [R, w.thickness], [r, w.thickness]], LIGHT), { model: w });
417
+ const w = washerModel(m, o),
418
+ r = (w.bore / 2) * CLEAR,
419
+ R = w.diameter / 2;
420
+ return Object.assign(
421
+ lathe(
422
+ [
423
+ [r, 0],
424
+ [R, 0],
425
+ [R, w.thickness],
426
+ [r, w.thickness],
427
+ ],
428
+ LIGHT,
429
+ ),
430
+ { model: w },
431
+ );
162
432
  }
163
433
 
164
434
  /**
@@ -166,24 +436,62 @@ export function washer(m, o = {}) {
166
436
  * face it went in by, its far end at z = -length.
167
437
  * @param {Fixing | string} m an insert's model or name ('M3 insert', 'M3x3 insert'), or a thread
168
438
  * @param {{ sets?: object }} [o]
169
- * @returns {{ item: 'insert', name: string, thread: string, diameter: number, length: number, bore: number, hole: number, mass: number, material: string, anchors: { seat: Anchor, bottom: Anchor, axis: Anchor }, rests: object[], findings: object[] }}
439
+ * @returns {{ item: 'insert', name: string, thread: string, diameter: number, length: number, bore: number,
440
+ * hole: number, mass: number, material: string, anchors: { seat: Anchor, bottom: Anchor, axis: Anchor }, rests:
441
+ * object[], findings: object[] }}
170
442
  * diameter: over its knurls; hole: the hole it melts into, as drawn
171
443
  */
172
444
  export function insertModel(m, o = {}) {
173
445
  const what = 'insert';
174
446
  optionsOf(what, o, '{}');
175
447
  const fx = fixingOf(what, m, o);
176
- if (!['insert', 'thread'].includes(fx.item)) throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not an insert: use ${fx.item}()`);
448
+ if (!['insert', 'thread'].includes(fx.item))
449
+ throw new Error(`${what}: ${fx.name} is ${a(fx.item)}, not an insert: use ${fx.item}()`);
177
450
  if (!fx.made.insert) throw new Error(`${what}: ${fx.thread} has no heat-set insert in its rule set (${fx.set})`);
178
- const diameter = figure(what, fx, 'insert_diameter'), length = figure(what, fx, 'insert_length');
179
- const w = massOf(what, ((Math.PI / 4) * diameter ** 2 - threadArea(what, fx)) * length, 'brass', 'brass_density'), name = fx.item === 'insert' ? fx.name : `${fx.thread} insert`;
180
- return { item: 'insert', name, thread: fx.thread, diameter, length, bore: fx.major, hole: figure(what, fx, 'insert_hole'), mass: w.mass, material: 'brass', anchors: { seat: anchor(0), bottom: anchor(-length, -1), axis: anchor(0) },
181
- rests: [...['insert_diameter', 'insert_length'].map((k) => rest(cite(fx, k), fx.figures[k])), w.density], findings: [weighed(name, w.mass, 'brass', 'a plain tube over its knurls: the alloy is assumed')] };
451
+ const diameter = figure(what, fx, 'insert_diameter'),
452
+ length = figure(what, fx, 'insert_length');
453
+ const w = massOf(what, ((Math.PI / 4) * diameter ** 2 - threadArea(what, fx)) * length, 'brass', 'brass_density'),
454
+ name = fx.item === 'insert' ? fx.name : `${fx.thread} insert`;
455
+ return {
456
+ item: 'insert',
457
+ name,
458
+ thread: fx.thread,
459
+ diameter,
460
+ length,
461
+ bore: fx.major,
462
+ hole: figure(what, fx, 'insert_hole'),
463
+ mass: w.mass,
464
+ material: 'brass',
465
+ anchors: { seat: anchor(0), bottom: anchor(-length, -1), axis: anchor(0) },
466
+ rests: [...['insert_diameter', 'insert_length'].map((k) => rest(cite(fx, k), fx.figures[k])), w.density],
467
+ findings: [weighed(name, w.mass, 'brass', 'a plain tube over its knurls: the alloy is assumed')],
468
+ };
182
469
  }
183
470
 
184
- /** A heat-set insert as a light solid: a plain tube. @param {Fixing | string} m @param {{ sets?: object }} [o] @returns {object} a JSCAD geom3, with .model */
471
+ /** A heat-set insert as a light solid: a plain
472
+ * tube. @param {Fixing | string} m @param {{ sets?: object }} [o] @returns {object} a JSCAD geom3, with .model */
185
473
  export function insert(m, o = {}) {
186
- const i = insertModel(m, o), r = (i.bore / 2) * CLEAR, R = i.diameter / 2;
187
- if (!(R > r)) throw new Error(`insert: ${i.name} is ${mm(i.diameter)} mm over its knurls, no wider than its ${mm(i.bore)} mm thread (got ${shown(i.diameter)})`);
188
- return Object.assign(translate([0, 0, -i.length], lathe([[r, 0], [R, 0], [R, i.length], [r, i.length]], LIGHT)), { model: i });
474
+ const i = insertModel(m, o),
475
+ r = (i.bore / 2) * CLEAR,
476
+ R = i.diameter / 2;
477
+ if (!(R > r))
478
+ throw new Error(
479
+ `insert: ${i.name} is ${mm(i.diameter)} mm over its knurls, no wider than its ${mm(i.bore)} mm thread ` +
480
+ `(got ${shown(i.diameter)})`,
481
+ );
482
+ return Object.assign(
483
+ translate(
484
+ [0, 0, -i.length],
485
+ lathe(
486
+ [
487
+ [r, 0],
488
+ [R, 0],
489
+ [R, i.length],
490
+ [r, i.length],
491
+ ],
492
+ LIGHT,
493
+ ),
494
+ ),
495
+ { model: i },
496
+ );
189
497
  }