## Monday, February 14, 2011

### OpenSCAD Tip: Round 1 of 3 - Basic Rounding

This is the beginning of a three part series on rounding edges in OpenSCAD. This tutorial shows how to make a box with rounded edges, but this technique can be used anywhere you are trying to round an edge where both surfaces are flat and meet at a 90 degree angle.

Starting with a box in OpenSCAD
box_l = 10; // Length
box_w = 10; // Width
box_h = 10; // Height
cube([box_l, box_w, box_h], center = true);

So what is a rounded corner? One way to think about it is that it is shaped in a way similar to a cylinder. The idea is to make the edge of the box into a cylinder, so the first attempt will to be to remove an absence of a cylinder. Next move a cylinder into place for the edge to be rounded. The padding is there to maintain manifoldness.
pad = 0.1; // Padding to maintain manifold
box_l = 10; // Length
box_w = 10; // Width
box_h = 10; // Height
round_r = 2; // Radius of round
smooth = 45; // Number of facets of rounding cylinder
difference() {
cube([box_l, box_w, box_h], center = true);

translate([0, -box_w/2+round_r, box_h/2-round_r]) {
rotate(a=[0,90,0])
cylinder(box_l+4*pad,round_r,round_r,center=true,\$fn=smooth);
}
}

Now that all the padding and minus signs are correct, a box is moved into place to make the part to round the edge off.
translate([0, -box_w/2+round_r, box_h/2-round_r]) {
difference() {
translate([0,-round_r+pad,round_r+pad])
cube([box_l+2*pad, round_r*2+pad, round_r*2+pad], center = true);
rotate(a=[0,90,0])
cylinder(box_l+4*pad,round_r,round_r,center=true,\$fn=smooth);
}
}

Now by subtracting this from the original box one of the edges is rounded off.

difference() {
cube([box_l, box_w, box_h], center = true);
translate([0, -box_w/2+round_r, box_h/2-round_r]) {
difference() {
translate([0,-round_r+pad,round_r+pad])
cube([box_l+2*pad, round_r*2+pad, round_r*2+pad], center = true);
rotate(a=[0,90,0])
cylinder(box_l+4*pad,round_r,round_r,center=true,\$fn=smooth);
}
}
}

Now by repeating the technique on all of the sides a new problem is discovered.

To fix the corners, the absence of a sphere must be subtracted from each corner.

If a cylinder isn't a good analogy then the question becomes, how do you mathematically represent a rounded edge. One way to do that is to think of the shape produced by a sphere moving along the edge. We will explore this concept of rounding using a rolling ball further in the next tutorial, Advanced Rounding with OpenSCAD.

All of the .scad files can be found on Thingiverse and our previous OpenSCAD tutorials can be found here.

#### 2 comments:

whosawhatsis? said...

It gets really interesting when you want to round the edges with two different radii. The solution involves using toriods (rotate_extrude) instead of spheres for the corners. I demonstrated this technique on my Better thumb z-crank Thing.

I Heart Robotics said...

That looks great! I think you will like part 3 - Impossible Rounding.