use <threads_v2.scad>;
$fa = 1;
$fs = 0.4;

outer_radius = 30;
height_total = 35;
thickness = 2;

cap_height = 10;
corner_radius = 2;

inner_radius = outer_radius - thickness;
height_base = height_total - cap_height;
height_thread = cap_height - thickness;

thread_radius = inner_radius + (thickness / 2);


// base shape
difference() {
  //cylinder(h=height_base, r=outer_radius);
  rounded_cylinder(outer_radius, height_base, corner_radius);
  translate([0, 0, thickness])
    cylinder(h=height_base, r = inner_radius);
}
// divider

translate([0, 0, height_base/2 - 0.01])
  cube([outer_radius*2 - thickness * 2, thickness, height_base+0.01], center=true);

// divider stage 2
translate([0, 0, height_base + (height_thread/2) - 0.01])
  cube([inner_radius*2 - (thickness * 3), thickness, height_thread+0.01], center=true);

// thread and neck
translate([0, 0, height_base - 1]) {
  difference() {
    union() {
      ScrewThread( thread_radius*2, height_thread + 1, pitch=4);
      cylinder(h = 1, r=outer_radius);
    }
    translate([0, 0, -0.01])
      cylinder(h=height_thread+2, r=thread_radius - thickness*2);
  }
}

// cap
translate([70, 0, cap_height])  rotate([180, 0,0]) {
  ScrewHole(thread_radius * 2, cap_height - thickness, pitch=4)
    rotate([180, 0, 0]) translate([0, 0, -cap_height]) rounded_cylinder(outer_radius, cap_height, corner_radius);
}

// test rounded edges
//translate([-70, 0, 0]){
//  rounded_cylinder(30, height_base, 5);
//}
module rounded_cylinder(r,h,c) {
  rotate_extrude(convexity = 10)
    hull() {
      translate([r-c,c,0]) circle(c);
      translate([0,0,0]) square([r-2*c, 2*c]);
      translate([0,2*c,0]) square([r, h-2*c]);
    }
}