Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenSCAD example of one way to chamfer when extruding a 2D profile
$fs=0.5;
$fa=1;
// example usage
chamfer_extrude(height=10,ch1=-3,ch2=3) {
hull() {
translate([-10,0]) circle(5, $fn=64);
translate([10,0]) circle(5, $fn=64);
}
square([10,30],center=true);
}
// linear_extrude with optional chamfer on each end
// Parameters:
// height - total extrusion length including chamfers
// ch1 - bottom chamfer
// ch2 - top chamfer
// positive chamfer shrinks inward towards its end ("normal" chamfer)
// negative expands outward towards its end
// Limitations:
// - individual children of chamfer_extrude must be convex
// - only straight extrudes with no twist or scaling supported
module chamfer_extrude(height=100, ch1=0, ch2=0) {
eps = 1/1024;
// in case chamfer*2 matches width of child object, don't make its layer zero width
off1 = ch1 > 0 ? -ch1*(1-eps) : -ch1;
off2 = ch2 > 0 ? -ch2*(1-eps) : -ch2;
h1 = abs(ch1);
h2 = abs(ch2);
assert(h1+h2 <= height);
midh = height-h1-h2;
union() for(i=[0:1:$children-1]) {
if (midh > 0) translate([0,0,h1]) linear_extrude(midh) children(i);
if (ch1 != 0) { // bottom
intersection() {
linear_extrude(h1) offset(delta=off1 > 0 ? off1 : 0) children(i);
translate([0,0,off1 > 0 ? -eps : 0]) hull() {
linear_extrude(eps) offset(delta=off1) children(i);
translate([0,0,h1]) linear_extrude(eps) children(i);
}
}
}
if (ch2!=0) translate([0,0,height-h2]) { // top
intersection() {
linear_extrude(h2) offset(delta=off2 > 0 ? off2 : 0) children(i);
translate([0,0,off2 > 0 ? 0 : -eps]) hull() {
linear_extrude(eps) children(i);
translate([0,0,h2]) linear_extrude(eps) offset(delta=off2) children(i);
}
}
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.