Skip to content

Instantly share code, notes, and snippets.

@haruo31
Created June 1, 2023 00:22
Show Gist options
  • Save haruo31/31c6a455eba8ca63ba247273764d34a8 to your computer and use it in GitHub Desktop.
Save haruo31/31c6a455eba8ca63ba247273764d34a8 to your computer and use it in GitHub Desktop.
screw_util.scad
$fa = 24;
$fs = 0.1;
thread_profiles = [
["Tr", function (P) (
let (H1 = 0.5 * P, H = P / (tan(15) * 2), _w1 = P / H * (H + H1) / 2, _w = P / H * (H - H1) / 2)
[
[[_w1 * 0.5, -H1], [_w * 0.5, 0], [_w * -0.5, 0], [_w1 * -0.5, -H1]], // ext
[[_w1 * 0.5, -H1], [_w * 0.5, 0], [_w * -0.5, 0], [_w1 * -0.5, -H1]], // int
]
)]
];
echo(thread_profiles[0][1](1.5));
function slice_points(D=8.0, P=1.5, rot=5, kind="ext", prof=thread_profiles[0][1]) = [
// See: https://files.openscad.org/documentation/manual/Other_Language_Features.html#Special_variables
let (
fn = ( $fn > 0 ?
( $fn >= 3 ? $fn : 3 ) :
ceil(max(min(360 / $fa, D * PI / $fs), 5))),
_v = (kind == "ext" ? prof(P)[0] : prof(P)[1]),
r = D / 2
)
for (n = [0 : ceil(fn * rot)])
let (_dn = n / fn, _deg = 360 * _dn)
for (p = _v)
[cos(_deg) * (p[1] + r), sin(_deg) * (p[1] + r), P * _dn + p[0]]
];
shapes = slice_points();
_connections = [
[4,5,1,0],
[5,6,2,1],
[6,7,3,2],
[7,4,0,3]
];
faces = let (_nshapes = len(shapes), _last = _nshapes - 1) [
[0, 1, 2, 3],
for (n = [0 : 4 : _nshapes - 5])
for (_v = _connections)
[ for (i = _v) n + i ],
[_last, _last - 1, _last - 2, _last - 3],
];
echo([ for (i = [0 : 7]) faces[i]]);
module screw_cylinder(D=8.0, P=1.5, rot=5, kind="ext", prof=thread_profiles[0][1]) {
let (
fn = ( $fn > 0 ?
( $fn >= 3 ? $fn : 3 ) :
ceil(max(min(360 / $fa, D * PI / $fs), 5))),
_v = (kind == "ext" ? prof(P)[0] : prof(P)[1]),
d2 = D + min([ for (p = _v) p[1] ]) * 2
)
translate([0, 0, -P / 2]) cylinder(h = P * (rot + 1), d = d2);
}
union() {
polyhedron(shapes, faces);
screw_cylinder();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment