]>
Commit | Line | Data |
---|---|---|
1 | // Retrieved from https://www.thingiverse.com/thing:4120804 | |
2 | ||
3 | /* | |
4 | roundedCube() v1.0.3 by robert@cosmicrealms.com from https://github.com/Sembiance/openscad-modules | |
5 | Allows you to round any edge of a cube | |
6 | ||
7 | Usage | |
8 | ===== | |
9 | Prototype: roundedCube(dim, r, x, y, z, xcorners, ycorners, zcorners, $fn) | |
10 | Arguments: | |
11 | - dim = Array of x,y,z numbers representing cube size | |
12 | - r = Radius of corners. Default: 1 | |
13 | - x = Round the corners along the X axis of the cube. Default: false | |
14 | - y = Round the corners along the Y axis of the cube. Default: false | |
15 | - z = Round the corners along the Z axis of the cube. Default: true | |
16 | - xcorners = Array of 4 booleans, one for each X side of the cube, if true then round that side. Default: [true, true, true, true] | |
17 | - ycorners = Array of 4 booleans, one for each Y side of the cube, if true then round that side. Default: [true, true, true, true] | |
18 | - zcorners = Array of 4 booleans, one for each Z side of the cube, if true then round that side. Default: [true, true, true, true] | |
19 | - rx = Radius of the x corners. Default: [r, r, r, r] | |
20 | - ry = Radius of the y corners. Default: [r, r, r, r] | |
21 | - rz = Radius of the z corners. Default: [r, r, r, r] | |
22 | - center = Whether to render the cube centered or not. Default: false | |
23 | - $fn = How smooth you want the rounding to be. Default: 128 | |
24 | ||
25 | Change Log | |
26 | ========== | |
27 | 2018-08-21: v1.0.3 - Added ability to set the radius of each corner individually with vectors: rx, ry, rz | |
28 | 2017-05-15: v1.0.2 - Fixed bugs relating to rounding corners on the X axis | |
29 | 2017-04-22: v1.0.1 - Added center option | |
30 | 2017-01-04: v1.0.0 - Initial Release | |
31 | ||
32 | Thanks to Sergio Vilches for the initial code inspiration | |
33 | */ | |
34 | ||
35 | // Example code: | |
36 | ||
37 | /*cube([5, 10, 4]); | |
38 | ||
39 | translate([8, 0, 0]) { roundedCube([5, 10, 4], r=1); } | |
40 | translate([16, 0, 0]) { roundedCube([5, 10, 4], r=1, zcorners=[true, false, true, false]); } | |
41 | ||
42 | translate([24, 0, 0]) { roundedCube([5, 10, 4], r=1, y=true, z=false); } | |
43 | translate([32, 0, 0]) { roundedCube([5, 10, 4], r=1, x=true, z=false); } | |
44 | translate([40, 0, 0]) { roundedCube([5, 10, 4], r=1, x=true, y=true, z=true); } | |
45 | */ | |
46 | ||
47 | module roundedCube(dim, r=1, x=false, y=false, z=true, xcorners=[true,true,true,true], ycorners=[true,true,true,true], zcorners=[true,true,true,true], center=false, rx=[undef, undef, undef, undef], ry=[undef, undef, undef, undef], rz=[undef, undef, undef, undef], $fn=128) | |
48 | { | |
49 | translate([(center==true ? (-(dim[0]/2)) : 0), (center==true ? (-(dim[1]/2)) : 0), (center==true ? (-(dim[2]/2)) : 0)]) | |
50 | { | |
51 | difference() | |
52 | { | |
53 | cube(dim); | |
54 | ||
55 | if(z) | |
56 | { | |
57 | translate([0, 0, -0.1]) | |
58 | { | |
59 | if(zcorners[0]) | |
60 | translate([0, dim[1]-(rz[0]==undef ? r : rz[0])]) { rotateAround([0, 0, 90], [(rz[0]==undef ? r : rz[0])/2, (rz[0]==undef ? r : rz[0])/2, 0]) { meniscus(h=dim[2], r=(rz[0]==undef ? r : rz[0]), fn=$fn); } } | |
61 | if(zcorners[1]) | |
62 | translate([dim[0]-(rz[1]==undef ? r : rz[1]), dim[1]-(rz[1]==undef ? r : rz[1])]) { meniscus(h=dim[2], r=(rz[1]==undef ? r : rz[1]), fn=$fn); } | |
63 | if(zcorners[2]) | |
64 | translate([dim[0]-(rz[2]==undef ? r : rz[2]), 0]) { rotateAround([0, 0, -90], [(rz[2]==undef ? r : rz[2])/2, (rz[2]==undef ? r : rz[2])/2, 0]) { meniscus(h=dim[2], r=(rz[2]==undef ? r : rz[2]), fn=$fn); } } | |
65 | if(zcorners[3]) | |
66 | rotateAround([0, 0, -180], [(rz[3]==undef ? r : rz[3])/2, (rz[3]==undef ? r : rz[3])/2, 0]) { meniscus(h=dim[2], r=(rz[3]==undef ? r : rz[3]), fn=$fn); } | |
67 | } | |
68 | } | |
69 | ||
70 | if(y) | |
71 | { | |
72 | translate([0, -0.1, 0]) | |
73 | { | |
74 | if(ycorners[0]) | |
75 | translate([0, 0, dim[2]-(ry[0]==undef ? r : ry[0])]) { rotateAround([0, 180, 0], [(ry[0]==undef ? r : ry[0])/2, 0, (ry[0]==undef ? r : ry[0])/2]) { rotateAround([-90, 0, 0], [0, (ry[0]==undef ? r : ry[0])/2, (ry[0]==undef ? r : ry[0])/2]) { meniscus(h=dim[1], r=(ry[0]==undef ? r : ry[0])); } } } | |
76 | if(ycorners[1]) | |
77 | translate([dim[0]-(ry[1]==undef ? r : ry[1]), 0, dim[2]-(ry[1]==undef ? r : ry[1])]) { rotateAround([0, -90, 0], [(ry[1]==undef ? r : ry[1])/2, 0, (ry[1]==undef ? r : ry[1])/2]) { rotateAround([-90, 0, 0], [0, (ry[1]==undef ? r : ry[1])/2, (ry[1]==undef ? r : ry[1])/2]) { meniscus(h=dim[1], r=(ry[1]==undef ? r : ry[1])); } } } | |
78 | if(ycorners[2]) | |
79 | translate([dim[0]-(ry[2]==undef ? r : ry[2]), 0]) { rotateAround([-90, 0, 0], [0, (ry[2]==undef ? r : ry[2])/2, (ry[2]==undef ? r : ry[2])/2]) { meniscus(h=dim[1], r=(ry[2]==undef ? r : ry[2])); } } | |
80 | if(ycorners[3]) | |
81 | rotateAround([0, 90, 0], [(ry[3]==undef ? r : ry[3])/2, 0, (ry[3]==undef ? r : ry[3])/2]) { rotateAround([-90, 0, 0], [0, (ry[3]==undef ? r : ry[3])/2, (ry[3]==undef ? r : ry[3])/2]) { meniscus(h=dim[1], r=(ry[3]==undef ? r : ry[3])); } } | |
82 | } | |
83 | } | |
84 | ||
85 | if(x) | |
86 | { | |
87 | translate([-0.1, 0, 0]) | |
88 | { | |
89 | if(xcorners[0]) | |
90 | translate([0, dim[1]-(rx[0]==undef ? r : rx[0])]) { rotateAround([0, 90, 0], [(rx[0]==undef ? r : rx[0])/2, 0, (rx[0]==undef ? r : rx[0])/2]) { meniscus(h=dim[0], r=(rx[0]==undef ? r : rx[0])); } } | |
91 | if(xcorners[1]) | |
92 | translate([0, dim[1]-(rx[1]==undef ? r : rx[1]), dim[2]-(rx[1]==undef ? r : rx[1])]) { rotateAround([90, 0, 0], [0, (rx[1]==undef ? r : rx[1])/2, (rx[1]==undef ? r : rx[1])/2]) { rotateAround([0, 90, 0], [(rx[1]==undef ? r : rx[1])/2, 0, (rx[1]==undef ? r : rx[1])/2]) { meniscus(h=dim[0], r=(rx[1]==undef ? r : rx[1])); } } } | |
93 | if(xcorners[2]) | |
94 | translate([0, 0, dim[2]-(rx[2]==undef ? r : rx[2])]) { rotateAround([180, 0, 0], [0, (rx[2]==undef ? r : rx[2])/2, (rx[2]==undef ? r : rx[2])/2]) { rotateAround([0, 90, 0], [(rx[2]==undef ? r : rx[2])/2, 0, (rx[2]==undef ? r : rx[2])/2]) { meniscus(h=dim[0], r=(rx[2]==undef ? r : rx[2])); } } } | |
95 | if(xcorners[3]) | |
96 | rotateAround([-90, 0, 0], [0, (rx[3]==undef ? r : rx[3])/2, (rx[3]==undef ? r : rx[3])/2]) { rotateAround([0, 90, 0], [(rx[3]==undef ? r : rx[3])/2, 0, (rx[3]==undef ? r : rx[3])/2]) { meniscus(h=dim[0], r=(rx[3]==undef ? r : rx[3])); } } | |
97 | } | |
98 | } | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | module meniscus(h, r, fn=128) | |
104 | { | |
105 | $fn=fn; | |
106 | ||
107 | difference() | |
108 | { | |
109 | cube([r+0.2, r+0.2, h+0.2]); | |
110 | translate([0, 0, -0.1]) { cylinder(h=h+0.4, r=r); } | |
111 | } | |
112 | } | |
113 | ||
114 | module rotateAround(a, v) { translate(v) { rotate(a) { translate(-v) { children(); } } } } |