
include <BOSL2/std.scad>;
include <BOSL2/beziers.scad>;

include <Round-Anything/polyround.scad>


$fn=64;

OFFSET=0.01;

/**
 * US Box profile
 
 * @param length - Length of the fin without the tab and back space
 *
 **/
module boxProfileUS( length ,height=23, tabHeight = 8,tabLength = 20, backRounding = 8, backExtra = 10 ) {

    
    tabExtra = opp_ang_to_adj(height-tabHeight,45);
    points = [
        [ -tabLength                    , 0             ,1],            // 0
        [ length+backExtra              , 0             ,2],            // 1
        [ length+backExtra              , -height       ,backRounding], // 2
        [ tabExtra                      , -height       ,4],            // 3
        [ 0                             , -tabHeight    ,4],            // 4
        [ -tabLength                    , -tabHeight    ,1]             // 5
    ];
    polygon(
        polyRound(points)
    );
}    

function profileUS( length,height=23,tabHeight = 8,tabLength = 20, backRounding = 8, backExtra = 10 ) = polyRound([
        [ -tabLength                    , 0             ,1],            // 0
        [ length+backExtra              , 0             ,2],            // 1
        [ length+backExtra              , -height       ,backRounding], // 2
        [ opp_ang_to_adj(height-tabHeight,45) , -height ,4],            // 3
        [ 0                             , -tabHeight    ,4],            // 4
        [ -tabLength                    , -tabHeight    ,1]             // 5
    ]);


module usBox( length, height=23, thickness = 9, tabLength = 20,pinDiameter = 5,pinInset = 8, backExtra = 10,cutReduction = 0.5,screw_diameter=4.5,drill = true, color="Grey" ) {
    color(color) mirror_copy([0,0,1], offset=-OFFSET) difference() {
        linear_extrude ( height = thickness/2 ) boxProfileUS( length, tabLength = tabLength, height=height, backExtra=backExtra );
        // Thickness cut
        translate([length + backExtra + OFFSET,-OFFSET,thickness/2-cutReduction+OFFSET])
            thickness_cut( cutReduction , height );
        // Pin drill
        if (drill) translate([length+backExtra-pinInset,-height+pinInset,0]) color("Red")
            cyl(h=thickness*2,d=pinDiameter,$fn=32);
            
        // Screw Cut
        if (drill) translate ([-tabLength/2,-height/2+OFFSET,0]) color ("Red") cyl( h=height, d=screw_diameter, orient=FRONT );    
    }
}


module usBoxMold( length, height=23, thickness = 9, moldThickness = 4,tabLength = 20,part = "bottom",skirtAngle=80 ) {

    moldHeight = thickness + 2 * moldThickness;
    skirtHeight = 3 * thickness;
    deviation = opp_ang_to_adj (skirtHeight,skirtAngle);    // Skirt devitation
    
    baseInt = profileUS (length);
    baseExt = offset(baseInt,delta=moldThickness            , chamfer=false, same_length=true );   
    topInt  = offset(baseInt,delta=deviation                , chamfer=false, same_length=true );    
    topExt  = offset(baseInt,delta=deviation+moldThickness  , chamfer=false, same_length=true );
    
    union() {
        difference() {
            // Mold
            {
                if ( part == "bottom")
                    down (moldHeight/2) linear_extrude(moldHeight)    
                        offset(r=moldThickness) 
                            boxProfileUS( length, height );
                if ( part == "top") {
                    //baseInt = profileUS (length);
                    //baseInt = profileUS (length);
                    //topInt  = offset(baseInt,delta=-deviation,chamfer=false,same_length=true);
                    baseInt2  = offset(baseInt,delta=-moldThickness,chamfer=false,same_length=true);
                    topInt2  = offset(baseInt,delta=deviation-moldThickness,chamfer=false,same_length=true);
                    
                    difference(){
                        skin([baseInt   , topInt    ],z=[0,skirtHeight],slices=0);
                        skin([baseInt2  , topInt2   ],z=[thickness/2,skirtHeight+OFFSET],slices=0);
                    }
                    
                    /*    
                    down (moldHeight/2) linear_extrude(moldHeight)    
                        offset(r=moldThickness) 
                            boxProfileUS( length, height );                            
                    */        
                }                    
            }        
            // US Box to subtract
            usBox(length,height,drill=false,color="Red");

            // Mask Top  
            verticalMask = 
                part == "bottom" ? 0 : 
                part == "top"    ? moldHeight : 
                100;        
            down( verticalMask )
                linear_extrude( moldHeight )    
                    offset( r = moldThickness*2 ) 
                        boxProfileUS( length, height );                    
        }
        
        if ( part == "bottom") {
   // offset is a function
            
            //topInt  = offset(baseInt,delta=deviation,chamfer=false,same_length=true);
            showSkirt = true;
            if (showSkirt) difference(){
                skin([baseExt,topExt],z=[0,skirtHeight],slices=0);
                skin([baseInt,topInt],z=[0,skirtHeight+OFFSET],slices=0);
            }
        }
        if ( part == "top") {
        
        }
    }       
    
}

//test = profileUS (100);
//test2 = offset(test,30);
//region1 = 
//region([test,test2]);
//polygon(region1);



module thickness_cut(thickness,height) {
    color("Orange")
    linear_extrude(thickness) 
        polygon([
           [0           , 0         ],
           [0           , -height   ],
           [-height*2   , -height   ] 
        ]);
}




//boxProfileUS( length= 188);

//usBox( 130,drill=false );
usBoxMold( 130,part="top" );
down(50) usBoxMold( 130,part="bottom" );


