Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- params = [
- { "id": "r1", "displayName": "R1", "type": "float", "rangeMin": 0, "rangeMax": 100, "default": 10.0 },
- { "id": "r2", "displayName": "R2", "type": "float", "rangeMin": 0, "rangeMax": 100, "default": 10.0 },
- { "id": "height", "displayName": "Height", "type": "float", "rangeMin": 1.0, "rangeMax": 100.0, "default": 20.0 }
- { "id": "pitch", "displayName": "Pitch", "type": "float", "rangeMin": 1.0, "rangeMax": 100.0, "default": 5.0 }
- ]
- function process(params) {
- var height = params["height"];
- var r1 = params["r1"];
- var r2 = params["r2"];
- /* r1 is the base and r2 is the top of the screw thread. If r1<r2, then this is an internal thread */
- /* height is the total length of the thread */
- /* pitch is the distance the thread advances per turn */
- /* 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 */
- var rise=pitch/4;
- var fall=rise;
- var top=rise;
- var gap=rise;
- /* ndivs is the number of steps around the circle */
- var ndivs = Tess.circleDivisions(Math.max(r1,r2));
- var n = ndivs*params.height/params.pitch;
- /* n is the total number of steps */
- /* set up the mesh */
- var mesh = new Mesh3D();
- /* set up the first face */
- var x0 = r1;
- var x1=r2;
- var y0=0;
- var y1=0;
- var z0=0;
- var z1=rise;
- var z2 = z1+top;
- var z3 = z2+fall;
- /* calculate the amount the screw rises over each step */
- var lift = pitch/ndivs;
- mesh.quad([x0, y0, z0, x1,y1,z1,x1,y1,z2,x0,y0,z3]);
- for (var i = 0; i < n; i++) {
- var theta = (i+1)/ndivs * Math.PI*2;
- var s = Math.sin(theta);
- var c = Math.cos(theta);
- var x2 = r1*c;
- var x3 = r2*c;
- var y2 = r1*s;
- var y3 = r2*s;
- var z4 = (i+1)*lift;
- var z5 = z4 + rise;
- var z6 = z5 + top;
- var z7 = z6+fall;
- /* add 4 quads: base, underside, end and top side */
- mesh.quad([x0, y0,z0,x2,y2,z4,x2,y2,z7,x0,y0,z3]);
- mesh.quad([x0,y0,z0,x1,y1,z1,x3,y3,z5,x2,y2,z4]);
- mesh.quad([x1,y1,z1,x1,y1,z2,x3,y3,z6,x3,y3,z5]);
- mesh.quad([x0,y0,z3,x1,y1,z2,x3,y3,z6,x2,y2,z7]);
- /* shift vertices */
- x0=x2;
- x1=x3;
- y0=y2;
- y1= y3;
- z0=z4;
- z1=z5;
- z2=z6;
- z3=z7;
- }
- /* add end cap */
- mesh.quad([x0, y0, z0, x1,y1,z1,x1,y1,z2,x0,y0,z3]);
- var solid = Solid.make(mesh);
- return solid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement