/* [OpenSCAD] */
$fn=90;

//* [Spielstein] */
// Form, Größe, Füllung
//Form = 0; // [0:Eckig, 1:Rund]
//Size = 1; // [0:Klein, 1:Groß]
//Hohl = 0; // [0:Nein, 1:Ja]

/* [Maße] */
// Breite bzw. Durchmesser
Breite = 20; 
// Höhe [lang, kurz] +5mm
Hoehe = [20, 30];

/* [Spielstein] */
// Loch für Magnet im Fuß
Magnet = 0; // [0:Nein, 1:Ja]

/* [Hidden] */

/////////////////////////////////////////////////////////////

translate([0,15,0]) spielstein(0,0,0);
translate([30,15,0]) spielstein(0,0,1);
translate([60,15,0]) spielstein(0,1,0);
translate([90,15,0]) spielstein(0,1,1);
translate([0,-15,0]) spielstein(1,0,0);
translate([30,-15,0]) spielstein(1,0,1);
translate([60,-15,0]) spielstein(1,1,0);
translate([90,-15,0]) spielstein(1,1,1);

/////////////////////////////////////////////////////////////

// Param: Form, Size, Hohl
module spielstein(F,S,H) {
  difference() {
    union() {
      if (F==0) {
        eckig(F,S,H);
      }
      else {
        rund(F,S,H);
      }
    }
    if (Magnet==1) {
      translate([0,0,-0.01]) union() {
        cylinder(d=3, h=5);
        translate([0,0,5]) cylinder(d1=3, d2=1, h=1.5);
      }
      rotate([90,0,0]) cylinder(d=1, h=2*Breite, center=true);
      rotate([90,0,90]) cylinder(d=1, h=2*Breite, center=true);
      translate([0,0,0.5*Hoehe[S]]) cylinder(d=1, h=2*Hoehe[S], center=true);
      // Luer-Konus fuer Spritze
      translate([0,0,0.75]) mirror([0,0,1]) cylinder(d1=4.0, d2=4.3, h=7.5); 
    }
  }
}

module eckig_umfang(hoehe) {
  Kanten_Radius = 1.5;
  hull() {
    for (i=[0:90:360]) {  
      rotate([0,0,i]) translate([ (Breite/2-Kanten_Radius), (Breite/2-Kanten_Radius), 0]) cylinder(r=Kanten_Radius, h=hoehe);
    }
  }
}
module oben(F){
  intersection(){
    translate([0,0,-20]) sphere(25);
    if (F==1) {
      translate([0,0,3]) cylinder(d=Breite, h=6, center=true);
    } 
    else {
      eckig_umfang(6);
    }
  }
}

module eckig(F,S,H) {
  difference() {
    hull() {
      translate([0,0,Hoehe[S]]) oben(F);
      eckig_umfang(2);
    }
    if (H==1) { 
      translate([0,0,Hoehe[S]+5]) cylinder(d=8, h=10, center=true);
    }
    translate([0,0,8]) for (i=[0:90:360]) {
      rotate([0,0,i]) translate([Breite/2,0,0]) rotate([90,0,0]) cylinder(d=2, h=2*Breite, center=true);
    }
  }
}

module rund(F,S,H) {
  difference() {
    hull() {
      translate([0,0,Hoehe[S]]) oben(F);
      translate([0,0,1]) cylinder(d=Breite, h=2, center=true);
    }
    if (H==1) { 
      translate([0,0,Hoehe[S]+5]) cylinder(d=8, h=10, center=true);
    }
    translate([0,0,8]) rotate_extrude() translate([Breite/2,0]) circle(d=2);
  }
}
