use <./common/common.scad>;

$fn = 160;
time = 0;

L = 254;
W = 13;
H = 3;
CENTER_D = 6.39;
CENTER_H = 9.76;
SIDE_D = 3.24;
SIDE_L = 11.92;
SIDE_SPACE = 192; //SPACE BETWEEN INNER PARTS

//http://www.cartoonsupplies.com/content/acme-pegbar-plastic

module rounded_bar (arr = [50, 10, 10], fn = 60) {
	$fn = fn;
	cube([arr[0] - arr[1], arr[1], arr[2]], center = true);
	translate ([(arr[0] / 2) - (arr[1] / 2), 0, 0]) {
		cylinder(r = R(arr[1]), h = arr[2], center = true);
	}
	translate ([-(arr[0] / 2) + (arr[1] / 2), 0, 0]) {
		cylinder(r = R(arr[1]), h = arr[2], center = true);
	}
}

module pegbar_acme () {
    rounded_bar([L, W, H], $fn);
    //cube([20, W, H], center = true);
    center_peg([0, 0, (CENTER_H / 2) - .5], CENTER_D, CENTER_H);
    acme_peg([(SIDE_SPACE / 2) + (SIDE_L / 2), 0, (CENTER_H / 2) + .3 ], SIDE_D, SIDE_L, CENTER_H);
    acme_peg([-(SIDE_SPACE / 2) - (SIDE_L / 2), 0, (CENTER_H / 2) + .3 ], SIDE_D, SIDE_L, CENTER_H);
}

module center_peg (pos = [0, 0, 0], D = 6, H = 9) {
    translate(pos) {
        cylinder(r = R(D), h = H - (D / 2), center = true);
        translate([0, 0, (H / 2) - (D / 4)]) {
            difference () {
                sphere(r = R(D));
                translate([0, 0, -D]) cube([D*2, D*2, D*2], center = true);
            }
        }
    }
}

module acme_peg (pos = [0, 0, 0], D = 3, L = 11, H = 9) {
    translate(pos) {
        rounded_bar([L, D, H - (D / 2)], fn = $fn);
        translate([0, 0, (H / 2) - (D / 4)]) {
            difference () {
                union () {
                    rotate([0, 90, 0]) cylinder(r = R(D), h = L - D,  center = true);
                    translate([(L / 2) - (D / 2), 0, 0]) sphere(r = R(D));
                    translate([-(L / 2) + (D / 2), 0, 0]) sphere(r = R(D));
                }
                translate([0, 0, -L]) cube([L * 2, L * 2, L * 2], center = true);
            }
        }
    }
}

module pegbar_acme_small_printer () {
    difference () {
        pegbar_acme();
        translate([150 + 20, 0, 0]) cube([300, 300, 300], center = true);
        translate([20, 0, .75]) {
            difference (){
                cube([5, 13  + 1, 1.5], center = true);
                cube([5, 4, 10], center = true);
            }
        }
    }
    translate([20, 20, 0]) {
        difference () {
            pegbar_acme();
            translate([150 - 20, 0, 0]) cube([300, 300, 300], center = true);
            translate([-20, 0, -.75]) {
                cube([5, 13  + 1, 1.5], center = true);
                cube([5, 4, 10], center = true);
            }
        }    
    }

}

module pegbar_acme_three_piece () {
    difference () {
        pegbar_acme();
        translate([150 + (254 / 6), 0, 0]) cube([300, 300, 300], center = true);
        translate([-150 - (254 / 6), 0, 0]) cube([300, 300, 300], center = true);
        translate([(254 / 6), 0, .75]) {
            difference () {
                cube([5, 13  + 1, 1.5], center = true);
                cube([5, 4, 10], center = true);
            }
        }
        translate([-(254 / 6), 0, .75]) {
            difference () {
                cube([5, 13  + 1, 1.5], center = true);
                cube([5, 4, 10], center = true);
            }
        }
    }
    translate([(254 / 3), 20, 0]) {
        difference () {
            pegbar_acme();
            translate([150 - (254 / 6), 0, 0]) cube([300, 300, 300], center = true);
            translate([-(254 / 6), 0, -.75]) {
                cube([5, 13  + 1, 1.5], center = true);
                cube([5, 4, 10], center = true);
            }
        }    
    }
    translate([(254 / 3), -20, 0]) {
        difference () {
            pegbar_acme();
            translate([150 - (254 / 6), 0, 0]) cube([300, 300, 300], center = true);
            translate([-(254 / 6), 0, -.75]) {
                cube([5, 13  + 1, 1.5], center = true);
                cube([5, 4, 10], center = true);
            }
        }    
    }

}

PART = "acme";

if (PART == "acme") {
    pegbar_acme();
} else if (PART == "acme_small_printer") {
    pegbar_acme_small_printer();
} else if (PART == "acme_three_piece") {
    pegbar_acme_three_piece();
}

//
//
