threads.scad 15 KB

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