/*
Creator : Kerem Yollu 
date : 30.06.2021
Description : library for din rail and accessories. 
*/
	

include <box.scad>
$fn = 100; 
va = 0.02;      // Visual addition for difference
vap = va/2;     // Half of visual addition 
van = -(va/2);  // Negative half of visual addition

pbswiss_mounting_size = 7.2;

// Works perfectly !
pb_10_9_big_dia = 13.3;
pb_10_9_small_dia = 10.9;
pb_10_9_lenght = 13;
pb_10_9_extra_height = 0; 
pb_10_9_clip_offset = 1.9;
pb_10_9_clip_dia = 1.2;


pb_12_8_big_dia = 15;
pb_12_8_small_dia = 12.8;
pb_12_8_lenght = 14;
pb_12_8_extra_height = 0; 
pb_12_8_clip_offset = 2.2;
pb_12_8_clip_dia = 1.2;

pb_13_5_big_dia = 16.3;
pb_13_5_small_dia = 13.5;
pb_13_5_lenght = 15;
pb_13_5_extra_height = 0; 
pb_13_5_clip_offset = 2.6;
pb_13_5_clip_dia = 1.2;


//pb_holder(pb_10_9_big_dia,pb_10_9_small_dia,pb_10_9_lenght,pb_10_9_extra_height, pb_10_9_clip_offset, pb_10_9_clip_dia); 
//pb_holder(pb_13_5_big_dia,pb_13_5_small_dia,pb_13_5_lenght,pb_13_5_extra_height, pb_13_5_clip_offset, pb_13_5_clip_dia); 
//pb_holder(pb_12_8_big_dia,pb_12_8_small_dia,pb_12_8_lenght,pb_12_8_extra_height, pb_12_8_clip_offset, pb_12_8_clip_dia); 

module pb_holder(big_dia = 12, small_dia = 6 , distance = 10, extra_height = 0, clip_offset = 0, clip_dia = 1)
{   
    wall_thickness = 1.6; 
    
   
    hull_thickness = 1; 


    main_frame_big_x = big_dia + 2*wall_thickness;      // 2 Side walls
    main_frame_big_y = big_dia + wall_thickness-clip_offset;        // one Bottom Wall
 
    main_frame_small_x = small_dia + 2*wall_thickness;  // 2 Side walls
    main_frame_small_y = small_dia + wall_thickness-clip_offset;    // one Bottom Wall

    main_frame_y_offset_flat_bottom =(big_dia - small_dia)/2;

    chopping_square_size = main_frame_big_x + 4*wall_thickness + extra_height +pbswiss_mounting_size ;
    chopping_square_height = 4*hull_thickness; 

    latch_circle_big = clip_dia;
    latch_circle_small = (latch_circle_big / (big_dia - small_dia)) + (latch_circle_big / (big_dia - small_dia))/2; 
    latch_circle_big_y_offset = big_dia/2 + wall_thickness/2 - latch_circle_big/2 - clip_offset/2 ;
    latch_circle_small_y_offset = small_dia/2 + wall_thickness/2 - latch_circle_small/2 - clip_offset/2 ;
    
    union()
    {
        difference()
        {
            union()
            {
                difference()
                {
                    union()
                    {
                        // Main Frame
                        hull()
                        {
                            translate([0,0,distance/2+hull_thickness/2])
                                cube(size=([main_frame_big_x,main_frame_big_y,hull_thickness]),center=true);
                            translate([0,0,-distance/2+hull_thickness/2])
                                rotate([0,0,0])
                                cube(size=([main_frame_small_x,main_frame_small_y,hull_thickness]),center=true);
                        }
                        // Main Frame for flat bottom
                        hull()
                        {
                            translate([0,0,distance/2+hull_thickness/2])
                                cube(size=([main_frame_big_x,main_frame_big_y,hull_thickness]),center=true);
                            translate([0,-main_frame_y_offset_flat_bottom,-distance/2+hull_thickness/2])
                                cube(size=([main_frame_small_x,main_frame_small_y,hull_thickness]),center=true);
                        }
                    }

                    // Screw holder cylindrical Removal
                    hull()
                    {
                        translate([0,wall_thickness/2 + clip_offset ,distance/2+hull_thickness/2])
                            cylinder(d=big_dia, h=hull_thickness, center = true);

                        translate([0,wall_thickness/2 + clip_offset,-distance/2+hull_thickness/2])
                            cylinder(d=small_dia, h=hull_thickness, center = true);
                    }

                    // Top oppening for the Screw holder cylindrical Removal.  
                    hull()
                    {
                        translate([0,big_dia/2+wall_thickness/2 + clip_offset ,distance/2+hull_thickness/2])
                            cube(size=([big_dia,big_dia,hull_thickness]),center=true);
                        translate([0,small_dia/2+wall_thickness/2 + clip_offset ,-distance/2+hull_thickness/2])
                            cube(size=([small_dia,small_dia,hull_thickness]),center=true);
                    }


                    // Screw holder cylindrical Removal visual extra removal
                    translate([0,wall_thickness/2 + clip_offset,-distance/2])
                            cylinder(d=small_dia, h=hull_thickness, center = true);

                    // Top oppening for the Screw holder cylindrical Removal visual extra removal    
                    translate([0,small_dia/2+wall_thickness/2 + clip_offset,-distance/2])
                            cube(size=([small_dia,small_dia,hull_thickness]),center=true);
             
                }
                

                // Latching Rounding 1
                hull()
                {
                    translate([big_dia/2,latch_circle_big_y_offset,distance/2])
                            cylinder(d=latch_circle_big, h=0.001, center = true);

                    translate([small_dia/2,latch_circle_small_y_offset,-distance/2])
                            cylinder(d=latch_circle_small, h=0.001, center = true);
                }

                // Latching Rounding 2
                hull()
                {
                    translate([-big_dia/2,latch_circle_big_y_offset,distance/2])
                            cylinder(d=latch_circle_big, h=0.001, center = true);

                    translate([-small_dia/2,latch_circle_small_y_offset,-distance/2])
                            cylinder(d=latch_circle_small, h=0.001, center = true);
                }


                // Extra thickness addition to the botom
                translate([0,-(big_dia+wall_thickness)/2-extra_height/2+clip_offset/2,0])
                hull()
                {
                    translate([0,0,distance/2+hull_thickness/2])
                        cube(size=([main_frame_big_x,extra_height,hull_thickness]),center=true);
                    translate([0,0,-distance/2+hull_thickness/2])
                        cube(size=([main_frame_small_x,extra_height,hull_thickness]),center=true);
                }

                // Mounting screw hole
                translate([0,-(big_dia+wall_thickness)/2-pbswiss_mounting_size/2-extra_height+clip_offset/2,distance/2-pbswiss_mounting_size/2])
                difference()
                {
                    cube(size=([pbswiss_mounting_size,pbswiss_mounting_size,pbswiss_mounting_size]),center=true);
                    cylinder(d=2.6, h=pbswiss_mounting_size+va, center = true);
                }
                
            }
            // Cutting the hull the right distance big side
            translate([0,0,distance/2+chopping_square_height/2])
                cube(size=([chopping_square_size,chopping_square_size,chopping_square_height]),center=true);

            // Cutting the hull the right distance small side
            translate([0,0,-distance/2-chopping_square_height/2])
                cube(size=([chopping_square_size,chopping_square_size,chopping_square_height]),center=true);
        }
    }
}