/*[bakje famili]*/

CHAMFER = 5.0; // mm
UNIT = 50.0; // mm
HIEGHT = 1.0; // units

/*[andere dingen]*/

OUTER_TOLLERANCE = 0.2; // [0.05:0.05:0.5]
WALL_THIKNESS = 1.2; // [0.5:0.1:5]

WIDTH = 1; // [1:1:10] 
DEPTH = 1; // [1:1:10]

LABLE_HIEGHT = 10.0; // [1.0:0.125:20.0]
//LABLE_WIDTH = floor(0.25*WIDTH) + 0.8; // [0.1:0.1:3.0]
LABLE_WIDTH = max(0.08, 0.9 - 0.1*WIDTH) * WIDTH; // [0.1:0.1:3.0]

RENDER = "bakje"; // [bakje: bakje, grid: grid, all: all]

if (RENDER == "bakje" || RENDER == "all")
	bakje();
if (RENDER == "grid" || RENDER == "all")
	grid();

module grid(){
	intersection(){
		union(){
			for (x = [ -(WIDTH)/2 : 1.0 : (WIDTH)/2 ]){
				translate([x*UNIT, 0, 0])
				rotate([0, 45, 0])
					cube([
						CHAMFER*sqrt(2),
						UNIT*DEPTH,
						CHAMFER*sqrt(2)
					], center=true);
			}
			for (y = [ -(DEPTH)/2 : 1.0 : (DEPTH)/2 ]){
				translate([0, y*UNIT, 0])
				rotate([45, 0, 0])
					cube([
						UNIT*WIDTH,
						CHAMFER*sqrt(2),
						CHAMFER*sqrt(2)
					], center=true);
			}
		}
		translate([0, 0, 5*CHAMFER])
			cube([
				UNIT*WIDTH - 2*OUTER_TOLLERANCE,
				UNIT*DEPTH - 2*OUTER_TOLLERANCE,
				10*CHAMFER
			], center=true);
	}
}

module bakje(){
	difference(){
		union(){
			for (x = [-(WIDTH-1)/2:1.0:(WIDTH-1)/2])
			for (y = [-(DEPTH-1)/2:1.0:(DEPTH-1)/2]){
				translate([
					x*UNIT,
					y*UNIT,
					0
				])
					floor();
			}
			walls();
			lable();
		}
		walls(true);
	}
}

module walls(cut=false){
	union(){
		thinkness = (cut ? 50 : 1) * WALL_THIKNESS;
		for (side = [-1,1])
		translate([
			0,
			side*(0.5*UNIT*DEPTH - ((cut) ? -0.5 : 0.5)*thinkness - OUTER_TOLLERANCE),
			0.5*UNIT*HIEGHT + (cut ? 0 : 0.5*CHAMFER)
		])
		union(){
			cube([
				UNIT*WIDTH - 2*CHAMFER - 2*OUTER_TOLLERANCE,
				(cut ? 1 : 1.001) * thinkness,
				UNIT*HIEGHT - (cut ? -CHAMFER : CHAMFER)
			], center=true);
			for (edge = [-1,1])
			translate([
				edge*(0.5*UNIT*WIDTH - 0.5*CHAMFER),
				-side*(0.5*CHAMFER - (cut ? -0.5 : 0.5)*thinkness),
				(cut ? 0 : -0.5*CHAMFER)
			])
			intersection(){
				translate([
					edge*(-OUTER_TOLLERANCE - (cut ? -0.5 : 0.5) * thinkness/sqrt(2)), 
					-side*((cut ? -0.5 : 0.5) * thinkness/sqrt(2)), 
					0
				])
				rotate([0, 0, side*edge*-45])
					cube([
						((cut) ? 15 : 1) * CHAMFER * sqrt(2),
						(cut ? 1 : 1.001) * thinkness,
						UNIT*HIEGHT + (cut ? 0.5*CHAMFER : 0)
					], center=true);

				if(!cut)
				translate([
					-edge*0.5*CHAMFER, 
					-side*(0.5*CHAMFER - OUTER_TOLLERANCE), 
					CHAMFER - 0.5*UNIT*HIEGHT])
				union(){
					translate([0, 0, 0.5*UNIT])
						cube([2*CHAMFER, 2*CHAMFER, UNIT*HIEGHT], center=true);
					intersection(){
						rotate([45, 0, 0])
							cube([UNIT*HIEGHT, CHAMFER*sqrt(2), CHAMFER*sqrt(2)], center=true);
						rotate([0, 45, 0])
							cube([CHAMFER*sqrt(2), UNIT*HIEGHT, CHAMFER*sqrt(2)], center=true);
					}
				}
			}
		}
		for (side = [-1,1])
		translate([
			side*(0.5*UNIT*WIDTH - ((cut) ? -0.5 : 0.5)*thinkness - OUTER_TOLLERANCE),
			0,
			0.5*UNIT*HIEGHT + (cut ? 0 : 0.5*CHAMFER)
		])
			cube([
				(cut ? 1 : 1.001) * thinkness,
				UNIT*DEPTH - 2*CHAMFER - 2*OUTER_TOLLERANCE,
				UNIT*HIEGHT - (cut ? -CHAMFER : CHAMFER)
			], center=true);
	}
}

module floor() {
	module chamfer(cut=false){
		thinkness = (cut) ? 5.0 : WALL_THIKNESS;
		translate([
			0, 
			0.5*UNIT - 0.5*CHAMFER, 
			0.5*CHAMFER + WALL_THIKNESS/sqrt(2)
		])
		rotate([45, 0, 0])
		translate([
			0,
			0,
			(cut) ? -0.5*(WALL_THIKNESS+thinkness) + 0.001 : 0,
		])
			cube([
				UNIT,
				CHAMFER * sqrt(2) + WALL_THIKNESS - 0.001,
				thinkness
			], center=true);
	}
	union(){
		translate([0, 0, 0.5*WALL_THIKNESS])
			cube([
				UNIT - 2*CHAMFER,
				UNIT - 2*CHAMFER,
				WALL_THIKNESS
			], center=true);
		difference(){
			union(){
				for (d = [0:90:360])
					rotate([0, 0, d])
						chamfer();
			}
			for (d = [0:90:360])
				rotate([0, 0, d])
					chamfer(true);
		}
	}
}

module lable(){
	translate([
		-0.5*WIDTH*UNIT,
		0.5*DEPTH*UNIT - OUTER_TOLLERANCE - WALL_THIKNESS,
		HIEGHT*UNIT - LABLE_HIEGHT*sqrt(2)
	])
	difference(){
		rotate([45, 0, 0])
			cube([LABLE_WIDTH*UNIT, LABLE_HIEGHT, LABLE_HIEGHT]);
		translate([-WALL_THIKNESS, -1*LABLE_HIEGHT, -WALL_THIKNESS])
			cube([LABLE_WIDTH*UNIT, 2*LABLE_HIEGHT, 0.5*LABLE_HIEGHT*sqrt(2)]);
	}
}
