Created
September 4, 2021 09:16
-
-
Save thehans/52f46f87a8469f54861465bbf7035278 to your computer and use it in GitHub Desktop.
Offset Extrude and Fillet Extrude for OpenSCAD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$fs=0.5; | |
$fa=1; | |
// General offset_extrude and fillet_extrude. | |
// These use minkowski which can be incredibly slow and resource intensive, but should theoretically work on any 2d geometry. | |
// fillet_extrude is also much slower than offset_extrude, due to significantly more facets needed for rounded edges. | |
// There are some alternative methods which are much faster, | |
// ***BUT*** they have the limitation of only working for CONVEX children. | |
// chamfer_extrude (basically like offset_extrude): https://gist.github.com/thehans/072005c68e5fcef3394b8c08e37d1c35 | |
// fillet_extrude : https://gist.github.com/thehans/b47ab7077c862361eb5d8f095448b2d4 | |
//fillet_extrude(r=1, convexity=10) | |
offset_extrude(r=-1, convexity=10) | |
test_shape(); | |
module test_shape() { | |
square(5); | |
difference() { | |
circle(r=5); | |
circle(r=3); | |
} | |
} | |
module offset_extrude(r, convexity) { | |
ar = abs(r); | |
if (r>0) { | |
intersection() { | |
linear_extrude(height=ar) offset(delta=r, chamfer=false) hull() children(); | |
minkowski(convexity=convexity) { | |
rotate(45) cylinder(r1=0, r2=ar, h=ar); | |
linear_extrude(ar/2, convexity=convexity) children(); | |
} | |
} | |
} else { | |
difference() { | |
linear_extrude(height=ar, convexity=convexity) children(); | |
minkowski(convexity=convexity) { | |
rotate(45) cylinder(r1=0, r2=ar, h=ar); | |
linear_extrude(ar/2, convexity=convexity) difference() { // get negative shape | |
offset(delta=1, chamfer=false) hull() children(); | |
children(); | |
} | |
} | |
} | |
} | |
} | |
function fragments(r=1) = ($fn > 0) ? | |
($fn >= 3 ? $fn : 3) : | |
ceil(max(min(360.0 / $fa, r*2*PI / $fs), 5)); | |
module fillet_extrude(r, convexity=5) { | |
ar = abs(r); | |
if (r>0) { | |
intersection() { | |
minkowski(convexity=convexity) { | |
rotate_extrude() difference() { | |
square(ar); | |
translate([ar,0]) circle(r=ar, $fn=ceil(fragments(ar)/4)*4); | |
} | |
linear_extrude(3*ar) children(); | |
} | |
linear_extrude(height=ar) offset(delta=r, chamfer=false) hull() children(); | |
} | |
} else { | |
difference() { | |
linear_extrude(ar, convexity=convexity) children(); | |
minkowski(convexity=convexity) { | |
rotate_extrude() difference() { | |
square(ar); | |
translate([ar,0]) circle(r=ar, $fn=ceil(fragments(ar)/4)*4); | |
} | |
linear_extrude(3*ar) { | |
difference() { // get negative shape | |
offset(delta=ar, chamfer=false) hull() children(); | |
children(); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment