//Paramteric Flash Memory Organizer
//by mbrami
//19 DEC 2020 - V1.0

/* [General] */

//How many slots per each row (select 1 to have a single column)
slots_per_row = 6;//[1:50]

//How many slots per each column (select 1 to have a single row)
slots_per_column = 2;//[1:50]

//Slots distance [mm]
Slot_distance = 1; //[0:0.1:20]

//Slot orientation on xy plane [Deg]
Slot_orientation = 0;//[0:0.1:90]

//Outer Thickness [mm](increases margin on all sides - NOT on bottom)
Outer_thickness = 2;//[0:0.25:40]

//Fillet corners
Fillet = "YES";//[YES, NO]

//Fillet radius [mm](applies to all corners and increases margin on all sides - ALSO on bottom)
Fillet_radius = 2;//[0:0.25:40]

/* [Hidden] */

USB = [12.3, 5.1, 12.8];
SD  = [24.3, 2.4, 18];
mSD = [1.25, 11.3, 10];

slot_size = [
    max(USB.x, SD.x, mSD.x)* cos (Slot_orientation) + max(USB.y, SD.y, mSD.y)*sin(Slot_orientation) + mSD.x * sin(Slot_orientation),
    max(USB.x, SD.x, mSD.x)* sin (Slot_orientation) + max(USB.y, SD.y, mSD.y) * cos(Slot_orientation)+ mSD.x * cos(Slot_orientation),
    max(USB.z, SD.z, mSD.z)
    ];

slot_min_dist = [slot_size.x + Slot_distance, slot_size.y + Slot_distance];


echo (slot_size.x, slot_size.y, slot_size.z);
echo(-slot_min_dist);

module build(obj){
    translate([0,0,-obj.z/2])cube([obj.x, obj.y, obj.z], center = true);  
    }
    
module assemble(){
    build(USB);
    build(SD);
    build(mSD);
    translate([-SD.x/2+mSD.x/2, 0, 0])build(mSD);
    translate([SD.x/2-mSD.x/2, 0, 0])build(mSD);
    }
    
//assemble();

function overall_size()= [
    slot_size.x + (slot_size.x + Slot_distance) * (slots_per_column-1) ,
    slot_size.y + (slot_size.y + Slot_distance) * (slots_per_row-1) ,
    slot_size.z
    ];

 function overall_trans()= [
        -(slot_size.x + Slot_distance) * ((slots_per_column-1)/2),
        -(slot_size.y + Slot_distance) * ((slots_per_row-1)/2),
        slot_size.z/2+0.1
        ];   
   
module buildOuterShell(){
  
    translate([0,0, -Slot_distance])
    minkowski(){
        cube([overall_size().x+(2*Slot_distance), overall_size().y+(2*Slot_distance), overall_size().z+(Slot_distance)],center=true);
        cube([Outer_thickness*2,Outer_thickness*2,0.01], center=true);
        }
    }


difference(){
    
    if (Fillet=="YES"){
        translate([0,0,-Fillet_radius])
        minkowski(){
            buildOuterShell();
            sphere(Fillet_radius, $fn=72);
            }
    } else {
        buildOuterShell();
        }
    
    translate(overall_trans())
    for (i = [0:slots_per_column-1], j = [0:slots_per_row-1]){
        echo (i, j);
        translate([slot_min_dist.x*i,slot_min_dist.y*j,0])
        rotate([0,0,Slot_orientation])
        assemble();
        };
    };