Skip to content

Instantly share code, notes, and snippets.

@reox
Last active June 2, 2021 18:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reox/92a8e7cf59fa4574cfcdb5fd3c9bfd46 to your computer and use it in GitHub Desktop.
Save reox/92a8e7cf59fa4574cfcdb5fd3c9bfd46 to your computer and use it in GitHub Desktop.
Create a bend tube in OpenSCAD. Work ONLY with openscad >=2016, as the angle parameter in rotate_extrude was introduced there.
// fine mesh
fn = 1000;
// epsilon for clean intersection
eps = 0.1;
module tube(inner=5, outer=10, angle=45, length=250){
// we want the thing in z direction, thus we rotate on x axis
// the real length of the back rotated piece
rlength = (sin(angle) * (length * 2)) / cos(angle/2);
// the dimension for x can be calculated as well (used for the intersection
r = (length*2);
s = 2 * r * sin(angle/2);
rwidth = r - 0.5*sqrt(4*r*r - s*s);
// ... rlength, rwidth give the bounding box of the tube
// so when looking at a section of a circle,
// angle gives the angle of the section and
// length is in reality half the radius of that circle.
// the actual tube is then drawn around the central axis.
//
// that means, that the resulting tube section will be
// witht the center at 0,0 and has a height of rlength in z-direction
echo("Bounding box:");
echo(x=rwidth, y=outer, z=rlength-(2*offset));
echo(x=rwidth, y=outer, z=rlength-(2*offset)+10);
offset = sin(angle/2) * outer;
trans = 10;
translate([outer+trans/2,0,5])
rotate([90, 0, 0]) {
union(){
// Because of Rotation:
// x z y
echo((cos(angle/2)*outer+outer));
echo(outer/cos(angle/2));
translate([-outer-trans, -5, -outer])
cube([2*outer/cos(angle/2)+trans, 5, 2*outer]);
translate([-outer-trans, rlength-(2*offset), -outer])
cube([2*outer/cos(angle/2)+trans, 5, 2*outer]);
intersection(){
translate([-outer-eps, 0, -outer-eps])
cube([rwidth+(2*(outer+eps)),rlength-(2*offset),(outer+eps)*2]);
// the tube structure
// beware: rotation and translation are not communtative
// move down and recenter
translate([-outer/cos(angle/2)+outer, -offset, 0])
rotate([0, 0, -angle/2])
// put into 0,0
translate([-r, 0, 0])
rotate_extrude(angle=angle, $fn=fn)
translate([r, 0, 0])
difference(){
circle(r = outer, $fn=fn);
if (inner > 0){
circle(r = inner, $fn=fn);
}
};
}
}
};
}
tube(8, 11, 45, 250);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment