diff --git a/README.md b/README.md index 8ea2b427..26b9da14 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Some modules may depend on other modules. For example, the `polyline2d` module d - [hexagons](https://openhome.cc/eGossip/OpenSCAD/lib-hexagons.html) - 3D + - [rounded_cube](https://openhome.cc/eGossip/OpenSCAD/lib-rounded_cube.html) - [line3d](https://openhome.cc/eGossip/OpenSCAD/lib-line3d.html) - [polyline3d](https://openhome.cc/eGossip/OpenSCAD/lib-polyline3d.html) - [hull_polyline3d](https://openhome.cc/eGossip/OpenSCAD/lib-hull_polyline3d.html) diff --git a/docs/images/lib-rounded_cube-1.JPG b/docs/images/lib-rounded_cube-1.JPG new file mode 100644 index 00000000..74601881 Binary files /dev/null and b/docs/images/lib-rounded_cube-1.JPG differ diff --git a/docs/images/lib-rounded_cube-2.JPG b/docs/images/lib-rounded_cube-2.JPG new file mode 100644 index 00000000..465d701f Binary files /dev/null and b/docs/images/lib-rounded_cube-2.JPG differ diff --git a/docs/images/lib-rounded_cube-3.JPG b/docs/images/lib-rounded_cube-3.JPG new file mode 100644 index 00000000..5c2b4117 Binary files /dev/null and b/docs/images/lib-rounded_cube-3.JPG differ diff --git a/docs/lib-rounded_cube.md b/docs/lib-rounded_cube.md new file mode 100644 index 00000000..aa645b6d --- /dev/null +++ b/docs/lib-rounded_cube.md @@ -0,0 +1,44 @@ +# rounded_cube + +Creates a cube in the first octant. When center is true, the cube is centered on the origin. + +## Parameters + +- `size` : Accepts a single value, cube with all sides this length. It also accepts 3 value array `[x, y, z]`, cube with dimensions `x`, `y` and `z`. +- `corner_r` : The corner is one-eight of a sphere. The `corner_r` parameter determines the sphere radius. +- `center` : `false` (default), 1st (positive) octant, one corner at (0,0,0). `true`, cube is centered at (0,0). +- `$fa`, `$fs`, `$fn` : Used to control the four quadrants. Check [the circle module](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_the_2D_Subsystem#circle) for more details. The final fragments of a sphere will be a multiple of 4 to fit edges. + +## Examples + + include ; + + rounded_cube(20, 5); + +![rounded_cube](images/lib-rounded_cube-1.JPG) + + include ; + + rounded_cube( + size = [50, 25, 15], + corner_r = 5, + center = true + ); + +![rounded_cube](images/lib-rounded_cube-2.JPG) + + include ; + + $fn = 8; + + rounded_cube( + size = [50, 25, 15], + corner_r = 5, + center = true + ); + +![rounded_cube](images/lib-rounded_cube-3.JPG) + + + + diff --git a/src/rounded_cube.scad b/src/rounded_cube.scad new file mode 100644 index 00000000..8b836457 --- /dev/null +++ b/src/rounded_cube.scad @@ -0,0 +1,57 @@ +/** +* rounded_cube.scad +* +* Creates a rounded cube in the first octant. +* When center is true, the cube is centered on the origin. +* +* @copyright Justin Lin, 2017 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib-rounded_cube.html +* +**/ + +module rounded_cube(size, corner_r, center = false) { + x = len(size) == undef ? size : size[0]; + y = len(size) == undef ? size : size[1]; + z = len(size) == undef ? size : size[2]; + + frags = $fn > 0 ? + ($fn >= 3 ? $fn : 3) : + max(min(360 / $fa, corner_r * 6.28318 / $fs), 5); + + remain = frags % 4; + corner_frags = (remain / 4) > 0.5 ? frags - remain + 4 : frags - remain; + edge_d = corner_r * cos(180 / corner_frags); + + half_x = x / 2; + half_y = y / 2; + half_z = z / 2; + + half_l = half_x - edge_d; + half_w = half_y - edge_d; + half_h = half_z - edge_d; + + half_cube_leng = size / 2; + half_leng = half_cube_leng - edge_d; + + translate(center ? [0, 0, 0] : [half_x, half_y, half_z]) hull() { + translate([-half_l, -half_w, half_h]) + sphere(corner_r, $fn = corner_frags); + translate([half_l, -half_w, half_h]) + sphere(corner_r, $fn = corner_frags); + translate([half_l, half_w, half_h]) + sphere(corner_r, $fn = corner_frags); + translate([-half_l, half_w, half_h]) + sphere(corner_r, $fn = corner_frags); + + translate([-half_l, -half_w, -half_h]) + sphere(corner_r, $fn = corner_frags); + translate([half_l, -half_w, -half_h]) + sphere(corner_r, $fn = corner_frags); + translate([half_l, half_w, -half_h]) + sphere(corner_r, $fn = corner_frags); + translate([-half_l, half_w, -half_h]) + sphere(corner_r, $fn = corner_frags); + } +} \ No newline at end of file