sticki
sticki

Reputation: 45

Howto make holes in cubes with openscad / for-loop?

I want to make a hole in each of these cubes. Here is the code:

y=45;
for (i=[1:8]){
    z = y*i;
    difference(){
        rotate([0,0,z]) translate([57,0,-5]) cube(center = true,[5,10,10]);
        rotate([90,90,z]) translate([6,0,-60]) cylinder(5,2,2);  
    }
    
}
// This is a reference, translate([6,0,-60]) is correct position
rotate([90,90,z]) translate([16,0,-60]) cylinder(5,2,2); 

Why

rotate([90,90,z]) translate([6,0,-60]) cylinder(5,2,2); 

do not work in a for loop? When z is setting manually to 45, 90, 135, 180...315 the holes are correct.

Upvotes: 2

Views: 3816

Answers (1)

wsdookadr
wsdookadr

Reputation: 2662

So the main loop will position your cuboids rotated around the origin at an angle which is a multiple of 45 degrees. Inside the loop, you now want to draw the cuboids, and right after that, relative to the position of each cuboid, you make a few more transformations (a rotation and translation) to get the cylinders to pass through the centers of the cuboids (It also helps if the height of the cylinder is bigger than the side of the cuboid so you can actually see it passing through):

y=45;
for (i=[1:8]){
    z = y*i;    
    rotate([0,0,z]) translate([57,0,-5])
    {
        cube(center=true,[5,10,10]);
        rotate([0,90,0]) translate([0,0,-5]) cylinder(r=2,h=10,$fn=100);
    };
}

enter image description here

Now that you know the positions are correct, you can apply the boolean difference and obtain the hole at the center of each cuboid:

y=45;
for (i=[1:8]){
    z = y*i;    
    rotate([0,0,z]) translate([57,0,-5])
    difference() {
        cube(center=true,[5,10,10]);
        rotate([0,90,0]) translate([0,0,-5]) cylinder(r=2,h=10,$fn=100) ;
    }
}


enter image description here

You can find all the code for this here.

Upvotes: 2

Related Questions