Creating "pie slice" in OpenSCAD?

5

When making a cylinder, sometimes I need to only take a pie slice. I'm currently using this neat trick to make pie slices for angles under 90 degrees. However, I have need of a few angles over 90 but under 180 degrees. Is there a way to generalize/extend this to work for these bigger angles?

module pie_slice(r=3.0,a=30) {     
   intersection() {
    circle(r=r);
    square(r);
    rotate(a-90) square(r);  
  } 
}

pie_slice(r=10,a=15);

Hamy

Posted 2019-07-20T22:11:16.773

Reputation: 223

I think the easiest would be to use difference. Circle minus two cubes with sides 2R. (Or one of size 2R and one of size R.) – Tomas By – 2019-07-20T22:19:45.440

IIUC, this is similar to my current workaround (I've posted below for reference). I use union instead of intersection. Unfortunately, you have to examine how many degrees the caller wants and use one or the other - I have yet to find a universal method – Hamy – 2019-07-20T22:33:44.647

Think of it this way: you have two rectangles that cover each half of the circle. Then you open a gap between them of the angle. That should work for any angle between 0 and 180. – Tomas By – 2019-07-20T22:38:41.743

Oh snap, that sounds perfect. I'll give it a try, thanks – Hamy – 2019-07-20T22:46:07.570

Or use a module that can make pie slices, e.g. Chamfers-for-OpenSCAD.

– 0scar – 2019-07-21T06:58:41.580

Answers

1

My current workaround is to use union instead of intersection. Unfortunately, that means I have to use an if clause which makes the code have two paths instead of one clean approach. Also, unlike the above method, this does not result in a clean cylindrical shape but must instead by combined with a proper cylinder to get the final pie slice

   size = length + 2;
    if (angle_deg <= 90) {
      translate([0,0,-1]) 
      intersection() {
        cube(size);
        rotate(angle_deg-90) cube(size);
      }
    } else if (angle_deg <= 180) {
      translate([0,0,-1]) 
      union() {
        cube(size);
        rotate(angle_deg-90) cube(size);
      }      
    } else {
      echo(str("FAILURE - Angle cannot exceed 180"));
    } 


Hamy

Posted 2019-07-20T22:11:16.773

Reputation: 223

0

Although generating complex shapes by combining primitive OpenSCAD shapes is a well-established tradition, and is often all that is needed, it would be more elegant in this case to generate a pie slice directly using the polygon function and a list comprehension.

module pie_slice(r=3.0, a=30) {
  polygon(points=[
    [0, 0],
    for(theta=0; theta<a; theta=theta+$fa)
      [r*cos(theta), r*sin(theta)],
    [r*cos(a), r*sin(a)]
  ]);
}

Note that the above code is a little crude, since it does no error checking, but it works. It uses the $fa special variable for the step angle.

Mick

Posted 2019-07-20T22:11:16.773

Reputation: 2 597

0

This is what I use:

module pieSlice(a, r, h){
  // a:angle, r:radius, h:height
  rotate_extrude(angle=a) square([r,h]);
}
pieSlice(110,20,3);

CFreitas

Posted 2019-07-20T22:11:16.773

Reputation: 101