
/*
Creator : Kerem Yollu 
date : 30.06.2021
Description : 

*/

$fn=50; 
/*"
a=[0,0,10];
b=[10,0,0];
c=[10,10,4];
d=[0,10,0];
height = 5;
roundingRadious = 1;
*/


startWidht = 21; 
stopWidht = 18;
startHeight = 0.6; 
stopHeight = 0.6; 
lenght = 113; 
shortLenght = 75; 
shaftDia = 2; 
rolloverLenght = 25; 
rolloverThikcness = 0.6;
holeCount = 5; 
holeDia = 2; 
holeSpace = 7;
holeOffset = 20;
endDia= 20;
endLen = 15;

stichResess = 2; 
stichStep = 4; 
stichHole = 2.2;
stichAngle = 45;
stichWidth = 0.5;
stichGroove = 2;

buckleDia = 4; 
BuckleWidth = 1.5;

leatherThikness = 2;


//trapeziod(lenght,stopWidht,startWidht,startHeight,stopHeight);
//trapeziod(40,10,20,5,1);
module trapeziod(lenght,startWidht,stopWidht,startHeight,stopHeight)
{
    widthOffset = (startWidht-stopWidht)/2; 
    trapPoints = [
        [0,0,0], //0
        [lenght,widthOffset,0], //1
        [lenght,stopWidht + widthOffset,0], //2
        [0,startWidht,0], // 3
        [0,0,startHeight], // 4 
        [lenght,widthOffset,stopHeight], // 5
        [lenght,stopWidht + widthOffset,stopHeight], //6
        [0,startWidht,startHeight]];

    trapFaces = [
        [0,1,2,3],
        [4,5,6,7],
        [4,5,1,0],
        [5,1,2,6],
        [6,7,3,2],
        [4,7,3,0]];

    polyhedron(trapPoints,trapFaces,100);  
}          



//filledTrapeziod(lenght,startWidht,stopWidht,startHeight,stopHeight);
module filledTrapeziod(lenght,startWidht,stopWidht,startHeight,stopHeight)
{
    adj = lenght;
    opp = (startWidht - stopWidht)/2;
    alpha = atan(opp/adj);

    echo(alpha);
    polygon(points=[[0,0],[1,0],[1,1]]);

}


//translate([0,0,0])polyFull(lenght,startWidht,stopWidht,startHeight,stopHeight);
module polyFull(lenght,startWidht,stopWidht,startHeight,stopHeight)
{
    extraHeight = startHeight + stopHeight;
    extraWidth = startWidht + stopWidht; 
    widthOffset = (startWidht-stopWidht)/2; 
        trapPoints = [
            [0,0], //0
            [lenght,widthOffset], //1
            [lenght,startWidht-widthOffset], //2
            [0,startWidht]]; //3

    if(startWidht >= stopWidht)
    {
        difference()
        {
            if(startHeight >= stopHeight)
            {
                 linear_extrude(startHeight)polygon(trapPoints);
            }
            else 
            {
                linear_extrude(stopHeight)polygon(trapPoints);
            }
            translate([0,startWidht,0])rotate([90,0,0])linear_extrude(startWidht)polygon(points=[[0,startHeight],[lenght,stopHeight],[lenght,stopHeight+extraHeight],[0,startHeight+extraHeight]]);  
        }
    }
    else 
    {
        difference()
        {
            if(startHeight >= stopHeight)
            {
                 linear_extrude(startHeight)polygon(trapPoints);
            }
            else 
            {
                linear_extrude(stopHeight)polygon(trapPoints);
            } 
            translate([0,stopWidht + widthOffset,0])rotate([90,0,0])linear_extrude(stopWidht)polygon(points=[[0,startHeight],[lenght,stopHeight],[lenght,stopHeight+extraHeight],[0,startHeight+extraHeight]]);  
        }
    }
}



//whatchBandLong(lenght,startWidht,stopWidht,startHeight,stopHeight,shaftDia,rolloverLenght,rolloverThikcness,endDia,endLen,holeCount,holeDia,holeSpace,holeOffset,stichGroove);
module whatchBandLong(lenght,startWidht,stopWidht,startHeight,stopHeight,oDia,oLenght,oThinning,endDia,endLen,holeCount,holeDia,holeSpace,oleOffset,stichGroove)
{
    widthOffset = (startWidht-stopWidht)/2; 
    oLenghtDia = oLenght + (PI*(oDia/2))/2;
    lenghtWoEnding = lenght - endLen;
    alpha = atan(widthOffset/lenght);
    oStopBright = startWidht - (2*oLenghtDia*tan(alpha));
    holeStart = lenght + oLenghtDia - holeOffset; 
    roundingLen = lenght + oLenghtDia - endDia/2;
    roundingOpp = (roundingLen-oLenghtDia-endDia)/atan(alpha);


    difference()
    {
    union()
    {
        difference()
        {
            union()
            {
                translate([oLenghtDia,0,0])polyFull(lenghtWoEnding,startWidht,stopWidht,startHeight,stopHeight);
                translate([lenghtWoEnding+oLenghtDia,widthOffset,0])ending(0,endLen,stopWidht,stopHeight);
            }
            for(a=[0:holeCount]){translate([holeStart-a*holeSpace,startWidht/2,0])cylinder(d=holeDia,h=startHeight+stopHeight);}
            translate([holeStart+holeOffset-stichGroove,startWidht/2,0])cylinder(d=1,h=startHeight+stopHeight);
        }
        difference()
        {
            rotate([180,180,0])translate([-oLenghtDia,-startWidht,0])polyFull(oLenghtDia,startWidht,oStopBright,startHeight,oThinning);
            union()
            {
                translate([oLenght+(PI*(oDia/2))/4,startWidht/2,0])cylinder(d=1,h=startHeight+stopHeight);
                translate([oLenght+(PI*(oDia/2))/4,stichGroove,0])cylinder(d=1,h=startHeight+stopHeight);
                translate([oLenght+(PI*(oDia/2))/4,startWidht-stichGroove,0])cylinder(d=1,h=startHeight+stopHeight);
            }
        }
    }
    translate([oLenghtDia,0,0])stiching(5,stichGroove,lenghtWoEnding,widthOffset+stichGroove,stichStep,stichHole,stichWidth,stichAngle,startHeight);
    translate([oLenghtDia,0,0])stiching(5,startWidht-stichGroove,lenghtWoEnding,startWidht-widthOffset-stichGroove,stichStep,stichHole,stichWidth,stichAngle,startHeight);
    }
}


//stiching(10,10,112,2,stichStep,stichHole,stichWidth,stichAngle,1);
module stiching(startX,startY,stopX,stopY, space, holeLenght, holeWidth, angle, height)
{
    *hull()
    {
    translate([startX,startY,0])color("red")cylinder(d=stichWidth,h=2,center=true);
    translate([stopX,stopY,0])color("red")cylinder(d=stichWidth,h=2,center=true);
    }
    if(startY <= stopY)
    {
        alpa = atan((stopY-startY)/(stopX-startX));
        stichCount = floor((stopX-startX)/(space));
        for(a=[0:stichCount])
        {
            oppOffset = tan(alpa)*(a*space);
            translate([startX+a*space,startY+oppOffset,height/2])rotate([0,0,-angle])cube([holeWidth,holeLenght,height],center=true);
        }
    }
    else
    {
        alpa = atan((startY-stopY)/(stopX-startX));
        stichCount = floor((stopX-startX)/(space));
        for(a=[0:stichCount])
        {
            oppOffset = tan(alpa)*(a*space);
            translate([startX+a*space,startY-oppOffset,height/2])rotate([0,0,-angle])cube([holeWidth,holeLenght,height],center=true);
        }

    }
}




//ending(0,endLen,stopWidht,stopHeight);
module ending(dia, lengh, bright, height)
{
    ray = (sqrt(lengh*lengh+(bright/2)*(bright/2)))+dia; 
    translate([0,bright/2,0])//intersection()
    {
        difference()
        {
            intersection()
            {
                rotate([0,0,0])translate([0,(ray-bright/2),0])cylinder(r=ray,h=height);
                rotate([0,0,0])translate([0,-(ray-bright/2),0])cylinder(r=ray,h=height);
            }
            translate([-ray,-bright/2,0])cube([ray,bright,height]); 
        }
    }
}

%translate([-lenght+11.5,-startWidht/2,0])whatchBandShort(shortLenght,startWidht,stopWidht,startHeight,stopHeight,shaftDia,rolloverLenght,rolloverThikcness,buckleDia,BuckleWidth);
module whatchBandShort(lenght,startWidht,stopWidht,startHeight,stopHeight,oDia,oLenght,oThinning,buckleDia, BuckleWidth)
{
    widthOffset = (startWidht-stopWidht)/2; 
    oLenghtDia = oLenght+ PI*(oDia/2)/2;
    angle = atan(widthOffset/lenght);
    oStopBrightFirst = startWidht - (2*oLenghtDia*tan(angle));
    oStopBrightSecond = startWidht - 2*((lenght - oLenghtDia)*tan(angle));
    buckleDia = buckleDia +leatherThikness*2;
    difference()
    {
        union()
        {       
            translate([oLenghtDia,0,0])polyFull(lenght,startWidht,stopWidht,startHeight,stopHeight);
            rotate(a=[180,180,0])translate([-oLenghtDia,-startWidht,0])polyFull(oLenghtDia,startWidht,oStopBrightFirst,startHeight,oThinning);
            translate([lenght+oLenghtDia,widthOffset,0])polyFull(oLenghtDia,stopWidht,oStopBrightSecond,stopHeight,oThinning);
        }
        union()
        {
            translate([oLenght+(PI*(oDia/2))/4,startWidht/2,0])cylinder(d=1,h=startHeight+stopHeight);
            translate([oLenght+(PI*(oDia/2))/4,stichGroove,0])cylinder(d=1,h=startHeight+stopHeight);
            translate([oLenght+(PI*(oDia/2))/4,startWidht-stichGroove,0])cylinder(d=1,h=startHeight+stopHeight);
        }
        translate([lenght+oLenghtDia-buckleDia/2,startWidht/2-BuckleWidth/2,0])cube([buckleDia,BuckleWidth,startHeight+stopHeight]);
    }
}



//keyPolyRounded(a,b,c,d,height,roundingRadious);
module keyPolyRounded(a=[0,0,0],b=[1,0,0],c=[1,1,0],d=[0,1,0],height=1,rad=1)
{
    hull()
    {
        translate([a[0]+rad,a[1]+rad,a[2]])cylinder(r=rad, h=height);
        translate([b[0]-rad,b[1]+rad,b[2]])cylinder(r=rad, h=height);
        translate([c[0]-rad,c[1]-rad,c[2]])cylinder(r=rad, h=height);
        translate([d[0]+rad,d[1]-rad,d[2]])cylinder(r=rad, h=height);
    }
}