|
4 | 4 | // Copyright: 2010 |
5 | 5 | // License: 2-clause BSD License (http://opensource.org/licenses/BSD-2-Clause) |
6 | 6 |
|
7 | | -// roundedBox([width, height, depth], float radius, bool sidesonly); |
| 7 | +// |
| 8 | +// roundedCube([x, y, z], r, sidesonly=true/false, center=true/false); |
| 9 | +// roundedCube(x, r, sidesonly=true/false, center=true/false); |
8 | 10 |
|
9 | 11 | // EXAMPLE USAGE: |
10 | | -// roundedBox([20, 30, 40], 5, true); |
| 12 | +// roundedCube([20, 30, 40], 5, true, true); |
11 | 13 |
|
12 | | -// size is a vector [w, h, d] |
| 14 | +// Only for backwards compatibility with existing scripts, (always centered, radius instead of consistent "r" naming. |
13 | 15 | module roundedBox(size, radius, sidesonly) |
14 | 16 | { |
15 | | - rot = [ [0,0,0], [90,0,90], [90,90,0] ]; |
16 | | - if (sidesonly) { |
17 | | - cube(size - [2*radius,0,0], true); |
18 | | - cube(size - [0,2*radius,0], true); |
19 | | - for (x = [radius-size[0]/2, -radius+size[0]/2], |
20 | | - y = [radius-size[1]/2, -radius+size[1]/2]) { |
21 | | - translate([x,y,0]) cylinder(r=radius, h=size[2], center=true); |
22 | | - } |
23 | | - } |
24 | | - else { |
25 | | - cube([size[0], size[1]-radius*2, size[2]-radius*2], center=true); |
26 | | - cube([size[0]-radius*2, size[1], size[2]-radius*2], center=true); |
27 | | - cube([size[0]-radius*2, size[1]-radius*2, size[2]], center=true); |
| 17 | + echo("WARNING: roundedBox(size, radius, sidesonly) is deprecated, use roundedCube(size, r, sidesonly, center)"); |
| 18 | + roundedCube(size, radius, sidesonly, true); |
| 19 | +} |
28 | 20 |
|
29 | | - for (axis = [0:2]) { |
30 | | - for (x = [radius-size[axis]/2, -radius+size[axis]/2], |
31 | | - y = [radius-size[(axis+1)%3]/2, -radius+size[(axis+1)%3]/2]) { |
32 | | - rotate(rot[axis]) |
33 | | - translate([x,y,0]) |
34 | | - cylinder(h=size[(axis+2)%3]-2*radius, r=radius, center=true); |
| 21 | +// New implementation |
| 22 | +module roundedCube(size, r, sidesonly, center) { |
| 23 | + s = is_list(size) ? size : [size,size,size]; |
| 24 | + translate(center ? -s/2 : [0,0,0]) { |
| 25 | + if (sidesonly) { |
| 26 | + hull() { |
| 27 | + translate([ r, r]) cylinder(r=r, h=s[2]); |
| 28 | + translate([ r,s[1]-r]) cylinder(r=r, h=s[2]); |
| 29 | + translate([s[0]-r, r]) cylinder(r=r, h=s[2]); |
| 30 | + translate([s[0]-r,s[1]-r]) cylinder(r=r, h=s[2]); |
35 | 31 | } |
36 | 32 | } |
37 | | - for (x = [radius-size[0]/2, -radius+size[0]/2], |
38 | | - y = [radius-size[1]/2, -radius+size[1]/2], |
39 | | - z = [radius-size[2]/2, -radius+size[2]/2]) { |
40 | | - translate([x,y,z]) sphere(radius); |
| 33 | + else { |
| 34 | + hull() { |
| 35 | + translate([ r, r, r]) sphere(r=r); |
| 36 | + translate([ r, r,s[2]-r]) sphere(r=r); |
| 37 | + translate([ r,s[1]-r, r]) sphere(r=r); |
| 38 | + translate([ r,s[1]-r,s[2]-r]) sphere(r=r); |
| 39 | + translate([s[0]-r, r, r]) sphere(r=r); |
| 40 | + translate([s[0]-r, r,s[2]-r]) sphere(r=r); |
| 41 | + translate([s[0]-r,s[1]-r, r]) sphere(r=r); |
| 42 | + translate([s[0]-r,s[1]-r,s[2]-r]) sphere(r=r); |
| 43 | + } |
41 | 44 | } |
42 | 45 | } |
43 | 46 | } |
0 commit comments