include <BOSL2/std.scad>
// model 1 - palik
palik_d = 30;
palik_h = 165;
t=2.5;

conn_clear=0.175; // clearance
connect_h=15;

// model 2 - podstawka
nozki_l = 30;
nozki_t = 4;
nozki_ile = 3;

noga_h = 70;
noga_d = 7;

gora_h = 15;

dz_ile_kw = 3;
dz_ile_kol = 1;
dz_t = 3;

module column_hex(ile_h, ile_d, size, h, angle, d) {
  for(i=[0:ile_h]) {
    translate([0,0,i*h]) rotate([0,0,i*(360/ile_d/2)]) union() {
      for (j=[0:360/ile_d:360]) {
        rotate([angle,30,j]) cylinder(d1=0, d2=size, $fn=6, h=d/2);
      }
    }
  }
}

smallhex = [11,6,13.5,13.5,90];
bighex = [8,4,20,18,90];
hugehex = [5,3,25,24,90];

tab = smallhex;

union() {
  // palik z dziurami
  difference() {
    cylinder(d=palik_d, h=palik_h, $fn=360);
    down(0.1) cylinder(d=palik_d-2*t, h=palik_h+0.2, $fn=360);
    up(tab[2]/2) column_hex(tab[0],tab[1],tab[2],tab[3],tab[4], palik_d);
  }
  // cienszy element do łączenia
  rotate([0,180,0]) difference() {
    union() {
      cylinder(d=palik_d, h=5, $fn=360);
      cylinder(d=palik_d-2*t-2*conn_clear, h=connect_h, $fn=360);
    }
    down(1) cylinder(d=palik_d-4*t-2*conn_clear, h=connect_h+3, $fn=360/2);
  }
}

// podstawka
right(2*palik_d) union() {
  // nóżki
  for (i=[0:360/nozki_ile: 360]) {
    rotate([0,0,i]) translate([0,-nozki_t/2,0]) union() {
        cube([nozki_l, nozki_t, nozki_t]);
        translate([noga_d/3,nozki_t/2,nozki_t]) rotate([90,0,0])
          rounding_edge_mask(l=nozki_t, r=10);
      }
  }
  // noga
  cylinder(h=noga_h, d=noga_d, $fn=20);
  // góra
  up(noga_h - dz_t) union() {
    up (gora_h/2) difference() {
      cyl(d=palik_d, h=gora_h, $fn=360, rounding1=3);
      down(t) cyl(d=palik_d-2*t, h=gora_h+3*t, $fn=360, rounding2=-5);
    }
      // dziury + podstawka
    up(dz_t/2 + 0.2) union() {
      for(i=[0:360/dz_ile_kw:360]) {
        rotate([0,0,i]) translate([(palik_d - 2*t)/4,0,0]) 
          cube([(palik_d-2*t)/2,dz_t,dz_t], center=true);
      }
      for(i=[0:(palik_d-2*t)/dz_ile_kol: (palik_d-2*t)]) {
        down(dz_t/2) difference() {
          cylinder(d=i+0.1, h=dz_t, $fn=360);
          down(0.1) cylinder(d=i-2*dz_t, h=dz_t+0.2, $fn=360);
        }
      }
    }
  }
}