$fn=90;
use <threads.scad>
use <gardena.scad>

Debug=false; // true false
Schnitt=false;
Delta=0.01;

Inch=25.4;


// Anschluss 25.4x14G Aussen auf 20x14G Duschschlauch für Gartenspritze
module dusche_spritze() {
  Bohrung=12;
  TPI=14;
  SW=20;
  G_25=[25.4, TPI, 9]; // Dia, Pitch, Length
  G_20=[20, TPI, 11]; // Dia, Pitch, Length
  difference() {
    union() {
      L1=1; // Fase
      translate([0,0,0]) cylinder(d1=G_25.x-3, d2=G_25.x-1, h=L1); 
      // Gewinde Spritze
      translate([0,0,L1]) metric_thread(diameter=G_25.x, pitch=Inch/G_25.y, length=G_25.z, leadin=2, test=Debug);
      L2=5; // Sechskant SW20
      translate([0,0,G_25.z+L1]) cylinder(r=SW/sqrt(3), h=L2, $fn=6); 
      // Gewinde Duschschlauch
      translate([0,0,G_25.z+L2+L1]) metric_thread(diameter=G_20.x, pitch=Inch/G_20.y, length=G_20.z, leadin=2, test=Debug);
    }
    translate([0,0,-Delta]) cylinder(d=Bohrung, h=40);
  }
}


module halbzoll_f_gardena() {
  Bohrung=9;
  TPI=14;
  SW=20;
  G_20=[20, TPI, 11]; // Dia, Pitch, Length
  difference() {
    union() {
      L1=14; D=25;
      translate([0,0,0]) hull() {
        //cylinder(d=D, h=L1);
        Dr=3; 
        translate([0,0,L1-Dr/2]) rotate_extrude() translate([(D-Dr)/2,0]) circle(d=Dr);
        cylinder(d=D, h=Delta);
      }
      translate([0,0,L1]) gardena_nippel();
      for (i=[0:360/60:359]) rotate([0,0,i]) translate([D/2,0,2]) cylinder(d=1, h=6, $fn=4);
    }
    translate([0,0,-Delta]) union() {
      L1=7;
      metric_thread(diameter=SW+0.2, pitch=Inch/TPI, length=L1, leadin=2, internal=true, test=Debug);
      L2=4;
      translate([0,0,L1]) cylinder(d=20, h=L2);
      cylinder(d=Bohrung, h=40);
      D1=13;
      translate([0,0,L1+L2-Delta]) cylinder(d1=D1, d2=Bohrung, h=7);
    }
  }
}


module halbzoll_m_gardena() {
  Bohrung=9;
  TPI=14;
  SW=20;
  G_20=[20, TPI, 12]; // Dia, Pitch, Length
  difference() {
    union() {
      L1=5; D=25;
      // Gewinde Duschschlauch
      translate([0,0,0]) metric_thread(diameter=G_20.x, pitch=Inch/G_20.y, length=G_20.z, leadin=2, test=Debug);
      translate([0,0,G_20.z]) cylinder(r=SW/sqrt(3), h=L1, $fn=6);
      translate([0,0,G_20.z+L1]) gardena_nippel();
    }
    translate([0,0,-Delta]) union() {
      cylinder(d=Bohrung, h=45);
    }
  }
}


module gardena_f_dusche() {
  // M27 x 1.5 Innen -> M20 x 14TPI Außen
  // Griff: D32
  Raendel=12;
  difference() { 
    // Aussen
    union() {
      Da=[32, 28]; Dr=4;
      L1=11; // Aussenform zum Innengewinde
      cylinder(d=Da.x, h=L1);
      L2=12; // Konus mit Abrundung, sh. Da[von, bis]
      translate([0,0,L1]) hull() {
        cylinder(d=Da.x, h=Delta);
        translate([0,0,L2-Dr/2]) rotate_extrude() translate([(Da.y-Dr)/2,0]) circle(d=Dr);
      }
      // Aussengewinde für Duschschlauch
      translate([0,0,L1+L2]) metric_thread(diameter=20, pitch=25.4/14, length=12, leadin=2, test=Debug);
      // Rändelung zum Anfassen/Einschrauben
      for (i=[0:360/Raendel:359]) rotate([0,0,i]) translate([Da.x/2,0,2.5]) hull() {
        sphere(d=3);
        translate([0,0,6.5]) sphere(d=3);
      }
      *for (i=[0:360/90:359]) rotate([0,0,i]) translate([Da.x/2,0,1]) cylinder(d=1, h=9, $fn=4);
    }
    // Innen
    translate([0,0,-Delta]) union() {
      L1=10.5; // Innengewinde plus 0.2mm Toleranz+Spiel
      translate([0,0,0]) metric_thread(diameter=27.5, pitch=1.5, length=L1, leadin=2, internal=true, test=Debug);
      L2=9; // Konus zur Verjüngung wie aussen
      translate([0,0,L1]) cylinder(d1=27, d2=22, h=L2);
      // Zentrale "Bohrung" = Innenzwölfkant SW8, passt durch Gardenabuchse durch.
      for (i=[0,30]) rotate([0,0,i]) cylinder(r=8.05/sqrt(3), h=45, $fn=6);
      *cylinder(d=9, h=45);
    }
    // Belüftung für Harzdruck, ein Kanal pro Rändel
    for (i=[0:360/Raendel:359]) rotate([0,0,i]) rotate([0,90,0]) cylinder(d=1.5, h=20); //scale([1.5,1.5,1])
  }
}


intersection() {
  //dusche_spritze();
  //gardena_f_dusche();
  //halbzoll_f_gardena();
  halbzoll_m_gardena();
  translate([0,(Schnitt)?50:0,0]) cube(100, center=true);
}
