@rulecad/fixings 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +641 -2
- package/draw.d.mts +46 -0
- package/draw.mjs +85 -0
- package/fixings.d.mts +35 -0
- package/fixings.mjs +53 -0
- package/hardware.d.mts +215 -0
- package/hardware.mjs +181 -0
- package/holes.d.mts +135 -0
- package/holes.mjs +157 -0
- package/model.d.mts +253 -0
- package/model.mjs +382 -0
- package/package.json +56 -4
- package/seats.d.mts +353 -0
- package/seats.mjs +334 -0
- package/strength.d.mts +221 -0
- package/strength.mjs +262 -0
- package/threads.d.mts +216 -0
- package/threads.mjs +411 -0
package/threads.mjs
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// Threads cut for printing: a real screw thread as a solid, where a pilot, an insert or a nut will not do (a lid, a
|
|
2
|
+
// knob, a lead-screw nut, a bolt of M6 or more). threadModel gives the numbers with what they rest on; thread gives
|
|
3
|
+
// the solid: an external thread to add, or the cutter of an internal one to subtract. The forms are the ISO metric
|
|
4
|
+
// thread (the basic profile of ISO 68-1, from the major diameter and the pitch), the unified inch thread (the same
|
|
5
|
+
// profile) and the metric trapezoidal thread of ISO 2904 (a lead screw's). A thread stands on the z axis as it is
|
|
6
|
+
// printed: the flank of each tooth that faces down is laid back to the printer's overhang, the printer's thread_gap is
|
|
7
|
+
// left between it and its mate, and each end has a lead-in. Units: mm, degrees.
|
|
8
|
+
import jscad from '@jscad/modeling';
|
|
9
|
+
import t8screw from '@rulecad/rules/t8-screw.json' with { type: 'json' };
|
|
10
|
+
import { DEFAULTS, fixingOf, fixingModel, printerOf, threads as threadNames, cite, rest, finding, positive, oneOf, isNum, shown, mm, r3, a, ENGAGEMENT, engagementOf } from './model.mjs';
|
|
11
|
+
import { along, lathe, OUT, SEGMENTS, UP } from './draw.mjs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {import('./model.mjs').Fixing} Fixing a fixing's model, as fixingModel gives it
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const { geom3, poly3 } = jscad.geometries, { rotateY, rotateZ } = jscad.transforms;
|
|
18
|
+
const rad = (deg) => (deg * Math.PI) / 180, tan = (deg) => Math.tan(rad(deg));
|
|
19
|
+
/** The lead screws of the catalogue a thread can be named by: 'T8' is the kit's t8-screw rule set. */
|
|
20
|
+
const LEADSCREWS = { t8: t8screw };
|
|
21
|
+
const TR = /^tr\s*(\d+(?:\.\d+)?)\s*[x×*]\s*(\d+(?:\.\d+)?)(?:\s*\(\s*p\s*(\d+(?:\.\d+)?)\s*\))?\s*(lh)?$/i;
|
|
22
|
+
/** The forms of thread there are profiles for. */
|
|
23
|
+
export const THREAD_FORMS = ['metric', 'unified', 'trapezoidal'];
|
|
24
|
+
// ISO 2904's crest clearance by pitch, as DIN 103 tabulates it (https://en.wikipedia.org/wiki/Trapezoidal_thread_form)
|
|
25
|
+
const crestClearance = (what, P) => {
|
|
26
|
+
if (P >= 1.5 - 1e-9 && P <= 12 + 1e-9) return P < 2 - 1e-9 ? 0.15 : P <= 5 + 1e-9 ? 0.25 : 0.5;
|
|
27
|
+
throw new Error(`${what}: a trapezoidal pitch of ${mm(P)} mm is outside the crest clearances this package holds (ISO 2904, pitches from 1.5 to 12 mm): give the thread as { form: 'trapezoidal', diameter, pitch, ac } with its own`);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// ---------- what thread is meant ----------
|
|
31
|
+
// A thread's basic profile, from whatever names it: a fixing ('M10', '1/4-20'), a trapezoidal designation
|
|
32
|
+
// ('Tr8x8(P2)': 8 mm outside, 8 mm lead, 2 mm pitch, so four starts), a lead screw of the catalogue ('T8'), a lead
|
|
33
|
+
// screw's or its nut's rule set, or its numbers ({ form, diameter, pitch, starts }).
|
|
34
|
+
function basisOf(what, spec, o) {
|
|
35
|
+
const given = (name, value, why) => ({ name, value, unit: 'mm', kind: 'given', source: why });
|
|
36
|
+
const trapezoid = (said, d, P, starts, rests, ac) => ({ form: 'trapezoidal', said, d, P, starts, alpha: 15, ac: ac ?? crestClearance(what, P), rests, fx: null });
|
|
37
|
+
const designation = (text, rests = null) => {
|
|
38
|
+
const t = TR.exec(String(text).trim());
|
|
39
|
+
if (!t) return null;
|
|
40
|
+
const d = Number(t[1]), lead = Number(t[2]), P = t[3] ? Number(t[3]) : lead, starts = lead / P;
|
|
41
|
+
if (!(d > 0 && P > 0) || Math.abs(starts - Math.round(starts)) > 1e-9) throw new Error(`${what}: "${text}" is a trapezoidal thread as diameter x lead (P pitch), the lead a whole number of pitches: Tr8x2, Tr8x8(P2) (got a lead of ${mm(lead)} mm on a ${mm(P)} mm pitch)`);
|
|
42
|
+
return { ...trapezoid(`Tr${mm(d)}x${mm(lead)}${starts > 1 ? `(P${mm(P)})` : ''}`, d, P, Math.round(starts), rests ?? [given('the designation', text, 'the thread as it was named: diameter x lead (P pitch), as ISO 2904 writes it')]), hand: t[4] ? 'left' : null };
|
|
43
|
+
};
|
|
44
|
+
const fromSet = (set) => { // a lead screw's rule set, or its nut's: the thread it names, with a variant's figures over it
|
|
45
|
+
let rules = set.rules;
|
|
46
|
+
if (o.variant != null) {
|
|
47
|
+
const v = Object.keys(set.variants ?? {}).find((k) => k.toLowerCase() === String(o.variant).toLowerCase());
|
|
48
|
+
if (!v) throw new Error(`${what}: ${set.name} has no variant "${o.variant}" (${Object.keys(set.variants ?? {}).join(', ') || 'none'})`);
|
|
49
|
+
rules = { ...rules, ...Object.fromEntries(Object.entries(set.variants[v].values ?? {}).map(([k, value]) => [k, { ...rules[k], value, kind: set.variants[v].kind ?? rules[k]?.kind, source: set.variants[v].source ?? rules[k]?.source }])) };
|
|
50
|
+
}
|
|
51
|
+
const text = rules.thread?.value, b = typeof text === 'string' ? designation(text, [rest(`${set.name}.thread`, rules.thread)]) : null;
|
|
52
|
+
if (!b) throw new Error(`${what}: ${set.name ?? 'the rule set'} names no trapezoidal thread: its thread rule is ${shown(text)}, where a lead screw's says Tr8x8(P2). For a screw thread, give its name ('M10')`);
|
|
53
|
+
if (isNum(rules.starts?.value) && rules.starts.value !== b.starts) throw new Error(`${what}: ${set.name} says ${rules.starts.value} starts and its thread ${b.said} has ${b.starts}: one of the two rules is wrong`);
|
|
54
|
+
for (const k of ['pitch', 'lead', 'starts']) if (rules[k]) b.rests.push(rest(`${set.name}.${k}`, rules[k]));
|
|
55
|
+
return b;
|
|
56
|
+
};
|
|
57
|
+
if (typeof spec === 'string' && LEADSCREWS[spec.trim().toLowerCase()]) return fromSet(LEADSCREWS[spec.trim().toLowerCase()]);
|
|
58
|
+
if (typeof spec === 'string' && /^tr/i.test(spec.trim())) { const b = designation(spec); if (!b) throw new Error(`${what}: "${spec}" does not read as a trapezoidal thread: Tr8x2 (8 mm, 2 mm pitch), Tr8x8(P2) (8 mm lead on a 2 mm pitch: four starts)`); return b; }
|
|
59
|
+
if (spec && typeof spec === 'object' && spec.kind !== 'fixing' && spec.rules && typeof spec.rules === 'object') return fromSet(spec);
|
|
60
|
+
if (spec && typeof spec === 'object' && spec.kind !== 'fixing' && 'form' in spec) {
|
|
61
|
+
oneOf(what, 'form', spec.form, THREAD_FORMS);
|
|
62
|
+
const d = positive(what, 'diameter', spec.diameter), P = positive(what, 'pitch', spec.pitch), starts = spec.starts ?? 1;
|
|
63
|
+
if (spec.ac != null && !(isNum(spec.ac) && spec.ac >= 0)) throw new Error(`${what}: ac is the crest clearance of a trapezoidal thread, 0 mm or more (got ${shown(spec.ac)})`);
|
|
64
|
+
const rests = [given('diameter', d, 'given to threadModel'), given('pitch', P, 'given to threadModel')];
|
|
65
|
+
return spec.form === 'trapezoidal' ? trapezoid(`Tr${mm(d)}x${mm(P * starts)}${starts > 1 ? `(P${mm(P)})` : ''}`, d, P, starts, rests, spec.ac) : { form: spec.form, said: `${mm(d)} x ${mm(P)}`, d, P, starts, alpha: 30, rests, fx: null };
|
|
66
|
+
}
|
|
67
|
+
if (typeof spec !== 'string' && !(spec && spec.kind === 'fixing')) throw new Error(`${what}: give a thread: a fixing's name or model ('M10', '1/4-20'), a trapezoidal thread ('Tr8x2', 'Tr8x8(P2)', or 'T8' for the catalogue's lead screw), a lead screw's rule set, or { form, diameter, pitch }, not ${shown(spec)}`);
|
|
68
|
+
const fx = fixingOf(what, spec, o);
|
|
69
|
+
if (fx.form === 'plastic') throw new Error(`${what}: ${fx.thread} is a thread-forming screw for plastic: it makes its own thread in a pilot (hole kind 'pilot'), and there is no thread to print for it`);
|
|
70
|
+
let P = fx.pitch; const rests = [rest(cite(fx, 'major_diameter'), fx.figures.major_diameter)];
|
|
71
|
+
if (o.pitch != null) { P = positive(what, 'pitch', o.pitch); rests.push(given('pitch', P, 'given to threadModel')); } else rests.push(rest(cite(fx, 'pitch'), fx.figures.pitch));
|
|
72
|
+
return { form: fx.form, said: o.pitch != null ? `${fx.thread}x${mm(P)}` : fx.thread, d: fx.major, P, starts: 1, alpha: 30, rests, fx };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// One member of a thread (the screw, or the nut) on the basic profile: where its teeth are rooted, how wide a tooth is
|
|
76
|
+
// there, how high it stands and how wide its crest is. Radii in mm; out: the teeth point away from the axis.
|
|
77
|
+
function basicMember(b, internal) {
|
|
78
|
+
const { d, P, alpha } = b, t = tan(alpha);
|
|
79
|
+
if (b.form === 'trapezoidal') { // ISO 2904: H1 = 0.5 P, h3 = H4 = H1 + ac, d3 = d - 2 h3, D1 = d - P, D4 = d + 2 ac
|
|
80
|
+
const h = 0.5 * P + b.ac, crest = P / 2 - (P / 2) * t, root = P - crest - 2 * h * t;
|
|
81
|
+
return internal ? { out: false, root: d / 2 + b.ac, h, crest, rootFlat: root, pitchRadius: d / 2 - 0.25 * P } : { out: true, root: d / 2 - h, h, crest, rootFlat: root, pitchRadius: d / 2 - 0.25 * P };
|
|
82
|
+
}
|
|
83
|
+
const H = (Math.sqrt(3) / 2) * P, h = (5 / 8) * H; // ISO 68-1: the triangle's height, less an eighth at the crest and a quarter at the root
|
|
84
|
+
return internal ? { out: false, root: d / 2, h, crest: P / 4, rootFlat: P / 8, pitchRadius: d / 2 - (3 / 8) * H } : { out: true, root: d / 2 - h, h, crest: P / 8, rootFlat: P / 4, pitchRadius: d / 2 - (3 / 8) * H };
|
|
85
|
+
}
|
|
86
|
+
// The same member as it is drawn: moved off the basic profile by its share of the gap, and its down-facing flank laid
|
|
87
|
+
// back to the angle beta (from the axis's normal) about the tooth's root, so the tooth stays inside the standard's
|
|
88
|
+
// outline. Its height is what is left under the least crest.
|
|
89
|
+
function drawnMember(b, internal, shift, beta, least) {
|
|
90
|
+
const m = basicMember(b, internal), ta = tan(b.alpha), tb = tan(beta), width = b.P - m.rootFlat, laid = beta > b.alpha + 1e-9;
|
|
91
|
+
const h = laid ? Math.min(m.h, (width - Math.min(m.crest, least)) / (ta + tb)) : m.h, crest = width - h * (ta + tb);
|
|
92
|
+
const root = m.root + (m.out ? -shift : shift);
|
|
93
|
+
return { ...m, root, h, crest, tip: m.out ? root + h : root - h, up: h * ta, down: h * tb, standard: m.h };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @typedef {{ kind: 'thread', thread: string, form: 'metric' | 'unified' | 'trapezoidal', internal: boolean, hand: 'right' | 'left', starts: number, pitch: number, lead: number, length: number,
|
|
98
|
+
* major: number, basic: { major: number, pitch: number, minor: number }, drawn: { major: number, minor: number }, depth: number, standardDepth: number, crest: number,
|
|
99
|
+
* flank: { up: number, down: number }, overhang: { standard: number, drawn: number, limit: number }, clearance: number, gap: number, comp: number, mate: 'printed' | 'bought', overlap: number, standardOverlap: number,
|
|
100
|
+
* leadIn: number, run: number, segments: number, triangles: number, display: 'thread' | 'plain', top: '+z' | '-z', axis: 'x' | 'y' | 'z', flat: number | null, engaged: number,
|
|
101
|
+
* outline: { lo: number, hi: number, flatLo: number, rise: number, flatHi: number, fall: number }, rests: object[], findings: object[] }} Thread
|
|
102
|
+
* thread: as it is written ('M10', 'Tr8x8(P2)'); lead: the travel in one turn, pitch times starts; major: the nominal diameter;
|
|
103
|
+
* basic: the standard's basic diameters of this member; drawn: its diameters as drawn (an internal thread's are its cutter's);
|
|
104
|
+
* depth: a tooth's height as drawn, and standardDepth the standard's; crest: the flat left on a tooth;
|
|
105
|
+
* flank: each flank's angle from the axis's normal, the one facing up and the one facing down as printed;
|
|
106
|
+
* overhang: what the down-facing flank overhangs from upright, by the standard and as drawn, and the printer's limit;
|
|
107
|
+
* clearance: the gap between this thread and its mate, on the radius; gap: this member's share of it; comp: the hole compensation across an internal thread;
|
|
108
|
+
* overlap: how far its flanks overlap its mate's, on the radius, and standardOverlap the standard's; leadIn: the lead-in's size on the radius, run: its length along the axis;
|
|
109
|
+
* triangles: the most the solid has; flat: on its side, how far from the axis its flat lies; engaged: the share of a turn left with thread
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A thread to print, as numbers: its profile as drawn, the clearance to its mate, how its flanks print, and what each
|
|
114
|
+
* figure rests on. Refused by name where the printer cannot resolve the pitch (with the smallest thread that would
|
|
115
|
+
* print), and where it is asked for lying on its side without a flat.
|
|
116
|
+
* @param {Fixing | string | object} spec a fixing's name or model ('M10', '1/4-20'), a trapezoidal thread ('Tr8x2', 'Tr8x8(P2)',
|
|
117
|
+
* 'T8' for the catalogue's lead screw), a lead screw's or its nut's rule set, or { form, diameter, pitch, starts }
|
|
118
|
+
* @param {{ length: number, internal?: boolean, printer?: object, starts?: number, hand?: 'right' | 'left', pitch?: number, mate?: 'printed' | 'bought', gap?: number,
|
|
119
|
+
* flanks?: 'printed' | 'standard', leadIn?: number, segments?: number, display?: 'thread' | 'plain', top?: '+z' | '-z', axis?: 'x' | 'y' | 'z', flat?: boolean, variant?: string, sets?: object }} o
|
|
120
|
+
* length: of the thread, along its axis; internal: the thread of a nut or a threaded hole (the solid is then its cutter);
|
|
121
|
+
* starts: how many threads run side by side (default 1, or what the designation says); hand: right (the default) or left;
|
|
122
|
+
* pitch: another pitch on a fixing's diameter (a fine thread); mate: what it screws into, printed (each takes half the
|
|
123
|
+
* gap) or bought (this one takes all of it); gap: the gap on the radius, in place of the printer's thread_gap;
|
|
124
|
+
* flanks: printed (the down-facing flank laid back to the printer's overhang, the default) or standard (the standard's
|
|
125
|
+
* own form); leadIn: the size of the lead-in at each end, on the radius (default a tooth's height); segments: faces
|
|
126
|
+
* round, a multiple of 4 from 12 to 144 (default 48): it bounds the triangle count; display: thread, or plain for a
|
|
127
|
+
* plain cylinder; top: which end is up when it prints, +z (as drawn, the default) or -z; axis: z standing up (the
|
|
128
|
+
* default), x or y lying on its side, which needs flat: true (a flat to lie on, or a pointed roof in a hole)
|
|
129
|
+
* @returns {Thread}
|
|
130
|
+
*/
|
|
131
|
+
export function threadModel(spec, o = {}) {
|
|
132
|
+
const what = o?.what ?? 'threadModel';
|
|
133
|
+
if (o == null || typeof o !== 'object' || Array.isArray(o)) throw new Error(`${what}: the second argument is its options ({ length: 20, internal: true }), not ${shown(o)}`);
|
|
134
|
+
const b = basisOf(what, spec, o), pr = printerOf(what, o.printer), out = [], rests = [...b.rests];
|
|
135
|
+
const internal = o.internal ?? false, hand = o.hand ?? b.hand ?? 'right', starts = o.starts ?? b.starts, flanks = o.flanks ?? 'printed', mate = o.mate ?? 'printed', display = o.display ?? 'thread', top = o.top ?? '+z', axis = o.axis ?? 'z', segments = o.segments ?? SEGMENTS;
|
|
136
|
+
if (typeof internal !== 'boolean') throw new Error(`${what}: internal is true (a nut's thread, a threaded hole) or false (a screw's) (got ${shown(o.internal)})`);
|
|
137
|
+
oneOf(what, 'hand', hand, ['right', 'left']); oneOf(what, 'flanks', flanks, ['printed', 'standard']); oneOf(what, 'mate', mate, ['printed', 'bought']);
|
|
138
|
+
oneOf(what, 'display', display, ['thread', 'plain']); oneOf(what, 'top', top, ['+z', '-z']); oneOf(what, 'axis', axis, ['x', 'y', 'z']);
|
|
139
|
+
if (!(Number.isInteger(starts) && starts >= 1)) throw new Error(`${what}: starts is how many threads run side by side, a whole number of 1 or more (got ${shown(starts)})`);
|
|
140
|
+
if (!(Number.isInteger(segments) && segments >= 12 && segments <= 144 && segments % 4 === 0)) throw new Error(`${what}: segments is how many faces go round, a multiple of 4 from 12 to 144 (got ${shown(segments)})`);
|
|
141
|
+
if (o.flat != null && typeof o.flat !== 'boolean') throw new Error(`${what}: flat is true or false: a flat along a thread lying on its side (got ${shown(o.flat)})`);
|
|
142
|
+
if (o.length == null) throw new Error(`${what}: give a length, the thread's along its axis, in mm ({ length: 20 })`);
|
|
143
|
+
const length = positive(what, 'length', o.length), { P, d } = b, lead = P * starts;
|
|
144
|
+
|
|
145
|
+
// What the printer resolves: layers to a pitch, and a tooth as deep as a line is wide
|
|
146
|
+
const layer = pr.n('layer'), line = pr.n('line'), limit = pr.n('overhang'), basicTooth = basicMember(b, false).h - (b.form === 'trapezoidal' ? b.ac : 0);
|
|
147
|
+
const byLayers = DEFAULTS.thread_layers.value * layer, byLine = (DEFAULTS.thread_lines.value * line * P) / basicTooth, least = Math.max(byLayers, byLine);
|
|
148
|
+
if (P < least - 1e-9) {
|
|
149
|
+
const family = b.fx ? threadNames(o).map((t) => { try { return fixingModel(t, {}, { sets: o.sets }); } catch { return null; } }).filter((m) => m && m.form === b.fx.form && m.pitch >= least - 1e-9).sort((x, y) => x.major - y.major)[0] : null;
|
|
150
|
+
throw new Error(`${what}: ${b.said} has a ${mm(P)} mm pitch, finer than ${pr.name} prints: a thread needs ${DEFAULTS.thread_layers.value} layers to a pitch (${mm(byLayers)} mm at ${pr.cite('layer')} ${mm(layer)} mm) and a tooth a line deep (a pitch of ${mm(r3(byLine))} mm at ${pr.cite('line')} ${mm(line)} mm). `
|
|
151
|
+
+ `${family ? `The smallest ${b.fx.form} thread that would print is ${family.thread} (${mm(family.pitch)} mm pitch)` : `A pitch of ${mm(r3(least))} mm or more would print`}. ${b.fx ? `For ${b.said} use a pilot the screw forms its own thread in, an insert, or a nut (hole kind 'pilot' or 'insert', nutPocket)` : 'Use a coarser thread, or a finer layer'}`);
|
|
152
|
+
}
|
|
153
|
+
if (!(limit > 0 && limit < 90)) throw new Error(`${what}: ${pr.cite('overhang')} is the steepest overhang from upright that prints, between 0° and 90° (got ${shown(limit)})`);
|
|
154
|
+
if (axis !== 'z' && !o.flat) throw new Error(`${what}: a thread lying on its side (axis ${axis}) has ${internal ? 'the crests of its top turns hanging in the hole with nothing under them' : 'its lower half hanging over the bed'}: no printer lays that without support. Stand it up (axis z), or give { flat: true }: ${internal ? 'a pointed roof over the hole, as a teardrop has' : 'a flat along the thread for it to lie on'}, which leaves thread on about three quarters of each turn`);
|
|
155
|
+
if (axis === 'z' && o.flat) throw new Error(`${what}: flat is for a thread lying on its side (axis x or y): standing up, it needs none`);
|
|
156
|
+
|
|
157
|
+
// The gap, and this member's share of it
|
|
158
|
+
const gapRule = o.gap == null;
|
|
159
|
+
if (!gapRule && !(isNum(o.gap) && o.gap >= 0)) throw new Error(`${what}: gap is the gap between this thread and its mate on the radius, 0 mm or more (got ${shown(o.gap)})`);
|
|
160
|
+
const clearance = gapRule ? pr.n('thread_gap') : o.gap, gap = r3(mate === 'printed' ? clearance / 2 : clearance), comp = internal ? pr.n('hole_comp') : 0;
|
|
161
|
+
if (gapRule) rests.push(pr.rest('thread_gap')); if (comp) rests.push(pr.rest('hole_comp'));
|
|
162
|
+
rests.push(pr.rest('layer'), pr.rest('line'), pr.rest('overhang'), rest('fixings.thread_layers', DEFAULTS.thread_layers), rest('fixings.thread_lines', DEFAULTS.thread_lines));
|
|
163
|
+
|
|
164
|
+
// Flanks: a flank at alpha from the axis's normal overhangs 90 - alpha from upright where it faces down. Lying on
|
|
165
|
+
// its side, a flank faces sideways and needs nothing.
|
|
166
|
+
const laid = flanks === 'printed' && axis === 'z' && 90 - b.alpha > limit + 1e-9, beta = laid ? 90 - limit : b.alpha, crestLeast = DEFAULTS.thread_crest.value * P;
|
|
167
|
+
if (laid) rests.push(rest('fixings.thread_crest', DEFAULTS.thread_crest));
|
|
168
|
+
const me = drawnMember(b, internal, gap + comp / 2, beta, crestLeast), std = basicMember(b, internal);
|
|
169
|
+
const other = mate === 'printed' ? drawnMember(b, !internal, gap + (internal ? 0 : pr.n('hole_comp') / 2), beta, crestLeast) : { tip: (() => { const x = basicMember(b, !internal); return x.out ? x.root + x.h : x.root - x.h; })() };
|
|
170
|
+
const overlap = r3(internal ? other.tip - me.tip : me.tip - other.tip), standardOverlap = r3(b.form === 'trapezoidal' ? 0.5 * P : std.h);
|
|
171
|
+
if (!(me.crest > 1e-9 && me.h > 1e-9)) throw new Error(`${what}: ${b.said} leaves no tooth once its flank is laid back to ${mm(limit)}° (a crest of ${mm(me.crest)} mm): use { flanks: 'standard' }, or a coarser thread`);
|
|
172
|
+
|
|
173
|
+
// The outline of the solid over one pitch, going up z: a flat at its low radius, a flank out, a flat at its high
|
|
174
|
+
// radius, a flank back. On a screw the rising flank is the tooth's underside; in a cutter it is the top of the nut's tooth below.
|
|
175
|
+
const downFirst = (top === '+z') !== internal, lo = internal ? me.tip : me.root, hi = internal ? me.root : me.tip;
|
|
176
|
+
const outline = { lo: r3(lo), hi: r3(hi), // (to a micron: the solid is drawn from these, and the diameters below are twice them)
|
|
177
|
+
flatLo: internal ? me.crest : me.rootFlat, rise: downFirst ? me.down : me.up, flatHi: internal ? me.rootFlat : me.crest, fall: downFirst ? me.up : me.down };
|
|
178
|
+
const slope = Math.min(1, Math.tan(rad(limit)));
|
|
179
|
+
if (o.leadIn != null && !(isNum(o.leadIn) && o.leadIn >= 0)) throw new Error(`${what}: leadIn is the lead-in's size at each end, on the radius, 0 mm or more (got ${shown(o.leadIn)})`);
|
|
180
|
+
const leadIn = r3(o.leadIn ?? me.h), run = r3(leadIn / slope);
|
|
181
|
+
if (length < 2 * run + P - 1e-9) throw new Error(`${what}: a ${b.said} thread ${mm(length)} mm long is shorter than its two lead-ins and one pitch (${mm(r3(2 * run + P))} mm): make it longer, or give a smaller { leadIn }`);
|
|
182
|
+
|
|
183
|
+
// Lying on its side: a flat where the root's underside would pass the overhang, snapped to a face of the solid
|
|
184
|
+
let flat = null, engaged = 1;
|
|
185
|
+
if (axis !== 'z') { const step = 360 / segments, cut = Math.ceil((90 - limit) / step - 1e-9) * step; flat = r3(me.root * (internal ? 1 : Math.cos(rad(cut)))); engaged = r3(1 - (internal ? 90 - limit : cut) / 180); }
|
|
186
|
+
|
|
187
|
+
const rows = 4 * (Math.ceil((length + 2 * OUT + lead + (internal ? hi / slope : 0)) / P) - Math.floor(-(lead + OUT) / P) + 2), triangles = display === 'plain' ? 8 * segments : 2 * segments * (rows + 1);
|
|
188
|
+
const dia = (r) => r3(2 * r3(r)), basic = { major: dia(internal ? std.root : std.root + std.h), pitch: dia(std.pitchRadius), minor: dia(internal ? std.root - std.h : std.root) };
|
|
189
|
+
const says = internal ? 'an internal' : 'an external', angle = (x) => mm(r3(x));
|
|
190
|
+
out.push(finding('info', `${says} ${b.said} thread drawn ${mm(dia(hi))} mm over its ${internal ? 'roots' : 'crests'} and ${mm(dia(lo))} mm over its ${internal ? 'crests' : 'roots'}, ${mm(length)} mm long: ${b.form === 'trapezoidal' ? 'the trapezoidal form of ISO 2904' : b.form === 'unified' ? 'the unified form, the basic profile of ISO 68-1' : 'the basic profile of ISO 68-1'} on a ${mm(P)} mm pitch${starts > 1 ? `, ${starts} starts, a lead of ${mm(lead)} mm` : ''}${hand === 'left' ? ', left hand' : ''} (basic: ${mm(basic.major)} major, ${mm(basic.pitch)} pitch, ${mm(basic.minor)} minor)`,
|
|
191
|
+
'thread.drawn', { values: { major: dia(hi), minor: dia(lo), pitch: P, lead, starts, length }, refs: { thread: b.said, form: b.form, internal, hand } }));
|
|
192
|
+
out.push(finding('info', `${mm(clearance)} mm between this thread and its mate, on the radius: ${gapRule ? `${pr.cite('thread_gap')}, ${pr.rest('thread_gap').kind === 'estimate' ? 'an estimate until the fixings coupon measures it' : `a ${pr.rest('thread_gap').kind} figure`}` : 'the gap given'}. ${mate === 'printed' ? `A printed pair takes half each, so this one is drawn ${mm(gap)} mm off the basic profile` : `Its mate is bought, so this one takes all of it`}${comp ? `, and ${pr.cite('hole_comp')} ${mm(comp)} mm across as any printed hole` : ''}`,
|
|
193
|
+
'thread.clearance', { values: { clearance, gap, comp }, refs: { rule: gapRule ? pr.cite('thread_gap') : 'given', kind: gapRule ? pr.rest('thread_gap').kind : 'given', mate } }));
|
|
194
|
+
if (gapRule && pr.rest('thread_gap').borrowed) out.push(finding('info', `${pr.name} has no thread_gap: the X1C's ${mm(clearance)} mm is used`, 'thread.printer', { values: { clearance } }));
|
|
195
|
+
const overhang = { standard: 90 - b.alpha, drawn: r3(90 - beta), limit };
|
|
196
|
+
if (axis !== 'z') out.push(finding('pass', `lying along ${axis}, ${internal ? `the hole has a pointed roof that leaves its roots, ${mm(flat)} mm from the axis, at ${angle(limit)}°, so nothing hangs` : `the thread has a flat ${mm(flat)} mm from its axis to lie on`}: thread is left on ${mm(r3(engaged * 100))}% of each turn, and its flanks face sideways, so they keep the standard's ${angle(b.alpha)}°`, 'thread.printable', { values: { axis, flat, engaged } }));
|
|
197
|
+
else if (laid) out.push(finding('pass', `standing up, the standard's flank lies ${angle(b.alpha)}° from the axis's normal, which is a ${angle(overhang.standard)}° overhang from upright where it faces down, past ${pr.cite('overhang')} ${angle(limit)}°. The down-facing flank is laid back to ${angle(beta)}° about the tooth's root, a ${angle(overhang.drawn)}° overhang, so the tooth stays inside the standard's outline and still fits a bought mate; the up-facing flank keeps ${angle(b.alpha)}°. Each tooth is ${mm(r3(me.h))} mm high where the standard's is ${mm(r3(std.h))} mm, with a ${mm(r3(me.crest))} mm crest. Print it with ${top} up`,
|
|
198
|
+
'thread.printable', { values: { top, standard: overhang.standard, drawn: overhang.drawn, limit, depth: r3(me.h), standardDepth: r3(std.h) } }));
|
|
199
|
+
else out.push(finding(overhang.standard <= limit + 1e-9 ? 'pass' : 'warn', overhang.standard <= limit + 1e-9 ? `standing up, the flank's ${angle(overhang.standard)}° overhang from upright is within ${pr.cite('overhang')} ${angle(limit)}°: the standard's form prints as it is`
|
|
200
|
+
: `standing up, the standard's flank overhangs ${angle(overhang.standard)}° from upright where it faces down, past ${pr.cite('overhang')} ${angle(limit)}°, over a reach of ${mm(r3(std.h))} mm: the form is the standard's own, and whether the printer lays it is for a test print to say`, 'thread.printable', { values: { top, standard: overhang.standard, drawn: overhang.standard, limit } }));
|
|
201
|
+
out.push(finding(overlap > 0 ? 'info' : 'fail', `its flanks overlap ${mate === 'printed' ? 'a printed mate drawn the same way' : 'a bought mate at its basic size'} by ${mm(overlap)} mm on the radius, where the standard's overlap ${mm(standardOverlap)} mm (${mm(r3((overlap / standardOverlap) * 100))}%)`, 'thread.overlap', { values: { overlap, standard: standardOverlap } }));
|
|
202
|
+
return { kind: 'thread', thread: b.said, form: b.form, internal, hand, starts, pitch: P, lead, length, major: d, basic, drawn: { major: dia(hi), minor: dia(lo) }, depth: r3(me.h), standardDepth: r3(std.h), crest: r3(me.crest),
|
|
203
|
+
flank: { up: b.alpha, down: r3(beta) }, overhang, clearance, gap, comp, mate, overlap, standardOverlap, leadIn, run, segments, triangles, display, top, axis, flat, engaged, outline, rests, findings: out };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ---------- the solid ----------
|
|
207
|
+
// A thread's surface as triangles: columns of points round the axis, rows up it. Each row is one corner of the outline
|
|
208
|
+
// carried round as a helix, so the corners of the teeth are exact however few faces go round, and a point past either
|
|
209
|
+
// end is brought back to the end's plane with the radius the thread has there. shape(r, z, i) bends every radius (a
|
|
210
|
+
// lead-in, a roof), and place(i, r, z) may put a point somewhere of its own (a flat). Returns the triangles and the
|
|
211
|
+
// two end rings.
|
|
212
|
+
function sweep({ outline: o, P, lead, turn, n, z0, z1, shape, place = null, inside = false, caps = true }) {
|
|
213
|
+
const offs = [0, o.flatLo, o.flatLo + o.rise, o.flatLo + o.rise + o.flatHi], radii = [o.lo, o.lo, o.hi, o.hi], per = 4 * Math.round(lead / P);
|
|
214
|
+
const cos = Array.from({ length: n }, (_, i) => Math.cos((2 * Math.PI * i) / n)), sin = Array.from({ length: n }, (_, i) => Math.sin((2 * Math.PI * i) / n));
|
|
215
|
+
const mod = (x, m) => ((x % m) + m) % m;
|
|
216
|
+
const profile = (z) => { // the outline's radius at a height, on column 0
|
|
217
|
+
const u = mod(z, P);
|
|
218
|
+
if (u <= offs[1]) return o.lo;
|
|
219
|
+
if (u < offs[2]) return o.lo + ((o.hi - o.lo) * (u - offs[1])) / (offs[2] - offs[1]);
|
|
220
|
+
if (u <= offs[3]) return o.hi;
|
|
221
|
+
return o.hi - ((o.hi - o.lo) * (u - offs[3])) / (P - offs[3]);
|
|
222
|
+
};
|
|
223
|
+
const point = (i, r, z) => place?.(i, r, z) ?? [Math.max(r, 0) * cos[i], Math.max(r, 0) * sin[i], z];
|
|
224
|
+
const ringAt = (z) => Array.from({ length: n }, (_, i) => point(i, shape(profile(z - (turn * lead * i) / n), z, i), z));
|
|
225
|
+
const lo = ringAt(z0), hi = ringAt(z1);
|
|
226
|
+
const at = (i, k) => {
|
|
227
|
+
if (i === n) { i = 0; k += turn * per; }
|
|
228
|
+
const z = Math.floor(k / 4) * P + offs[mod(k, 4)] + (turn * lead * i) / n;
|
|
229
|
+
return z <= z0 ? lo[i] : z >= z1 ? hi[i] : point(i, shape(radii[mod(k, 4)], z, i), z);
|
|
230
|
+
};
|
|
231
|
+
const same = (p, q) => p === q || (Math.abs(p[0] - q[0]) < 1e-9 && Math.abs(p[1] - q[1]) < 1e-9 && Math.abs(p[2] - q[2]) < 1e-9);
|
|
232
|
+
const tris = [], add = (p, q, r) => { if (!same(p, q) && !same(q, r) && !same(p, r)) tris.push(inside ? [p, r, q] : [p, q, r]); };
|
|
233
|
+
for (let k = 4 * (Math.floor((z0 - lead) / P) - 1); k < 4 * (Math.ceil((z1 + lead) / P) + 1); k++) for (let i = 0; i < n; i++) {
|
|
234
|
+
const p = at(i, k), q = at(i + 1, k), r = at(i + 1, k + 1), s = at(i, k + 1);
|
|
235
|
+
// (a face is split corner to corner; where a flat cuts it, along the diagonal that lies flattest on the flat)
|
|
236
|
+
if (place && Math.abs(place.depth(q) - place.depth(s)) < Math.abs(place.depth(p) - place.depth(r)) - 1e-9) { add(p, q, s); add(q, r, s); } else { add(p, q, r); add(p, r, s); }
|
|
237
|
+
}
|
|
238
|
+
for (let i = 0; i < n; i++) { const j = (i + 1) % n; if (caps === true || caps === 'low') add([0, 0, z0], lo[j], lo[i]); if (caps === true || caps === 'high') add(hi[i], hi[j], [0, 0, z1]); }
|
|
239
|
+
return { tris, lo, hi };
|
|
240
|
+
}
|
|
241
|
+
const solid = (tris) => geom3.create(tris.map((t) => poly3.create(t)));
|
|
242
|
+
// The flat face between an outline and a ring inside it, both running anticlockwise round the axis: z up or down
|
|
243
|
+
function annulus(outer, inner, up) {
|
|
244
|
+
const angle = (p) => Math.atan2(p[1], p[0]), turn = (x) => ((x % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI), from = angle(outer[0]);
|
|
245
|
+
const first = inner.map((p, j) => [turn(angle(p) - from + 1e-9), j]).sort((x, y) => x[0] - y[0])[0][1], ring = inner.map((_, j) => inner[(first + j) % inner.length]);
|
|
246
|
+
const M = outer.length, N = ring.length, tris = [], next = (list, i, count) => (i + 1 >= count ? 2 * Math.PI : 0) + turn(angle(list[(i + 1) % count]) - from + 1e-9);
|
|
247
|
+
// (a corner of the outline sees the ring only as far as its tangent: a face that would turn over waits for the next corner)
|
|
248
|
+
const turns = (p, q, r) => (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]) > 1e-12;
|
|
249
|
+
for (let i = 0, j = 0; i < M || j < N;) {
|
|
250
|
+
if (j >= N || (i < M && (next(outer, i, M) <= next(ring, j, N) || !turns(outer[i % M], ring[(j + 1) % N], ring[j % N])))) { tris.push([outer[i % M], outer[(i + 1) % M], ring[j % N]]); i++; } else { tris.push([outer[i % M], ring[(j + 1) % N], ring[j % N]]); j++; }
|
|
251
|
+
}
|
|
252
|
+
return up ? tris : tris.map(([p, q, r]) => [p, r, q]);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// How the two ends of a thread are finished, as a function on the radius. A screw's thread is cut back by a cone at a
|
|
256
|
+
// chamfered end. A cutter is flared at a face it opens on, and ends in a cone where it stops under a ceiling.
|
|
257
|
+
function ends(t, { z0, z1, low, high, face0 = z0, face1 = z1 }) {
|
|
258
|
+
const { lo, hi } = t.outline, k = t.leadIn / t.run || 1;
|
|
259
|
+
if (!t.internal) return (r, z) => Math.min(r, low === 'chamfer' ? hi - t.leadIn + (z - z0) * k : Infinity, high === 'chamfer' ? hi - t.leadIn + (z1 - z) * k : Infinity);
|
|
260
|
+
return (r, z) => {
|
|
261
|
+
let x = Math.max(r, low === 'flare' ? lo + t.leadIn - (z - face0) * k : 0, high === 'flare' ? lo + t.leadIn - (face1 - z) * k : 0);
|
|
262
|
+
if (low === 'point') x = Math.min(x, (z - z0) * k); if (high === 'point') x = Math.min(x, (z1 - z) * k);
|
|
263
|
+
return x;
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
// A flat along a screw's thread lying on its side, as where its points go: what lies past the flat's plane is brought
|
|
267
|
+
// back on to it, and the last point of each row to be cut is put where its own circle meets the plane, so the edge of
|
|
268
|
+
// the flat is true. toward: the angle the flat faces.
|
|
269
|
+
function flatOf(t, n, toward) {
|
|
270
|
+
if (t.flat == null || t.internal) return null;
|
|
271
|
+
const step = (2 * Math.PI) / n, f = t.flat;
|
|
272
|
+
return Object.assign((i, r, z) => {
|
|
273
|
+
let d = (i * step - toward) % (2 * Math.PI); if (d > Math.PI) d -= 2 * Math.PI; if (d < -Math.PI) d += 2 * Math.PI; // the column's angle from the flat's middle
|
|
274
|
+
if (!(Math.abs(d) < Math.PI / 2 && r * Math.cos(d) > f + 1e-9)) return null;
|
|
275
|
+
const meets = Math.acos(f / r), last = meets - Math.abs(d) < step - 1e-9 && Math.abs(d) > 1e-9, angle = toward + (last ? Math.sign(d) * meets : d), out = last ? r : f / Math.cos(d);
|
|
276
|
+
return [out * Math.cos(angle), out * Math.sin(angle), z];
|
|
277
|
+
}, { depth: (p) => Math.min(f, p[0] * Math.cos(toward) + p[1] * Math.sin(toward)) }); // how far a point lies towards the flat, as far as the flat itself
|
|
278
|
+
}
|
|
279
|
+
// A pointed roof over a threaded hole lying on its side, as a function on the radius: what lies under the roof is
|
|
280
|
+
// pushed out to it. The roof leaves the circle of the roots at the overhang and rises to a point, as a teardrop's.
|
|
281
|
+
function roofOf(t, n, toward) {
|
|
282
|
+
if (t.flat == null || !t.internal) return (r) => r;
|
|
283
|
+
const tangent = Math.PI / 2 - rad(t.overhang.limit), reach = Array.from({ length: n }, (_, i) => { let d = Math.abs((2 * Math.PI * i) / n - toward) % (2 * Math.PI); if (d > Math.PI) d = 2 * Math.PI - d; return d; });
|
|
284
|
+
return (r, z, i) => (reach[i] < tangent - 1e-9 ? Math.max(r, t.flat / Math.cos(tangent - reach[i])) : r);
|
|
285
|
+
}
|
|
286
|
+
const turnOf = (t) => (t.hand === 'left' ? -1 : 1);
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* A thread's surface between two heights, for a solid that carries a thread as part of itself (a threaded screw, a
|
|
290
|
+
* threaded nut): its triangles and its two end rings, to be joined to the rest.
|
|
291
|
+
* @param {Thread} t @param {{ z0: number, z1: number, low?: string, high?: string, inside?: boolean, caps?: boolean | 'low' | 'high' }} o
|
|
292
|
+
* low, high: how each end is finished: chamfer or plain on a screw, flare on a nut; inside: faces turned inwards (a nut's bore);
|
|
293
|
+
* caps: close both ends (the default), one, or neither
|
|
294
|
+
* @returns {{ tris: number[][][], lo: number[][], hi: number[][] }}
|
|
295
|
+
*/
|
|
296
|
+
export function threadSurface(t, o) {
|
|
297
|
+
const shape = ends(t, o);
|
|
298
|
+
return sweep({ outline: t.outline, P: t.pitch, lead: t.lead, turn: turnOf(t), n: t.segments, z0: o.z0, z1: o.z1, shape, inside: !!o.inside, caps: o.caps ?? true });
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A thread to print: an external thread as a solid to add, standing on z from 0 to its length, or (internal: true)
|
|
303
|
+
* the cutter of an internal thread to subtract, which runs a little past both faces of a part as thick as the thread
|
|
304
|
+
* is long. Each end has its lead-in. With display: 'plain' it is a plain cylinder: a screw's at its crests, a hole's at
|
|
305
|
+
* its crests too (the bore you would see).
|
|
306
|
+
* @param {Fixing | string | object} spec as threadModel
|
|
307
|
+
* @param {{ length: number, internal?: boolean, printer?: object, starts?: number, hand?: 'right' | 'left', pitch?: number, mate?: 'printed' | 'bought', gap?: number,
|
|
308
|
+
* flanks?: 'printed' | 'standard', leadIn?: number, segments?: number, display?: 'thread' | 'plain', top?: '+z' | '-z', axis?: 'x' | 'y' | 'z', flat?: boolean, variant?: string, sets?: object }} o as threadModel
|
|
309
|
+
* @returns {object} a JSCAD geom3, with .model holding threadModel's numbers. Lying on its side (axis x or y) it runs
|
|
310
|
+
* along that axis from the origin, its flat facing down (a screw's) or its roof up (a hole's)
|
|
311
|
+
*/
|
|
312
|
+
export function thread(spec, o = {}) {
|
|
313
|
+
if (o == null || typeof o !== 'object' || Array.isArray(o)) throw new Error(`thread: the second argument is its options ({ length: 20, internal: true }), not ${shown(o)}`);
|
|
314
|
+
const t = threadModel(spec, { ...o, what: o.what ?? 'thread' });
|
|
315
|
+
const g = t.internal ? cutter(t, { z0: -OUT, z1: t.length + OUT, face0: 0, face1: t.length, low: 'flare', high: 'flare', toward: Math.PI }) : screwThread(t);
|
|
316
|
+
if (t.axis === 'z') return Object.assign(g, { model: t });
|
|
317
|
+
const laid = rotateY(Math.PI / 2, g); // z to x: the thread's own x (where a screw's flat is) turns down, its -x (a hole's roof) up
|
|
318
|
+
return Object.assign(t.axis === 'x' ? laid : rotateZ(Math.PI / 2, laid), { model: t });
|
|
319
|
+
}
|
|
320
|
+
function screwThread(t) {
|
|
321
|
+
const { hi } = t.outline, L = t.length;
|
|
322
|
+
if (t.display === 'plain') return lathe([[0, 0], [hi - t.leadIn, 0], [hi, t.run], [hi, L - t.run], [hi - t.leadIn, L], [0, L]], t.segments);
|
|
323
|
+
return solid(sweep({ outline: t.outline, P: t.pitch, lead: t.lead, turn: turnOf(t), n: t.segments, z0: 0, z1: L, shape: ends(t, { z0: 0, z1: L, low: 'chamfer', high: 'chamfer' }), place: flatOf(t, t.segments, 0) }).tris);
|
|
324
|
+
}
|
|
325
|
+
// An internal thread's cutter between two heights of its own drawing. toward: where its roof points, lying on its side
|
|
326
|
+
function cutter(t, o) {
|
|
327
|
+
const { lo } = t.outline, end = ends(t, o), roof = roofOf(t, t.segments, o.toward ?? 0);
|
|
328
|
+
if (t.display === 'plain') {
|
|
329
|
+
const k = t.leadIn / t.run || 1, steps = [[0, o.z0]];
|
|
330
|
+
if (o.low === 'flare') steps.push([lo + t.leadIn + (o.face0 - o.z0) * k, o.z0], [lo, o.face0 + t.run]); else if (o.low !== 'point') steps.push([lo, o.z0]);
|
|
331
|
+
if (o.high === 'flare') steps.push([lo, o.face1 - t.run], [lo + t.leadIn + (o.z1 - o.face1) * k, o.z1]); else if (o.high !== 'point') steps.push([lo, o.z1]); else steps.push([lo, o.z1 - lo / k]);
|
|
332
|
+
if (o.low === 'point') steps.splice(1, 0, [lo, o.z0 + lo / k]);
|
|
333
|
+
return lathe([...steps, [0, o.z1]], t.segments);
|
|
334
|
+
}
|
|
335
|
+
return solid(sweep({ outline: t.outline, P: t.pitch, lead: t.lead, turn: turnOf(t), n: t.segments, z0: o.z0, z1: o.z1, shape: (r, z, i) => roof(end(r, z), z, i) }).tris);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ---------- a threaded hole ----------
|
|
339
|
+
/**
|
|
340
|
+
* A threaded hole, as numbers in a hole's shape: hole(m, { kind: 'thread' }) is this. The thread is modelled, so a
|
|
341
|
+
* printed part takes a screw with no tap, no insert and no nut.
|
|
342
|
+
* @param {string} what @param {Fixing} fx @param {object} o hole's options, and a thread's (mate, gap, flanks, hand, starts, segments, display, flat)
|
|
343
|
+
* @param {{ into: string, axis: 'x' | 'y' | 'z', across: boolean }} dir
|
|
344
|
+
* @returns {object} a Hole, with thread holding threadModel's numbers
|
|
345
|
+
*/
|
|
346
|
+
export function threadedHoleModel(what, fx, o, dir) {
|
|
347
|
+
const pr = printerOf(what, o.printer), e = engagementOf(o.material), need = r3(ENGAGEMENT[e].value * fx.major), through = o.through ?? false;
|
|
348
|
+
if (typeof through !== 'boolean') throw new Error(`${what}: through is true or false (got ${shown(o.through)})`);
|
|
349
|
+
const depth = o.depth != null ? positive(what, 'depth', o.depth) : through && o.thickness != null ? positive(what, 'thickness', o.thickness) : need;
|
|
350
|
+
if (o.lead != null && !(isNum(o.lead) && o.lead >= 0)) throw new Error(`${what}: lead is the lead-in chamfer, 0 mm or more (got ${shown(o.lead)})`);
|
|
351
|
+
const t = threadModel(fx, { what, length: depth, internal: true, 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, display: o.display,
|
|
352
|
+
leadIn: o.lead, top: dir.into === '+z' ? '-z' : '+z', axis: dir.across ? dir.axis : 'z', flat: dir.across ? o.flat : undefined });
|
|
353
|
+
const point = !through && dir.into === '+z' ? r3(t.outline.lo / (t.leadIn / t.run || 1)) : 0, out = [...t.findings];
|
|
354
|
+
if (o.thickness != null) { positive(what, 'thickness', o.thickness); if (!through && depth + point > o.thickness - 1e-9) throw new Error(`${what}: a blind threaded hole ${mm(depth)} mm deep${point ? ` with its ${mm(point)} mm point` : ''} goes ${mm(r3(depth + point))} mm into a part ${mm(o.thickness)} mm thick there: it is deeper than the part, and would break out of its far face. Make it shallower, or the part thicker`); }
|
|
355
|
+
if (depth < need - 1e-9) out.push(finding('warn', `${a(fx.thread)} thread ${mm(depth)} mm deep is short: engagement.${e} asks ${mm(need)} mm of thread`, 'hole.depth', { values: { depth, need } }));
|
|
356
|
+
out.push(finding('info', `${a(fx.thread)} threaded hole drawn ${mm(t.drawn.major)} mm over its roots: it rests on ${cite(fx, 'major_diameter')} (${mm(fx.major)} mm), the ${t.mate === 'bought' ? 'whole' : 'half'} of ${t.rests.find((r) => /thread_gap$/.test(r.name))?.name ?? 'the gap given'} each side and ${pr.cite('hole_comp')} ${mm(t.comp)} mm across`, 'hole.drawn', { values: { diameter: t.drawn.major, nominal: fx.major, comp: t.comp }, refs: { thread: fx.thread, set: fx.set, rule: cite(fx, 'major_diameter'), kind: fx.figures.major_diameter.kind } }));
|
|
357
|
+
if (point) out.push(finding('info', `going up from the bed, the hole ends in a point ${mm(point)} mm high past its thread, so its end needs no bridge`, 'hole.point', { values: { point } }));
|
|
358
|
+
return { thread: fx.thread, kind: 'thread', diameter: t.drawn.major, nominal: fx.major, comp: t.comp, depth, through, into: dir.into, axis: dir.axis, teardrop: dir.across, lead: t.leadIn, point,
|
|
359
|
+
basis: { rule: cite(fx, 'major_diameter'), kind: fx.figures.major_diameter.kind, source: fx.figures.major_diameter.source }, rests: [...t.rests, rest(`engagement.${e}`, ENGAGEMENT[e])], findings: out, threaded: t };
|
|
360
|
+
}
|
|
361
|
+
/** A threaded hole's cutter, from its model: drawn going down from the face, then turned to go along `into` from `at`. */
|
|
362
|
+
export function threadedHole(h, at) {
|
|
363
|
+
const t = h.threaded, L = h.depth, up = h.into === '+z';
|
|
364
|
+
const g = cutter(t, { z0: h.through ? -L - OUT : up ? -L - h.point : -L, z1: OUT, face0: -L, face1: 0, low: h.through ? 'flare' : up ? 'point' : 'flat', high: 'flare', toward: h.teardrop ? Math.atan2(UP[h.into][1], UP[h.into][0]) : 0 });
|
|
365
|
+
return Object.assign(along(g, h.into, at), { model: h });
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ---------- hardware with its thread drawn ----------
|
|
369
|
+
/**
|
|
370
|
+
* A screw with its thread drawn, as one closed solid, in a screw's own place: the face its head seats on at z = 0, the
|
|
371
|
+
* thread down to z = -length. It prints standing on its head, so the model it is given is a thread with top: '-z'.
|
|
372
|
+
* @param {{ head: string, length: number, headDiameter: number, headHeight: number, flats?: number, sink?: number }} s a screw's numbers @param {Thread} t
|
|
373
|
+
* @returns {object} a JSCAD geom3
|
|
374
|
+
*/
|
|
375
|
+
export function threadedScrew(s, t) {
|
|
376
|
+
const n = t.segments, L = s.length, R = s.headDiameter / 2, k = s.headHeight, sink = s.head === 'countersunk' ? s.sink : 0;
|
|
377
|
+
if (s.head === 'set') return solid(threadSurface(t, { z0: -L, z1: 0, low: 'chamfer', high: 'chamfer' }).tris);
|
|
378
|
+
const { tris: out, hi: neck } = threadSurface(t, { z0: -L, z1: -sink, low: 'chamfer', high: 'plain', caps: 'low' }); // the thread and its tip, open under the head
|
|
379
|
+
const ring = (r, z) => Array.from({ length: n }, (_, i) => [r * Math.cos((2 * Math.PI * i) / n), r * Math.sin((2 * Math.PI * i) / n), z]);
|
|
380
|
+
const band = (p, q) => { for (let i = 0; i < n; i++) { const j = (i + 1) % n; out.push([p[i], p[j], q[j]], [p[i], q[j], q[i]]); } };
|
|
381
|
+
const lid = (p, z) => { for (let i = 0; i < n; i++) out.push([p[i], p[(i + 1) % n], [0, 0, z]]); };
|
|
382
|
+
if (s.head === 'hex') {
|
|
383
|
+
const hex = (z) => Array.from({ length: 6 }, (_, i) => [R * Math.cos((Math.PI * i) / 3), R * Math.sin((Math.PI * i) / 3), z]), lower = hex(0), upper = hex(k);
|
|
384
|
+
out.push(...annulus(lower, neck, false));
|
|
385
|
+
for (let i = 0; i < 6; i++) { const j = (i + 1) % 6; out.push([lower[i], lower[j], upper[j]], [lower[i], upper[j], upper[i]], [upper[i], upper[j], [0, 0, k]]); }
|
|
386
|
+
} else if (s.head === 'countersunk') { const rim = ring(R, 0); band(neck, rim); lid(rim, 0); }
|
|
387
|
+
else {
|
|
388
|
+
const steps = s.head === 'button' ? [[R, 0], [R, 0.3 * k], [0.55 * R, k]] : [[R, 0], [R, k]];
|
|
389
|
+
let last = neck; for (const [r, z] of steps) { const next = ring(r, z); band(last, next); last = next; }
|
|
390
|
+
lid(last, k);
|
|
391
|
+
}
|
|
392
|
+
return solid(out);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* A nut with its thread drawn, as one closed solid in a nut's own place: on z from 0 to its height, a hexagon's
|
|
397
|
+
* corner on x. It prints as it stands.
|
|
398
|
+
* @param {{ kind: string, across: number, height: number }} nut a nut's numbers @param {Thread} t an internal thread as long as the nut is high
|
|
399
|
+
* @returns {object} a JSCAD geom3
|
|
400
|
+
*/
|
|
401
|
+
export function threadedNut(nut, t) {
|
|
402
|
+
const sides = nut.kind === 'square' ? 4 : 6, R = nut.across / 2, h = nut.height;
|
|
403
|
+
const outline = (z) => Array.from({ length: sides }, (_, i) => { const th = (2 * Math.PI * (i + (sides === 4 ? 0.5 : 0))) / sides; return [R * Math.cos(th), R * Math.sin(th), z]; });
|
|
404
|
+
const bore = t.display === 'plain' ? null : threadSurface(t, { z0: 0, z1: h, low: 'flare', high: 'flare', inside: true, caps: false });
|
|
405
|
+
const ringAt = (r, z) => Array.from({ length: t.segments }, (_, i) => [r * Math.cos((2 * Math.PI * i) / t.segments), r * Math.sin((2 * Math.PI * i) / t.segments), z]);
|
|
406
|
+
const lower = outline(0), upper = outline(h), lo = bore ? bore.lo : ringAt(t.outline.lo, 0), hi = bore ? bore.hi : ringAt(t.outline.lo, h), out = bore ? bore.tris : [];
|
|
407
|
+
if (!bore) for (let i = 0; i < t.segments; i++) { const j = (i + 1) % t.segments; out.push([lo[i], hi[j], lo[j]], [lo[i], hi[i], hi[j]]); }
|
|
408
|
+
out.push(...annulus(lower, lo, false), ...annulus(upper, hi, true));
|
|
409
|
+
for (let i = 0; i < sides; i++) { const j = (i + 1) % sides; out.push([lower[i], lower[j], upper[j]], [lower[i], upper[j], upper[i]]); }
|
|
410
|
+
return solid(out);
|
|
411
|
+
}
|