Skip to content

Instantly share code, notes, and snippets.

@Pandapip1
Created March 31, 2024 22:14
Show Gist options
  • Save Pandapip1/f054ac8011d200fa5ce6126de84d3e63 to your computer and use it in GitHub Desktop.
Save Pandapip1/f054ac8011d200fa5ce6126de84d3e63 to your computer and use it in GitHub Desktop.
A helper to create custom mounts
// Params
height_difference = 14; // mm
top_mount_thickness = 2; // mm
top_mount_diameter = 6; // mm
top_mount_dimension_x = 85; // mm
top_mount_dimension_y = 40; // mm
bottom_mount_thickness = 2; // mm
bottom_mount_diameter = 3; //mm
bottom_mount_dimension_x = 62.5; // mm
bottom_mount_dimension_y = 62.5; // mm
// Calculations
dx = abs(top_mount_dimension_x - bottom_mount_dimension_x) / 2;
dy = abs(top_mount_dimension_y - bottom_mount_dimension_y) / 2;
d_both = sqrt(dx * dx + dy * dy);
angle = atan2(d_both, height_difference);
top_ellipse_semi_major = top_mount_diameter / 2 / cos(angle);
top_ellipse_semi_minor = top_mount_diameter / 2;
bottom_ellipse_semi_major = bottom_mount_diameter / 2 / cos(angle);
bottom_ellipse_semi_minor = bottom_mount_diameter / 2;
// Interpolate
function lerp(a, b, frac) = a * (1 - frac) + b * frac;
// Function to generate an interpolated shape between two ellipses
function interpolateEllipses(rx1, ry1, x1, y1, z1, rx2, ry2, x2, y2, z2, steps) =
let(
center1 = [x1, y1, z1],
center2 = [x2, y2, z2],
vertices = concat(
// Generate vertices for interpolated ellipses
[for (j = [0 : steps])
for (i = [0 : 359])
let(
fraction = j / steps,
rx = lerp(rx1, rx2, fraction),
ry = lerp(rx1, rx2, fraction),
x = lerp(x1, x2, fraction),
y = lerp(y1, y2, fraction),
z = lerp(z1, z2, fraction)
) [x + cos(i) * rx, y + sin(i) * ry, z]
],
[center1, center2]
),
faces = concat(
// Make the sides
[for (j = [0 : steps - 1])
for (i = [0 : 359])
[
i + (j + 1) * 360, (i + 1) % 360 + (j + 1) * 360, (i + 1) % 360 + j * 360, i + j * 360
],
],
// Cap ellipse 1
[for (i = [0 : 359])
[i, (i + 1) % 360, len(vertices) - 2]
],
// Cap ellipse 2
[for (i = [0 : 359])
[(i + 1) % 360 + 360 * steps, i + 360 * steps, len(vertices) - 1]
]
)
) [vertices, faces];
module interp_ellipses(rx1, ry1, x1, y1, z1, rx2, ry2, x2, y2, z2, steps) {
polyhedron_points_faces = interpolateEllipses(rx1, ry1, x1, y1, z1, rx2, ry2, x2, y2, z2, steps);
polyhedron(polyhedron_points_faces[0], polyhedron_points_faces[1], convexity=10);
}
interp_ellipses(bottom_ellipse_semi_minor, bottom_ellipse_semi_minor, -dx / 2, 0, - height_difference / 2, top_ellipse_semi_minor, top_ellipse_semi_minor, dx / 2, 0, height_difference / 2, 100);
translate([-dx / 2, 0, -height_difference / 2 - bottom_mount_thickness])
cylinder(h = bottom_mount_thickness, r = bottom_mount_diameter / 2, $fn = 359);
translate([dx / 2, 0, height_difference / 2])
cylinder(h = top_mount_thickness, r = top_mount_diameter / 2, $fn = 359);
translate([-dx / 2, 0, -height_difference / 2 - bottom_mount_thickness - bottom_mount_diameter])
cylinder(h = bottom_mount_diameter, r1 = 0, r2 = bottom_mount_diameter, $fn = 359);
translate([dx / 2, 0, height_difference / 2 + top_mount_thickness])
cylinder(h = top_mount_diameter, r1 = top_mount_diameter, r2 = 0, $fn = 359);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment