diff --git a/README.md b/README.md index 24e6aed4..121b4a0c 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Too many dependencies? Because OpenSCAD doesn't provide namespace management, I - 3D - [rounded_cube](https://openhome.cc/eGossip/OpenSCAD/lib-rounded_cube.html) + - [rounded_cylinder](https://openhome.cc/eGossip/OpenSCAD/lib-rounded_cylinder.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_cylinder-1.JPG b/docs/images/lib-rounded_cylinder-1.JPG new file mode 100644 index 00000000..25f4cd09 Binary files /dev/null and b/docs/images/lib-rounded_cylinder-1.JPG differ diff --git a/docs/lib-rounded_cylinder.md b/docs/lib-rounded_cylinder.md new file mode 100644 index 00000000..4824a9b3 --- /dev/null +++ b/docs/lib-rounded_cylinder.md @@ -0,0 +1,26 @@ +# rounded_cylinder + +Creates a rounded cylinder. + +## Parameters + +- `radius` : The radius of the cylinder. It also accepts a vector `[r1, r2]`. `r1` is the bottom radius and `r2` is the top radius of a cone. +- `h` : The height of the cylinder or cone. + +- `round_r` : The sphere radius which fits the edges of the bottom and the top. +- `center` : `false` (default), z ranges from 0 to h. `true`, z ranges from -h/2 to +h/2 +- `convexity` : See [Rotate Extrude](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_the_2D_Subsystem#Rotate_Extrude) for details. +- `slices` : The fragments of the rounded edge. +- `$fa`, `$fs`, `$fn` : Used to control the eight 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_cylinder( + radius = [20, 10], + h = 25, + round_r = 3 + ); + +![rounded_cylinder](images/lib-rounded_cylinder-1.JPG) \ No newline at end of file diff --git a/src/rounded_cylinder.scad b/src/rounded_cylinder.scad new file mode 100644 index 00000000..00ca3f29 --- /dev/null +++ b/src/rounded_cylinder.scad @@ -0,0 +1,54 @@ +/** +* rounded_cylinder.scad +* +* Creates a rounded cylinder. +* +* @copyright Justin Lin, 2017 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib-rounded_cylinder.html +* +**/ + +include <__private__/__is_vector.scad>; +include <__private__/__frags.scad>; + +module rounded_cylinder(radius, h, round_r, convexity = 2, center = false, slices = undef) { + is_vt = __is_vector(radius); + r1 = is_vt ? radius[0] : radius; + r2 = is_vt ? radius[1] : radius; + + frags = __frags(round_r); + + function step_a(sector_angle) = + sector_angle / (slices == undef ? frags * sector_angle / 360 : slices); + + b_ang = atan2(h, r1 - r2); + b_sector_angle = 180 - b_ang; + b_leng = r1 - round_r / tan(b_ang / 2); + + t_sector_angle = b_ang; + t_leng = r2 - round_r * tan(t_sector_angle / 2); + + translate(center ? [0, 0, -h/2] : [0, 0, 0]) rotate_extrude(convexity = convexity) + polygon( + concat( + [[0, 0], [b_leng, 0]], + [ + for(ang = [-90:step_a(b_sector_angle):-90 + b_sector_angle]) + [ + round_r * cos(ang) + b_leng, + round_r * sin(ang) + round_r + ] + ], + [ + for(ang = [90 - t_sector_angle:step_a(t_sector_angle):90]) + [ + round_r * cos(ang) + t_leng, + round_r * sin(ang) + h - round_r + ] + ], + [[t_leng, h], [0, h]] + ) + ); +} \ No newline at end of file