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

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





/*
    Description : Standart din rail height of 7.5 mm (7p5)
        - lenght : Is the totla lenght of the rail no negative is allowed
            > Value greater than 0 [mm]

        - mounting_hole_starting_offset : is the staring offset of the forst mounting hole 
            > Value between 0 and 10 [mm]

        - tollerance : Will oversize the thickness, with and height of the bar proportionaly and has no effect on the mounting holes
            > Value greater than 0 [mm]
*/

//din_rail_7p5();
//din_rail_7p5(230, 2, 0.1);
module din_rail_7p5(lenght = 100, mounting_hole_starting_offset = 5 , tollerance=0)
{
    thickness = 1 + tollerance/2; 
    base_width = 27 + tollerance/2; 
    mount_width = 35 + tollerance/2; 
    wing_width = (mount_width - base_width)/2;
    total_height = 7.5 + tollerance/2; 
    hole_lenght = 15; 
    hole_spacing = 10; 
    hole_dia = 6.2;
    hole_ray = hole_dia/2;  
    rounding_dia = 1.6 - tollerance/2; 
    rounding_ray = rounding_dia/2; 


    assert(mounting_hole_starting_offset > 0);
    assert(mounting_hole_starting_offset <= 10);

    // Difference of outer roundings with the main body
    difference()
    {
        // Union of the main body with the wings to be able to substract outer roundings 
        union()
        {
            translate([0,wing_width,0])
            {
                // Inner roundings for base
                difference()
                {
                //Main cube for the base dimention without the wings
                cube(size=([lenght,base_width,total_height]));
                
                    //Union for the inside substraction with the roundin diameters. 
                    union()
                    {
                        //Fisrts cube with total width with 2 thinkness and roundin rayon thinner  
                        translate([van,thickness+rounding_ray,thickness])
                            cube(size=([lenght+va,base_width-2*thickness-rounding_dia,total_height-thickness-rounding_ray]));

                        //Fisrts cube with total width with 2 thinkness thinner and Z offset or rounding rayon
                        translate([van,thickness,thickness+rounding_ray])
                            cube(size=([lenght+va,base_width-2*thickness,total_height-thickness-rounding_ray+va]));

                        //Inside rounding 1 near origin
                        translate([van,rounding_ray+thickness,rounding_ray+thickness])
                            rotate([0,90,0])
                                cylinder(d=rounding_dia, h=lenght+va);
                                    
                        //Inside rounding 2 with y offset 
                        translate([van,base_width-rounding_ray-thickness,rounding_ray+thickness])
                            rotate([0,90,0])
                                cylinder(d=rounding_dia, h=lenght+va);
                    }
                }
            } 

            // Inner roundings for wings
            difference()
            {
                // wings
                union()
                {
                    // Wing 1 near ereference
                    translate([0,0,total_height-thickness])
                        cube(size=([lenght,wing_width,thickness])); 
                    
                    // Wing filling 1 for rounting extrusion near ereference
                    translate([0,wing_width-rounding_ray,total_height-thickness-rounding_ray])
                        cube(size=([lenght,rounding_ray,rounding_ray]));
                    
                    // Wing 2 Y offset
                    translate([0,mount_width-wing_width,total_height-thickness])
                        cube(size=([lenght,wing_width,thickness]));   
                    
                    // Wing filling 2 for rounting extrusion near ereference
                    translate([0,mount_width-wing_width,total_height-thickness-rounding_ray])
                        cube(size=([lenght,rounding_ray,rounding_ray]));  
                }

                // Rounding 1 for the underside of wing 1
                translate([van,wing_width-rounding_ray,total_height-rounding_ray-thickness])
                    rotate([0,90,0])
                        cylinder(d=rounding_dia, h=lenght+va);

                // Rounding 2 for the underside of wing 2 (Y offset)
                translate([van,mount_width-wing_width+rounding_ray,total_height-rounding_ray-thickness])
                    rotate([0,90,0])
                        cylinder(d=rounding_dia, h=lenght+va);
            }
        }
    
        // Union of all outer roundings.
        union()
        {
            // Rounding 3 for the upperderside of wing 1
            translate([van,wing_width-rounding_ray,total_height - (rounding_ray+thickness)])
            {
                difference()
                {   
                    cube(size=([lenght+va,rounding_ray+thickness+va,rounding_ray+thickness+va])); 
                    
                    translate([-va,0,0])
                        rotate([0,90,0])
                            cylinder(d=rounding_dia+2*thickness, h=lenght+2*va);
                }
            }
            
            // Rounding 4 for the outer side of base near reference
            translate([van,rounding_ray+thickness+wing_width,rounding_ray+thickness])
                rotate([180,0,0])  
                {
                    difference()
                    {   
                        cube(size=([lenght+va,rounding_ray+thickness+va,rounding_ray+thickness+va])); 
                    
                        translate([-va,0,0])
                            rotate([0,90,0])
                                cylinder(d=rounding_dia+2*thickness, h=lenght+2*va);
                    }
                }

            // Rounding 5 for the upperderside of wing 2
            translate([van,base_width+wing_width+rounding_ray ,total_height - (rounding_ray+thickness)])
                rotate([90,0,0])
                {
                    difference()
                    {   
                        cube(size=([lenght+va,rounding_ray+thickness+va,rounding_ray+thickness+va])); 
                    
                        translate([-va,0,0])
                            rotate([0,90,0])
                                cylinder(d=rounding_dia+2*thickness, h=lenght+2*va);
                    }
                }

            // Rounding 6 for the outer side of base with Y offset
            translate([van,base_width+wing_width-(rounding_ray+thickness),rounding_ray+thickness])
                rotate([270,0,0])
            {
                difference()
                {   
                    cube(size=([lenght+va,rounding_ray+thickness+va,rounding_ray+thickness+va])); 
                
                    translate([-va,0,0])
                        rotate([0,90,0])
                            cylinder(d=rounding_dia+2*thickness, h=lenght+2*va);
                }
            }
        }

        //Mounting holes 
        translate([mounting_hole_starting_offset, wing_width + base_width/2, 0])
        {   
            // repeating the mouting hole count depending of the lenght of the rail
            for (i = [0:(lenght/(hole_lenght+hole_spacing))])
            {
                translate([i*(hole_lenght+hole_spacing), 0, 0])
                    hull()
                {
                    translate([hole_ray,0,van])
                        cylinder(d=hole_dia, h=thickness+va);

                    translate([hole_lenght-hole_ray,0,van])
                        cylinder(d=hole_dia, h=thickness+va);
                }
            }
        }
    }
}





module din_mount_wago_style(thickness = 1)
{

}