OpensCAD holes in cylinder around the center but rotated

382 Views Asked by At

I want to do a cylinder, which has holes on it(like in the inserted photo). I managed to do the holes, but I don't have any idea how to make them rotated, to look like on the photo.

Can you please help me? I ran out of ideas.

My code:

h=150;
r_cylinder=62.5;
r_hole1=2;
r_hole2=3;
r_hole3=4;
r_bighole=15;
distance=30;
n=24; 
$fn=120;

difference()
{
  cylinder(h, r_cylinder,r_cylinder,center=true);
    cylinder(h+1,r_bighole,r_bighole,center=true); 
    for(i=[1:n])
    {
      translate([distance*cos(i*(360/n)),distance*sin(i*(360/n)),0])
        circle(r_hole1);
      translate([(distance+10)*cos(i*(360/(n))),(distance+10)*sin(i*(360/(n))),0]) cylinder(h+1,r_hole2,r_hole2,center=true);
      translate([(distance+20)*cos(i*(360/(n))),(distance+20)*sin(i*(360/(n))),0]) cylinder(h+1,r_hole3,r_hole3,center=true); 
    } 
}

Screenshot:

enter image description here

I am trying to get the holes like on the photo.

1

There are 1 best solutions below

2
chrslg On

Well, using your logic and code (Once corrected: I've change the 1st series of holes from circle to cylinder since it was obviously your goal to have 3 ranks of holes), the only thing you have to do is to shift the angles.

Since you have one hole every 360/24=15 degrees, just add 5 degrees to the angle of second holes, and 10 to the angle of 3rd.

Or, to avoid hard coding, and keep it as a formula of n, use i*(360/n) as you did for angle of first rank holes, (i+1/3)*(360/n) for second, and (1+2/3)*(360/n) for third.

h=150;
r_cylinder=62.5;
r_hole1=2;
r_hole2=3;
r_hole3=4;
r_bighole=15;
distance=30;
n=24; 
$fn=120;

difference()
{    
  cylinder(h, r_cylinder,r_cylinder,center=true);
  cylinder(h+1,r_bighole,r_bighole,center=true); 
  for(i=[1:n])
  {  
    translate([distance*cos(i*(360/n)),distance*sin(i*(360/n)),0]) cylinder(h+1,r_hole1, r_hole1, center=true);   
    translate([(distance+10)*cos((i+1/3)*(360/(n))),(distance+10)*sin((i+1/3)*(360/(n))),0]) cylinder(h+1,r_hole2,r_hole2,center=true);
    translate([(distance+20)*cos((i+2/3)*(360/(n))),(distance+20)*sin((i+2/3)*(360/(n))),0]) cylinder(h+1,r_hole3,r_hole3,center=true); 
  } 
}

enter image description here

That being said, rather than doing trigonometry yourself, and, avoid some code duplication, I would rather rely on rotate to do that. Understand that rotates rotates around an axes that passes through the origin. So, it depends whether you rotate first and translate after, or translate first and translate after.

For example cube([1,1,1], center=true) is a cube at the origin

    ###########
    ###########
    #####o#####
    ###########
    ###########

rotate([0,0,45]) cube([1,1,1]) is, at the origin, a cube rotated around the origin (a losange from the top)

           #
         #####
       #########
     #############
   ########o########
     #############
       #########
         #####
           #

So translate([2,0,0]) rotate([0,0,45]) cube([1,1,1], center=True) is the same thing shifted

                                 #
                               #####
                             #########
                           #############
           o             #################
                           #############
                             #########
                               #####
                                 #

If I do it the other way: `translate([2,0,0]) cube([1,1,1], center=true) is a cube away from the origin

                          ###########
                          ###########
         o                ###########
                          ###########
                          ###########

So rotate([0,0,45]) is that last figure, rotated around the origin

                            #
                          #####
                        #########
                      #############
                    #################
                      #############
                        #########
                          #####
                            #






    o

So, that is the last construct I would rather use. Instead of digging a hole at position [D*cos(α), D*sin(α), 0], I would dig it at position [D,0,0] and then rotate that shifted hole with angle α.

Like this

translate([D*cos(alpha), D*sin(alpha),0]) cylinder(h,r,center=true)

is replaced by

rotate([0,0,alpha]) translate([D,0,0]) cylinder(h,r,center=true)

Not only it is a bit shorter (especially when, like in your case, D and alpha are longer expressions, since the 1st version write them twice, while the second just once), but also it relies more on openscad for geometry, since, after all, it's its jobs. Plus, I think it makes it easier to think in terms of angles.

So

h=150;
r_cylinder=62.5;
r_hole1=2;
r_hole2=3;
r_hole3=4;
r_bighole=15;
distance=30;
n=24; 
$fn=120;

difference()
{    
  cylinder(h, r_cylinder,r_cylinder,center=true);
  cylinder(h+1,r_bighole,r_bighole,center=true); 
  for(i=[1:n])
  {  
    rotate([0,0,i*360/n]) translate([distance, 0, 0]) cylinder(h+1,r_hole1, r_hole1, center=true);   
    rotate([0,0,(i+1/3)*360/n]) translate([distance+10,0,0]) cylinder(h+1,r_hole2,r_hole2,center=true);
    rotate([0,0,(i+2/3)*360/n]) translate([distance+20,0,0]) cylinder(h+1,r_hole3,r_hole3,center=true); 
  } 
}

Or using double for loop

h=150;
r_cylinder=62.5;
r_hole=[2,3,4];
r_bighole=15;
distance=30;
n=24; 
$fn=120;

difference(){    
  cylinder(h, r=r_cylinder,center=true);
  cylinder(h+1,r=r_bighole,center=true); 
  for(i=[1:n])
      for(j=[0:2])
          rotate([0,0,(i+j/3)*360/n]) translate([distance+j*10,0,0]) cylinder(h+1, r=r_hole[j], center=true);
}

Or, last suggestion, you could create a repeatInCircle modifier. And also, use different distances for the 3 ranks (for now, the same distance separate holes of rank 1 and rank 2, than what separates rank 2 from rank 3. 10 each time. But since the diameter of the holes are bigger and bigger, it is probably better if distance is smaller at first and bigger then. As it is so in your example image. Like this

h=150;
r_cylinder=62.5;
r_hole=[2,3,4];
r_bighole=15;
distances=[32,40,50];
n=24; 
$fn=120;

module repeatInCircle(n,di){
    for(i=[0:n-1])
        rotate([0,0,(i+di)*360/n]) children();
}

difference(){    
  cylinder(h, r=r_cylinder,center=true);
  cylinder(h+1,r=r_bighole,center=true); 
  for(j=[0:2])
     repeatInCircle(n,j/3) translate([distances[j],0,0]) cylinder(h+1, r=r_hole[j], center=true);
}

enter image description here