// fount-0.0.5.scad
// an extender for 5 gal water bottles to avoid lifting.

documenting = false; // set to true to generate only a cross-section

delta = 0.01;
flange_height = 10;
plug_height = 15;
pipe_id = 21;
pipe_taper = 0.5;
tube_od = 9.6;
tube_hole_d = tube_od + 0.6; // increased from 0.1 to 0.6 because was too tight.
$fn = 256;

module tube_hole() {
  translate([0,0,-delta]) cylinder(d = tube_hole_d, h = flange_height+plug_height+2*delta);
}

module pipe_plug() {
  cylinder(d1 = pipe_id, d2 = pipe_id-pipe_taper, h = plug_height);
}

module flange_grip() {
  cylinder(r = 26.9/2, h = flange_height);
}

module tube_holder() {
  difference() {
    union() {
      flange_grip();
      translate([0,0,flange_height]) pipe_plug();

    }
    // minus:
    tube_hole();
  }
}

// ---------------------------------------------------------------------

// 5 Gal bottle neck measurement

bowser_id = 45.4 - 1.0; // measured +/- slack  changed from -1.4 to -1.0
bowser_od = 55.8 + 0.7; // reduced from +1.2 to +0.7
neck_depth = 65; // enough to get past the lower bump on the outside
post_od = bowser_id - 2;
post_id = post_od - 5; // determines stiffness of vertical holder.

module outer_neck() {
  translate([0,0,neck_depth-neck_depth]) difference() {
    cylinder(d = bowser_od + 5, h=neck_depth);
    translate([0,0,-delta]) cylinder(d = bowser_od, h=neck_depth+2*delta);
  }
}

module inner_neck() {
  difference() {
    cylinder(d = bowser_id, h=neck_depth+plug_height);
    translate([0,0,-delta]) cylinder(d1 = post_od-5, d2 = tube_hole_d, h=neck_depth+2*delta);
  }
}

module bottle_connector() {
  difference() {
    union() {
      outer_neck();
      inner_neck();
      translate([0,0,neck_depth+plug_height]) {
          // pipe-holder
          cylinder(d1 = pipe_id, d2 = pipe_id-pipe_taper, h = plug_height*4);
      }
      translate([0,0,neck_depth+plug_height]) {
        cylinder(d1=bowser_od, d2=pipe_id-pipe_taper, h=10);
      }
      translate([0,0,neck_depth]) { // top neck
        difference() {
          cylinder(d1 = bowser_od+5, d2 = bowser_od, h = plug_height);
          translate([0,0,-delta]) cylinder(d1 = bowser_od, d2=bowser_id-6, h = plug_height+2*delta);
        }
      }
    }
  // minus:
  cylinder(h = 200, d = tube_hole_d);
  }
}

module cross_sections() {
 union() {
  translate([-30,0,0]) intersection() {
    bottle_connector();
    translate([-50,0, 0]) cube([100,100,300]);
  }
  translate([30,0,0]) intersection() {
    pump_stand();
    translate([-50,0,0]) cube([100,100,300]);
  }
 }
}

module pump_stand() {
  // If your pump doesn't balance well on top of the pipe, it might help to add
  // some sort of base that rests on the pipe and offers a wider base for the
  // pump to stand on.  You may need to customise this yourself to suit your 
  // particular brand of pump.
  pump_protrusion_id = 20;
  pump_protrusion_depth = 27; // 25.2 plus a little slack
  pipe_insertion_depth = plug_height*3;
  pipe_od = 27.1;
  // This goes on top of the pipe to simulate the next of a bottle, for the pump to sit on.
  // Invert for printing
  translate([0,0,(pipe_insertion_depth+pump_protrusion_depth)]/2)
  rotate([0,180,0])
  translate([0,0,-(pipe_insertion_depth+pump_protrusion_depth)]/2)
  difference() {
    // for pump to sit on:
    union() {
      cylinder(d = bowser_id, h=pipe_insertion_depth+pump_protrusion_depth);
      // add a slight bulge to ensure a tight fit:
      translate([0,0,pipe_insertion_depth]) cylinder(d1 = bowser_id+1, d2 = bowser_id, h=pump_protrusion_depth);    
    }
    // smaller protrusion below pump:
    translate([0,0,pipe_insertion_depth-delta])
      cylinder(d = pump_protrusion_id, h=pump_protrusion_depth+delta*2);
    // for pole to fit into.  -pipe_taper to ensure tight fit near end of insertion. (most recent change, still to print and test.)
    translate([0,0,-delta])
      cylinder(d1 = pipe_od, d2 = pipe_od-pipe_taper, h=pipe_insertion_depth+delta*2);
    // make sure tube hole is there regardless of anything else:
    translate([0,0,-delta])
      cylinder(d = tube_hole_d, h=100);
    // thin out the pipe enclosure
    difference() {
      translate([0,0,-delta])
        cylinder(d = bowser_id+delta, h=pipe_insertion_depth+delta*2);
      translate([0,0,-delta])
        cylinder(d = pipe_od+5, h=pipe_insertion_depth+delta*2);
    }
  }
}

//translate([0,0,90]) tube_holder(); // no longer needed.

if (documenting) {
  cross_sections(); // for documentation only
} else {
  translate([-30,0,0]) bottle_connector();
  translate([30,0,0]) pump_stand();
}

