summaryrefslogtreecommitdiff
authorMichael Krelin <hacker@klever.net>2016-07-15 12:42:08 (UTC)
committer Michael Krelin <hacker@klever.net>2016-07-15 12:42:08 (UTC)
commit7fdbf9d3267810cd4a92fd02bf899ae739bd2a2f (patch) (unidiff)
treefff421daa55ce82e109be64dc595e8df6c8fc3b3
parent586fcd2f3b7147bf7d8813f84cc2c0cddeb3bb3c (diff)
downloadcarriagery-7fdbf9d3267810cd4a92fd02bf899ae739bd2a2f.zip
carriagery-7fdbf9d3267810cd4a92fd02bf899ae739bd2a2f.tar.gz
carriagery-7fdbf9d3267810cd4a92fd02bf899ae739bd2a2f.tar.bz2
Threads library
as seen at http://dkprojects.net/openscad-threads/
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--threads.scad332
1 files changed, 332 insertions, 0 deletions
diff --git a/threads.scad b/threads.scad
new file mode 100644
index 0000000..8dd0b7b
--- a/dev/null
+++ b/threads.scad
@@ -0,0 +1,332 @@
1/*
2 * ISO-standard metric threads, following this specification:
3 * http://en.wikipedia.org/wiki/ISO_metric_screw_thread
4 *
5 * Dan Kirshner - dan_kirshner@yahoo.com
6 *
7 * You are welcome to make free use of this software. Retention of my
8 * authorship credit would be appreciated.
9 *
10 * Version 1.9. 2016-07-03 Option: tapered.
11 * Version 1.8. 2016-01-08 Option: (non-standard) angle.
12 * Version 1.7. 2015-11-28 Larger x-increment - for small-diameters.
13 * Version 1.6. 2015-09-01 Options: square threads, rectangular threads.
14 * Version 1.5. 2015-06-12 Options: thread_size, groove.
15 * Version 1.4. 2014-10-17 Use "faces" instead of "triangles" for polyhedron
16 * Version 1.3. 2013-12-01 Correct loop over turns -- don't have early cut-off
17 * Version 1.2. 2012-09-09 Use discrete polyhedra rather than linear_extrude ()
18 * Version 1.1. 2012-09-07 Corrected to right-hand threads!
19 */
20
21// Examples.
22//
23// Standard M8 x 1.
24// metric_thread (diameter=8, pitch=1, length=4);
25
26// Square thread.
27// metric_thread (diameter=8, pitch=1, length=4, square=true);
28
29// Non-standard: long pitch, same thread size.
30//metric_thread (diameter=8, pitch=4, length=4, thread_size=1, groove=true);
31
32// Non-standard: 20 mm diameter, long pitch, square "trough" width 3 mm,
33// depth 1 mm.
34//metric_thread (diameter=20, pitch=8, length=16, square=true, thread_size=6,
35// groove=true, rectangle=0.333);
36
37// English: 1/4 x 20.
38//english_thread (diameter=1/4, threads_per_inch=20, length=1);
39
40// Tapered. Example -- pipe size 3/4" -- per:
41// http://www.engineeringtoolbox.com/npt-national-pipe-taper-threads-d_750.html
42// english_thread (diameter=1.05, threads_per_inch=14, length=3/4, taper=1/16);
43
44// Thread for mounting on Rohloff hub.
45//difference () {
46// cylinder (r=20, h=10, $fn=100);
47//
48// metric_thread (diameter=34, pitch=1, length=10, internal=true, n_starts=6);
49//}
50
51
52// ----------------------------------------------------------------------------
53function segments (diameter) = min (50, ceil (diameter*6));
54
55
56// ----------------------------------------------------------------------------
57// internal - true = clearances for internal thread (e.g., a nut).
58// false = clearances for external thread (e.g., a bolt).
59// (Internal threads should be "cut out" from a solid using
60// difference ()).
61// n_starts - Number of thread starts (e.g., DNA, a "double helix," has
62// n_starts=2). See wikipedia Screw_thread.
63// thread_size - (non-standard) size of a single thread "V" - independent of
64// pitch. Default: same as pitch.
65// groove - (non-standard) subtract inverted "V" from cylinder (rather than
66// add protruding "V" to cylinder).
67// square - Square threads (per
68// https://en.wikipedia.org/wiki/Square_thread_form).
69// rectangle - (non-standard) "Rectangular" thread - ratio depth/width
70// Default: 1 (square).
71// angle - (non-standard) angle (deg) of thread side from perpendicular to
72// axis (default = standard = 30 degrees).
73// taper - diameter change per length (National Pipe Thread/ANSI B1.20.1
74// is 1" diameter per 16" length).
75module metric_thread (diameter=8, pitch=1, length=1, internal=false, n_starts=1,
76 thread_size=-1, groove=false, square=false, rectangle=0,
77 angle=30, taper=0)
78{
79 // thread_size: size of thread "V" different than travel per turn (pitch).
80 // Default: same as pitch.
81 local_thread_size = thread_size == -1 ? pitch : thread_size;
82 local_rectangle = rectangle ? rectangle : 1;
83
84 n_segments = segments (diameter);
85 h = (square || rectangle) ? local_thread_size*local_rectangle/2 : local_thread_size * cos (angle);
86
87 h_fac1 = (square || rectangle) ? 0.90 : 0.625;
88
89 // External thread includes additional relief.
90 h_fac2 = (square || rectangle) ? 0.95 : 5.3/8;
91
92 if (! groove) {
93 metric_thread_turns (diameter, pitch, length, internal, n_starts,
94 local_thread_size, groove, square, rectangle, angle,
95 taper);
96 }
97
98 difference () {
99
100 // Solid center, including Dmin truncation.
101 tapered_diameter = diameter - length*taper;
102 if (groove) {
103 cylinder (r1=diameter/2, r2=tapered_diameter/2,
104 h=length, $fn=n_segments);
105 } else if (internal) {
106 cylinder (r1=diameter/2 - h*h_fac1, r2=tapered_diameter/2 - h*h_fac1,
107 h=length, $fn=n_segments);
108 } else {
109
110 // External thread.
111 cylinder (r1=diameter/2 - h*h_fac2, r2=tapered_diameter/2 - h*h_fac2,
112 h=length, $fn=n_segments);
113 }
114
115 if (groove) {
116 metric_thread_turns (diameter, pitch, length, internal, n_starts,
117 local_thread_size, groove, square, rectangle,
118 angle, taper);
119 }
120 }
121}
122
123
124// ----------------------------------------------------------------------------
125// Input units in inches.
126// Note: units of measure in drawing are mm!
127module english_thread (diameter=0.25, threads_per_inch=20, length=1,
128 internal=false, n_starts=1, thread_size=-1, groove=false,
129 square=false, rectangle=0, angle=30, taper=0)
130{
131 // Convert to mm.
132 mm_diameter = diameter*25.4;
133 mm_pitch = (1.0/threads_per_inch)*25.4;
134 mm_length = length*25.4;
135
136 echo (str ("mm_diameter: ", mm_diameter));
137 echo (str ("mm_pitch: ", mm_pitch));
138 echo (str ("mm_length: ", mm_length));
139 metric_thread (mm_diameter, mm_pitch, mm_length, internal, n_starts,
140 thread_size, groove, square, rectangle, angle, taper);
141}
142
143// ----------------------------------------------------------------------------
144module metric_thread_turns (diameter, pitch, length, internal, n_starts,
145 thread_size, groove, square, rectangle, angle,
146 taper)
147{
148 // Number of turns needed.
149 n_turns = floor (length/pitch);
150
151 intersection () {
152
153 // Start one below z = 0. Gives an extra turn at each end.
154 for (i=[-1*n_starts : n_turns+1]) {
155 translate ([0, 0, i*pitch]) {
156 metric_thread_turn (diameter, pitch, internal, n_starts,
157 thread_size, groove, square, rectangle, angle,
158 taper, i*pitch);
159 }
160 }
161
162 // Cut to length.
163 translate ([0, 0, length/2]) {
164 cube ([diameter*3, diameter*3, length], center=true);
165 }
166 }
167}
168
169
170// ----------------------------------------------------------------------------
171module metric_thread_turn (diameter, pitch, internal, n_starts, thread_size,
172 groove, square, rectangle, angle, taper, z)
173{
174 n_segments = segments (diameter);
175 fraction_circle = 1.0/n_segments;
176 for (i=[0 : n_segments-1]) {
177 rotate ([0, 0, i*360*fraction_circle]) {
178 translate ([0, 0, i*n_starts*pitch*fraction_circle]) {
179 current_diameter = diameter - taper*(z + i*n_starts*pitch*fraction_circle);
180 thread_polyhedron (current_diameter/2, pitch, internal, n_starts,
181 thread_size, groove, square, rectangle, angle);
182 }
183 }
184 }
185}
186
187
188// ----------------------------------------------------------------------------
189// z (see diagram) as function of current radius.
190// (Only good for first half-pitch.)
191function z_fct (current_radius, radius, pitch, angle)
192 = 0.5* (current_radius - (radius - 0.875*pitch*cos (angle)))
193 /cos (angle);
194
195// ----------------------------------------------------------------------------
196module thread_polyhedron (radius, pitch, internal, n_starts, thread_size,
197 groove, square, rectangle, angle)
198{
199 n_segments = segments (radius*2);
200 fraction_circle = 1.0/n_segments;
201
202 local_rectangle = rectangle ? rectangle : 1;
203
204 h = (square || rectangle) ? thread_size*local_rectangle/2 : thread_size * cos (angle);
205 outer_r = radius + (internal ? h/20 : 0); // Adds internal relief.
206 //echo (str ("outer_r: ", outer_r));
207
208 // A little extra on square thread -- make sure overlaps cylinder.
209 h_fac1 = (square || rectangle) ? 1.1 : 0.875;
210 inner_r = radius - h*h_fac1; // Does NOT do Dmin_truncation - do later with
211 // cylinder.
212
213 translate_y = groove ? outer_r + inner_r : 0;
214 reflect_x = groove ? 1 : 0;
215
216 // Make these just slightly bigger (keep in proportion) so polyhedra will
217 // overlap.
218 x_incr_outer = (! groove ? outer_r : inner_r) * fraction_circle * 2 * PI * 1.02;
219 x_incr_inner = (! groove ? inner_r : outer_r) * fraction_circle * 2 * PI * 1.02;
220 z_incr = n_starts * pitch * fraction_circle * 1.005;
221
222 /*
223 (angles x0 and x3 inner are actually 60 deg)
224
225 /\ (x2_inner, z2_inner) [2]
226 / \
227 (x3_inner, z3_inner) / \
228 [3] \ \
229 |\ \ (x2_outer, z2_outer) [6]
230 | \ /
231 | \ /|
232 z |[7]\/ / (x1_outer, z1_outer) [5]
233 | | | /
234 | x | |/
235 | / | / (x0_outer, z0_outer) [4]
236 | / | / (behind: (x1_inner, z1_inner) [1]
237 |/ | /
238 y________| |/
239 (r) / (x0_inner, z0_inner) [0]
240
241 */
242
243 x1_outer = outer_r * fraction_circle * 2 * PI;
244
245 z0_outer = z_fct (outer_r, radius, thread_size, angle);
246 //echo (str ("z0_outer: ", z0_outer));
247
248 //polygon ([[inner_r, 0], [outer_r, z0_outer],
249 // [outer_r, 0.5*pitch], [inner_r, 0.5*pitch]]);
250 z1_outer = z0_outer + z_incr;
251
252 // Give internal square threads some clearance in the z direction, too.
253 bottom = internal ? 0.235 : 0.25;
254 top = internal ? 0.765 : 0.75;
255
256 translate ([0, translate_y, 0]) {
257 mirror ([reflect_x, 0, 0]) {
258
259 if (square || rectangle) {
260
261 // Rule for face ordering: look at polyhedron from outside: points must
262 // be in clockwise order.
263 polyhedron (
264 points = [
265 [-x_incr_inner/2, -inner_r, bottom*thread_size], // [0]
266 [x_incr_inner/2, -inner_r, bottom*thread_size + z_incr], // [1]
267 [x_incr_inner/2, -inner_r, top*thread_size + z_incr], // [2]
268 [-x_incr_inner/2, -inner_r, top*thread_size], // [3]
269
270 [-x_incr_outer/2, -outer_r, bottom*thread_size], // [4]
271 [x_incr_outer/2, -outer_r, bottom*thread_size + z_incr], // [5]
272 [x_incr_outer/2, -outer_r, top*thread_size + z_incr], // [6]
273 [-x_incr_outer/2, -outer_r, top*thread_size] // [7]
274 ],
275
276 faces = [
277 [0, 3, 7, 4], // This-side trapezoid
278
279 [1, 5, 6, 2], // Back-side trapezoid
280
281 [0, 1, 2, 3], // Inner rectangle
282
283 [4, 7, 6, 5], // Outer rectangle
284
285 // These are not planar, so do with separate triangles.
286 [7, 2, 6], // Upper rectangle, bottom
287 [7, 3, 2], // Upper rectangle, top
288
289 [0, 5, 1], // Lower rectangle, bottom
290 [0, 4, 5] // Lower rectangle, top
291 ]
292 );
293 } else {
294
295 // Rule for face ordering: look at polyhedron from outside: points must
296 // be in clockwise order.
297 polyhedron (
298 points = [
299 [-x_incr_inner/2, -inner_r, 0], // [0]
300 [x_incr_inner/2, -inner_r, z_incr], // [1]
301 [x_incr_inner/2, -inner_r, thread_size + z_incr], // [2]
302 [-x_incr_inner/2, -inner_r, thread_size], // [3]
303
304 [-x_incr_outer/2, -outer_r, z0_outer], // [4]
305 [x_incr_outer/2, -outer_r, z0_outer + z_incr], // [5]
306 [x_incr_outer/2, -outer_r, thread_size - z0_outer + z_incr], // [6]
307 [-x_incr_outer/2, -outer_r, thread_size - z0_outer] // [7]
308 ],
309
310 faces = [
311 [0, 3, 7, 4], // This-side trapezoid
312
313 [1, 5, 6, 2], // Back-side trapezoid
314
315 [0, 1, 2, 3], // Inner rectangle
316
317 [4, 7, 6, 5], // Outer rectangle
318
319 // These are not planar, so do with separate triangles.
320 [7, 2, 6], // Upper rectangle, bottom
321 [7, 3, 2], // Upper rectangle, top
322
323 [0, 5, 1], // Lower rectangle, bottom
324 [0, 4, 5] // Lower rectangle, top
325 ]
326 );
327 }
328 }
329 }
330}
331
332