/** # Parameters */ $box_inner_width = 88; $box_inner_depth = 56; $box_inner_height = 56; $box_wall_thickness = 6; // [0, 1] Higher the ratio, the bigger the bottom will be. $box_bottom_top_height_fraction = 0.2; // [0, 1] The amount the bottom and top will overlap. $overlap_fraction = 0.45; // [0, 1] The thickness of the overlap as a fraction of the wall thickness. $overlap_thickness_fraction = 0.5; // In mm, the gap between the lid and bottom in the overlap area. Increase this number if the lid is too tight, lower it if the lid is too loose. $overlap_gap = 0.25; // [0, 1] Screw scale. Lower this number if the screw is too tight and won't go through the hole. $lid_screw_scale = 0.94; $lid = true; $bottom = true; $screw = true; /** # Code */ include ; $fn = $preview ? 12 : 200; $box_outer_width = $box_inner_width + $box_wall_thickness * 2; $box_outer_depth = $box_inner_depth + $box_wall_thickness * 2; $box_outer_height = $box_inner_height + $box_wall_thickness * 2; $bottom_height = $box_inner_height * $box_bottom_top_height_fraction; $top_height = $box_inner_height * (1 - $box_bottom_top_height_fraction); $overlap_height = $box_inner_height * $overlap_fraction; $overlap_thickness = $box_wall_thickness * $overlap_thickness_fraction; /* ==== Screw ==== */ if ($screw) { scale([$lid_screw_scale, $lid_screw_scale, $lid_screw_scale]) translate([0, 0, 5]) rotate([90, 0, 0]) screw(); }; /* ==== Lid ==== */ if ($lid) { difference() { bottom(overlap_gap=$overlap_gap * -1); thread(); }; }; /* ==== Box ==== */ if ($bottom) { translate([-10, 0, $bottom_height + $top_height + 2 * $box_wall_thickness]) rotate([0, 180, 0]) difference() { color("blue") translate([$box_outer_width, 0, $bottom_height + $top_height + 2 * $box_wall_thickness]) rotate([0, 180, 0]) top(); thread(); }; }; module top() { difference() { box_half($box_inner_width, $box_inner_depth, $top_height, $box_wall_thickness); translate([-0.002, -0.002, 0]) scale([1.001, 1.001, 1.001]) translate([0, 0, $top_height + $bottom_height + $box_wall_thickness * 2]) color("red") mirror([0, 0, 1]) bottom(); }; } module bottom(overlap_gap=0) { union() { box_half($box_inner_width, $box_inner_depth, $bottom_height, $box_wall_thickness); color("#FF0") box_half( $box_inner_width + $box_wall_thickness - overlap_gap * 2, $box_inner_depth + $box_wall_thickness - overlap_gap * 2, $overlap_height + $box_wall_thickness, $overlap_thickness + overlap_gap ); }; }; module box_half($width, $depth, $height, $wall_thickness) { difference() { cube([ $width + $wall_thickness * 2, $depth + $wall_thickness * 2, $height + $wall_thickness ]); translate([$wall_thickness, $wall_thickness, $wall_thickness]) cube([$width, $depth, $height + 0.1]); }; }; $thread_z_offset = $bottom_height + $overlap_height / 2 + 2; $screw_scale = 0.5; module thread(length=$box_wall_thickness * (1/$screw_scale)) { color("pink") translate([$box_outer_width / 2, $box_wall_thickness + 1.1, $thread_z_offset]) rotate([90, 0, 0]) scale([$screw_scale, $screw_scale, $screw_scale]) trapezoidThread(length=length); } module screw() { color("orange") translate([$box_outer_width / 2, 0, $thread_z_offset]) rotate([90, 0, 0]) cylinder(h=5, d=18, $fn=6); color("red") translate([0, 0, 0]) thread(length=$box_wall_thickness * (1/$screw_scale)); }