Skip to content

Instantly share code, notes, and snippets.

@patniemeyer
Created January 15, 2019 17:29
Show Gist options
  • Save patniemeyer/42eaca8d740e84a9d573e69ece9090e6 to your computer and use it in GitHub Desktop.
Save patniemeyer/42eaca8d740e84a9d573e69ece9090e6 to your computer and use it in GitHub Desktop.
OpenSCAD Utils...
///
/// OpenSCAD utils
/// by Pat Niemeyer (pat@pat.net)
///
// Rounded cube fitting exactly inside a cube of the same dimensions.
//
// Demo roundedCube
//%cube(size=[s,s,h], center=true);
//roundedCube([s, s, h], 1, center=true);
//%cube(size=[s,s,h], center=false);
//roundedCube([s, s, h], 1, center=false);
//
module rounded_cube(size, radius, center=true)
{
width = size[0] - radius*2;
length = size[1] - radius*2;
height = size[2] - radius*2;
_translateIfNeeded(radius,center)
minkowski() {
cube(size=[width,length,height], center=center);
sphere(r=radius, center=center);
}
}
module _translateIfNeeded(r, center) {
if (center) {
children();
} else {
translate([r,r,r]) children();
}
}
//
// Minkowski helpers
//
// Use minkowski to add radius r outside the children
module round_outside(r=1.0) {
minkowski() {
children();
sphere(r=r);
}
}
//
// Translate helpers
//
module tx(x) { translate([x,0,0]) children(); }
module ty(y) { translate([0,y,0]) children(); }
module tz(z) { translate([0,0,z]) children(); }
module rotx(x) { rotate([x,0,0]) children(); }
module roty(y) { rotate([0,y,0]) children(); }
module rotz(z) { rotate([0,0,z]) children(); }
//
// Cut helpers
//
// cut x "left/right"
module cutx(x=0, keep="left", max=120) {
difference() {
children();
if (keep=="left") {
tx(x+max/2) cube([max,max,max], center=true);
} else {
tx(x-max/2) cube([max,max,max], center=true);
}
}
}
// cut y "front/back"
module cuty(y=0, keep="front", max=120) {
difference() {
children();
if (keep=="front") {
ty(y+max/2) cube([max,max,max], center=true);
} else {
ty(y-max/2) cube([max,max,max], center=true);
}
}
}
// cut z "top/bottom"
module cutz(z=0, keep="bottom", max=120) {
difference() {
children();
if (keep=="bottom") {
tz(z+max/2) cube([max,max,max], center=true);
} else {
tz(z-max/2) cube([max,max,max], center=true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment