Refactor box generator & add more parameters.
This commit is contained in:
parent
ec51898f0b
commit
ecf7566467
242
2d/box/box.scad
242
2d/box/box.scad
|
|
@ -1,27 +1,40 @@
|
||||||
mode = "project"; // default
|
mode = "2D"; // default
|
||||||
//mode = "parts";
|
//mode = "layout";
|
||||||
//mode = "assembly";
|
//mode = "assembly";
|
||||||
|
|
||||||
thickness = 3.25; // 3.25mm = 1/8"
|
|
||||||
spacing = 3.25;
|
|
||||||
|
|
||||||
outer_width = 52*0.75;
|
|
||||||
outer_height = 52*3*0.75;
|
|
||||||
outer_depth = 52*1.333*0.75;
|
|
||||||
|
|
||||||
golden_ratio = 1.61803398875;
|
golden_ratio = 1.61803398875;
|
||||||
notch_size = golden_ratio*thickness;
|
inch = 25.4/*mm*/;
|
||||||
notches_w = round((outer_width - 6*thickness) / (2*notch_size));
|
|
||||||
notches_h = round((outer_height - 6*thickness) / (2*notch_size));
|
|
||||||
notches_d = round((outer_depth - 6*thickness) / (2*notch_size));
|
|
||||||
|
|
||||||
module label(label_text) {
|
thickness = 3.25; // 3.25mm = 1/8" medium draftboard
|
||||||
if (mode != "project") text(label_text, halign="center", valign="center");
|
spacing = thickness;
|
||||||
}
|
explode = $t*(5*thickness);
|
||||||
|
|
||||||
|
outer_width = 5*inch;
|
||||||
|
outer_depth = outer_width/golden_ratio;
|
||||||
|
outer_height = outer_depth/golden_ratio;
|
||||||
|
|
||||||
|
inc_top = true;
|
||||||
|
inc_bottom = true;
|
||||||
|
inc_left = true;
|
||||||
|
inc_right = true;
|
||||||
|
inc_front = true;
|
||||||
|
inc_back = true;
|
||||||
|
|
||||||
|
notch_margin = 3*thickness;
|
||||||
|
notch_size = golden_ratio*thickness;
|
||||||
|
|
||||||
|
notches_w = round((outer_width - 2*notch_margin) / (2*notch_size));
|
||||||
|
notches_h = round((outer_height - 2*notch_margin) / (2*notch_size));
|
||||||
|
notches_d = round((outer_depth - 2*notch_margin) / (2*notch_size));
|
||||||
|
|
||||||
|
inc_labels = mode == "layout" || mode == "assembly";
|
||||||
|
label_size = min(outer_width, min(outer_depth, outer_height)) / 5;
|
||||||
|
|
||||||
// 2D notch pattern for an edge from [-width/2,0] to [width/2,0] in the XY plane with a +Y normal.
|
// 2D notch pattern for an edge from [-width/2,0] to [width/2,0] in the XY plane with a +Y normal.
|
||||||
// Meant to be subtracted from the shape with difference().
|
// Meant to be subtracted from the shape with difference().
|
||||||
module notches_neg(n, width, depth=thickness) {
|
module notches(n, width, depth=thickness) {
|
||||||
|
// The side with notches cut out and the remainder flush.
|
||||||
|
module notches_neg(n, width, depth=thickness) {
|
||||||
// n notches + n-1 spaces
|
// n notches + n-1 spaces
|
||||||
notch_width = width / (2*n - 1);
|
notch_width = width / (2*n - 1);
|
||||||
space_width = notch_width;
|
space_width = notch_width;
|
||||||
|
|
@ -29,121 +42,166 @@ module notches_neg(n, width, depth=thickness) {
|
||||||
translate([-width/2 + i*notch_width + i*space_width + notch_width/2, -depth/2, 0])
|
translate([-width/2 + i*notch_width + i*space_width + notch_width/2, -depth/2, 0])
|
||||||
square([notch_width, depth], center=true);
|
square([notch_width, depth], center=true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The inverse of notches_neg() for the same parameters, for the mating edge.
|
// The inverse of notches_neg() for the same parameters, for the mating edge.
|
||||||
module notches_pos(n, width, depth=thickness) {
|
module notches_pos(n, width, depth=thickness, extra_width=10000) {
|
||||||
// n spaces + n-1 notches
|
// n spaces + n-1 notches
|
||||||
space_width = width / (2*n - 1);
|
space_width = width / (2*n - 1);
|
||||||
notch_width = space_width;
|
notch_width = space_width;
|
||||||
translate([-1e6/2 - width/2, -depth/2, 0]) square([1e6, depth], center=true);
|
translate([-extra_width/2 - width/2, -depth/2, 0]) square([extra_width, depth], center=true);
|
||||||
for (i = [0 : n-1]) {
|
for (i = [0 : n-1]) {
|
||||||
translate([-width/2 + (i+1)*space_width + i*notch_width + notch_width/2, -depth/2, 0])
|
translate([-width/2 + (i+1)*space_width + i*notch_width + notch_width/2, -depth/2, 0])
|
||||||
square([notch_width, depth], center=true);
|
square([notch_width, depth], center=true);
|
||||||
}
|
}
|
||||||
translate([10000/2 + width/2, -depth/2, 0]) square([10000, depth], center=true);
|
translate([extra_width/2 + width/2, -depth/2, 0]) square([extra_width, depth], center=true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n < 0) notches_neg(-n, width, depth);
|
||||||
|
else if (n > 0) notches_pos( n, width, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module panel(label_text, xsize, ysize, yp, xn, yn, xp) {
|
||||||
|
difference() {
|
||||||
|
square([xsize, ysize], center=true);
|
||||||
|
translate([0,ysize/2+0.005,0]) rotate([0,0,0]) notches(yp, xsize - 2*notch_margin);
|
||||||
|
translate([-xsize/2-0.005,0,0]) rotate([0,0,90]) notches(xn, ysize - 2*notch_margin);
|
||||||
|
translate([0,-ysize/2-0.005,0]) rotate([0,0,180]) notches(yn, xsize - 2*notch_margin);
|
||||||
|
translate([xsize/2+0.005,0,0]) rotate([0,0,270]) notches(xp, ysize - 2*notch_margin);
|
||||||
|
children();
|
||||||
|
if (inc_labels) text(label_text, size=label_size, halign="center", valign="center");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module top_cutouts() {}
|
||||||
module top_side() {
|
module top_side() {
|
||||||
difference() {
|
panel("Top", outer_width, outer_depth,
|
||||||
square([outer_width, outer_depth], center=true);
|
inc_back ? -notches_w : 0,
|
||||||
translate([0,outer_depth/2+0.005,0]) rotate([0,0,0]) notches_neg(notches_w, outer_width - 6*thickness);
|
inc_left ? -notches_d : 0,
|
||||||
translate([-outer_width/2-0.005,0,0]) rotate([0,0,90]) notches_neg(notches_d, outer_depth - 6*thickness);
|
inc_front ? -notches_w : 0,
|
||||||
translate([0,-outer_depth/2-0.005,0]) rotate([0,0,180]) notches_neg(notches_w, outer_width - 6*thickness);
|
inc_right ? -notches_d : 0) {
|
||||||
translate([outer_width/2+0.005,0,0]) rotate([0,0,270]) notches_neg(notches_d, outer_depth - 6*thickness);
|
top_cutouts();
|
||||||
label("Top");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module bottom_cutouts() {}
|
||||||
module bottom_side() {
|
module bottom_side() {
|
||||||
difference() {
|
panel("Bottom", outer_width, outer_depth,
|
||||||
square([outer_width, outer_depth], center=true);
|
inc_front ? -notches_w : 0,
|
||||||
translate([0,outer_depth/2+0.005,0]) rotate([0,0,0]) notches_neg(notches_w, outer_width - 6*thickness);
|
inc_left ? -notches_d : 0,
|
||||||
translate([-outer_width/2-0.005,0,0]) rotate([0,0,90]) notches_neg(notches_d, outer_depth - 6*thickness);
|
inc_back ? -notches_w : 0,
|
||||||
translate([0,-outer_depth/2-0.005,0]) rotate([0,0,180]) notches_neg(notches_w, outer_width - 6*thickness);
|
inc_right ? -notches_d : 0) {
|
||||||
translate([outer_width/2+0.005,0,0]) rotate([0,0,270]) notches_neg(notches_d, outer_depth - 6*thickness);
|
bottom_cutouts();
|
||||||
label("Bottom");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module left_cutouts() {}
|
||||||
module left_side() {
|
module left_side() {
|
||||||
difference() {
|
panel("Left", outer_depth, outer_height,
|
||||||
square([outer_height, outer_depth], center=true);
|
inc_top ? notches_d : 0,
|
||||||
translate([0,outer_depth/2+0.005,0]) rotate([0,0,0]) notches_neg(notches_h, outer_height - 6*thickness);
|
inc_back ? -notches_h : 0,
|
||||||
translate([-outer_height/2-0.005,0,0]) rotate([0,0,90]) notches_pos(notches_d, outer_depth - 6*thickness);
|
inc_bottom ? notches_d : 0,
|
||||||
translate([0,-outer_depth/2-0.005,0]) rotate([0,0,180]) notches_neg(notches_h, outer_height - 6*thickness);
|
inc_front ? -notches_h : 0) {
|
||||||
translate([outer_height/2+0.005,0,0]) rotate([0,0,270]) notches_pos(notches_d, outer_depth - 6*thickness);
|
left_cutouts();
|
||||||
label("Left");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module right_cutouts() {}
|
||||||
module right_side() {
|
module right_side() {
|
||||||
difference() {
|
panel("Right", outer_depth, outer_height,
|
||||||
square([outer_height, outer_depth], center=true);
|
inc_top ? notches_d : 0,
|
||||||
translate([0,outer_depth/2+0.005,0]) rotate([0,0,0]) notches_neg(notches_h, outer_height - 6*thickness);
|
inc_front ? -notches_h : 0,
|
||||||
translate([-outer_height/2-0.005,0,0]) rotate([0,0,90]) notches_pos(notches_d, outer_depth - 6*thickness);
|
inc_bottom ? notches_d : 0,
|
||||||
translate([0,-outer_depth/2-0.005,0]) rotate([0,0,180]) notches_neg(notches_h, outer_height - 6*thickness);
|
inc_back ? -notches_h : 0) {
|
||||||
translate([outer_height/2+0.005,0,0]) rotate([0,0,270]) notches_pos(notches_d, outer_depth - 6*thickness);
|
right_cutouts();
|
||||||
label("Right");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module front_cutouts() {}
|
||||||
module front_side() {
|
module front_side() {
|
||||||
difference() {
|
panel("Front", outer_width, outer_height,
|
||||||
square([outer_width, outer_height], center=true);
|
inc_top ? notches_w : 0,
|
||||||
translate([0,outer_height/2+0.005,0]) rotate([0,0,0]) notches_pos(notches_w, outer_width - 6*thickness);
|
inc_left ? notches_h : 0,
|
||||||
translate([-outer_width/2-0.005,0,0]) rotate([0,0,90]) notches_pos(notches_h, outer_height - 6*thickness);
|
inc_bottom ? notches_w : 0,
|
||||||
translate([0,-outer_height/2-0.005,0]) rotate([0,0,180]) notches_pos(notches_w, outer_width - 6*thickness);
|
inc_right ? notches_h : 0) {
|
||||||
translate([outer_width/2+0.005,0,0]) rotate([0,0,270]) notches_pos(notches_h, outer_height - 6*thickness);
|
front_cutouts();
|
||||||
label("Front");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module back_cutouts() {}
|
||||||
module back_side() {
|
module back_side() {
|
||||||
difference() {
|
panel("Back", outer_width, outer_height,
|
||||||
square([outer_width, outer_height], center=true);
|
inc_top ? notches_w : 0,
|
||||||
translate([0,outer_height/2+0.005,0]) rotate([0,0,0]) notches_pos(notches_w, outer_width - 6*thickness);
|
inc_right ? notches_h : 0,
|
||||||
translate([-outer_width/2-0.005,0,0]) rotate([0,0,90]) notches_pos(notches_h, outer_height - 6*thickness);
|
inc_bottom ? notches_w : 0,
|
||||||
translate([0,-outer_height/2-0.005,0]) rotate([0,0,180]) notches_pos(notches_w, outer_width - 6*thickness);
|
inc_left ? notches_h : 0) {
|
||||||
translate([outer_width/2+0.005,0,0]) rotate([0,0,270]) notches_pos(notches_h, outer_height - 6*thickness);
|
back_cutouts();
|
||||||
label("Back");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Optimized for height > depth > width.
|
||||||
|
module part_layout_hdw() {
|
||||||
|
if (inc_front) front_side();
|
||||||
|
translate([-(outer_width/2 + spacing + outer_depth/2), 0, 0]) {
|
||||||
|
if (inc_left) left_side();
|
||||||
|
translate([0, -(outer_height/2 + spacing + outer_width/2), 0]) {
|
||||||
|
if (inc_bottom) rotate([0,0,270]) bottom_side();
|
||||||
|
}
|
||||||
|
translate([-(outer_depth/2 + spacing + outer_width/2), 0, 0]) {
|
||||||
|
if (inc_back) back_side();
|
||||||
|
}
|
||||||
|
translate([0, outer_height/2 + spacing + outer_width/2, 0]) {
|
||||||
|
if (inc_top) rotate([0,0,90]) top_side();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate([outer_width/2 + spacing + outer_depth/2, 0, 0]) {
|
||||||
|
if (inc_right) right_side();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optimized for width > depth > height.
|
||||||
|
module part_layout_wdh() {
|
||||||
|
if (inc_front) front_side();
|
||||||
|
translate([0, outer_height/2 + spacing + outer_depth/2, 0]) {
|
||||||
|
if (inc_top) top_side();
|
||||||
|
translate([-(outer_width/2 + spacing + outer_height/2), 0, 0]) {
|
||||||
|
if (inc_left) rotate([0,0,270]) left_side();
|
||||||
|
}
|
||||||
|
translate([0, outer_depth/2 + spacing + outer_height/2, 0]) {
|
||||||
|
if (inc_back) rotate([0,0,180]) back_side();
|
||||||
|
}
|
||||||
|
translate([outer_width/2 + spacing + outer_height/2, 0, 0]) {
|
||||||
|
if (inc_right) rotate([0,0,90]) right_side();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate([0, -(outer_height/2 + spacing + outer_depth/2), 0]) {
|
||||||
|
if (inc_bottom) bottom_side();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module part_layout() {
|
||||||
|
if (outer_width > outer_height)
|
||||||
|
part_layout_wdh();
|
||||||
|
else
|
||||||
|
part_layout_hdw();
|
||||||
|
}
|
||||||
|
|
||||||
module thicken() {
|
module thicken() {
|
||||||
linear_extrude(height=thickness, center=true, convexity=10) children();
|
linear_extrude(height=thickness, center=true, convexity=10) children();
|
||||||
}
|
}
|
||||||
|
|
||||||
module std_part_layout() {
|
|
||||||
translate([0,0,0]) top_side();
|
|
||||||
translate([-(outer_width/2 + outer_height/2 + spacing), 0, 0]) left_side();
|
|
||||||
translate([ (outer_width/2 + outer_height/2 + spacing), 0, 0]) right_side();
|
|
||||||
translate([0, -(outer_depth/2 + outer_height/2 + spacing), 0]) front_side();
|
|
||||||
translate([0, (outer_depth/2 + outer_height/2 + spacing), 0]) back_side();
|
|
||||||
translate([0, -(outer_depth + outer_height + 2*spacing), 0]) bottom_side();
|
|
||||||
}
|
|
||||||
|
|
||||||
module part_layout() {
|
|
||||||
translate([-2*(outer_depth/2 + outer_width/2 + spacing), 0, 0]) back_side();
|
|
||||||
translate([-1*(outer_depth/2 + outer_width/2 + spacing), 0, 0]) rotate([0,0,90]) left_side();
|
|
||||||
front_side();
|
|
||||||
translate([outer_depth/2 + outer_width/2 + spacing, 0, 0]) rotate([0,0,90]) right_side();
|
|
||||||
translate([outer_depth + outer_width + 2*spacing,(outer_height-outer_depth)/2,0]) top_side();
|
|
||||||
translate([outer_depth + outer_width + 2*spacing,-(outer_height-outer_depth)/2,0]) bottom_side();
|
|
||||||
}
|
|
||||||
|
|
||||||
module part_assembly() {
|
module part_assembly() {
|
||||||
color("red") translate([0,0, outer_height/2 - thickness/2]) rotate([0,0,0]) thicken() top_side();
|
if (inc_left) color("red") translate([ -(outer_width/2 - thickness/2 + explode),0,0]) rotate([90,0,270]) thicken() left_side();
|
||||||
color("green") translate([0,0,-outer_height/2 + thickness/2]) rotate([180,0,0]) thicken() bottom_side();
|
if (inc_front) color("green") translate([0, -(outer_depth/2 - thickness/2 + explode),0]) rotate([90,0,0]) thicken() front_side();
|
||||||
color("blue") translate([-outer_width/2 + thickness/2,0,0]) rotate([0,-90,0]) thicken() left_side();
|
if (inc_right) color("blue") translate([ (outer_width/2 - thickness/2 + explode),0,0]) rotate([90,0,90]) thicken() right_side();
|
||||||
color("cyan") translate([ outer_width/2 - thickness/2,0,0]) rotate([0, 90,0]) thicken() right_side();
|
if (inc_back) color("yellow") translate([0, (outer_depth/2 - thickness/2 + explode),0]) rotate([90,0,180]) thicken() back_side();
|
||||||
color("magenta") translate([0,-outer_depth/2 + thickness/2,0]) rotate([ 90,0,0]) thicken() front_side();
|
if (inc_top) color("cyan") translate([0,0, (outer_height/2 - thickness/2 + explode)]) rotate([0,0,0]) thicken() top_side();
|
||||||
color("yellow") translate([0, outer_depth/2 - thickness/2,0]) rotate([-90,0,0]) thicken() back_side();
|
if (inc_bottom) color("magenta") translate([0,0,-(outer_height/2 - thickness/2 + explode)]) rotate([180,0,0]) thicken() bottom_side();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "project")
|
if (mode == "assembly")
|
||||||
part_layout();
|
part_assembly();
|
||||||
else if (mode == "parts")
|
else if (mode == "layout")
|
||||||
thicken() part_layout();
|
thicken() part_layout();
|
||||||
else
|
else
|
||||||
part_assembly();
|
part_layout();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue