Skip to content

Instantly share code, notes, and snippets.

@tinkerology
Created February 17, 2020 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tinkerology/ae257c5340a33ee2f149ff3ae97d9826 to your computer and use it in GitHub Desktop.
Save tinkerology/ae257c5340a33ee2f149ff3ae97d9826 to your computer and use it in GitHub Desktop.
Create a cube with rounded sides
$fn=60;
// Answer from: https://stackoverflow.com/users/1320888/cutetare
// https://stackoverflow.com/questions/33146741/way-to-round-edges-of-objects-openscad/33289349#33289349
module roundedcube(xx, yy, height, radius) {
difference(){
cube([xx,yy,height]);
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
translate([xx,0,0])
rotate(90)
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
translate([xx,yy,0])
rotate(180)
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
translate([0,yy,0])
rotate(270)
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
}
}
// Simpler code with same result
module roundedcube2(xx, yy, height, radius)
{
translate([0,0,height/2])
hull()
{
translate([radius,radius,0])
cylinder(height,radius,radius,true);
translate([xx-radius,radius,0])
cylinder(height,radius,radius,true);
translate([xx-radius,yy-radius,0])
cylinder(height,radius,radius,true);
translate([radius,yy-radius,0])
cylinder(height,radius,radius,true);
}
}
roundedcube(20, 30, 50, 5);
translate([30,0,0])
roundedcube2(20, 30, 50, 5);
@mofosyne
Copy link

mofosyne commented Dec 27, 2023

Here's my suggestion to add the center feature that normal cube() has (Plus centerxy, since that seems to be needed often)

module roundedcube(xx, yy, height, radius, center=false, centerxy=false)
{
    translate([(center||centerxy)?-xx/2:0,(center||centerxy)?-yy/2:0,center?0:height/2])
    hull()
    {
        translate([radius,radius,0])
        cylinder(height,radius,radius,true);

        translate([xx-radius,radius,0])
        cylinder(height,radius,radius,true);

        translate([xx-radius,yy-radius,0])
        cylinder(height,radius,radius,true);

        translate([radius,yy-radius,0])
        cylinder(height,radius,radius,true);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment