Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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