use <picatinny-rail.scad>

length=35;
barrel_diameter=22.2;
barrel_acurracy=30;
wall_thickness=4;
wall_acurracy=50;
slit_thickness=2;
post_thickness=15;
post_length=15;

picatinny_move=15;

hex_size=4;
screw_size=2;

difference() {
  // celownik
  union() {
    // obejma wokół lufy
    cylinder(h=length, d=barrel_diameter + wall_thickness*2, $fn=wall_acurracy);
    // na górze picatinny
    rotate([90,0,-90]) translate([0,0,picatinny_move])
      picatinny_rail(l=length, d=picatinny_move, c=1.5);
    // na dole wystające do przykręcenia
    translate([barrel_diameter/2, post_thickness*-0.5, 0])
      cube([post_length, post_thickness, length]);

  }
  // lufa, śruby oraz przecięcie
  union() {
    // lufa karabinu
    translate([0,0,-1])
    cylinder(h=length+2, d=barrel_diameter, $fn=barrel_acurracy);

    // przecięcie
    translate([0,(slit_thickness * -0.5),-1])
      cube([(post_length+barrel_diameter)*1.1,slit_thickness,length+2]);

    // miejsce na śrubę
    if (length > 5){
      translate([(barrel_diameter + post_length)/2,-slit_thickness*2,length*3/4]) 
      rotate([90,90,0]) 
      union() { 
        cylinder(r=hex_size, h=post_thickness, $fn=6);
        translate([0,0,-post_thickness+1]) cylinder(r=screw_size, h=post_thickness+1);
      }
      translate([(barrel_diameter + post_length)/2,-slit_thickness*2,length/4]) 
      rotate([90,90,0])
      union() { 
        cylinder(r=hex_size, h=post_thickness, $fn=6);
        translate([0,0,-post_thickness+1]) cylinder(r=screw_size, h=post_thickness+1);
      }
    }
  }
}