example020.scad 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. module screw(type = 2, r1 = 15, r2 = 20, n = 7, h = 100, t = 8)
  2. {
  3. linear_extrude(height = h, twist = 360*t/n, convexity = t)
  4. difference() {
  5. circle(r2);
  6. for (i = [0:n-1]) {
  7. if (type == 1) rotate(i*360/n) polygon([
  8. [ 2*r2, 0 ],
  9. [ r2, 0 ],
  10. [ r1*cos(180/n), r1*sin(180/n) ],
  11. [ r2*cos(360/n), r2*sin(360/n) ],
  12. [ 2*r2*cos(360/n), 2*r2*sin(360/n) ],
  13. ]);
  14. if (type == 2) rotate(i*360/n) polygon([
  15. [ 2*r2, 0 ],
  16. [ r2, 0 ],
  17. [ r1*cos(90/n), r1*sin(90/n) ],
  18. [ r1*cos(180/n), r1*sin(180/n) ],
  19. [ r2*cos(270/n), r2*sin(270/n) ],
  20. [ 2*r2*cos(270/n), 2*r2*sin(270/n) ],
  21. ]);
  22. }
  23. }
  24. }
  25. module nut(type = 2, r1 = 16, r2 = 21, r3 = 30, s = 6, n = 7, h = 100/5, t = 8/5)
  26. {
  27. difference() {
  28. cylinder($fn = s, r = r3, h = h);
  29. translate([ 0, 0, -h/2 ]) screw(type, r1, r2, n, h*2, t*2);
  30. }
  31. }
  32. module spring(r1 = 100, r2 = 10, h = 100, hr = 12)
  33. {
  34. stepsize = 1/16;
  35. module segment(i1, i2) {
  36. alpha1 = i1 * 360*r2/hr;
  37. alpha2 = i2 * 360*r2/hr;
  38. len1 = sin(acos(i1*2-1))*r2;
  39. len2 = sin(acos(i2*2-1))*r2;
  40. if (len1 < 0.01)
  41. polygon([
  42. [ cos(alpha1)*r1, sin(alpha1)*r1 ],
  43. [ cos(alpha2)*(r1-len2), sin(alpha2)*(r1-len2) ],
  44. [ cos(alpha2)*(r1+len2), sin(alpha2)*(r1+len2) ]
  45. ]);
  46. if (len2 < 0.01)
  47. polygon([
  48. [ cos(alpha1)*(r1+len1), sin(alpha1)*(r1+len1) ],
  49. [ cos(alpha1)*(r1-len1), sin(alpha1)*(r1-len1) ],
  50. [ cos(alpha2)*r1, sin(alpha2)*r1 ],
  51. ]);
  52. if (len1 >= 0.01 && len2 >= 0.01)
  53. polygon([
  54. [ cos(alpha1)*(r1+len1), sin(alpha1)*(r1+len1) ],
  55. [ cos(alpha1)*(r1-len1), sin(alpha1)*(r1-len1) ],
  56. [ cos(alpha2)*(r1-len2), sin(alpha2)*(r1-len2) ],
  57. [ cos(alpha2)*(r1+len2), sin(alpha2)*(r1+len2) ]
  58. ]);
  59. }
  60. linear_extrude(height = 100, twist = 180*h/hr,
  61. $fn = (hr/r2)/stepsize, convexity = 5) {
  62. for (i = [ stepsize : stepsize : 1+stepsize/2 ])
  63. segment(i-stepsize, min(i, 1));
  64. }
  65. }
  66. translate([ -30, 0, 0 ])
  67. screw();
  68. translate([ 30, 0, 0 ])
  69. nut();
  70. spring();