threads.scad 18 KB

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