Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active June 21, 2022 02:14
Show Gist options
  • Save cowboy/0a536021bcf1d07b2a59e808dcddd278 to your computer and use it in GitHub Desktop.
Save cowboy/0a536021bcf1d07b2a59e808dcddd278 to your computer and use it in GitHub Desktop.
OpenSCAD utility modules
// ===============================================================
// Misc utilities for OpenSCAD
// 2022 "Cowboy" Ben Alman
//
// https://gist.github.com/cowboy/0a536021bcf1d07b2a59e808dcddd278
// ===============================================================
// Epsilon value for merging solids
eps=0.001;
eps2=2*eps;
// ================================================
// TRANSFORMS
// ================================================
module center_x(d) { center_xy(0,d) children(); }
module center_y(w) { center_xy(w,0) children(); }
module center_xy(w,d) { translate([-w/2,-d/2,0]) children(); }
module mirror_x() { for(i=[0,1]) mirror([i,0,0]) children(); }
module mirror_y() { for(i=[0,1]) mirror([0,i,0]) children(); }
module mirror_xy() { mirror_x() mirror_y() children(); }
module mirror_origin() { for(i=[0,1]) mirror([i,0,0]) mirror([0,i,0]) children(); }
module flip_to_z0(h) { rotate(180,[1,0,0]) translate([0,0,-h]) children(); }
module flip_from_z0(h) { translate([0,0,h]) rotate(180,[1,0,0]) children(); }
// ================================================
// 3D
// ================================================
module box(w,d,h) {
cube([w,d,h]);
}
// https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Primitive_Solids#polyhedron
// points numbered as in "Example 1 Using polyhedron to generate cube"
module cubic_polyhedron(points) {
polyhedron(points,[
[0,1,2,3],
[4,5,1,0],
[7,6,5,4],
[5,6,2,1],
[6,7,3,2],
[7,4,0,3]
]);
}
// Polyhedron where top and bottom are rectangles coplanar with xy plane
module cubic_polyhedron_parallel(p0, p2, z_bot, p4, p6, z_top) {
cubic_polyhedron([
[p0[0],p0[1],z_bot],
[p2[0],p0[1],z_bot],
[p2[0],p2[1],z_bot],
[p0[0],p2[1],z_bot],
[p4[0],p4[1],z_top],
[p6[0],p4[1],z_top],
[p6[0],p6[1],z_top],
[p4[0],p6[1],z_top]
]);
}
// ================================================
// COUNTERSUNK M3 SCREW
// ================================================
m3_screw_top_r=2.7;
m3_screw_top_h=1.8; //2.35;
m3_screw_shaft_r=1.6;
m3_screw_support_r=m3_screw_top_r+0.95; // adjusted to reduce infill
module m3_screw_support(screw_h=0,radius=m3_screw_support_r) {
h=max(screw_h,m3_screw_top_h);
cylinder(h,radius,radius);
}
module m3_screw_hole(screw_h=0) {
h=max(screw_h,m3_screw_top_h);
translate([0,0,-eps]) {
cylinder(m3_screw_top_h+eps2,m3_screw_top_r,m3_screw_shaft_r);
translate([0,0,m3_screw_top_h])
cylinder(h-m3_screw_top_h+eps2,m3_screw_shaft_r,m3_screw_shaft_r);
}
}
module m3_screw(height,screws=[],radius=m3_screw_support_r) {
screws_list=is_list(screws[0])?screws:[screws];
difference() {
union() {
children();
for(s=screws_list)
translate([s.x,s.y,is_undef(s.z)?0:s.z])
m3_screw_support(height,radius);
}
for(s=screws_list)
translate([s.x,s.y,is_undef(s.z)?0:s.z])
m3_screw_hole(height);
}
}
// ================================================
// M3 BRASS INSERT
// ================================================
m3_insert_inner_r=2.5;
m3_insert_outer_r=m3_insert_inner_r+2;
module m3_insert(height) {
difference() {
center_xy(m3_insert_outer_r*2,m3_insert_outer_r*2)
box(m3_insert_outer_r*2,m3_insert_outer_r*2,height);
translate([0,0,-eps])
cylinder(height+eps2,m3_insert_inner_r,m3_insert_inner_r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment