@code3d/screws 0.0.1-alpha.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/LICENSE +22 -0
- package/README.md +46 -0
- package/bld/library/index.d.ts +2 -0
- package/bld/library/index.d.ts.map +1 -0
- package/bld/library/index.js +2 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/iso-4762.d.ts +184 -0
- package/bld/library/iso-4762.d.ts.map +1 -0
- package/bld/library/iso-4762.js +219 -0
- package/bld/library/iso-4762.js.map +1 -0
- package/bld/library/thread.d.ts +10 -0
- package/bld/library/thread.d.ts.map +1 -0
- package/bld/library/thread.js +181 -0
- package/bld/library/thread.js.map +1 -0
- package/package.json +33 -0
- package/src/library/index.ts +1 -0
- package/src/library/iso-4762.ts +372 -0
- package/src/library/thread.ts +296 -0
- package/src/library/tsconfig.json +15 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { definePrimitive, replicad, } from '@code3d/core/replicad';
|
|
2
|
+
const { Plane, draw, makeCylinder, makeHelix, makeSolid, weldShellsAndFaces } = replicad;
|
|
3
|
+
const loopSteps = Array.from({ length: 21 }, (_, index) => index / 20);
|
|
4
|
+
const threadBreps = new Map();
|
|
5
|
+
const maximumCachedBrepCharacters = 8 * 1024 * 1024;
|
|
6
|
+
let cachedBrepCharacters = 0;
|
|
7
|
+
export const helicalThread = definePrimitive((options) => {
|
|
8
|
+
validateHelicalThread(options);
|
|
9
|
+
const { majorDiameter, minorDiameter, leftHanded = false, ...dimensions } = options;
|
|
10
|
+
return cachedHelicalThreadShape({
|
|
11
|
+
...dimensions,
|
|
12
|
+
majorRadius: majorDiameter / 2,
|
|
13
|
+
minorRadius: minorDiameter / 2,
|
|
14
|
+
leftHanded,
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
function validateHelicalThread({ pitch, y, majorDiameter, minorDiameter, rootWidth, crestWidth, }) {
|
|
18
|
+
assertPositive('pitch', pitch);
|
|
19
|
+
assertPositive('y', y);
|
|
20
|
+
assertPositive('majorDiameter', majorDiameter);
|
|
21
|
+
assertPositive('minorDiameter', minorDiameter);
|
|
22
|
+
assertPositive('rootWidth', rootWidth);
|
|
23
|
+
assertPositive('crestWidth', crestWidth);
|
|
24
|
+
if (y < pitch) {
|
|
25
|
+
throw new Error('y must be at least one thread pitch.');
|
|
26
|
+
}
|
|
27
|
+
if (minorDiameter >= majorDiameter) {
|
|
28
|
+
throw new Error('minorDiameter must be smaller than majorDiameter.');
|
|
29
|
+
}
|
|
30
|
+
if (crestWidth >= rootWidth || rootWidth > pitch) {
|
|
31
|
+
throw new Error('The thread profile requires crestWidth < rootWidth <= pitch.');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function cachedHelicalThreadShape(options) {
|
|
35
|
+
const key = JSON.stringify([
|
|
36
|
+
options.pitch,
|
|
37
|
+
options.y,
|
|
38
|
+
options.majorRadius,
|
|
39
|
+
options.minorRadius,
|
|
40
|
+
options.rootWidth,
|
|
41
|
+
options.crestWidth,
|
|
42
|
+
options.leftHanded,
|
|
43
|
+
]);
|
|
44
|
+
let brep = threadBreps.get(key);
|
|
45
|
+
if (brep === undefined) {
|
|
46
|
+
const shape = makeHelicalThreadShape(options);
|
|
47
|
+
try {
|
|
48
|
+
brep = shape.serialize();
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
shape.delete();
|
|
52
|
+
}
|
|
53
|
+
if (brep.length <= maximumCachedBrepCharacters) {
|
|
54
|
+
threadBreps.set(key, brep);
|
|
55
|
+
cachedBrepCharacters += brep.length;
|
|
56
|
+
while (cachedBrepCharacters > maximumCachedBrepCharacters) {
|
|
57
|
+
const [oldestKey, oldestBrep] = threadBreps.entries().next().value;
|
|
58
|
+
threadBreps.delete(oldestKey);
|
|
59
|
+
cachedBrepCharacters -= oldestBrep.length;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
threadBreps.delete(key);
|
|
65
|
+
threadBreps.set(key, brep);
|
|
66
|
+
}
|
|
67
|
+
// Cache data, not native handles or disposable models. Each invocation owns
|
|
68
|
+
// a fresh shape in the current kernel. Reading both misses and hits also
|
|
69
|
+
// applies the same B-Rep normalization before core identifies the geometry.
|
|
70
|
+
return replicad.deserializeShape(brep);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Builds a closed external thread around its minor-diameter core. The lofted
|
|
74
|
+
* end treatment follows the MIT-licensed replicad-threads construction by
|
|
75
|
+
* Steve Genoud, adapted here to code3d's Y-axis and geometry ownership model.
|
|
76
|
+
*/
|
|
77
|
+
function makeHelicalThreadShape({ pitch, y, majorRadius, minorRadius, rootWidth, crestWidth, leftHanded, }) {
|
|
78
|
+
const toothHeight = majorRadius - minorRadius;
|
|
79
|
+
const profile = threadProfile(rootWidth, crestWidth, toothHeight);
|
|
80
|
+
const teeth = fadedThread(pitch, minorRadius, y, profile, leftHanded);
|
|
81
|
+
const core = makeCylinder(minorRadius + 0.05, y);
|
|
82
|
+
const threaded = core.fuse(teeth);
|
|
83
|
+
core.delete();
|
|
84
|
+
teeth.delete();
|
|
85
|
+
return threaded.rotate(-90, [0, 0, 0], [1, 0, 0]).translate([0, -y / 2, 0]);
|
|
86
|
+
}
|
|
87
|
+
function threadProfile(rootWidth, crestWidth, toothHeight) {
|
|
88
|
+
const rootHalf = rootWidth / 2;
|
|
89
|
+
const crestHalf = crestWidth / 2;
|
|
90
|
+
return draw([rootHalf, -0.05])
|
|
91
|
+
.vLine(0.05)
|
|
92
|
+
.lineTo([crestHalf, toothHeight])
|
|
93
|
+
.hLineTo(-crestHalf)
|
|
94
|
+
.lineTo([-rootHalf, 0])
|
|
95
|
+
.vLine(-0.05)
|
|
96
|
+
.close();
|
|
97
|
+
}
|
|
98
|
+
function fadedThread(pitch, radius, length, profile, leftHanded) {
|
|
99
|
+
const bottomEnd = fadedEnd(pitch, radius, profile, true, leftHanded);
|
|
100
|
+
const totalLoops = length / pitch - 0.5;
|
|
101
|
+
const fullLoops = Math.floor(totalLoops);
|
|
102
|
+
const leftover = totalLoops - fullLoops;
|
|
103
|
+
const singleLoop = basicThreadLoop(pitch, radius, profile, 1, 'none', leftHanded);
|
|
104
|
+
const shells = Array.from({ length: fullLoops }, (_, index) => singleLoop.clone().translate([0, 0, index * pitch]));
|
|
105
|
+
singleLoop.delete();
|
|
106
|
+
if (leftover > 1e-9) {
|
|
107
|
+
shells.push(basicThreadLoop(pitch, radius, profile, leftover, 'none', leftHanded).translate([0, 0, fullLoops * pitch]));
|
|
108
|
+
}
|
|
109
|
+
const topEnd = fadedEnd(pitch, radius, profile, false, leftHanded)
|
|
110
|
+
.translate([0, 0, totalLoops * pitch])
|
|
111
|
+
.rotate(360 * leftover, [0, 0, 0], [0, 0, 1]);
|
|
112
|
+
const thread = makeSolid([bottomEnd, ...shells, topEnd]).translate([
|
|
113
|
+
0,
|
|
114
|
+
0,
|
|
115
|
+
pitch / 4,
|
|
116
|
+
]);
|
|
117
|
+
bottomEnd.delete();
|
|
118
|
+
shells.forEach(shell => shell.delete());
|
|
119
|
+
topEnd.delete();
|
|
120
|
+
return thread;
|
|
121
|
+
}
|
|
122
|
+
function basicThreadLoop(pitch, radius, profile, loopFraction, includeEnd, leftHanded) {
|
|
123
|
+
const helix = makeHelix(pitch, pitch * clamp(loopFraction, 0, 1), radius, [0, 0, 0], [0, 0, 1], leftHanded);
|
|
124
|
+
const profiles = loopSteps.map(step => {
|
|
125
|
+
const position = helix.pointAt(step);
|
|
126
|
+
const tangent = helix.tangentAt(step);
|
|
127
|
+
const plane = new Plane(position, [0, 0, 1], tangent);
|
|
128
|
+
const sketch = profile.sketchOnPlane(plane);
|
|
129
|
+
plane.delete();
|
|
130
|
+
return sketch;
|
|
131
|
+
});
|
|
132
|
+
helix.delete();
|
|
133
|
+
const ends = [];
|
|
134
|
+
if (includeEnd === 'first' || includeEnd === 'both') {
|
|
135
|
+
ends.push(profiles[0].faces());
|
|
136
|
+
}
|
|
137
|
+
if (includeEnd === 'last' || includeEnd === 'both') {
|
|
138
|
+
const face = profiles.at(-1).faces();
|
|
139
|
+
face.wrapped.Reverse();
|
|
140
|
+
ends.push(face);
|
|
141
|
+
}
|
|
142
|
+
const shell = profiles[0].loftWith(profiles.slice(1), { ruled: false }, true);
|
|
143
|
+
if (ends.length === 0)
|
|
144
|
+
return shell;
|
|
145
|
+
const closed = weldShellsAndFaces([shell, ...ends]);
|
|
146
|
+
shell.delete();
|
|
147
|
+
ends.forEach(face => face.delete());
|
|
148
|
+
return closed;
|
|
149
|
+
}
|
|
150
|
+
function fadedEnd(pitch, radius, profile, bottom, leftHanded) {
|
|
151
|
+
const length = pitch / 4;
|
|
152
|
+
const helix = makeHelix(pitch, length, radius, [0, 0, 0], [0, 0, bottom ? -1 : 1], leftHanded);
|
|
153
|
+
const profiles = loopSteps.map((step, index) => {
|
|
154
|
+
const position = helix.pointAt(step);
|
|
155
|
+
let tangent = helix.tangentAt(step);
|
|
156
|
+
if (bottom)
|
|
157
|
+
tangent = tangent.multiply(-1);
|
|
158
|
+
const plane = new Plane(position, [0, 0, 1], tangent);
|
|
159
|
+
const sketch = profile
|
|
160
|
+
.scale((loopSteps.length - index) / loopSteps.length, [0, 0])
|
|
161
|
+
.sketchOnPlane(plane);
|
|
162
|
+
plane.delete();
|
|
163
|
+
return sketch;
|
|
164
|
+
});
|
|
165
|
+
helix.delete();
|
|
166
|
+
const endFace = profiles.at(-1).faces();
|
|
167
|
+
const shell = profiles[0].loftWith(profiles.slice(1), { ruled: false }, true);
|
|
168
|
+
const closed = weldShellsAndFaces([shell, endFace]);
|
|
169
|
+
shell.delete();
|
|
170
|
+
endFace.delete();
|
|
171
|
+
return bottom ? closed.rotate(180, [0, 0, 0], [0, 0, 1]) : closed;
|
|
172
|
+
}
|
|
173
|
+
function assertPositive(name, value) {
|
|
174
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
175
|
+
throw new Error(`${name} must be a positive finite number.`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function clamp(value, minimum, maximum) {
|
|
179
|
+
return Math.min(Math.max(value, minimum), maximum);
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=thread.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread.js","sourceRoot":"","sources":["../../src/library/thread.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,QAAQ,GAKT,MAAM,uBAAuB,CAAC;AAE/B,MAAM,EAAC,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,kBAAkB,EAAC,GACzE,QAAQ,CAAC;AAYX,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,EAAC,MAAM,EAAE,EAAE,EAAC,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,EAAE,CAAC,CAAC;AACrE,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;AAC9C,MAAM,2BAA2B,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AACpD,IAAI,oBAAoB,GAAG,CAAC,CAAC;AAE7B,MAAM,CAAC,MAAM,aAAa,GAAG,eAAe,CAC1C,CAAC,OAA6B,EAAE,EAAE;IAChC,qBAAqB,CAAC,OAAO,CAAC,CAAC;IAC/B,MAAM,EACJ,aAAa,EACb,aAAa,EACb,UAAU,GAAG,KAAK,EAClB,GAAG,UAAU,EACd,GAAG,OAAO,CAAC;IACZ,OAAO,wBAAwB,CAAC;QAC9B,GAAG,UAAU;QACb,WAAW,EAAE,aAAa,GAAG,CAAC;QAC9B,WAAW,EAAE,aAAa,GAAG,CAAC;QAC9B,UAAU;KACX,CAAC,CAAC;AACL,CAAC,CACF,CAAC;AAEF,SAAS,qBAAqB,CAAC,EAC7B,KAAK,EACL,CAAC,EACD,aAAa,EACb,aAAa,EACb,SAAS,EACT,UAAU,GACW;IACrB,cAAc,CAAC,OAAO,EAAE,KAAK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C5D,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC;AACrD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@code3d/screws",
|
|
3
|
+
"version": "0.0.1-alpha.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
8
|
+
"author": "vilicvane",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prepack": "node ../../scripts/package-license.mjs",
|
|
12
|
+
"test": "node ../../node_modules/typescript/bin/tsc --build src/library/tsconfig.json && npm run test:types && npm run test:run",
|
|
13
|
+
"test:run": "node --test \"test/*.test.ts\"",
|
|
14
|
+
"test:types": "node ../../node_modules/typescript/bin/tsc --noEmit -p test/tsconfig.json"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./bld/library/index.d.ts",
|
|
19
|
+
"default": "./bld/library/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"bld",
|
|
24
|
+
"README.md",
|
|
25
|
+
"src"
|
|
26
|
+
],
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@code3d/core": "^0.0.1-alpha.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@code3d/core": "*"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as ISO4762 from './iso-4762.js';
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cylinder,
|
|
3
|
+
cut,
|
|
4
|
+
frustum,
|
|
5
|
+
regularPrism,
|
|
6
|
+
union,
|
|
7
|
+
type CanonicalElements,
|
|
8
|
+
type Bound,
|
|
9
|
+
type LineAnchor,
|
|
10
|
+
type SolidModel,
|
|
11
|
+
} from '@code3d/core';
|
|
12
|
+
import {helicalThread} from './thread.js';
|
|
13
|
+
|
|
14
|
+
export type ClearanceFit = 'close' | 'normal' | 'loose';
|
|
15
|
+
|
|
16
|
+
export type Specification = Readonly<{
|
|
17
|
+
designation: string;
|
|
18
|
+
nominalDiameter: number;
|
|
19
|
+
pitch: number;
|
|
20
|
+
headDiameter: number;
|
|
21
|
+
headHeight: number;
|
|
22
|
+
hexSocketWidth: number;
|
|
23
|
+
hexSocketDepth: number;
|
|
24
|
+
underHeadRadius: number;
|
|
25
|
+
clearance: Readonly<Record<ClearanceFit, number>>;
|
|
26
|
+
counterboreDiameter: number;
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
// ISO 4762 coarse-thread socket-head cap screws; millimetres.
|
|
30
|
+
// Clearance series follow ISO 273. Counterbores follow DIN 974-1 normal series.
|
|
31
|
+
export const specifications = {
|
|
32
|
+
M3: {
|
|
33
|
+
designation: 'M3',
|
|
34
|
+
nominalDiameter: 3,
|
|
35
|
+
pitch: 0.5,
|
|
36
|
+
headDiameter: 5.5,
|
|
37
|
+
headHeight: 3,
|
|
38
|
+
hexSocketWidth: 2.5,
|
|
39
|
+
hexSocketDepth: 1.3,
|
|
40
|
+
underHeadRadius: 0.1,
|
|
41
|
+
clearance: {close: 3.2, normal: 3.4, loose: 3.6},
|
|
42
|
+
counterboreDiameter: 6,
|
|
43
|
+
},
|
|
44
|
+
M4: {
|
|
45
|
+
designation: 'M4',
|
|
46
|
+
nominalDiameter: 4,
|
|
47
|
+
pitch: 0.7,
|
|
48
|
+
headDiameter: 7,
|
|
49
|
+
headHeight: 4,
|
|
50
|
+
hexSocketWidth: 3,
|
|
51
|
+
hexSocketDepth: 2,
|
|
52
|
+
underHeadRadius: 0.2,
|
|
53
|
+
clearance: {close: 4.3, normal: 4.5, loose: 4.8},
|
|
54
|
+
counterboreDiameter: 8,
|
|
55
|
+
},
|
|
56
|
+
M5: {
|
|
57
|
+
designation: 'M5',
|
|
58
|
+
nominalDiameter: 5,
|
|
59
|
+
pitch: 0.8,
|
|
60
|
+
headDiameter: 8.5,
|
|
61
|
+
headHeight: 5,
|
|
62
|
+
hexSocketWidth: 4,
|
|
63
|
+
hexSocketDepth: 2.5,
|
|
64
|
+
underHeadRadius: 0.2,
|
|
65
|
+
clearance: {close: 5.3, normal: 5.5, loose: 5.8},
|
|
66
|
+
counterboreDiameter: 10,
|
|
67
|
+
},
|
|
68
|
+
M6: {
|
|
69
|
+
designation: 'M6',
|
|
70
|
+
nominalDiameter: 6,
|
|
71
|
+
pitch: 1,
|
|
72
|
+
headDiameter: 10,
|
|
73
|
+
headHeight: 6,
|
|
74
|
+
hexSocketWidth: 5,
|
|
75
|
+
hexSocketDepth: 3,
|
|
76
|
+
underHeadRadius: 0.25,
|
|
77
|
+
clearance: {close: 6.4, normal: 6.6, loose: 7},
|
|
78
|
+
counterboreDiameter: 11,
|
|
79
|
+
},
|
|
80
|
+
M8: {
|
|
81
|
+
designation: 'M8',
|
|
82
|
+
nominalDiameter: 8,
|
|
83
|
+
pitch: 1.25,
|
|
84
|
+
headDiameter: 13,
|
|
85
|
+
headHeight: 8,
|
|
86
|
+
hexSocketWidth: 6,
|
|
87
|
+
hexSocketDepth: 4,
|
|
88
|
+
underHeadRadius: 0.4,
|
|
89
|
+
clearance: {close: 8.4, normal: 9, loose: 10},
|
|
90
|
+
counterboreDiameter: 15,
|
|
91
|
+
},
|
|
92
|
+
M10: {
|
|
93
|
+
designation: 'M10',
|
|
94
|
+
nominalDiameter: 10,
|
|
95
|
+
pitch: 1.5,
|
|
96
|
+
headDiameter: 16,
|
|
97
|
+
headHeight: 10,
|
|
98
|
+
hexSocketWidth: 8,
|
|
99
|
+
hexSocketDepth: 5,
|
|
100
|
+
underHeadRadius: 0.4,
|
|
101
|
+
clearance: {close: 10.5, normal: 11, loose: 12},
|
|
102
|
+
counterboreDiameter: 18,
|
|
103
|
+
},
|
|
104
|
+
M12: {
|
|
105
|
+
designation: 'M12',
|
|
106
|
+
nominalDiameter: 12,
|
|
107
|
+
pitch: 1.75,
|
|
108
|
+
headDiameter: 18,
|
|
109
|
+
headHeight: 12,
|
|
110
|
+
hexSocketWidth: 10,
|
|
111
|
+
hexSocketDepth: 6,
|
|
112
|
+
underHeadRadius: 0.6,
|
|
113
|
+
clearance: {close: 13, normal: 13.5, loose: 14.5},
|
|
114
|
+
counterboreDiameter: 20,
|
|
115
|
+
},
|
|
116
|
+
} as const satisfies Readonly<Record<string, Specification>>;
|
|
117
|
+
|
|
118
|
+
export type Size = keyof typeof specifications;
|
|
119
|
+
export type ScrewInput = Size | Specification;
|
|
120
|
+
|
|
121
|
+
export type CounterboreOptions = Readonly<{
|
|
122
|
+
diameter?: number;
|
|
123
|
+
depth?: number;
|
|
124
|
+
axialClearance?: number;
|
|
125
|
+
}>;
|
|
126
|
+
|
|
127
|
+
export type ClearanceHoleOptions = Readonly<{
|
|
128
|
+
depth: number;
|
|
129
|
+
fit?: ClearanceFit;
|
|
130
|
+
diameter?: number;
|
|
131
|
+
counterbore?: boolean | CounterboreOptions;
|
|
132
|
+
}>;
|
|
133
|
+
|
|
134
|
+
export type PlainClearanceHoleOptions = ClearanceHoleOptions &
|
|
135
|
+
Readonly<{
|
|
136
|
+
counterbore: false;
|
|
137
|
+
}>;
|
|
138
|
+
|
|
139
|
+
export type CounterboredHoleOptions = Omit<
|
|
140
|
+
ClearanceHoleOptions,
|
|
141
|
+
'counterbore'
|
|
142
|
+
> &
|
|
143
|
+
Readonly<{
|
|
144
|
+
counterbore?: true | CounterboreOptions;
|
|
145
|
+
}>;
|
|
146
|
+
|
|
147
|
+
export type SocketCapScrewElements = CanonicalElements &
|
|
148
|
+
Readonly<{
|
|
149
|
+
headTop: Bound;
|
|
150
|
+
headBottom: Bound;
|
|
151
|
+
shankTop: Bound;
|
|
152
|
+
shankBottom: Bound;
|
|
153
|
+
shankAxis: LineAnchor;
|
|
154
|
+
}>;
|
|
155
|
+
|
|
156
|
+
export type SocketCapHoleElements = CanonicalElements &
|
|
157
|
+
Readonly<{
|
|
158
|
+
shaftTop: Bound;
|
|
159
|
+
shaftBottom: Bound;
|
|
160
|
+
shaftAxis: LineAnchor;
|
|
161
|
+
}>;
|
|
162
|
+
|
|
163
|
+
export type CounterboredSocketCapHoleElements = SocketCapHoleElements &
|
|
164
|
+
Readonly<{
|
|
165
|
+
counterboreTop: Bound;
|
|
166
|
+
counterboreBottom: Bound;
|
|
167
|
+
}>;
|
|
168
|
+
|
|
169
|
+
export type Screw = SolidModel<SocketCapScrewElements>;
|
|
170
|
+
export type ClearanceHole = SolidModel<SocketCapHoleElements>;
|
|
171
|
+
export type CounterboredHole = SolidModel<CounterboredSocketCapHoleElements>;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @code3d.arguments ['M6', 18]
|
|
175
|
+
* @code3d.arguments ['M8', 30]
|
|
176
|
+
* @code3d.param length {kind: 'length'}
|
|
177
|
+
*/
|
|
178
|
+
export function screw(input: ScrewInput, length: number): Screw {
|
|
179
|
+
const spec = resolveSpecification(input);
|
|
180
|
+
validateSpec(spec);
|
|
181
|
+
if (!Number.isFinite(length) || length <= spec.pitch + spec.underHeadRadius) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
'Screw length must leave room for at least one full thread pitch.',
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const headChamfer = Math.min(spec.pitch / 2, spec.headHeight * 0.12);
|
|
188
|
+
const headBarrel = cylinder(
|
|
189
|
+
spec.headDiameter / 2,
|
|
190
|
+
spec.headHeight - headChamfer,
|
|
191
|
+
);
|
|
192
|
+
const headTop = frustum(
|
|
193
|
+
spec.headDiameter / 2,
|
|
194
|
+
spec.headDiameter / 2 - headChamfer,
|
|
195
|
+
headChamfer,
|
|
196
|
+
).relate(top => top.on(headBarrel.up));
|
|
197
|
+
const headBlank = union([headBarrel, headTop]);
|
|
198
|
+
|
|
199
|
+
const socketToolY = spec.hexSocketDepth + 0.2;
|
|
200
|
+
const socketTool = regularPrism(
|
|
201
|
+
spec.hexSocketWidth / Math.sqrt(3),
|
|
202
|
+
socketToolY,
|
|
203
|
+
6,
|
|
204
|
+
30,
|
|
205
|
+
).relate(tool =>
|
|
206
|
+
tool.center.on(headBlank.up).offset(0, -spec.hexSocketDepth / 2 + 0.1, 0),
|
|
207
|
+
);
|
|
208
|
+
const head = cut(headBlank, [socketTool]);
|
|
209
|
+
|
|
210
|
+
const transition = frustum(
|
|
211
|
+
spec.nominalDiameter / 2,
|
|
212
|
+
spec.nominalDiameter / 2 + spec.underHeadRadius,
|
|
213
|
+
spec.underHeadRadius,
|
|
214
|
+
).relate(part => part.on(head.down));
|
|
215
|
+
const bodyLength = length - spec.underHeadRadius;
|
|
216
|
+
const threadedLength = Math.min(bodyLength, threadLength(spec, length));
|
|
217
|
+
const plainLength = bodyLength - threadedLength;
|
|
218
|
+
const overlap = Math.min(0.08, spec.pitch / 10);
|
|
219
|
+
const parts = [head, transition];
|
|
220
|
+
let previous = transition;
|
|
221
|
+
|
|
222
|
+
if (plainLength > overlap) {
|
|
223
|
+
const shank = cylinder(
|
|
224
|
+
spec.nominalDiameter / 2,
|
|
225
|
+
plainLength + overlap,
|
|
226
|
+
).relate(part => part.on(previous.down).offset(0, -overlap, 0));
|
|
227
|
+
parts.push(shank);
|
|
228
|
+
previous = shank;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const fundamentalHeight = (Math.sqrt(3) / 2) * spec.pitch;
|
|
232
|
+
const minorDiameter =
|
|
233
|
+
spec.nominalDiameter - 2 * ((5 / 8) * fundamentalHeight);
|
|
234
|
+
const thread = helicalThread({
|
|
235
|
+
pitch: spec.pitch,
|
|
236
|
+
y: threadedLength + overlap,
|
|
237
|
+
majorDiameter: spec.nominalDiameter,
|
|
238
|
+
minorDiameter,
|
|
239
|
+
rootWidth: (3 / 4) * spec.pitch,
|
|
240
|
+
crestWidth: (1 / 8) * spec.pitch,
|
|
241
|
+
}).relate(part => part.on(previous.down).offset(0, -overlap, 0));
|
|
242
|
+
parts.push(thread);
|
|
243
|
+
|
|
244
|
+
return union(parts).expose({
|
|
245
|
+
headTop: head.up,
|
|
246
|
+
headBottom: head.down,
|
|
247
|
+
shankTop: transition.up,
|
|
248
|
+
shankBottom: thread.down,
|
|
249
|
+
shankAxis: thread.axis,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* @code3d.arguments ['M6', 10]
|
|
255
|
+
* @code3d.arguments ['M6', {depth: 10, counterbore: false}]
|
|
256
|
+
* @code3d.param depth {kind: 'length', constraints: {exclusiveMin: 0}}
|
|
257
|
+
*/
|
|
258
|
+
export function clearanceHole(
|
|
259
|
+
input: ScrewInput,
|
|
260
|
+
depth: number,
|
|
261
|
+
): CounterboredHole;
|
|
262
|
+
export function clearanceHole(
|
|
263
|
+
input: ScrewInput,
|
|
264
|
+
options: PlainClearanceHoleOptions,
|
|
265
|
+
): ClearanceHole;
|
|
266
|
+
export function clearanceHole(
|
|
267
|
+
input: ScrewInput,
|
|
268
|
+
options: CounterboredHoleOptions,
|
|
269
|
+
): CounterboredHole;
|
|
270
|
+
export function clearanceHole(
|
|
271
|
+
input: ScrewInput,
|
|
272
|
+
options: ClearanceHoleOptions,
|
|
273
|
+
): ClearanceHole | CounterboredHole;
|
|
274
|
+
export function clearanceHole(
|
|
275
|
+
input: ScrewInput,
|
|
276
|
+
optionsOrDepth: ClearanceHoleOptions | number,
|
|
277
|
+
): ClearanceHole | CounterboredHole {
|
|
278
|
+
const options: ClearanceHoleOptions =
|
|
279
|
+
typeof optionsOrDepth === 'number'
|
|
280
|
+
? {depth: optionsOrDepth}
|
|
281
|
+
: optionsOrDepth;
|
|
282
|
+
const spec = resolveSpecification(input);
|
|
283
|
+
validateSpec(spec);
|
|
284
|
+
if (!Number.isFinite(options.depth) || options.depth <= 0) {
|
|
285
|
+
throw new Error('Hole depth must be a positive finite number.');
|
|
286
|
+
}
|
|
287
|
+
const fit = options.fit ?? 'normal';
|
|
288
|
+
const diameter = options.diameter ?? spec.clearance[fit];
|
|
289
|
+
if (!Number.isFinite(diameter) || diameter <= spec.nominalDiameter) {
|
|
290
|
+
throw new Error(
|
|
291
|
+
'Clearance-hole diameter must exceed the nominal screw diameter.',
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
const shaft = cylinder(diameter / 2, options.depth);
|
|
295
|
+
const counterboreOption = options.counterbore ?? true;
|
|
296
|
+
if (counterboreOption === false) {
|
|
297
|
+
return shaft.expose({
|
|
298
|
+
shaftTop: shaft.up,
|
|
299
|
+
shaftBottom: shaft.down,
|
|
300
|
+
shaftAxis: shaft.axis,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const counterbore = counterboreOption === true ? {} : counterboreOption;
|
|
305
|
+
const axialClearance = counterbore.axialClearance ?? 0.5;
|
|
306
|
+
const counterboreDepth =
|
|
307
|
+
counterbore.depth ?? spec.headHeight + axialClearance;
|
|
308
|
+
const counterboreDiameter = counterbore.diameter ?? spec.counterboreDiameter;
|
|
309
|
+
if (
|
|
310
|
+
!Number.isFinite(counterboreDepth) ||
|
|
311
|
+
counterboreDepth <= 0 ||
|
|
312
|
+
counterboreDepth > options.depth
|
|
313
|
+
) {
|
|
314
|
+
throw new Error('Counterbore depth must be within the total hole depth.');
|
|
315
|
+
}
|
|
316
|
+
if (
|
|
317
|
+
!Number.isFinite(counterboreDiameter) ||
|
|
318
|
+
counterboreDiameter < spec.headDiameter
|
|
319
|
+
) {
|
|
320
|
+
throw new Error('Counterbore diameter must accommodate the screw head.');
|
|
321
|
+
}
|
|
322
|
+
const recess = cylinder(
|
|
323
|
+
counterboreDiameter / 2,
|
|
324
|
+
counterboreDepth + 0.2,
|
|
325
|
+
).relate(tool =>
|
|
326
|
+
tool.center.on(shaft.up).offset(0, -counterboreDepth / 2 + 0.1, 0),
|
|
327
|
+
);
|
|
328
|
+
return union([shaft, recess]).expose({
|
|
329
|
+
shaftTop: shaft.up,
|
|
330
|
+
shaftBottom: shaft.down,
|
|
331
|
+
shaftAxis: shaft.axis,
|
|
332
|
+
counterboreTop: recess.up,
|
|
333
|
+
counterboreBottom: recess.down,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function resolveSpecification(input: ScrewInput): Specification {
|
|
338
|
+
return typeof input === 'string' ? specifications[input] : input;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function threadLength(spec: Specification, length: number): number {
|
|
342
|
+
if (length <= 125) return 2 * spec.nominalDiameter + 12;
|
|
343
|
+
if (length <= 200) return 2 * spec.nominalDiameter + 18;
|
|
344
|
+
return 2 * spec.nominalDiameter + 31;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function validateSpec(spec: Specification): void {
|
|
348
|
+
const values = [
|
|
349
|
+
spec.nominalDiameter,
|
|
350
|
+
spec.pitch,
|
|
351
|
+
spec.headDiameter,
|
|
352
|
+
spec.headHeight,
|
|
353
|
+
spec.hexSocketWidth,
|
|
354
|
+
spec.hexSocketDepth,
|
|
355
|
+
spec.underHeadRadius,
|
|
356
|
+
spec.clearance.close,
|
|
357
|
+
spec.clearance.normal,
|
|
358
|
+
spec.clearance.loose,
|
|
359
|
+
spec.counterboreDiameter,
|
|
360
|
+
];
|
|
361
|
+
if (values.some(value => !Number.isFinite(value) || value <= 0)) {
|
|
362
|
+
throw new Error(
|
|
363
|
+
'Fastener specifications must contain positive finite dimensions.',
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
if (spec.headDiameter <= spec.nominalDiameter) {
|
|
367
|
+
throw new Error('Head diameter must exceed the nominal thread diameter.');
|
|
368
|
+
}
|
|
369
|
+
if (spec.hexSocketDepth >= spec.headHeight) {
|
|
370
|
+
throw new Error('Hex socket depth must be smaller than the head height.');
|
|
371
|
+
}
|
|
372
|
+
}
|