CADjs Cloning and for loops wheel, hub and spoke assembly g1 = cube(5); g2=cylinder(1,20); g3=cylinder(1,20); g4=cylinder(20,2); g5=cylinder(10,2); g2.rotateX(90); g3.rotateZ(90); g=g4.difference(g5); g=g.union(g1); g=g.union(g2); g=g.union(g3); g.display(); Question Idea of Cloning g1 = cylinder(0.5,0.5); g1.display(); Want to create another similar cylinders, but translated g1 = cylinder(0.5,0.5); g1.display(); g2 = g1;//make a copy? g2.translate(1);//translate g2 g1 = cylinder(0.5,0.5); g1.display(); g2 = g1.clone();//makes a true copy g2.translate(1);// translate g2, but not g1 g2.display(); g2 = g1; … does not make copy g2 is exactly the same as g1 Translating g2 will also translate g1 Idea of Copies g1 = cylinder(0.5,0.5); g1.display(); g2 = g1.clone();//makes a true copy g2.translate(1);// translate g2, but not g1 g2.display(); g3 = g1.clone();// g3.translate(2); g3.display(); … can continue, but better to use for loops! for loops ‘for’ loop Start at i = 0 Stop when i = 5 Increase i by 1 every loop for (i = 0;i < 5;i = i+1) { } How many times is the loop executed? i =0 i=1 i=2 i=3 i=4 (stop) ‘for’ loop with Beeper resetBeeper(); for (i = 0; i < 5; i= i+1){ beep(); } Increase i by 1 every loop resetBeeper(); for (i = 0; i < 5; i++){ beep(); } ‘for’ loop with Beeper resetBeeper(); for (i = 0; i <= 10; i= i+2){ beep(); } How many beeps? resetBeeper(); for (i = -5; i <2; i= i+1){ beep(); } How many beeps? ‘for’ loop with textGeom for (i = 0; i < 5; i++){ textGeom(i).translateX(i).display(); } for (i = -2; i <= 2; i = i+2){ textGeom(i).translateX(i).display(); } ‘for’ loop with textGeom for (i = 0; i < 5; i++){ textGeom(i).translateX(i).display(); } for (i = 0; i < 5; i++){ textGeom(2*i).translateX(i).display(); } for (i = 0; i < 5; i++){ textGeom(2*i).translateX(2*i).display(); } For loop with geometry Use one cylinder and copy/move g = cylinder(.5,.5); for (i = 0;i < 5;i = i+1) { g1 = g.clone(); g1.translateX(i).display(); } Union with a base For loop with geometry Union with a base base = cube(5,0.2,1).translateX(2.); g = cylinder(.5,.5); for (i = 0;i < 5;i = i+1) { g1 = g.clone(); base = base.union(g1.translateX(i)); } base.display(); Union ‘for’ loop g = cube(.5,.5); g1 = g.clone(); for (i = 0;i < 36;i++) { g1.rotateZ(10); g = g.union(g1); } g.display(); Union ‘for’ loop & difference g = cube(.5,.5); g1 = g.clone(); g2=cylinder(1,.45).rotateX(90); for (i = 0;i < 36;i++) { g1.rotateZ(10); g = g.union(g1); } g=g2.difference(g) g.display(); Lego model Lego model: Step 1 g = cylinder(.5,.5); g1 = g.clone(); for (i = 0;i < 3;i++) { g1.translate(2,0,0); g = g.union(g1); } g.display(); Lego model: Step 2 g = cylinder(.5,.5); g1 = g.clone(); for (i = 0;i < 3;i++) { g1.translate(2,0,0); g = g.union(g1); } g3=g.clone().translate(0,0,2); g=g.union(g3); g.display(); Lego model: Step 3 g = cylinder(.5,.5); g1 = g.clone(); base=cube(8,3,4).translate(3,-1.75,1); for (i = 0;i < 3;i++) { g1.translate(2,0,0); g = g.union(g1); } g3=g.clone().translate(0,0,2); g=g.union(g3); g=g.union(base); g.display(); Lego model: Step 4 g = cylinder(.5,.5); g1 = g.clone(); base=cube(8,3,4).translate(3,-1.75,1); g4=cube(7,4,3).translate(3,-2.5,1); base=base.difference(g4); for (i = 0;i < 3;i++) { g1.translate(2,0,0); g = g.union(g1); } g3=g.clone().translate(0,0,2); g=g.union(g3); g=g.union(base); g.display(); Exercise (Ladder) Length = 10, width = 0.25 (2 pieces, 1.25 apart) 1.5 length, 0.1 width and height Exercise (Ladder) Exercise Radius = 0.1, height = 0.5 (centers on a circle of radius 0.8) Radius = 1.0, height = 0.25 8 pins Exercise Modify your code to handle N pins N = 3 pins N = 6 pins N = 8 pins Exercise setDefaultSegments(24); R = 1.0; H = 0.25; A = cylinder(R,H); r = 0.1; h = 0.5; s = 0.8; pin = cylinder(r,h).translateY(H/2+h/2).translateX(s); N = 8; // number of pins for (i = 0; i < N; i++) { A = A.union(pin); pin = pin.rotateY(360/N); } A.display();