|
@@ -0,0 +1,371 @@
|
|
1
|
+/*
|
|
2
|
+ * ISO-standard metric threads, following this specification:
|
|
3
|
+ * http://en.wikipedia.org/wiki/ISO_metric_screw_thread
|
|
4
|
+ *
|
|
5
|
+ * Copyright 2016 Dan Kirshner - dan_kirshner@yahoo.com
|
|
6
|
+ * This program is free software: you can redistribute it and/or modify
|
|
7
|
+ * it under the terms of the GNU General Public License as published by
|
|
8
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+ * (at your option) any later version.
|
|
10
|
+ *
|
|
11
|
+ * This program is distributed in the hope that it will be useful,
|
|
12
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+ * GNU General Public License for more details.
|
|
15
|
+ *
|
|
16
|
+ * See <http://www.gnu.org/licenses/>.
|
|
17
|
+ *
|
|
18
|
+ * Version 2.2. 2017-01-01 Correction for angle; leadfac option. (Thanks to
|
|
19
|
+ * Andrew Allen <a2intl@gmail.com>.)
|
|
20
|
+ * Version 2.1. 2016-12-04 Chamfer bottom end (low-z); leadin option.
|
|
21
|
+ * Version 2.0. 2016-11-05 Backwards compatibility (earlier OpenSCAD) fixes.
|
|
22
|
+ * Version 1.9. 2016-07-03 Option: tapered.
|
|
23
|
+ * Version 1.8. 2016-01-08 Option: (non-standard) angle.
|
|
24
|
+ * Version 1.7. 2015-11-28 Larger x-increment - for small-diameters.
|
|
25
|
+ * Version 1.6. 2015-09-01 Options: square threads, rectangular threads.
|
|
26
|
+ * Version 1.5. 2015-06-12 Options: thread_size, groove.
|
|
27
|
+ * Version 1.4. 2014-10-17 Use "faces" instead of "triangles" for polyhedron
|
|
28
|
+ * Version 1.3. 2013-12-01 Correct loop over turns -- don't have early cut-off
|
|
29
|
+ * Version 1.2. 2012-09-09 Use discrete polyhedra rather than linear_extrude ()
|
|
30
|
+ * Version 1.1. 2012-09-07 Corrected to right-hand threads!
|
|
31
|
+ */
|
|
32
|
+
|
|
33
|
+// Examples.
|
|
34
|
+//
|
|
35
|
+// Standard M8 x 1.
|
|
36
|
+// metric_thread (diameter=8, pitch=1, length=4);
|
|
37
|
+
|
|
38
|
+// Square thread.
|
|
39
|
+// metric_thread (diameter=8, pitch=1, length=4, square=true);
|
|
40
|
+
|
|
41
|
+// Non-standard: long pitch, same thread size.
|
|
42
|
+//metric_thread (diameter=8, pitch=4, length=4, thread_size=1, groove=true);
|
|
43
|
+
|
|
44
|
+// Non-standard: 20 mm diameter, long pitch, square "trough" width 3 mm,
|
|
45
|
+// depth 1 mm.
|
|
46
|
+//metric_thread (diameter=20, pitch=8, length=16, square=true, thread_size=6,
|
|
47
|
+// groove=true, rectangle=0.333);
|
|
48
|
+
|
|
49
|
+// English: 1/4 x 20.
|
|
50
|
+//english_thread (diameter=1/4, threads_per_inch=20, length=1);
|
|
51
|
+
|
|
52
|
+// Tapered. Example -- pipe size 3/4" -- per:
|
|
53
|
+// http://www.engineeringtoolbox.com/npt-national-pipe-taper-threads-d_750.html
|
|
54
|
+// english_thread (diameter=1.05, threads_per_inch=14, length=3/4, taper=1/16);
|
|
55
|
+
|
|
56
|
+// Thread for mounting on Rohloff hub.
|
|
57
|
+//difference () {
|
|
58
|
+// cylinder (r=20, h=10, $fn=100);
|
|
59
|
+//
|
|
60
|
+// metric_thread (diameter=34, pitch=1, length=10, internal=true, n_starts=6);
|
|
61
|
+//}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+// ----------------------------------------------------------------------------
|
|
65
|
+function segments (diameter) = min (50, ceil (diameter*6));
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+// ----------------------------------------------------------------------------
|
|
69
|
+// diameter - outside diameter of threads in mm. Default: 8.
|
|
70
|
+// pitch - thread axial "travel" per turn in mm. Default: 1.
|
|
71
|
+// length - overall axial length of thread in mm. Default: 1.
|
|
72
|
+// internal - true = clearances for internal thread (e.g., a nut).
|
|
73
|
+// false = clearances for external thread (e.g., a bolt).
|
|
74
|
+// (Internal threads should be "cut out" from a solid using
|
|
75
|
+// difference ()).
|
|
76
|
+// n_starts - Number of thread starts (e.g., DNA, a "double helix," has
|
|
77
|
+// n_starts=2). See wikipedia Screw_thread.
|
|
78
|
+// thread_size - (non-standard) axial width of a single thread "V" - independent
|
|
79
|
+// of pitch. Default: same as pitch.
|
|
80
|
+// groove - (non-standard) subtract inverted "V" from cylinder (rather than
|
|
81
|
+// add protruding "V" to cylinder).
|
|
82
|
+// square - Square threads (per
|
|
83
|
+// https://en.wikipedia.org/wiki/Square_thread_form).
|
|
84
|
+// rectangle - (non-standard) "Rectangular" thread - ratio depth/(axial) width
|
|
85
|
+// Default: 1 (square).
|
|
86
|
+// angle - (non-standard) angle (deg) of thread side from perpendicular to
|
|
87
|
+// axis (default = standard = 30 degrees).
|
|
88
|
+// taper - diameter change per length (National Pipe Thread/ANSI B1.20.1
|
|
89
|
+// is 1" diameter per 16" length). Taper decreases from 'diameter'
|
|
90
|
+// as z increases.
|
|
91
|
+// leadin - 1 (default): chamfer (45 degree) at max-z end; 0: no chamfer;
|
|
92
|
+// 2: chamfer at both ends, 3: chamfer at z=0 end.
|
|
93
|
+// leadfac - scale of leadin chamfer (default: 1.0 = 1/2 thread).
|
|
94
|
+module metric_thread (diameter=8, pitch=1, length=1, internal=false, n_starts=1,
|
|
95
|
+ thread_size=-1, groove=false, square=false, rectangle=0,
|
|
96
|
+ angle=30, taper=0, leadin=1, leadfac=1.0)
|
|
97
|
+{
|
|
98
|
+ // thread_size: size of thread "V" different than travel per turn (pitch).
|
|
99
|
+ // Default: same as pitch.
|
|
100
|
+ local_thread_size = thread_size == -1 ? pitch : thread_size;
|
|
101
|
+ local_rectangle = rectangle ? rectangle : 1;
|
|
102
|
+
|
|
103
|
+ n_segments = segments (diameter);
|
|
104
|
+ h = (square || rectangle) ? local_thread_size*local_rectangle/2 : local_thread_size / (2 * tan(angle));
|
|
105
|
+
|
|
106
|
+ h_fac1 = (square || rectangle) ? 0.90 : 0.625;
|
|
107
|
+
|
|
108
|
+ // External thread includes additional relief.
|
|
109
|
+ h_fac2 = (square || rectangle) ? 0.95 : 5.3/8;
|
|
110
|
+
|
|
111
|
+ tapered_diameter = diameter - length*taper;
|
|
112
|
+
|
|
113
|
+ difference () {
|
|
114
|
+ union () {
|
|
115
|
+ if (! groove) {
|
|
116
|
+ metric_thread_turns (diameter, pitch, length, internal, n_starts,
|
|
117
|
+ local_thread_size, groove, square, rectangle, angle,
|
|
118
|
+ taper);
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ difference () {
|
|
122
|
+
|
|
123
|
+ // Solid center, including Dmin truncation.
|
|
124
|
+ if (groove) {
|
|
125
|
+ cylinder (r1=diameter/2, r2=tapered_diameter/2,
|
|
126
|
+ h=length, $fn=n_segments);
|
|
127
|
+ } else if (internal) {
|
|
128
|
+ cylinder (r1=diameter/2 - h*h_fac1, r2=tapered_diameter/2 - h*h_fac1,
|
|
129
|
+ h=length, $fn=n_segments);
|
|
130
|
+ } else {
|
|
131
|
+
|
|
132
|
+ // External thread.
|
|
133
|
+ cylinder (r1=diameter/2 - h*h_fac2, r2=tapered_diameter/2 - h*h_fac2,
|
|
134
|
+ h=length, $fn=n_segments);
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ if (groove) {
|
|
138
|
+ metric_thread_turns (diameter, pitch, length, internal, n_starts,
|
|
139
|
+ local_thread_size, groove, square, rectangle,
|
|
140
|
+ angle, taper);
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ // chamfer z=0 end if leadin is 2 or 3
|
|
146
|
+ if (leadin == 2 || leadin == 3) {
|
|
147
|
+ difference () {
|
|
148
|
+ cylinder (r=diameter/2 + 1, h=h*h_fac1*leadfac, $fn=n_segments);
|
|
149
|
+
|
|
150
|
+ cylinder (r2=diameter/2, r1=diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,
|
|
151
|
+ $fn=n_segments);
|
|
152
|
+ }
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ // chamfer z-max end if leadin is 1 or 2.
|
|
156
|
+ if (leadin == 1 || leadin == 2) {
|
|
157
|
+ translate ([0, 0, length + 0.05 - h*h_fac1*leadfac]) {
|
|
158
|
+ difference () {
|
|
159
|
+ cylinder (r=diameter/2 + 1, h=h*h_fac1*leadfac, $fn=n_segments);
|
|
160
|
+ cylinder (r1=tapered_diameter/2, r2=tapered_diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,
|
|
161
|
+ $fn=n_segments);
|
|
162
|
+ }
|
|
163
|
+ }
|
|
164
|
+ }
|
|
165
|
+ }
|
|
166
|
+}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+// ----------------------------------------------------------------------------
|
|
170
|
+// Input units in inches.
|
|
171
|
+// Note: units of measure in drawing are mm!
|
|
172
|
+module english_thread (diameter=0.25, threads_per_inch=20, length=1,
|
|
173
|
+ internal=false, n_starts=1, thread_size=-1, groove=false,
|
|
174
|
+ square=false, rectangle=0, angle=30, taper=0, leadin=1)
|
|
175
|
+{
|
|
176
|
+ // Convert to mm.
|
|
177
|
+ mm_diameter = diameter*25.4;
|
|
178
|
+ mm_pitch = (1.0/threads_per_inch)*25.4;
|
|
179
|
+ mm_length = length*25.4;
|
|
180
|
+
|
|
181
|
+ echo (str ("mm_diameter: ", mm_diameter));
|
|
182
|
+ echo (str ("mm_pitch: ", mm_pitch));
|
|
183
|
+ echo (str ("mm_length: ", mm_length));
|
|
184
|
+ metric_thread (mm_diameter, mm_pitch, mm_length, internal, n_starts,
|
|
185
|
+ thread_size, groove, square, rectangle, angle, taper, leadin);
|
|
186
|
+}
|
|
187
|
+
|
|
188
|
+// ----------------------------------------------------------------------------
|
|
189
|
+module metric_thread_turns (diameter, pitch, length, internal, n_starts,
|
|
190
|
+ thread_size, groove, square, rectangle, angle,
|
|
191
|
+ taper)
|
|
192
|
+{
|
|
193
|
+ // Number of turns needed.
|
|
194
|
+ n_turns = floor (length/pitch);
|
|
195
|
+
|
|
196
|
+ intersection () {
|
|
197
|
+
|
|
198
|
+ // Start one below z = 0. Gives an extra turn at each end.
|
|
199
|
+ for (i=[-1*n_starts : n_turns+1]) {
|
|
200
|
+ translate ([0, 0, i*pitch]) {
|
|
201
|
+ metric_thread_turn (diameter, pitch, internal, n_starts,
|
|
202
|
+ thread_size, groove, square, rectangle, angle,
|
|
203
|
+ taper, i*pitch);
|
|
204
|
+ }
|
|
205
|
+ }
|
|
206
|
+
|
|
207
|
+ // Cut to length.
|
|
208
|
+ translate ([0, 0, length/2]) {
|
|
209
|
+ cube ([diameter*3, diameter*3, length], center=true);
|
|
210
|
+ }
|
|
211
|
+ }
|
|
212
|
+}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+// ----------------------------------------------------------------------------
|
|
216
|
+module metric_thread_turn (diameter, pitch, internal, n_starts, thread_size,
|
|
217
|
+ groove, square, rectangle, angle, taper, z)
|
|
218
|
+{
|
|
219
|
+ n_segments = segments (diameter);
|
|
220
|
+ fraction_circle = 1.0/n_segments;
|
|
221
|
+ for (i=[0 : n_segments-1]) {
|
|
222
|
+ rotate ([0, 0, i*360*fraction_circle]) {
|
|
223
|
+ translate ([0, 0, i*n_starts*pitch*fraction_circle]) {
|
|
224
|
+ //current_diameter = diameter - taper*(z + i*n_starts*pitch*fraction_circle);
|
|
225
|
+ thread_polyhedron ((diameter - taper*(z + i*n_starts*pitch*fraction_circle))/2,
|
|
226
|
+ pitch, internal, n_starts, thread_size, groove,
|
|
227
|
+ square, rectangle, angle);
|
|
228
|
+ }
|
|
229
|
+ }
|
|
230
|
+ }
|
|
231
|
+}
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+// ----------------------------------------------------------------------------
|
|
235
|
+module thread_polyhedron (radius, pitch, internal, n_starts, thread_size,
|
|
236
|
+ groove, square, rectangle, angle)
|
|
237
|
+{
|
|
238
|
+ n_segments = segments (radius*2);
|
|
239
|
+ fraction_circle = 1.0/n_segments;
|
|
240
|
+
|
|
241
|
+ local_rectangle = rectangle ? rectangle : 1;
|
|
242
|
+
|
|
243
|
+ h = (square || rectangle) ? thread_size*local_rectangle/2 : thread_size / (2 * tan(angle));
|
|
244
|
+ outer_r = radius + (internal ? h/20 : 0); // Adds internal relief.
|
|
245
|
+ //echo (str ("outer_r: ", outer_r));
|
|
246
|
+
|
|
247
|
+ // A little extra on square thread -- make sure overlaps cylinder.
|
|
248
|
+ h_fac1 = (square || rectangle) ? 1.1 : 0.875;
|
|
249
|
+ inner_r = radius - h*h_fac1; // Does NOT do Dmin_truncation - do later with
|
|
250
|
+ // cylinder.
|
|
251
|
+
|
|
252
|
+ translate_y = groove ? outer_r + inner_r : 0;
|
|
253
|
+ reflect_x = groove ? 1 : 0;
|
|
254
|
+
|
|
255
|
+ // Make these just slightly bigger (keep in proportion) so polyhedra will
|
|
256
|
+ // overlap.
|
|
257
|
+ x_incr_outer = (! groove ? outer_r : inner_r) * fraction_circle * 2 * PI * 1.02;
|
|
258
|
+ x_incr_inner = (! groove ? inner_r : outer_r) * fraction_circle * 2 * PI * 1.02;
|
|
259
|
+ z_incr = n_starts * pitch * fraction_circle * 1.005;
|
|
260
|
+
|
|
261
|
+ /*
|
|
262
|
+ (angles x0 and x3 inner are actually 60 deg)
|
|
263
|
+
|
|
264
|
+ /\ (x2_inner, z2_inner) [2]
|
|
265
|
+ / \
|
|
266
|
+ (x3_inner, z3_inner) / \
|
|
267
|
+ [3] \ \
|
|
268
|
+ |\ \ (x2_outer, z2_outer) [6]
|
|
269
|
+ | \ /
|
|
270
|
+ | \ /|
|
|
271
|
+ z |[7]\/ / (x1_outer, z1_outer) [5]
|
|
272
|
+ | | | /
|
|
273
|
+ | x | |/
|
|
274
|
+ | / | / (x0_outer, z0_outer) [4]
|
|
275
|
+ | / | / (behind: (x1_inner, z1_inner) [1]
|
|
276
|
+ |/ | /
|
|
277
|
+ y________| |/
|
|
278
|
+ (r) / (x0_inner, z0_inner) [0]
|
|
279
|
+
|
|
280
|
+ */
|
|
281
|
+
|
|
282
|
+ x1_outer = outer_r * fraction_circle * 2 * PI;
|
|
283
|
+
|
|
284
|
+ z0_outer = (outer_r - inner_r) * tan(angle);
|
|
285
|
+ //echo (str ("z0_outer: ", z0_outer));
|
|
286
|
+
|
|
287
|
+ //polygon ([[inner_r, 0], [outer_r, z0_outer],
|
|
288
|
+ // [outer_r, 0.5*pitch], [inner_r, 0.5*pitch]]);
|
|
289
|
+ z1_outer = z0_outer + z_incr;
|
|
290
|
+
|
|
291
|
+ // Give internal square threads some clearance in the z direction, too.
|
|
292
|
+ bottom = internal ? 0.235 : 0.25;
|
|
293
|
+ top = internal ? 0.765 : 0.75;
|
|
294
|
+
|
|
295
|
+ translate ([0, translate_y, 0]) {
|
|
296
|
+ mirror ([reflect_x, 0, 0]) {
|
|
297
|
+
|
|
298
|
+ if (square || rectangle) {
|
|
299
|
+
|
|
300
|
+ // Rule for face ordering: look at polyhedron from outside: points must
|
|
301
|
+ // be in clockwise order.
|
|
302
|
+ polyhedron (
|
|
303
|
+ points = [
|
|
304
|
+ [-x_incr_inner/2, -inner_r, bottom*thread_size], // [0]
|
|
305
|
+ [x_incr_inner/2, -inner_r, bottom*thread_size + z_incr], // [1]
|
|
306
|
+ [x_incr_inner/2, -inner_r, top*thread_size + z_incr], // [2]
|
|
307
|
+ [-x_incr_inner/2, -inner_r, top*thread_size], // [3]
|
|
308
|
+
|
|
309
|
+ [-x_incr_outer/2, -outer_r, bottom*thread_size], // [4]
|
|
310
|
+ [x_incr_outer/2, -outer_r, bottom*thread_size + z_incr], // [5]
|
|
311
|
+ [x_incr_outer/2, -outer_r, top*thread_size + z_incr], // [6]
|
|
312
|
+ [-x_incr_outer/2, -outer_r, top*thread_size] // [7]
|
|
313
|
+ ],
|
|
314
|
+
|
|
315
|
+ faces = [
|
|
316
|
+ [0, 3, 7, 4], // This-side trapezoid
|
|
317
|
+
|
|
318
|
+ [1, 5, 6, 2], // Back-side trapezoid
|
|
319
|
+
|
|
320
|
+ [0, 1, 2, 3], // Inner rectangle
|
|
321
|
+
|
|
322
|
+ [4, 7, 6, 5], // Outer rectangle
|
|
323
|
+
|
|
324
|
+ // These are not planar, so do with separate triangles.
|
|
325
|
+ [7, 2, 6], // Upper rectangle, bottom
|
|
326
|
+ [7, 3, 2], // Upper rectangle, top
|
|
327
|
+
|
|
328
|
+ [0, 5, 1], // Lower rectangle, bottom
|
|
329
|
+ [0, 4, 5] // Lower rectangle, top
|
|
330
|
+ ]
|
|
331
|
+ );
|
|
332
|
+ } else {
|
|
333
|
+
|
|
334
|
+ // Rule for face ordering: look at polyhedron from outside: points must
|
|
335
|
+ // be in clockwise order.
|
|
336
|
+ polyhedron (
|
|
337
|
+ points = [
|
|
338
|
+ [-x_incr_inner/2, -inner_r, 0], // [0]
|
|
339
|
+ [x_incr_inner/2, -inner_r, z_incr], // [1]
|
|
340
|
+ [x_incr_inner/2, -inner_r, thread_size + z_incr], // [2]
|
|
341
|
+ [-x_incr_inner/2, -inner_r, thread_size], // [3]
|
|
342
|
+
|
|
343
|
+ [-x_incr_outer/2, -outer_r, z0_outer], // [4]
|
|
344
|
+ [x_incr_outer/2, -outer_r, z0_outer + z_incr], // [5]
|
|
345
|
+ [x_incr_outer/2, -outer_r, thread_size - z0_outer + z_incr], // [6]
|
|
346
|
+ [-x_incr_outer/2, -outer_r, thread_size - z0_outer] // [7]
|
|
347
|
+ ],
|
|
348
|
+
|
|
349
|
+ faces = [
|
|
350
|
+ [0, 3, 7, 4], // This-side trapezoid
|
|
351
|
+
|
|
352
|
+ [1, 5, 6, 2], // Back-side trapezoid
|
|
353
|
+
|
|
354
|
+ [0, 1, 2, 3], // Inner rectangle
|
|
355
|
+
|
|
356
|
+ [4, 7, 6, 5], // Outer rectangle
|
|
357
|
+
|
|
358
|
+ // These are not planar, so do with separate triangles.
|
|
359
|
+ [7, 2, 6], // Upper rectangle, bottom
|
|
360
|
+ [7, 3, 2], // Upper rectangle, top
|
|
361
|
+
|
|
362
|
+ [0, 5, 1], // Lower rectangle, bottom
|
|
363
|
+ [0, 4, 5] // Lower rectangle, top
|
|
364
|
+ ]
|
|
365
|
+ );
|
|
366
|
+ }
|
|
367
|
+ }
|
|
368
|
+ }
|
|
369
|
+}
|
|
370
|
+
|
|
371
|
+
|