scad.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #pragma once
  2. // AUTOGENERATED FILE - DO NOT EDIT
  3. const char* threads_SCAD = "function segments (diameter) = min (50, max (ceil (diameter*6), 25));\n"
  4. "module metric_thread (diameter=8, pitch=1, length=1, internal=false, n_starts=1,\n"
  5. "thread_size=-1, groove=false, square=false, rectangle=0,\n"
  6. "angle=30, taper=0, leadin=0, leadfac=1.0, test=false)\n"
  7. "{\n"
  8. "local_thread_size = thread_size == -1 ? pitch : thread_size;\n"
  9. "local_rectangle = rectangle ? rectangle : 1;\n"
  10. "n_segments = segments (diameter);\n"
  11. "h = (test && ! internal) ? 0 : (square || rectangle) ? local_thread_size*local_rectangle/2 : local_thread_size / (2 * tan(angle));\n"
  12. "h_fac1 = (square || rectangle) ? 0.90 : 0.625;\n"
  13. "h_fac2 = (square || rectangle) ? 0.95 : 5.3/8;\n"
  14. "tapered_diameter = diameter - length*taper;\n"
  15. "difference () {\n"
  16. "union () {\n"
  17. "if (! groove) {\n"
  18. "if (! test) {\n"
  19. "metric_thread_turns (diameter, pitch, length, internal, n_starts,\n"
  20. "local_thread_size, groove, square, rectangle, angle,\n"
  21. "taper);\n"
  22. "}\n"
  23. "}\n"
  24. "difference () {\n"
  25. "if (groove) {\n"
  26. "cylinder (r1=diameter/2, r2=tapered_diameter/2,\n"
  27. "h=length, $fn=n_segments);\n"
  28. "} else if (internal) {\n"
  29. "cylinder (r1=diameter/2 - h*h_fac1, r2=tapered_diameter/2 - h*h_fac1,\n"
  30. "h=length, $fn=n_segments);\n"
  31. "} else {\n"
  32. "cylinder (r1=diameter/2 - h*h_fac2, r2=tapered_diameter/2 - h*h_fac2,\n"
  33. "h=length, $fn=n_segments);\n"
  34. "}\n"
  35. "if (groove) {\n"
  36. "if (! test) {\n"
  37. "metric_thread_turns (diameter, pitch, length, internal, n_starts,\n"
  38. "local_thread_size, groove, square, rectangle,\n"
  39. "angle, taper);\n"
  40. "}\n"
  41. "}\n"
  42. "}\n"
  43. "if (internal) {\n"
  44. "if (leadin == 2 || leadin == 3) {\n"
  45. "cylinder (r1=diameter/2, r2=diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,\n"
  46. "$fn=n_segments);\n"
  47. "}\n"
  48. "if (leadin == 1 || leadin == 2) {\n"
  49. "translate ([0, 0, length + 0.05 - h*h_fac1*leadfac]) {\n"
  50. "cylinder (r1=tapered_diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,\n"
  51. "r2=tapered_diameter/2,\n"
  52. "$fn=n_segments);\n"
  53. "}\n"
  54. "}\n"
  55. "}\n"
  56. "}\n"
  57. "if (! internal) {\n"
  58. "if (leadin == 2 || leadin == 3) {\n"
  59. "difference () {\n"
  60. "cylinder (r=diameter/2 + 1, h=h*h_fac1*leadfac, $fn=n_segments);\n"
  61. "cylinder (r2=diameter/2, r1=diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,\n"
  62. "$fn=n_segments);\n"
  63. "}\n"
  64. "}\n"
  65. "if (leadin == 1 || leadin == 2) {\n"
  66. "translate ([0, 0, length + 0.05 - h*h_fac1*leadfac]) {\n"
  67. "difference () {\n"
  68. "cylinder (r=diameter/2 + 1, h=h*h_fac1*leadfac, $fn=n_segments);\n"
  69. "cylinder (r1=tapered_diameter/2, r2=tapered_diameter/2 - h*h_fac1*leadfac, h=h*h_fac1*leadfac,\n"
  70. "$fn=n_segments);\n"
  71. "}\n"
  72. "}\n"
  73. "}\n"
  74. "}\n"
  75. "}\n"
  76. "}\n"
  77. "module english_thread (diameter=0.25, threads_per_inch=20, length=1,\n"
  78. "internal=false, n_starts=1, thread_size=-1, groove=false,\n"
  79. "square=false, rectangle=0, angle=30, taper=0, leadin=0,\n"
  80. "leadfac=1.0, test=false)\n"
  81. "{\n"
  82. "mm_diameter = diameter*25.4;\n"
  83. "mm_pitch = (1.0/threads_per_inch)*25.4;\n"
  84. "mm_length = length*25.4;\n"
  85. "echo (str (\"mm_diameter: \", mm_diameter));\n"
  86. "echo (str (\"mm_pitch: \", mm_pitch));\n"
  87. "echo (str (\"mm_length: \", mm_length));\n"
  88. "metric_thread (mm_diameter, mm_pitch, mm_length, internal, n_starts,\n"
  89. "thread_size, groove, square, rectangle, angle, taper, leadin,\n"
  90. "leadfac, test);\n"
  91. "}\n"
  92. "module metric_thread_turns (diameter, pitch, length, internal, n_starts,\n"
  93. "thread_size, groove, square, rectangle, angle,\n"
  94. "taper)\n"
  95. "{\n"
  96. "n_turns = floor (length/pitch);\n"
  97. "intersection () {\n"
  98. "for (i=[-1*n_starts : n_turns+1]) {\n"
  99. "translate ([0, 0, i*pitch]) {\n"
  100. "metric_thread_turn (diameter, pitch, internal, n_starts,\n"
  101. "thread_size, groove, square, rectangle, angle,\n"
  102. "taper, i*pitch);\n"
  103. "}\n"
  104. "}\n"
  105. "translate ([0, 0, length/2]) {\n"
  106. "cube ([diameter*3, diameter*3, length], center=true);\n"
  107. "}\n"
  108. "}\n"
  109. "}\n"
  110. "module metric_thread_turn (diameter, pitch, internal, n_starts, thread_size,\n"
  111. "groove, square, rectangle, angle, taper, z)\n"
  112. "{\n"
  113. "n_segments = segments (diameter);\n"
  114. "fraction_circle = 1.0/n_segments;\n"
  115. "for (i=[0 : n_segments-1]) {\n"
  116. "rotate ([0, 0, i*360*fraction_circle]) {\n"
  117. "translate ([0, 0, i*n_starts*pitch*fraction_circle]) {\n"
  118. "thread_polyhedron ((diameter - taper*(z + i*n_starts*pitch*fraction_circle))/2,\n"
  119. "pitch, internal, n_starts, thread_size, groove,\n"
  120. "square, rectangle, angle);\n"
  121. "}\n"
  122. "}\n"
  123. "}\n"
  124. "}\n"
  125. "module thread_polyhedron (radius, pitch, internal, n_starts, thread_size,\n"
  126. "groove, square, rectangle, angle)\n"
  127. "{\n"
  128. "n_segments = segments (radius*2);\n"
  129. "fraction_circle = 1.0/n_segments;\n"
  130. "local_rectangle = rectangle ? rectangle : 1;\n"
  131. "h = (square || rectangle) ? thread_size*local_rectangle/2 : thread_size / (2 * tan(angle));\n"
  132. "outer_r = radius + (internal ? h/20 : 0); // Adds internal relief.\n"
  133. "h_fac1 = (square || rectangle) ? 1.1 : 0.875;\n"
  134. "inner_r = radius - h*h_fac1; // Does NOT do Dmin_truncation - do later with\n"
  135. "translate_y = groove ? outer_r + inner_r : 0;\n"
  136. "reflect_x = groove ? 1 : 0;\n"
  137. "x_incr_outer = (! groove ? outer_r : inner_r) * fraction_circle * 2 * PI * 1.02;\n"
  138. "x_incr_inner = (! groove ? inner_r : outer_r) * fraction_circle * 2 * PI * 1.02;\n"
  139. "z_incr = n_starts * pitch * fraction_circle * 1.005;\n"
  140. "x1_outer = outer_r * fraction_circle * 2 * PI;\n"
  141. "z0_outer = (outer_r - inner_r) * tan(angle);\n"
  142. "z1_outer = z0_outer + z_incr;\n"
  143. "bottom = internal ? 0.235 : 0.25;\n"
  144. "top = internal ? 0.765 : 0.75;\n"
  145. "translate ([0, translate_y, 0]) {\n"
  146. "mirror ([reflect_x, 0, 0]) {\n"
  147. "if (square || rectangle) {\n"
  148. "polyhedron (\n"
  149. "points = [\n"
  150. "[-x_incr_inner/2, -inner_r, bottom*thread_size], // [0]\n"
  151. "[x_incr_inner/2, -inner_r, bottom*thread_size + z_incr], // [1]\n"
  152. "[x_incr_inner/2, -inner_r, top*thread_size + z_incr], // [2]\n"
  153. "[-x_incr_inner/2, -inner_r, top*thread_size], // [3]\n"
  154. "[-x_incr_outer/2, -outer_r, bottom*thread_size], // [4]\n"
  155. "[x_incr_outer/2, -outer_r, bottom*thread_size + z_incr], // [5]\n"
  156. "[x_incr_outer/2, -outer_r, top*thread_size + z_incr], // [6]\n"
  157. "[-x_incr_outer/2, -outer_r, top*thread_size] // [7]\n"
  158. "],\n"
  159. "faces = [\n"
  160. "[0, 3, 7, 4], // This-side trapezoid\n"
  161. "[1, 5, 6, 2], // Back-side trapezoid\n"
  162. "[0, 1, 2, 3], // Inner rectangle\n"
  163. "[4, 7, 6, 5], // Outer rectangle\n"
  164. "[7, 2, 6], // Upper rectangle, bottom\n"
  165. "[7, 3, 2], // Upper rectangle, top\n"
  166. "[0, 5, 1], // Lower rectangle, bottom\n"
  167. "[0, 4, 5] // Lower rectangle, top\n"
  168. "]\n"
  169. ");\n"
  170. "} else {\n"
  171. "polyhedron (\n"
  172. "points = [\n"
  173. "[-x_incr_inner/2, -inner_r, 0], // [0]\n"
  174. "[x_incr_inner/2, -inner_r, z_incr], // [1]\n"
  175. "[x_incr_inner/2, -inner_r, thread_size + z_incr], // [2]\n"
  176. "[-x_incr_inner/2, -inner_r, thread_size], // [3]\n"
  177. "[-x_incr_outer/2, -outer_r, z0_outer], // [4]\n"
  178. "[x_incr_outer/2, -outer_r, z0_outer + z_incr], // [5]\n"
  179. "[x_incr_outer/2, -outer_r, thread_size - z0_outer + z_incr], // [6]\n"
  180. "[-x_incr_outer/2, -outer_r, thread_size - z0_outer] // [7]\n"
  181. "],\n"
  182. "faces = [\n"
  183. "[0, 3, 7, 4], // This-side trapezoid\n"
  184. "[1, 5, 6, 2], // Back-side trapezoid\n"
  185. "[0, 1, 2, 3], // Inner rectangle\n"
  186. "[4, 7, 6, 5], // Outer rectangle\n"
  187. "[7, 2, 6], // Upper rectangle, bottom\n"
  188. "[7, 3, 2], // Upper rectangle, top\n"
  189. "[0, 5, 1], // Lower rectangle, bottom\n"
  190. "[0, 4, 5] // Lower rectangle, top\n"
  191. "]\n"
  192. ");\n"
  193. "}\n"
  194. "}\n"
  195. "}\n"
  196. "}\n"
  197. ;