Advertisement
DaveShook

screw thread v1

Dec 12th, 2012
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1.  
  2. params = [
  3. { "id": "r1", "displayName": "R1", "type": "float", "rangeMin": 0, "rangeMax": 100, "default": 10.0 },
  4. { "id": "r2", "displayName": "R2", "type": "float", "rangeMin": 0, "rangeMax": 100, "default": 10.0 },
  5. { "id": "height", "displayName": "Height", "type": "float", "rangeMin": 1.0, "rangeMax": 100.0, "default": 20.0 }
  6. { "id": "pitch", "displayName": "Pitch", "type": "float", "rangeMin": 1.0, "rangeMax": 100.0, "default": 5.0 }
  7.  
  8.  
  9. ]
  10.  
  11. function process(params) {
  12. var height = params["height"];
  13. var r1 = params["r1"];
  14. var r2 = params["r2"];
  15. /* r1 is the base and r2 is the top of the screw thread. If r1<r2, then this is an internal thread */
  16.  
  17. /* height is the total length of the thread */
  18. /* pitch is the distance the thread advances per turn */
  19. /* for now, set the tooth size to 3/4 of the pitch, with the rise, top, fall and gap all 1.4 of the pitch */
  20.  
  21. var rise=pitch/4;
  22. var fall=rise;
  23. var top=rise;
  24. var gap=rise;
  25.  
  26. /* ndivs is the number of steps around the circle */
  27. var ndivs = Tess.circleDivisions(Math.max(r1,r2));
  28. var n = ndivs*params.height/params.pitch;
  29. /* n is the total number of steps */
  30.  
  31. /* set up the mesh */
  32.  
  33. var mesh = new Mesh3D();
  34.  
  35. /* set up the first face */
  36.  
  37. var x0 = r1;
  38. var x1=r2;
  39. var y0=0;
  40. var y1=0;
  41. var z0=0;
  42. var z1=rise;
  43. var z2 = z1+top;
  44. var z3 = z2+fall;
  45.  
  46. /* calculate the amount the screw rises over each step */
  47. var lift = pitch/ndivs;
  48.  
  49. mesh.quad([x0, y0, z0, x1,y1,z1,x1,y1,z2,x0,y0,z3]);
  50.  
  51. for (var i = 0; i < n; i++) {
  52. var theta = (i+1)/ndivs * Math.PI*2;
  53. var s = Math.sin(theta);
  54. var c = Math.cos(theta);
  55.  
  56. var x2 = r1*c;
  57. var x3 = r2*c;
  58. var y2 = r1*s;
  59. var y3 = r2*s;
  60. var z4 = (i+1)*lift;
  61. var z5 = z4 + rise;
  62. var z6 = z5 + top;
  63. var z7 = z6+fall;
  64.  
  65. /* add 4 quads: base, underside, end and top side */
  66.  
  67. mesh.quad([x0, y0,z0,x2,y2,z4,x2,y2,z7,x0,y0,z3]);
  68. mesh.quad([x0,y0,z0,x1,y1,z1,x3,y3,z5,x2,y2,z4]);
  69. mesh.quad([x1,y1,z1,x1,y1,z2,x3,y3,z6,x3,y3,z5]);
  70. mesh.quad([x0,y0,z3,x1,y1,z2,x3,y3,z6,x2,y2,z7]);
  71.  
  72. /* shift vertices */
  73. x0=x2;
  74. x1=x3;
  75. y0=y2;
  76. y1= y3;
  77. z0=z4;
  78. z1=z5;
  79. z2=z6;
  80. z3=z7;
  81.  
  82. }
  83.  
  84. /* add end cap */
  85.  
  86. mesh.quad([x0, y0, z0, x1,y1,z1,x1,y1,z2,x0,y0,z3]);
  87.  
  88. var solid = Solid.make(mesh);
  89. return solid;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement