2 * ISO-standard metric threads, following this specification:
\r
3 * http://en.wikipedia.org/wiki/ISO_metric_screw_thread
\r
5 * Copyright 2020 Dan Kirshner - dan_kirshner@yahoo.com
\r
6 * This program is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * This program is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * See <http://www.gnu.org/licenses/>.
\r
18 * Version 2.5. 2020-04-11 Leadin option works for internal threads.
\r
19 * Version 2.4. 2019-07-14 Add test option - do not render threads.
\r
20 * Version 2.3. 2017-08-31 Default for leadin: 0 (best for internal threads).
\r
21 * Version 2.2. 2017-01-01 Correction for angle; leadfac option. (Thanks to
\r
22 * Andrew Allen <a2intl@gmail.com>.)
\r
23 * Version 2.1. 2016-12-04 Chamfer bottom end (low-z); leadin option.
\r
24 * Version 2.0. 2016-11-05 Backwards compatibility (earlier OpenSCAD) fixes.
\r
25 * Version 1.9. 2016-07-03 Option: tapered.
\r
26 * Version 1.8. 2016-01-08 Option: (non-standard) angle.
\r
27 * Version 1.7. 2015-11-28 Larger x-increment - for small-diameters.
\r
28 * Version 1.6. 2015-09-01 Options: square threads, rectangular threads.
\r
29 * Version 1.5. 2015-06-12 Options: thread_size, groove.
\r
30 * Version 1.4. 2014-10-17 Use "faces" instead of "triangles" for polyhedron
\r
31 * Version 1.3. 2013-12-01 Correct loop over turns -- don't have early cut-off
\r
32 * Version 1.2. 2012-09-09 Use discrete polyhedra rather than linear_extrude ()
\r
33 * Version 1.1. 2012-09-07 Corrected to right-hand threads!
\r
39 // metric_thread (diameter=8, pitch=1, length=4);
\r
42 // metric_thread (diameter=8, pitch=1, length=4, square=true);
\r
44 // Non-standard: long pitch, same thread size.
\r
45 //metric_thread (diameter=8, pitch=4, length=4, thread_size=1, groove=true);
\r
47 // Non-standard: 20 mm diameter, long pitch, square "trough" width 3 mm,
\r
49 //metric_thread (diameter=20, pitch=8, length=16, square=true, thread_size=6,
\r
50 // groove=true, rectangle=0.333);
\r
52 // English: 1/4 x 20.
\r
53 //english_thread (diameter=1/4, threads_per_inch=20, length=1);
\r
55 // Tapered. Example -- pipe size 3/4" -- per:
\r
56 // http://www.engineeringtoolbox.com/npt-national-pipe-taper-threads-d_750.html
\r
57 // english_thread (diameter=1.05, threads_per_inch=14, length=3/4, taper=1/16);
\r
59 // Thread for mounting on Rohloff hub.
\r
61 // cylinder (r=20, h=10, $fn=100);
\r
63 // metric_thread (diameter=34, pitch=1, length=10, internal=true, n_starts=6);
\r
67 // ----------------------------------------------------------------------------
\r
68 function segments (diameter) = min (50, max (ceil (diameter*6), 25));
\r
71 // ----------------------------------------------------------------------------
\r
72 // diameter - outside diameter of threads in mm. Default: 8.
\r
73 // pitch - thread axial "travel" per turn in mm. Default: 1.
\r
74 // length - overall axial length of thread in mm. Default: 1.
\r
75 // internal - true = clearances for internal thread (e.g., a nut).
\r
76 // false = clearances for external thread (e.g., a bolt).
\r
77 // (Internal threads should be "cut out" from a solid using
\r
78 // difference ()). Default: false.
\r
79 // n_starts - Number of thread starts (e.g., DNA, a "double helix," has
\r
80 // n_starts=2). See wikipedia Screw_thread. Default: 1.
\r
81 // thread_size - (non-standard) axial width of a single thread "V" - independent
\r
82 // of pitch. Default: same as pitch.
\r
83 // groove - (non-standard) true = subtract inverted "V" from cylinder
\r
84 // (rather thanadd protruding "V" to cylinder). Default: false.
\r
85 // square - true = square threads (per
\r
86 // https://en.wikipedia.org/wiki/Square_thread_form). Default:
\r
88 // rectangle - (non-standard) "Rectangular" thread - ratio depth/(axial) width
\r
89 // Default: 0 (standard "v" thread).
\r
90 // angle - (non-standard) angle (deg) of thread side from perpendicular to
\r
91 // axis (default = standard = 30 degrees).
\r
92 // taper - diameter change per length (National Pipe Thread/ANSI B1.20.1
\r
93 // is 1" diameter per 16" length). Taper decreases from 'diameter'
\r
94 // as z increases. Default: 0 (no taper).
\r
95 // leadin - 0 (default): no chamfer; 1: chamfer (45 degree) at max-z end;
\r
96 // 2: chamfer at both ends, 3: chamfer at z=0 end.
\r
97 // leadfac - scale of leadin chamfer length (default: 1.0 = 1/2 thread).
\r
98 // test - true = do not render threads (just draw "blank" cylinder).
\r
99 // Default: false (draw threads).
\r
100 module metric_thread (diameter=8, pitch=1, length=1, internal=false, n_starts=1,
\r
101 thread_size=-1, groove=false, square=false, rectangle=0,
\r
102 angle=30, taper=0, leadin=0, leadfac=1.0, test=false)
\r
104 // thread_size: size of thread "V" different than travel per turn (pitch).
\r
105 // Default: same as pitch.
\r
106 local_thread_size = thread_size == -1 ? pitch : thread_size;
\r
107 local_rectangle = rectangle ? rectangle : 1;
\r
109 n_segments = segments (diameter);
\r
110 h = (test && ! internal) ? 0 : (square || rectangle) ? local_thread_size*local_rectangle/2 : local_thread_size / (2 * tan(angle));
\r
112 h_fac1 = (square || rectangle) ? 0.90 : 0.625;
\r
114 // External thread includes additional relief.
\r
115 h_fac2 = (square || rectangle) ? 0.95 : 5.3/8;
\r
117 tapered_diameter = diameter - length*taper;
\r
123 metric_thread_turns (diameter, pitch, length, internal, n_starts,
\r
124 local_thread_size, groove, square, rectangle, angle,
\r
131 // Solid center, including Dmin truncation.
\r
133 cylinder (r1=diameter/2, r2=tapered_diameter/2,
\r
134 h=length, $fn=n_segments);
\r
135 } else if (internal) {
\r
136 cylinder (r1=diameter/2 - h*h_fac1, r2=tapered_diameter/2 - h*h_fac1,
\r
137 h=length, $fn=n_segments);
\r
140 // External thread.
\r
141 cylinder (r1=diameter/2 - h*h_fac2, r2=tapered_diameter/2 - h*h_fac2,
\r
142 h=length, $fn=n_segments);
\r
147 metric_thread_turns (diameter, pitch, length, internal, n_starts,
\r
148 local_thread_size, groove, square, rectangle,
\r
154 // Internal thread lead-in: take away from external solid.
\r
157 // "Negative chamfer" z=0 end if leadin is 2 or 3.
\r
158 if (leadin == 2 || leadin == 3) {
\r
159 cylinder (r1=diameter/2, r2=diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,
\r
163 // "Negative chamfer" z-max end if leadin is 1 or 2.
\r
164 if (leadin == 1 || leadin == 2) {
\r
165 translate ([0, 0, length + 0.05 - h*h_fac1*leadfac]) {
\r
166 cylinder (r1=tapered_diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,
\r
167 r2=tapered_diameter/2,
\r
176 // Chamfer z=0 end if leadin is 2 or 3.
\r
177 if (leadin == 2 || leadin == 3) {
\r
179 cylinder (r=diameter/2 + 1, h=h*h_fac1*leadfac, $fn=n_segments);
\r
181 cylinder (r2=diameter/2, r1=diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,
\r
186 // Chamfer z-max end if leadin is 1 or 2.
\r
187 if (leadin == 1 || leadin == 2) {
\r
188 translate ([0, 0, length + 0.05 - h*h_fac1*leadfac]) {
\r
190 cylinder (r=diameter/2 + 1, h=h*h_fac1*leadfac, $fn=n_segments);
\r
192 cylinder (r1=tapered_diameter/2, r2=tapered_diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,
\r
202 // ----------------------------------------------------------------------------
\r
203 // Input units in inches.
\r
204 // Note: units of measure in drawing are mm!
\r
205 module english_thread (diameter=0.25, threads_per_inch=20, length=1,
\r
206 internal=false, n_starts=1, thread_size=-1, groove=false,
\r
207 square=false, rectangle=0, angle=30, taper=0, leadin=0,
\r
208 leadfac=1.0, test=false)
\r
211 mm_diameter = diameter*25.4;
\r
212 mm_pitch = (1.0/threads_per_inch)*25.4;
\r
213 mm_length = length*25.4;
\r
215 echo (str ("mm_diameter: ", mm_diameter));
\r
216 echo (str ("mm_pitch: ", mm_pitch));
\r
217 echo (str ("mm_length: ", mm_length));
\r
218 metric_thread (mm_diameter, mm_pitch, mm_length, internal, n_starts,
\r
219 thread_size, groove, square, rectangle, angle, taper, leadin,
\r
223 // ----------------------------------------------------------------------------
\r
224 module metric_thread_turns (diameter, pitch, length, internal, n_starts,
\r
225 thread_size, groove, square, rectangle, angle,
\r
228 // Number of turns needed.
\r
229 n_turns = floor (length/pitch);
\r
233 // Start one below z = 0. Gives an extra turn at each end.
\r
234 for (i=[-1*n_starts : n_turns+1]) {
\r
235 translate ([0, 0, i*pitch]) {
\r
236 metric_thread_turn (diameter, pitch, internal, n_starts,
\r
237 thread_size, groove, square, rectangle, angle,
\r
243 translate ([0, 0, length/2]) {
\r
244 cube ([diameter*3, diameter*3, length], center=true);
\r
250 // ----------------------------------------------------------------------------
\r
251 module metric_thread_turn (diameter, pitch, internal, n_starts, thread_size,
\r
252 groove, square, rectangle, angle, taper, z)
\r
254 n_segments = segments (diameter);
\r
255 fraction_circle = 1.0/n_segments;
\r
256 for (i=[0 : n_segments-1]) {
\r
257 rotate ([0, 0, i*360*fraction_circle]) {
\r
258 translate ([0, 0, i*n_starts*pitch*fraction_circle]) {
\r
259 //current_diameter = diameter - taper*(z + i*n_starts*pitch*fraction_circle);
\r
260 thread_polyhedron ((diameter - taper*(z + i*n_starts*pitch*fraction_circle))/2,
\r
261 pitch, internal, n_starts, thread_size, groove,
\r
262 square, rectangle, angle);
\r
269 // ----------------------------------------------------------------------------
\r
270 module thread_polyhedron (radius, pitch, internal, n_starts, thread_size,
\r
271 groove, square, rectangle, angle)
\r
273 n_segments = segments (radius*2);
\r
274 fraction_circle = 1.0/n_segments;
\r
276 local_rectangle = rectangle ? rectangle : 1;
\r
278 h = (square || rectangle) ? thread_size*local_rectangle/2 : thread_size / (2 * tan(angle));
\r
279 outer_r = radius + (internal ? h/20 : 0); // Adds internal relief.
\r
280 //echo (str ("outer_r: ", outer_r));
\r
282 // A little extra on square thread -- make sure overlaps cylinder.
\r
283 h_fac1 = (square || rectangle) ? 1.1 : 0.875;
\r
284 inner_r = radius - h*h_fac1; // Does NOT do Dmin_truncation - do later with
\r
287 translate_y = groove ? outer_r + inner_r : 0;
\r
288 reflect_x = groove ? 1 : 0;
\r
290 // Make these just slightly bigger (keep in proportion) so polyhedra will
\r
292 x_incr_outer = (! groove ? outer_r : inner_r) * fraction_circle * 2 * PI * 1.02;
\r
293 x_incr_inner = (! groove ? inner_r : outer_r) * fraction_circle * 2 * PI * 1.02;
\r
294 z_incr = n_starts * pitch * fraction_circle * 1.005;
\r
297 (angles x0 and x3 inner are actually 60 deg)
\r
299 /\ (x2_inner, z2_inner) [2]
\r
301 (x3_inner, z3_inner) / \
\r
303 |\ \ (x2_outer, z2_outer) [6]
\r
306 z |[7]\/ / (x1_outer, z1_outer) [5]
\r
309 | / | / (x0_outer, z0_outer) [4]
\r
310 | / | / (behind: (x1_inner, z1_inner) [1]
\r
313 (r) / (x0_inner, z0_inner) [0]
\r
317 x1_outer = outer_r * fraction_circle * 2 * PI;
\r
319 z0_outer = (outer_r - inner_r) * tan(angle);
\r
320 //echo (str ("z0_outer: ", z0_outer));
\r
322 //polygon ([[inner_r, 0], [outer_r, z0_outer],
\r
323 // [outer_r, 0.5*pitch], [inner_r, 0.5*pitch]]);
\r
324 z1_outer = z0_outer + z_incr;
\r
326 // Give internal square threads some clearance in the z direction, too.
\r
327 bottom = internal ? 0.235 : 0.25;
\r
328 top = internal ? 0.765 : 0.75;
\r
330 translate ([0, translate_y, 0]) {
\r
331 mirror ([reflect_x, 0, 0]) {
\r
333 if (square || rectangle) {
\r
335 // Rule for face ordering: look at polyhedron from outside: points must
\r
336 // be in clockwise order.
\r
339 [-x_incr_inner/2, -inner_r, bottom*thread_size], // [0]
\r
340 [x_incr_inner/2, -inner_r, bottom*thread_size + z_incr], // [1]
\r
341 [x_incr_inner/2, -inner_r, top*thread_size + z_incr], // [2]
\r
342 [-x_incr_inner/2, -inner_r, top*thread_size], // [3]
\r
344 [-x_incr_outer/2, -outer_r, bottom*thread_size], // [4]
\r
345 [x_incr_outer/2, -outer_r, bottom*thread_size + z_incr], // [5]
\r
346 [x_incr_outer/2, -outer_r, top*thread_size + z_incr], // [6]
\r
347 [-x_incr_outer/2, -outer_r, top*thread_size] // [7]
\r
351 [0, 3, 7, 4], // This-side trapezoid
\r
353 [1, 5, 6, 2], // Back-side trapezoid
\r
355 [0, 1, 2, 3], // Inner rectangle
\r
357 [4, 7, 6, 5], // Outer rectangle
\r
359 // These are not planar, so do with separate triangles.
\r
360 [7, 2, 6], // Upper rectangle, bottom
\r
361 [7, 3, 2], // Upper rectangle, top
\r
363 [0, 5, 1], // Lower rectangle, bottom
\r
364 [0, 4, 5] // Lower rectangle, top
\r
369 // Rule for face ordering: look at polyhedron from outside: points must
\r
370 // be in clockwise order.
\r
373 [-x_incr_inner/2, -inner_r, 0], // [0]
\r
374 [x_incr_inner/2, -inner_r, z_incr], // [1]
\r
375 [x_incr_inner/2, -inner_r, thread_size + z_incr], // [2]
\r
376 [-x_incr_inner/2, -inner_r, thread_size], // [3]
\r
378 [-x_incr_outer/2, -outer_r, z0_outer], // [4]
\r
379 [x_incr_outer/2, -outer_r, z0_outer + z_incr], // [5]
\r
380 [x_incr_outer/2, -outer_r, thread_size - z0_outer + z_incr], // [6]
\r
381 [-x_incr_outer/2, -outer_r, thread_size - z0_outer] // [7]
\r
385 [0, 3, 7, 4], // This-side trapezoid
\r
387 [1, 5, 6, 2], // Back-side trapezoid
\r
389 [0, 1, 2, 3], // Inner rectangle
\r
391 [4, 7, 6, 5], // Outer rectangle
\r
393 // These are not planar, so do with separate triangles.
\r
394 [7, 2, 6], // Upper rectangle, bottom
\r
395 [7, 3, 2], // Upper rectangle, top
\r
397 [0, 5, 1], // Lower rectangle, bottom
\r
398 [0, 4, 5] // Lower rectangle, top
\r