include <BOSL2/std.scad>
include <BOSL2/screws.scad>


// To jest model celownika do karabinu CZ Varmint
// Koncepcyjnie składa się z 3 części - na górze jest szyna
// Picattiny (rail), na środku jest obejma lufy (clamp), i na dole
// wystaje pałąk do przykręcenia (post)

// parameters
length = 15; // ustaw poniżej 5 do testów wymiarów

clamp_inner = 22.15;
clamp_inner_acurracy = 50;
clamp_thickness = 4;

c_s = length>5 ? 1 : 0.4; // chamfer_size używane tylko do testów
rail_height = -9.4 + clamp_inner/2 + 6.8; // -9.4 is to center the picatinny

post_thickness=15;
post_length=10;
post_slit=2;
post_chamfer = 1.665;

nut_wall_t = -0.1;

screw = "M2.5";
info = screw_info(name=screw, head="socket");
hh = struct_val(info, "head_height");
screw_head_h = post_thickness - hh - nut_wall_t;

$slop = 0.1;

top_screw = 3.8;
top_h = 5;
top_w = 6;
top_lock_hole_distance = 4;
top_lock_hole_d = 1.8;


difference() {
  // celownik
  union() {
    // obejma wokół lufy
    cyl_size = clamp_inner/2 + clamp_thickness;
    cyl(h=length, r=cyl_size, rounding=1*c_s);

    // na dole wystający trzpień do przykręcenia
    translate([(clamp_inner + post_length)/2, 0, 0])
      cuboid([post_length, post_thickness, length], rounding=c_s);
      
    // top front sights
    yrot(-90) up((clamp_inner + top_h)/2) union() {
      cuboid([length,top_w,top_h], rounding=2);
    }

  }
  // lufa, śruby oraz przecięcie
  union() {
    // lufa karabinu
    cyl(h=length+0.2, r=clamp_inner/2, $fn=clamp_inner_acurracy, rounding=-c_s);

    // przecięcie
    translate([(clamp_inner+post_length)/2 - 0.1,0,0])
      cuboid([post_length + 0.25,post_slit,length+0.2], rounding=-1);

    // miejsce na śrubę
    if (length > 5){
      for (i = [length*-1/4, length*1/4]) {
        translate([(clamp_inner + post_length)/2,0,i]) 
        rotate([90,90,0]) 
          screw_hole(spec=screw, length=screw_head_h, head="socket", thread=0, $fn=100);
        translate([(clamp_inner + post_length)/2,post_thickness/2 - nut_wall_t,i]) 
        rotate([90,90,0]) 
          nut_trap_inline(spec=screw, h=hh);
        
      }
    }
    // chamfer for slit
    translate([clamp_inner/2 - post_slit/4,0,0]) rotate([0,0,45])
      cuboid([post_slit*post_chamfer,post_slit*post_chamfer,length+2]);
      
    // top front sights
    yrot(-90) up((clamp_inner + top_h)/2 - 0.2) union() {
      left(top_lock_hole_distance/2) cyl(h=top_h + 1, d=top_lock_hole_d, $fn=12);
      right(top_lock_hole_distance/2) cyl(d=top_screw, length=top_h + 1, $fn=12);
    }
    
    // debug - wyświetl rozmiar
    if(length < 5) {
      translate([clamp_inner/2 + 2 ,post_thickness/6,length-0.5])
        linear_extrude(5) text(str(clamp_inner), 3);
    }
  }
}

// rear sight
back(50) union() {
  // slits to hold onto optic mount
  difference() {
    union() {
      left(10) cuboid([5.4,16,10]);
      right(10) cuboid([5.4, 16, 10]);
    }
    union() {
      left(10) fwd(2.1) cuboid([3.4, 12, 11]);
      right(10) fwd(2.1) cuboid([3.4, 12, 11]);
      fwd(2.7) yrot(90) cyl(d=4.5, h=100, $fn=12);
      // hole for screw head for peepsight screw
      back(12) down(1.5) yrot(90) fwd(1) up(-50) cyl(d=9, h=100, $fn=36);
    }
  }
  // rear part for sight
  back(12) down(0.5) difference() {
    down(0.5) cuboid([20,12,8]);
    down(0.5) cuboid([15, 12.1, 8.1]);
    yrot(90) fwd(1) up(50) cyl(d=3.7, h=100, $fn=12);
    yrot(90) fwd(1) up(-50) cyl(d=5.4, h=100, $fn=12);
  }
  back(16.5) down(-1.5) cuboid([20,3,3]);
  back(16.5) down(4) cuboid([20,3,2]);
}