
include <BOSL2/std.scad>

$fn=20;

showScan=0;
showAnchors=0;
showDoor=1;
showSeparateObjects=0;

// split for smaller print beds
// will be automatically moved -60mm for the
// bottom offset (0 is modeled as where the door stops)
// ~490 is the total length
splitAt = [
  490/3*1-20,
  490/3*2-20
];

// model sizes
panelThickness=4.5;

// door measurements
doorGutter=6;
doorToBolt=34;

if(showScan) {
  // scan
  // lay flat and set the bottom-right as origin
  translate([-5,110,55]) rotate([-90,0,0])
    #rotate([26.9,15,-1]) scale(450) import("scan.stl");
}
if(showAnchors) {
  // scale measurement
  /*
  translate([-270,162,0])
    cuboid([20,210,1], anchor=BOTTOM+LEFT);
  translate([-250,47,0])
    cuboid([305-20,20,1], anchor=BOTTOM+LEFT);
  translate([-250,47+210+20,0])
    cuboid([305-20,20,1], anchor=BOTTOM+LEFT);
  */

  translate([35,47+10,0])
    #cylinder(h=10, d=5, anchor=BOTTOM);
  translate([-250,47+10,0])
    #cylinder(h=10, d=5, anchor=BOTTOM);
  translate([-250,47+210+10,0])
    #cylinder(h=10, d=5, anchor=BOTTOM);
  translate([35,47+210+10,0])
    #cylinder(h=10, d=5, anchor=BOTTOM);
}
if(showDoor) {
  if (showSeparateObjects) {
    rotate([0,0,-90]) translate([-20,8,10])
      color("orange") import("limit-switch-flag-and-chain-catch-assembled.stl");
  }
}

module limitSwitchMount(){
  switchHeight = 7;
  switchWidth = 12;
  switchLength = 20;

  switchHoleFromFront = 9; // we assume "front" is the left here, adjust for right
  switchHoleFromSide = 5.5;
  switchHoleSpacing = 9;

  // switch mount
  difference(){
    cuboid([switchWidth,switchLength,switchHeight], rounding=1, anchor=FRONT+LEFT+BOTTOM);
    translate([switchHoleFromFront, switchHoleFromSide, 10/2+switchHeight/2+0.1]) {
      cylinder(h=10, r=1.2, center=true);
    }
    translate([switchHoleFromFront, switchHoleFromSide+switchHoleSpacing, 10/2+switchHeight/2+0.1]) {
      cylinder(h=10, r=1.2, center=true);
    }
  }
}

module topPanel(){
  translate([doorGutter,230,0]){
    translate([15,-2,0]) cuboid([4,145+(2*INCH),10], rounding=1, edges=["Y"], anchor=FRONT+LEFT+BOTTOM);
    difference(){
      cuboid([40,145+(2*INCH),panelThickness], rounding=1, edges=["Y",BACK], anchor=FRONT+LEFT+BOTTOM) {
        if (showSeparateObjects) {
          // servo
          translate([44,-31.5,31]) position(TOP+BACK+LEFT) rotate([0,0,90]) color("gray") import("servo futaba s3003 v6.stl");
          // top pulley
          translate([-10,-21,31]) position(TOP+BACK+LEFT) rotate([0,90,0]) color("red") import("parametric_ball_pulley-improved-with-flares.stl");
        }
        // limit switch mount
        translate([6,-12-77.5,0]) position(TOP+BACK) color("pink") rotate(90) limitSwitchMount();
        // servo mount
        translate([17.8,-42,20]) position(TOP+BACK+LEFT) rotate([0,0,90]) color("pink") import("Futaba_3001_Servo_Mount.stl");
        difference(){
          translate([0,-1,0]) position(TOP+BACK+LEFT) cuboid([40,61,19], rounding=1, anchor=BACK+LEFT+BOTTOM);
          $fn=40;
          translate([2,-21,3]) position(TOP+BACK+LEFT) rotate([0,90,0]) cyl(h=5,d=55, anchor=RIGHT);
          // servo mount holes
          /*translate([26.6,-20.1,10]) position(TOP+BACK+LEFT)
            cyl(h=10,d=2, anchor=BACK+LEFT+BOTTOM);
          translate([26.6,-41.5,10]) position(TOP+BACK+LEFT)
            cyl(h=10,d=2, anchor=BACK+LEFT+BOTTOM);*/
        }
      }
      // mounting hole
      translate([29,37,-0.1]) {
        cyl(h=panelThickness+1,d=5, anchor=BOTTOM);
        translate([0,0,panelThickness-2])
          cyl(h=3,d=15, chamfer1=0.5, anchor=BOTTOM);
        translate([7,0,0])
          cuboid([15,5,panelThickness+1], anchor=BOTTOM);
      }
    }
  }
}

module middlePanel(){
  translate([doorGutter,85,0]) {
    translate([15,0,0]) cuboid([4,145,10], rounding=1, edges=["Y"], anchor=FRONT+LEFT+BOTTOM);
    cuboid([40,145,panelThickness], rounding=1, edges=["Y"], anchor=FRONT+LEFT+BOTTOM) {
      if (showSeparateObjects) {
        //arduino
        translate([25,0,0]) color("green") position(TOP) cuboid([2*INCH,2.5*INCH,1*INCH], anchor=BOTTOM);
      }
    }
  }
}

module bottomPanel(){
  translate([doorGutter,-60,0]) {
    translate([15,2,0]) cuboid([4,145,10], rounding=1, edges=["Y"], anchor=FRONT+LEFT+BOTTOM);
    difference() {
      cuboid([40,145,panelThickness], rounding=1, edges=["Y",FRONT], anchor=FRONT+LEFT+BOTTOM) {
        if (showSeparateObjects) {
          // bottom pulley
          translate([-3,-5,27.5]) position(TOP+FRONT+LEFT) color("teal") import("LBracket_Parametric_rev9.stl");
          translate([-10,14,31]) position(TOP+FRONT+LEFT) rotate([0,90,0]) color("red") import("parametric_ball_pulley-improved-with-flares.stl");
        }
        // pulley mount
        difference(){
          translate([2,0,0]) position(TOP+FRONT+LEFT) cuboid([35,27.5,27.5], rounding=1, anchor=FRONT+LEFT+BOTTOM);
          translate([20.7,12.9,20]) position(TOP+FRONT+LEFT)
            cyl(h=10,d=2, anchor=FRONT+LEFT+BOTTOM);
          translate([30.7,12.9,20]) position(TOP+FRONT+LEFT)
            cyl(h=10,d=2, anchor=FRONT+LEFT+BOTTOM);
        }
        // limit switch mount
        translate([6,72,0]) position(TOP+FRONT+LEFT) rotate(-90) color("pink") limitSwitchMount();
      }
      // mounting hole
      translate([29,117,-0.1]) {
        cyl(h=panelThickness+1,d=5, anchor=BOTTOM);
        translate([0,0,panelThickness-2])
          cyl(h=3,d=15, chamfer1=0.5, anchor=BOTTOM);
        translate([7,0,0])
          cuboid([15,5,panelThickness+1], anchor=BOTTOM);
      }
    }
  }
}

difference(){
  union(){
  topPanel();
  middlePanel();
  bottomPanel();
  }
  for(i=[0: len(splitAt)-1]){
    translate([doorGutter+19.5,splitAt[i]-60,20]) scale([0.5,0.5,1]) partition_cut_mask(gap=0, cutpath="dovetail", $slop=0.5);
  }
}













