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 = 9; // ustaw poniżej 5 do testów wymiarów

clamp_inner = 22.1;
clamp_inner_acurracy = 90;
clamp_thickness = 2;

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=7;
post_length=9;
post_slit=1;
post_chamfer = 1.665;

nut_wall_t = -1.5;

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.9;
top_h = 5;
top_w = 6;
top_lock_hole_distance = 4;
top_lock_hole_d = 1.8;

quality = 1;
$fn=36*quality;


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, edges=TOP);
    }

  }
  // 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){
    tab = length>12?[length*-1/4, length*1/4]:[0];
      for (i = tab) {
        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=120);
        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() {
      if(length>12) {
        left(top_lock_hole_distance/2) cyl(h=top_h + 1, d=top_lock_hole_d, $fn=12*quality);
        right(top_lock_hole_distance/2) cyl(d=top_screw, length=top_h + 1, $fn=12*quality);
      } else {
        cyl(d=top_screw, length=top_h + 1, $fn=12*quality);
      }
    }
    
    // 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
r_clamp_width = 20;
r_clamp_th = 3.4;
r_clamp_t = 1.5;
r_clamp_l = 16;
r_clamp_h = 10;
r_clamp_w = r_clamp_th + 2*r_clamp_t;

r_clamp_hole_pos = 2.8;
r_clamp_hole_d = 4.7;

r_sight_pos = 12;
r_sight_pos_h = 0.5;
r_sight_screw_d1 = 3.7;
r_sight_screw_d2 = 5.4;
r_sight_screw_head_d = 9.5;

back(50) union() {
  // slits to hold onto optic mount
  difference() {
    union() {
      left(r_clamp_width/2) cuboid([r_clamp_w,r_clamp_l,r_clamp_h]);
      right(r_clamp_width/2) cuboid([r_clamp_w, r_clamp_l, r_clamp_h]);
    }
    union() {
      left(r_clamp_width/2) fwd(r_clamp_t*2) 
        cuboid([r_clamp_th, r_clamp_l, r_clamp_h+1]);
      right(r_clamp_width/2) fwd(r_clamp_t*2) 
        cuboid([r_clamp_th, r_clamp_l, r_clamp_h+1]);
      fwd(r_clamp_hole_pos) yrot(90) cyl(d=r_clamp_hole_d, h=100, $fn=36*quality);
      // hole for screw head for peepsight screw
      back(r_sight_pos) down(r_sight_pos_h) yrot(90) fwd(1) up(-50) 
        cyl(d=r_sight_screw_head_d, h=100, $fn=36*quality);
    }
  }
  // rear part for sight
  back(r_sight_pos) down(r_sight_pos_h) difference() {
    down(r_sight_pos_h) cuboid([20,12,8]);
    down(r_sight_pos_h) cuboid([15, 12.1, 8.1]);
    yrot(90) fwd(1) up(50) cyl(d=r_sight_screw_d1, h=100, $fn=12*quality);
    yrot(90) fwd(1) up(-50) cyl(d=r_sight_screw_d2, h=100, $fn=12*quality);
  }
  back(16.5) down(-1.5) cuboid([20,3,3]);
  back(16.5) down(4) cuboid([20,3,2]);
}