Files
phone-case/primitives.scad
2023-12-22 02:30:27 +00:00

67 lines
2.1 KiB
OpenSCAD

//! general-purpose "primitives" lacking from the OpenSCAD builtins
include <defaults.scad>
/// cylinder() but with the axis on the x axis instead of the z axis.
/// and where `center=false` behaves like for `cube(center=false)`,
/// i.e. circle center is not at (0, 0) but (r, r)
module cylinderX(d=undef, r=undef, h=undef, center=false) {
_translateIf(!center, [0, _toRad(r=r, d=d), _toRad(r=r, d=d)]) {
rotate(a=[0,-90,0]) cylinder(d=d, r=r, h=h, center=center);
}
}
/// cylinder() but with the axis on the y axis instead of the z axis
/// and where `center=false` behaves like for `cube(center=false)`,
/// i.e. circle center is not at (0, 0) but (r, r)
module cylinderY(d=undef, r=undef, h=undef, center=false) {
_translateIf(!center, [_toRad(r=r, d=d), 0, _toRad(r=r, d=d)]) {
rotate(a=[-90,0,0]) cylinder(d=d, r=r, h=h, center=center);
}
}
/// cylinder() where `center=false` behaves like for `cube(center=false)`,
/// i.e. circle center is not at (0, 0) but (r, r)
module cylinderZ(d=undef, r=undef, h=undef, center=false) {
_translateIf(!center, [_toRad(r=r, d=d), _toRad(r=r, d=d), 0]) {
cylinder(d=d, r=r, h=h, center=center);
}
}
// a 2d-cylinder extruded along the x-axis
module pillX(dimX, dimY, dimZ, tol=tol, center=false) {
diam = min(dimY, dimZ) - tol;
minkowski() {
cube([dimX/2, dimY-diam, dimZ-diam], center=center);
cylinderX(d=diam, h=dimX/2, center=center);
}
}
// a 2d-cylinder extruded along the x-axis
module pillY(dimX, dimY, dimZ, tol=tol, center=false) {
diam = min(dimX, dimZ) - tol;
minkowski() {
cube([dimX-diam, dimY/2, dimZ-diam], center=center);
cylinderY(d=diam, h=dimY/2, center=center);
}
}
// a 2d-cylinder extruded along the z-axis
module pillZ(dimX, dimY, dimZ, tol=tol, center=false) {
diam = min(dimX, dimY) - tol;
minkowski() {
cube([dimX-diam, dimY-diam, dimZ/2], center=center);
cylinderZ(d=diam, h=dimZ/2, center=center);
}
}
function _toRad(r=undef, d=undef) = (r != undef) ? r : d/2;
module _translateIf(cond, amount) {
if (cond) {
translate(amount) children();
} else {
children();
}
}