Tweak front structs

Tweak front structs

shed_length = 5184;
shed_width = 1981;
shed_front_height = 2250;
//shed_back_height = 2030;

window_w=1830;
window_h=920;
door_w=858; // 1981 x 838mm + 20
door_h=1991; // 1981 x 838mm + 20
shed_back_height = door_h;

space_to_window_w=window_w/2;
space_to_window_h=door_h-window_h;
space_to_door_w=window_w + 2*space_to_window_w;

base_timber_w = 47;
base_timber_h = 75;
timber_construct_w = 38;
timber_construct_h = 63;
timber_length_unit=2400;
base_sheet_t = 18;
sheet_width = 1220;
sheet_length = 2440;

wrap_t = 1;

first_layer = base_timber_h+wrap_t+base_sheet_t;

opp = shed_front_height-shed_back_height;
adj = shed_width -timber_construct_h;
hyp = sqrt(opp*opp+adj*adj);
theta = atan(opp/adj);
thin_opp = tan(theta) * timber_construct_h;

echo("Theta ", theta);

module cubeI(size,comment="//", center=false)
{
    if (comment != "//")
        echo(comment);
    // Cube - but will echo dimensions in order of size
    if( (size[0]>size[1]) && (size[1]>=size[2]) )
       echo( size[0], size[1], size[2]);
    if( (size[0]>size[2]) && (size[2]>size[1]) )
       echo( size[0], size[2], size[1]);

    if( (size[1]>size[0]) && (size[0]>=size[2]) )
       echo( size[1], size[0], size[2]);
    if( (size[1]>size[2]) && (size[2]>size[0]) )
       echo( size[1], size[2], size[0]);

    if( (size[2]>size[0]) && (size[0]>=size[1]) )
       echo( size[2], size[0], size[1]);
    if( (size[2]>size[1]) && (size[1]>size[0]) )
       echo( size[2], size[1], size[0]);

    cube(size, center=center);
}

module houseWrapLayer(width,length, overlap=100)
{
    echo("House Wrap",width+overlap,length+overlap);
    color("Black",0.75)
    {
        cube([width, length, wrap_t]);
        cube([width, wrap_t, overlap]);
        translate([0,length-wrap_t,0]) cube([width, wrap_t, overlap]);
        cube([wrap_t, length, overlap]);
        translate([width-wrap_t,0,0]) cube([wrap_t, length, overlap]);
    }
}

module beamsWrap()
{

    translate([0,shed_width,first_layer])  rotate([90,0,0]) houseWrapLayer(shed_length,shed_back_height, overlap=100);

    translate([0,-1,first_layer])  rotate([90,0,90]) difference()
        {
        houseWrapLayer(shed_width,shed_front_height+timber_construct_h, overlap=100);
        translate([-opp,shed_front_height+timber_construct_h,-opp]) rotate([0,0,-theta]) cube([shed_width*2,opp*2, opp*2]);
        }

     translate([shed_length,shed_width-1,first_layer])  rotate([90,0,-90]) difference()
        {
        houseWrapLayer(shed_width,shed_front_height+timber_construct_h, overlap=100);
        translate([-opp,shed_front_height-opp,-opp]) rotate([0,0,theta]) cube([shed_width*2,opp*2, opp*2]);
        }
    difference()
        {
            translate([shed_length,-2,first_layer])  rotate([90,0,180]) houseWrapLayer(shed_length,shed_front_height, overlap=100);
           translate([space_to_window_w, -50, space_to_window_h+first_layer]) cube([window_w, 100, window_h]);
           translate([space_to_door_w, -50, first_layer]) cube([door_w, 100, door_h]);
        }

}

module OSB(size,center=false)
{
    echo("OSB");
    cubeI(size,center=center);
}

module floorPanel(width,length)
{
/*
    echo("Timber");
    //Timber supporting base along left side
    cubeI([base_timber_w, length, base_timber_h]);
    //Timber supporting base along right side
    translate([width-base_timber_w,0,0])  cubeI([base_timber_w, length, base_timber_h]);
    //Timber supporting base Middle of I
    translate([base_timber_w,(length-base_timber_h)/2,base_timber_h-base_timber_w])  cubeI([width-base_timber_w*2, base_timber_h, base_timber_w]);
*/
    //DuPont Tyvek Housewrap - 100m 1.4m
    translate([0,0,base_timber_h]) houseWrapLayer(width,length,overlap=25);

    //OSB Board - although the board is not cut by 1 mm (wrap_t) in each
    //              direction for drawing purposes it is shaved
    translate([wrap_t,wrap_t,base_timber_h+wrap_t]) OSB([width-wrap_t*2,length-wrap_t*2,base_sheet_t]);
}

module floorBeams(width = shed_width, length = shed_length, overlap = 150, cross_beams=5)
{
    echo("floorBeams");
    // from one end along the front
    translate([0,0,0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    // from oother end along the front
    translate([length-timber_length_unit, 0, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([timber_length_unit-overlap, base_timber_w, 0]) cubeI([length-2*(timber_length_unit-overlap), base_timber_w, base_timber_h]);

    //Again at the back
    // from one end along the front
    translate([0,width-base_timber_w,0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    // from oother end along the front
    translate([length-timber_length_unit, width-base_timber_w, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([timber_length_unit-overlap, width-base_timber_w*2, 0]) cubeI([length-2*(timber_length_unit-overlap), base_timber_w, base_timber_h]);

    //Each end of shed
    translate([0, base_timber_w, base_timber_h-base_timber_w]) cubeI([base_timber_h, width-base_timber_w*2, base_timber_w]);
    translate([length-base_timber_h, base_timber_w, base_timber_h-base_timber_w]) cubeI([base_timber_h, width-base_timber_w*2, base_timber_w]);

    // Middle suport
    translate([base_timber_h, (width-base_timber_w)/2, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([length-base_timber_h-timber_length_unit, (width-base_timber_w)/2, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([timber_length_unit+base_timber_h-overlap, (width-base_timber_w)/2+base_timber_w, 0]) cubeI([length-2*timber_length_unit+overlap, base_timber_w, base_timber_h]);

   // Middle Cross beams0
   translate([(length-base_timber_w)/2-base_timber_h/2, base_timber_w*2, base_timber_h-base_timber_w]) cubeI([base_timber_h, (width-3*base_timber_w)/2, base_timber_w]);
    translate([(length-base_timber_w)/2+base_timber_h/2, base_timber_w*3+(width-3*base_timber_w)/2, base_timber_h-base_timber_w]) cubeI([base_timber_h, (width-3*base_timber_w)/2-2*base_timber_w, base_timber_w]);

}

module floorPanels(width = shed_width, length = shed_length)
{
    echo("floorPanels");
    sheet_width_inc_wrap = sheet_width + 2*wrap_t;
    num_width_panels = round(-0.5+length/sheet_width_inc_wrap);
    for(base_x = [0 : num_width_panels-1])
    {
        translate([base_x*sheet_width_inc_wrap, 0, 0]) floorPanel(sheet_width_inc_wrap,width);
    }
    full_panels_width = sheet_width_inc_wrap*num_width_panels;
    translate([full_panels_width, 0, 0]) floorPanel(length-full_panels_width,width);
}

module verticalStruts(width, height, beams=0, extra_struct=0)
{
    cubeI([width,timber_construct_h,timber_construct_w]);
    translate([0,0,height-timber_construct_w]) cubeI([width,timber_construct_h,timber_construct_w]);
    translate([0,0,timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-timber_construct_w*2]);
    translate([width-timber_construct_w,0,timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-timber_construct_w*2]);

    if(beams>0 && extra_struct==0)
    {
        middle_height = height-timber_construct_w*2;
        beam_spacing = middle_height/(beams+1);
        for(loop=[1:beams])
        {
            translate([timber_construct_w, 0, beam_spacing*loop]) cubeI([width-timber_construct_w*2,timber_construct_h,timber_construct_w]);
        }
    }
    if(beams==0 && extra_struct>0)
    {
        middle_width = width-timber_construct_w*1;
        beam_spacing = middle_width/(extra_struct+1);
        for(loop=[1:extra_struct])
        {
            translate([beam_spacing*loop, 0, timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-2*timber_construct_w]);
        }
    }
    if(beams>0 && extra_struct>0)
    {
        middle_height = height-timber_construct_w*2;
        beam_spacing_h = middle_height/(beams+1);
        middle_width = width-timber_construct_w*1;
        beam_spacing_w = middle_width/(extra_struct+1);

        // Wee need to off set, so we divide by mod 2
        // move the all the beams down by timber_construct_w
        // but add loop mod 2 * timber_construct_w
        // (hence odd move up, even remain down

        for(loop=[0:extra_struct])
        {
            if (loop>0)
                translate([beam_spacing_w*loop, 0, timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-2*timber_construct_w]);
            for(loop_b=[1:beams])
            {
                offset = - timber_construct_w + (timber_construct_w * (loop%2));
                translate([timber_construct_w+beam_spacing_w*loop, 0, beam_spacing_h*loop_b+offset]) cubeI([beam_spacing_w-timber_construct_w,timber_construct_h,timber_construct_w]);
            }
        }
    }
}

module backStructs(width = shed_length, height = shed_back_height, structs_sets=3, beams=2, extra_struct=1)
{
    echo("backStructs");
    translate([wrap_t,shed_width-timber_construct_h-wrap_t,first_layer])
    {
        struct_set_w = (width-wrap_t*2)/structs_sets;
        for(structs=[0:structs_sets-1])
        {
            echo("backStructs - Section");
            translate([structs*struct_set_w,0,0]) verticalStruts(struct_set_w, height, beams=beams, extra_struct=extra_struct);
        }
    }

}

module sideStructs(width = shed_width, height = shed_back_height, beams=2, extra_struct=2)
{
    echo("SideStructs");
    translate([timber_construct_h+wrap_t,timber_construct_h,first_layer])
    {
        rotate([0,0,90]) verticalStruts(width-timber_construct_h*2, height, beams=beams, extra_struct=extra_struct);
    }
}

module leftSideStructs()
{
    echo("leftSideStructs");
    sideStructs();
}

module rightSideStructs()
{
    echo("rightSideStructs");
    translate([shed_length-timber_construct_h-wrap_t*2,0,0]) sideStructs();
}

module frontStructs()
{
    echo("frontStructs");
    translate([wrap_t,-wrap_t,first_layer+1])
    {
        verticalStruts(space_to_window_w, door_h, beams=2, extra_struct=0);
        translate([space_to_window_w,0,0]) verticalStruts(window_w, space_to_window_h, beams=1, extra_struct=2);
        translate([space_to_window_w+window_w,0,0])  verticalStruts(space_to_window_w, door_h, beams=2, extra_struct=0);
        translate([space_to_door_w+door_w,0,0])  verticalStruts(shed_length-(space_to_door_w+door_w), door_h, beams=3, extra_struct=1);
        // top part, which is longer than a standard timber_length_unit
        gap = (space_to_door_w - timber_length_unit)/2;
        remainder = shed_length - timber_length_unit - gap;
        translate([0,0,door_h])  verticalStruts(gap, shed_front_height-door_h, beams=0, extra_struct=0);
        translate([gap,0,door_h])  verticalStruts(timber_length_unit, shed_front_height-door_h, beams=0, extra_struct=2);
        translate([timber_length_unit + gap,0,door_h])  verticalStruts(remainder, shed_front_height-door_h, beams=0, extra_struct=2);
    }
}

module roof()
{
    echo("roof");
    // hyp
    inner_width = hyp+timber_construct_h;
    flat_offset = (sheet_length - inner_width)/2;

    translate([-flat_offset,-wrap_t,shed_front_height+ first_layer +1 +thin_opp ]) rotate([-theta,0,0])
    {
        frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2, left_offset=flat_offset);
        translate([sheet_width*4,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2, right_offset=flat_offset, sw= shed_length - sheet_width*4+flat_offset*2);

        translate([sheet_width,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2);
        translate([sheet_width*2,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2);
        translate([sheet_width*3,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2);
    }
}

module frameSheet(left_offset=0, right_offset=0, front_offset=0, back_offset=0, sheet_left_offset=0, sheet_right_offset=0, sheet_front_offset=0, sheet_back_offset=0, sw=sheet_width, sl = sheet_length)
{
    translate([left_offset, front_offset, 0]) cubeI([timber_construct_h, sl-front_offset-back_offset, timber_construct_w]);
    translate([sw-right_offset-timber_construct_h, front_offset, 0]) cubeI([timber_construct_h, sl-front_offset-back_offset, timber_construct_w]);

   beam_l = (sw-left_offset-right_offset-3*timber_construct_h)/2;

    translate([left_offset+beam_l+timber_construct_h, front_offset, 0]) cubeI([timber_construct_h, sl-front_offset-back_offset, timber_construct_w]);

   translate([left_offset+timber_construct_h, front_offset, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);
   translate([left_offset+timber_construct_h+beam_l+timber_construct_h, front_offset, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);

    translate([left_offset+timber_construct_h, sl - back_offset-timber_construct_h, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);
    translate([left_offset+timber_construct_h+beam_l+timber_construct_h, sl - back_offset-timber_construct_h, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);

    translate([sheet_left_offset, sheet_front_offset, timber_construct_w]) OSB([sw-sheet_back_offset,sl-sheet_right_offset, base_sheet_t]);
    mirror([0,0,1]) translate([sheet_left_offset, sheet_front_offset, -timber_construct_w-base_sheet_t-wrap_t])
        houseWrapLayer(sw-sheet_back_offset+2*wrap_t,sl-sheet_right_offset+2*wrap_t, overlap=25);
}

module prism(l, w, h)
{
    polyhedron(
        points=[[0,0,0], [l,0,0], [l,w,0], [0,w,0], [0,w,h], [l,w,h]],
        faces=[[0,1,2,3],[5,4,3,2],[0,4,5,1],[0,3,4],[5,2,1]]
        );
}

module cadding(x, board_height, board_width_b, board_width_s, degrees)
{
    echo("Cladding: ",  x, board_height);
    translate([0, -board_width_b, 0]) color("Peru",0.6) rotate([-degrees,0,0])
    {
        cube([x, board_width_b - board_width_s, board_height]);
        translate([0,- board_width_s,0]) prism(x, board_width_s, board_height);
    }
}

module caddings(x, z, board_height = 150, board_width_b = 11, board_width_s = 6, overlap = 34, degrees=3)
{
    panel_uplift = board_height - overlap;
    panels = round(-0.5+z/panel_uplift);
    for(p = [0 : panels-1])
    {
        translate([0,0,panel_uplift * p])
         cadding(x, board_height, board_width_b, board_width_s, degrees);
    }
}

module claddingBackWall()
{
    translate([shed_length,shed_width,first_layer]) rotate([0,0,180]) caddings(shed_length, shed_back_height);
}

module claddingLeftWall()
{
    translate([0,shed_width,first_layer]) rotate([0,0,180+90])
    difference()
    {
        caddings(shed_width, shed_front_height);
        translate([-opp,-opp,shed_front_height-opp]) rotate([0,-theta,0]) cube([shed_width*2,opp*2, opp*2]);
    }
}

module claddingRightWall()
{
    translate([shed_length,0,first_layer]) rotate([0,0,180-90])
    difference()
    {
        caddings(shed_width, shed_front_height);
        translate([-opp,-opp,shed_back_height+opp+base_timber_h]) rotate([0,theta,0]) cube([shed_width*2,opp*2, opp*2]);
    }
}

module claddingFrontWall()
{
    translate([0,0,first_layer]) rotate([0,0,0])
        difference()
    {
        caddings(shed_length, shed_front_height);
           translate([space_to_window_w, -50, space_to_window_h]) cube([window_w, 100, window_h]);
           translate([space_to_door_w, -50, 0]) cube([door_w, 100, door_h]);
    }
}

module innerInsulation()
{
    echo("Insulation");
    insulation_t = 50;
    translate([0,0,first_layer])
    {
    translate([0,shed_width-insulation_t,0])
        cube([shed_length, insulation_t, shed_back_height]);

    translate([0,insulation_t,0]) difference()
        {
            cube([insulation_t, shed_width-2*insulation_t, shed_front_height]);
            translate([-insulation_t,0,shed_front_height]) rotate([-theta,0,0]) cube([3*insulation_t,shed_width+2*insulation_t, opp*2]);
        }
    translate([shed_length-insulation_t,insulation_t,0]) difference()
        {
            cube([insulation_t, shed_width-2*insulation_t, shed_front_height]);
            translate([-insulation_t,0,shed_front_height]) rotate([-theta,0,0]) cube([3*insulation_t,shed_width+2*insulation_t, opp*2]);
        }

    translate([0,0,0])  difference()
        {
            cube([shed_length, insulation_t, shed_front_height]);
            translate([space_to_window_w, -insulation_t, space_to_window_h]) cube([window_w, 3*insulation_t, window_h]);
            translate([space_to_door_w, -insulation_t, 0]) cube([door_w, 3*insulation_t, door_h]);
        }

    translate([0,insulation_t,shed_front_height+60]) rotate([-theta-90,0,0])  cube([shed_length, insulation_t, hyp]);

    }
}

annimate_step = 1/20;

if($t>annimate_step*2)
    floorBeams();

if($t>annimate_step*3)
    floorPanels();

if($t>annimate_step*4)
    backStructs();
if($t>annimate_step*5)
    leftSideStructs();
if($t>annimate_step*6)
    rightSideStructs();

if($t>annimate_step*7)
    frontStructs();
if($t>annimate_step*8)
    roof();
if($t>annimate_step*9)
    beamsWrap();

if($t>annimate_step*10)
    claddingBackWall();
if($t>annimate_step*11)
    claddingLeftWall();
if($t>annimate_step*12)
    claddingRightWall();
if($t>annimate_step*13)
    claddingFrontWall();

if($t>annimate_step*14)
    innerInsulation();

shed_length = 5184;
shed_width = 1981;
shed_front_height = 2250;
//shed_back_height = 2030;

window_w=1830;
window_h=920;
door_w=858; // 1981 x 838mm + 20
door_h=1991; // 1981 x 838mm + 20
shed_back_height = door_h;

space_to_window_w=window_w/2;
space_to_window_h=door_h-window_h;
space_to_door_w=window_w + 2*space_to_window_w;

base_timber_w = 47;
base_timber_h = 75;
timber_construct_w = 38;
timber_construct_h = 63;
timber_length_unit=2400;
base_sheet_t = 18;
sheet_width = 1220;
sheet_length = 2440;

wrap_t = 1;

first_layer = base_timber_h+wrap_t+base_sheet_t;

opp = shed_front_height-shed_back_height;
adj = shed_width -timber_construct_h;
hyp = sqrt(opp*opp+adj*adj);
theta = atan(opp/adj);
thin_opp = tan(theta) * timber_construct_h;

echo("Theta ", theta);

module cubeI(size,comment="//", center=false)
{
    if (comment != "//")
        echo(comment);
    // Cube - but will echo dimensions in order of size
    if( (size[0]>size[1]) && (size[1]>=size[2]) )
       echo( size[0], size[1], size[2]);
    if( (size[0]>size[2]) && (size[2]>size[1]) )
       echo( size[0], size[2], size[1]);

    if( (size[1]>size[0]) && (size[0]>=size[2]) )
       echo( size[1], size[0], size[2]);
    if( (size[1]>size[2]) && (size[2]>size[0]) )
       echo( size[1], size[2], size[0]);

    if( (size[2]>size[0]) && (size[0]>=size[1]) )
       echo( size[2], size[0], size[1]);
    if( (size[2]>size[1]) && (size[1]>size[0]) )
       echo( size[2], size[1], size[0]);

    cube(size, center=center);
}

module houseWrapLayer(width,length, overlap=100)
{
    echo("House Wrap",width+overlap,length+overlap);
    color("Black",0.75)
    {
        cube([width, length, wrap_t]);
        cube([width, wrap_t, overlap]);
        translate([0,length-wrap_t,0]) cube([width, wrap_t, overlap]);
        cube([wrap_t, length, overlap]);
        translate([width-wrap_t,0,0]) cube([wrap_t, length, overlap]);
    }
}

module beamsWrap()
{

    translate([0,shed_width,first_layer])  rotate([90,0,0]) houseWrapLayer(shed_length,shed_back_height, overlap=100);

    translate([0,-1,first_layer])  rotate([90,0,90]) difference()
        {
        houseWrapLayer(shed_width,shed_front_height+timber_construct_h, overlap=100);
        translate([-opp,shed_front_height+timber_construct_h,-opp]) rotate([0,0,-theta]) cube([shed_width*2,opp*2, opp*2]);
        }

     translate([shed_length,shed_width-1,first_layer])  rotate([90,0,-90]) difference()
        {
        houseWrapLayer(shed_width,shed_front_height+timber_construct_h, overlap=100);
        translate([-opp,shed_front_height-opp,-opp]) rotate([0,0,theta]) cube([shed_width*2,opp*2, opp*2]);
        }
    difference()
        {
            translate([shed_length,-2,first_layer])  rotate([90,0,180]) houseWrapLayer(shed_length,shed_front_height, overlap=100);
           translate([space_to_window_w, -50, space_to_window_h+first_layer]) cube([window_w, 100, window_h]);
           translate([space_to_door_w, -50, first_layer]) cube([door_w, 100, door_h]);
        }

}

module OSB(size,center=false)
{
    echo("OSB");
    cubeI(size,center=center);
}

module floorPanel(width,length)
{
/*
    echo("Timber");
    //Timber supporting base along left side
    cubeI([base_timber_w, length, base_timber_h]);
    //Timber supporting base along right side
    translate([width-base_timber_w,0,0])  cubeI([base_timber_w, length, base_timber_h]);
    //Timber supporting base Middle of I
    translate([base_timber_w,(length-base_timber_h)/2,base_timber_h-base_timber_w])  cubeI([width-base_timber_w*2, base_timber_h, base_timber_w]);
*/
    //DuPont Tyvek Housewrap - 100m 1.4m
    translate([0,0,base_timber_h]) houseWrapLayer(width,length,overlap=25);

    //OSB Board - although the board is not cut by 1 mm (wrap_t) in each
    //              direction for drawing purposes it is shaved
    translate([wrap_t,wrap_t,base_timber_h+wrap_t]) OSB([width-wrap_t*2,length-wrap_t*2,base_sheet_t]);
}

module floorBeams(width = shed_width, length = shed_length, overlap = 150, cross_beams=5)
{
    echo("floorBeams");
    // from one end along the front
    translate([0,0,0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    // from oother end along the front
    translate([length-timber_length_unit, 0, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([timber_length_unit-overlap, base_timber_w, 0]) cubeI([length-2*(timber_length_unit-overlap), base_timber_w, base_timber_h]);

    //Again at the back
    // from one end along the front
    translate([0,width-base_timber_w,0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    // from oother end along the front
    translate([length-timber_length_unit, width-base_timber_w, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([timber_length_unit-overlap, width-base_timber_w*2, 0]) cubeI([length-2*(timber_length_unit-overlap), base_timber_w, base_timber_h]);

    //Each end of shed
    translate([0, base_timber_w, base_timber_h-base_timber_w]) cubeI([base_timber_h, width-base_timber_w*2, base_timber_w]);
    translate([length-base_timber_h, base_timber_w, base_timber_h-base_timber_w]) cubeI([base_timber_h, width-base_timber_w*2, base_timber_w]);

    // Middle suport
    translate([base_timber_h, (width-base_timber_w)/2, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([length-base_timber_h-timber_length_unit, (width-base_timber_w)/2, 0]) cubeI([timber_length_unit, base_timber_w, base_timber_h]);
    translate([timber_length_unit+base_timber_h-overlap, (width-base_timber_w)/2+base_timber_w, 0]) cubeI([length-2*timber_length_unit+overlap, base_timber_w, base_timber_h]);

   // Middle Cross beams0
   translate([(length-base_timber_w)/2-base_timber_h/2, base_timber_w*2, base_timber_h-base_timber_w]) cubeI([base_timber_h, (width-3*base_timber_w)/2, base_timber_w]);
    translate([(length-base_timber_w)/2+base_timber_h/2, base_timber_w*3+(width-3*base_timber_w)/2, base_timber_h-base_timber_w]) cubeI([base_timber_h, (width-3*base_timber_w)/2-2*base_timber_w, base_timber_w]);

}

module floorPanels(width = shed_width, length = shed_length)
{
    echo("floorPanels");
    sheet_width_inc_wrap = sheet_width + 2*wrap_t;
    num_width_panels = round(-0.5+length/sheet_width_inc_wrap);
    for(base_x = [0 : num_width_panels-1])
    {
        translate([base_x*sheet_width_inc_wrap, 0, 0]) floorPanel(sheet_width_inc_wrap,width);
    }
    full_panels_width = sheet_width_inc_wrap*num_width_panels;
    translate([full_panels_width, 0, 0]) floorPanel(length-full_panels_width,width);
}

module verticalStruts(width, height, beams=0, extra_struct=0)
{
    cubeI([width,timber_construct_h,timber_construct_w]);
    translate([0,0,height-timber_construct_w]) cubeI([width,timber_construct_h,timber_construct_w]);
    translate([0,0,timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-timber_construct_w*2]);
    translate([width-timber_construct_w,0,timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-timber_construct_w*2]);

    if(beams>0 && extra_struct==0)
    {
        middle_height = height-timber_construct_w*2;
        beam_spacing = middle_height/(beams+1);
        for(loop=[1:beams])
        {
            translate([timber_construct_w, 0, beam_spacing*loop]) cubeI([width-timber_construct_w*2,timber_construct_h,timber_construct_w]);
        }
    }
    if(beams==0 && extra_struct>0)
    {
        middle_width = width-timber_construct_w*1;
        beam_spacing = middle_width/(extra_struct+1);
        for(loop=[1:extra_struct])
        {
            translate([beam_spacing*loop, 0, timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-2*timber_construct_w]);
        }
    }
    if(beams>0 && extra_struct>0)
    {
        middle_height = height-timber_construct_w*2;
        beam_spacing_h = middle_height/(beams+1);
        middle_width = width-timber_construct_w*1;
        beam_spacing_w = middle_width/(extra_struct+1);

        // Wee need to off set, so we divide by mod 2
        // move the all the beams down by timber_construct_w
        // but add loop mod 2 * timber_construct_w
        // (hence odd move up, even remain down

        for(loop=[0:extra_struct])
        {
            if (loop>0)
                translate([beam_spacing_w*loop, 0, timber_construct_w]) cubeI([timber_construct_w,timber_construct_h,height-2*timber_construct_w]);
            for(loop_b=[1:beams])
            {
                offset = - timber_construct_w + (timber_construct_w * (loop%2));
                translate([timber_construct_w+beam_spacing_w*loop, 0, beam_spacing_h*loop_b+offset]) cubeI([beam_spacing_w-timber_construct_w,timber_construct_h,timber_construct_w]);
            }
        }
    }
}

module backStructs(width = shed_length, height = shed_back_height, structs_sets=3, beams=2, extra_struct=1)
{
    echo("backStructs");
    translate([wrap_t,shed_width-timber_construct_h-wrap_t,first_layer])
    {
        struct_set_w = (width-wrap_t*2)/structs_sets;
        for(structs=[0:structs_sets-1])
        {
            echo("backStructs - Section");
            translate([structs*struct_set_w,0,0]) verticalStruts(struct_set_w, height, beams=beams, extra_struct=extra_struct);
        }
    }

}

module sideStructs(width = shed_width, height = shed_back_height, beams=2, extra_struct=2)
{
    echo("SideStructs");
    translate([timber_construct_h+wrap_t,timber_construct_h,first_layer])
    {
        rotate([0,0,90]) verticalStruts(width-timber_construct_h*2, height, beams=beams, extra_struct=extra_struct);
    }
}

module leftSideStructs()
{
    echo("leftSideStructs");
    sideStructs();
}

module rightSideStructs()
{
    echo("rightSideStructs");
    translate([shed_length-timber_construct_h-wrap_t*2,0,0]) sideStructs();
}

module frontStructs()
{
    echo("frontStructs");
    translate([wrap_t,-wrap_t,first_layer+1])
    {
        verticalStruts(space_to_window_w, door_h, beams=2, extra_struct=0);
        translate([space_to_window_w,0,0]) verticalStruts(window_w, space_to_window_h, beams=1, extra_struct=2);
        translate([space_to_window_w+window_w,0,0])  verticalStruts(space_to_window_w, door_h, beams=2, extra_struct=1);
        translate([space_to_door_w+door_w,0,0])  verticalStruts(shed_length-(space_to_door_w+door_w), door_h, beams=2, extra_struct=0);
        // top part, which is longer than a standard timber_length_unit
        gap = (space_to_door_w - timber_length_unit)/2;
        remainder = shed_length - timber_length_unit - gap;
        translate([0,0,door_h])  verticalStruts(gap, shed_front_height-door_h, beams=0, extra_struct=0);
        translate([gap,0,door_h])  verticalStruts(timber_length_unit, shed_front_height-door_h, beams=0, extra_struct=2);
        translate([timber_length_unit + gap,0,door_h])  verticalStruts(remainder, shed_front_height-door_h, beams=0, extra_struct=2);
    }
}

module roof()
{
    echo("roof");
    // hyp
    inner_width = hyp+timber_construct_h;
    flat_offset = (sheet_length - inner_width)/2;

    translate([-flat_offset,-wrap_t,shed_front_height+ first_layer +1 +thin_opp ]) rotate([-theta,0,0])
    {
        frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2, left_offset=flat_offset);
        translate([sheet_width*4,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2, right_offset=flat_offset, sw= shed_length - sheet_width*4+flat_offset*2);

        translate([sheet_width,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2);
        translate([sheet_width*2,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2);
        translate([sheet_width*3,0,0]) frameSheet( sheet_front_offset=-flat_offset*1.5, front_offset=0, back_offset=flat_offset*2);
    }
}

module frameSheet(left_offset=0, right_offset=0, front_offset=0, back_offset=0, sheet_left_offset=0, sheet_right_offset=0, sheet_front_offset=0, sheet_back_offset=0, sw=sheet_width, sl = sheet_length)
{
    translate([left_offset, front_offset, 0]) cubeI([timber_construct_h, sl-front_offset-back_offset, timber_construct_w]);
    translate([sw-right_offset-timber_construct_h, front_offset, 0]) cubeI([timber_construct_h, sl-front_offset-back_offset, timber_construct_w]);

   beam_l = (sw-left_offset-right_offset-3*timber_construct_h)/2;

    translate([left_offset+beam_l+timber_construct_h, front_offset, 0]) cubeI([timber_construct_h, sl-front_offset-back_offset, timber_construct_w]);

   translate([left_offset+timber_construct_h, front_offset, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);
   translate([left_offset+timber_construct_h+beam_l+timber_construct_h, front_offset, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);

    translate([left_offset+timber_construct_h, sl - back_offset-timber_construct_h, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);
    translate([left_offset+timber_construct_h+beam_l+timber_construct_h, sl - back_offset-timber_construct_h, 0]) cubeI([beam_l, timber_construct_h, timber_construct_w]);

    translate([sheet_left_offset, sheet_front_offset, timber_construct_w]) OSB([sw-sheet_back_offset,sl-sheet_right_offset, base_sheet_t]);
    mirror([0,0,1]) translate([sheet_left_offset, sheet_front_offset, -timber_construct_w-base_sheet_t-wrap_t])
        houseWrapLayer(sw-sheet_back_offset+2*wrap_t,sl-sheet_right_offset+2*wrap_t, overlap=25);
}

module prism(l, w, h)
{
    polyhedron(
        points=[[0,0,0], [l,0,0], [l,w,0], [0,w,0], [0,w,h], [l,w,h]],
        faces=[[0,1,2,3],[5,4,3,2],[0,4,5,1],[0,3,4],[5,2,1]]
        );
}

module cadding(x, board_height, board_width_b, board_width_s, degrees)
{
    echo("Cladding: ",  x, board_height);
    translate([0, -board_width_b, 0]) color("Peru",0.6) rotate([-degrees,0,0])
    {
        cube([x, board_width_b - board_width_s, board_height]);
        translate([0,- board_width_s,0]) prism(x, board_width_s, board_height);
    }
}

module caddings(x, z, board_height = 150, board_width_b = 11, board_width_s = 6, overlap = 34, degrees=3)
{
    panel_uplift = board_height - overlap;
    panels = round(-0.5+z/panel_uplift);
    for(p = [0 : panels-1])
    {
        translate([0,0,panel_uplift * p])
         cadding(x, board_height, board_width_b, board_width_s, degrees);
    }
}

module claddingBackWall()
{
    translate([shed_length,shed_width,first_layer]) rotate([0,0,180]) caddings(shed_length, shed_back_height);
}

module claddingLeftWall()
{
    translate([0,shed_width,first_layer]) rotate([0,0,180+90])
    difference()
    {
        caddings(shed_width, shed_front_height);
        translate([-opp,-opp,shed_front_height-opp]) rotate([0,-theta,0]) cube([shed_width*2,opp*2, opp*2]);
    }
}

module claddingRightWall()
{
    translate([shed_length,0,first_layer]) rotate([0,0,180-90])
    difference()
    {
        caddings(shed_width, shed_front_height);
        translate([-opp,-opp,shed_back_height+opp+base_timber_h]) rotate([0,theta,0]) cube([shed_width*2,opp*2, opp*2]);
    }
}

module claddingFrontWall()
{
    translate([0,0,first_layer]) rotate([0,0,0])
        difference()
    {
        caddings(shed_length, shed_front_height);
           translate([space_to_window_w, -50, space_to_window_h]) cube([window_w, 100, window_h]);
           translate([space_to_door_w, -50, 0]) cube([door_w, 100, door_h]);
    }
}

module innerInsulation()
{
    echo("Insulation");
    insulation_t = 50;
    translate([0,0,first_layer])
    {
    translate([0,shed_width-insulation_t,0])
        cube([shed_length, insulation_t, shed_back_height]);

    translate([0,insulation_t,0]) difference()
        {
            cube([insulation_t, shed_width-2*insulation_t, shed_front_height]);
            translate([-insulation_t,0,shed_front_height]) rotate([-theta,0,0]) cube([3*insulation_t,shed_width+2*insulation_t, opp*2]);
        }
    translate([shed_length-insulation_t,insulation_t,0]) difference()
        {
            cube([insulation_t, shed_width-2*insulation_t, shed_front_height]);
            translate([-insulation_t,0,shed_front_height]) rotate([-theta,0,0]) cube([3*insulation_t,shed_width+2*insulation_t, opp*2]);
        }

    translate([0,0,0])  difference()
        {
            cube([shed_length, insulation_t, shed_front_height]);
            translate([space_to_window_w, -insulation_t, space_to_window_h]) cube([window_w, 3*insulation_t, window_h]);
            translate([space_to_door_w, -insulation_t, 0]) cube([door_w, 3*insulation_t, door_h]);
        }

    translate([0,insulation_t,shed_front_height+60]) rotate([-theta-90,0,0])  cube([shed_length, insulation_t, hyp]);

    }
}

annimate_step = 1/20;

if($t>annimate_step*2)
    floorBeams();

if($t>annimate_step*3)
    floorPanels();

if($t>annimate_step*4)
    backStructs();
if($t>annimate_step*5)
    leftSideStructs();
if($t>annimate_step*6)
    rightSideStructs();

if($t>annimate_step*7)
    frontStructs();
if($t>annimate_step*8)
    roof();
if($t>annimate_step*9)
    beamsWrap();

if($t>annimate_step*10)
    claddingBackWall();
if($t>annimate_step*11)
    claddingLeftWall();
if($t>annimate_step*12)
    claddingRightWall();
if($t>annimate_step*13)
    claddingFrontWall();

if($t>annimate_step*14)
    innerInsulation();


Adding size option for Open Structures-compatibility

// Description: "LEGO Technic-compatible gridbeam"
// Project home: http://bitbeam.org

// Each bitbeam is 8mm inches wide.
// Center of each hole is 8mm apart from each other
// Holes are 5.1 mm in diameter.

// Mini
//beam_width     = 4;
//hole_diameter  = 2.2;

// Standard
beam_width     = 8;
hole_diameter  = 5.1;

// Open Structures
//beam_width     = 10;
//hole_diameter  = 5.6;

hole_radius    = hole_diameter / 2;

module beam(number_of_holes) {
    beam_length = number_of_holes * beam_width;
    difference() {
        // Draw the beam...
        cube([beam_length,beam_width,beam_width]);

        // Cut the holes...
        for (x=[beam_width/2 : beam_width : beam_length]) {
            translate([x,beam_width/2,-2])
            cylinder(r=hole_radius, h=beam_width*1.5, $fn=30);
        }
        for (x=[beam_width/2 : beam_width : beam_length]) {
            rotate([90,0,0])
            translate([x,beam_width/2,-beam_width*1.5])
            cylinder(r=hole_radius, h=beam_width*2, $fn=30);
        }

        // Optional through-hole
        //rotate([0,90,0])
        //translate([-4,beam_width/2,-2])
        //cylinder(r=hole_radius, h=number_of_holes*beam_width+4, $fn=30);
        }
}

beam(9);

// Description: "LEGO Technic-compatible gridbeam"
// Project home: http://bitbeam.org

// Each bitbeam is 8mm inches wide.
// Center of each hole is 8mm apart from each other
// Holes are 5.1 mm in diameter.

// Mini
//beam_width     = 4;
//hole_diameter  = 2.2;

// Standard
beam_width     = 8;
hole_diameter  = 5.1;

hole_radius    = hole_diameter / 2;

module beam(number_of_holes) {
    beam_length = number_of_holes * beam_width;
    difference() {
        // Draw the beam...
        cube([beam_length,beam_width,beam_width]);

        // Cut the holes...
        for (x=[beam_width/2 : beam_width : beam_length]) {
            translate([x,beam_width/2,-2])
            cylinder(r=hole_radius, h=12, $fn=30);
        }
        for (x=[beam_width/2 : beam_width : beam_length]) {
            rotate([90,0,0])
            translate([x,beam_width/2,-10])
            cylinder(r=hole_radius, h=12, $fn=30);
        }

        // Optional through-hole
        //rotate([0,90,0])
        //translate([-4,beam_width/2,-2])
        //cylinder(r=hole_radius, h=number_of_holes*beam_width+4, $fn=30);
        }
}

beam(3);


House wrap

House wrap

shed_length = 5184;
shed_width = 1829+305/2;
shed_front_height = 2400;
shed_front_back = 2135;
base_timber = 47;
base_sheet_t = 18;

module cubeI(size,center=false)
{
    echo("Dim: ",size);
    cube(size, center=center);
}

module shedBaseSection(x,y,timber_width=75,timber_length=47, braces=0)
{
    echo("shedBaseSection: x,y,timber_width,timber_length, braces ", x,y,timber_width,timber_length, braces);
    echo("http://www.wickes.co.uk/Wickes-Sawn-Kiln-Dried-47X75-x-3000mm-Single/p/107186#tab-DynamicDeliveryWebTab_content");
    cubeI([x,timber_width,timber_length]);
    translate([0,y-timber_width,0]) cubeI([x,timber_width,timber_length]);
    translate([0,timber_width,0]) cubeI([timber_width,y-timber_width*2,timber_length]);
    translate([x-timber_width,timber_width,0]) cubeI([timber_width,y-timber_width*2,timber_length]);
    if(braces > 0)
    {
        middle_y = y - timber_width*2;
        part_y = middle_y/(braces+1);
        for(loop=[1 : braces ])
        {
                translate([timber_width,timber_width/(braces+1) + (loop*part_y),0]) cubeI([x-timber_width*2, timber_width,timber_length]);
        }
    }
}

module shedBase(x,y,number_x,number_y, braces=1)
{
    for(base_y = [0 : number_y-1])
    {
        for(base_x = [0 : number_x-1])
            {
                translate([base_x*(x/number_x),base_y*(y/number_y),0]) shedBaseSection(x/number_x,y/number_y, braces = braces);
            }
    }
}

module floorMembrane(x,y,thickness,color="Black", overlap=10)
{
    echo("x,y,thickness=1,color, overlap=1",x,y,thickness,color, overlap);
    echo("http://www.screwfix.com/p/dmp-damp-proof-membrane-black-1000ga-3-x-4m/50464#product_additional_details_container");
    translate([-overlap,-overlap,0]) color(color, 0.5) cube([x+overlap*2,y+overlap*2, thickness]);
}

module breatherMembrane(x,y, thickness=0.5)
{
    // google timber frame breather membrane
    rotate([90,0,0])  floorMembrane(x,y,thickness=thickness,color="Black", overlap=thickness);
}

module breatherMembraneSide(x,y, thickness=0.5)
{
    // google timber frame breather membrane

    opp = y-shed_front_back;
    adj = x - base_timber;
    hyp = sqrt(opp*opp+adj*adj);
    theta = atan(opp/adj);
    echo("Degrees: ", theta);
    translate([0,0,0]) rotate([90,0,90]) difference()
    {
        floorMembrane(x,y,thickness=thickness,color="Black", overlap=thickness);
        translate([0,y,-opp/2]) rotate([0,0,-theta]) cube([x+10,opp,opp]);
    }
}

//module shedLowerFloor(x,y, panel_slf_w = 607, panel_slf_l =   1829, panel_slf_t =   5.5, panel_min_dimentions = 100)
module shedLowerFloor(x,y, panel_slf_w = 1220, panel_slf_l = 2440, panel_slf_t = base_sheet_t, panel_min_dimentions = 100)
{
//    echo("http://www.wickes.co.uk/Wickes-Non-Structural-Hardwood-Plywood-5-5x607x1829mm/p/111196");
    echo("http://www.wickes.co.uk/Wickes-General-Purpose-OSB3-Board-18x1220x2440mm/p/110517");
    x_panels = round(-0.5+x/panel_slf_w);
    y_panels = round(-0.5+y/panel_slf_l);
    echo(y_panels);
    if(y_panels>0) // Panel is Not longer than width
    {
        for(base_y = [0 : y_panels-1]) //Truncate not round
        {
            for(base_x = [0 : x_panels-1])
            {
                translate([base_x*panel_slf_w,base_y*panel_slf_l,0]) cubeI([panel_slf_w, panel_slf_l,panel_slf_t]);
                if(y-(panel_slf_l*y_panels) > panel_min_dimentions)
                translate([base_x*panel_slf_w,y_panels*panel_slf_l,0]) cubeI([panel_slf_w, y-(panel_slf_l*y_panels),panel_slf_t]);
                }
            if(x-(panel_slf_w*x_panels) > panel_min_dimentions)
                translate([(x_panels)*panel_slf_w,(y_panels-1)*panel_slf_l,0]) cubeI([x-(panel_slf_w*x_panels), panel_slf_l,panel_slf_t]);
            if(y-(panel_slf_l*y_panels) > panel_min_dimentions)
                translate([(x_panels)*panel_slf_w,y_panels*panel_slf_l,0]) cubeI([x-(panel_slf_w*x_panels), y-(panel_slf_l*y_panels),panel_slf_t]);
        }
    }
    else // Panel is longer than width
    {
     for(base_x = [0 : x_panels-1])
     {
        translate([base_x*panel_slf_w,0,0]) cubeI([panel_slf_w, y,panel_slf_t]);
     }
     if(x-(panel_slf_w*x_panels) > panel_min_dimentions)
                translate([(x_panels)*panel_slf_w,0,0]) cubeI([x-(panel_slf_w*x_panels), y,panel_slf_t]);
    }
}

module verticalStruts(x,z,timber_width=47,timber_length=47, braces=0, beams=0)
{
//    echo("shedBaseSection(x,z,timber_width,timber_length, braces",x,z,timber_width,timber_length, braces);
//    echo("Walls - http://www.wickes.co.uk/Wickes-Sawn-Kiln-Dried-47-x-47-x-2400mm-Pack-6/p/107114");
    cubeI([x,timber_width,timber_length]);
    translate([0,0,z-timber_width]) cubeI([x,timber_width,timber_length]);
    translate([0,0,timber_width]) cubeI([timber_width,timber_length,z-timber_width*2]);
    translate([x-timber_width,0,timber_width]) cubeI([timber_width,timber_length,z-timber_width*2]);
    if(braces > 0)
    {
        middle_z = z - timber_width*2;
        part_z = middle_z/(braces+1);
        for(loop=[1 : braces ])
        {
                translate([timber_width,0,timber_width/(braces+1) + (loop*part_z)]) cubeI([x-timber_width*2, timber_width,timber_length]);
        }
    }
    if(beams> 0)
    {
        middle_x = x - timber_length*2;
        part_x = middle_x/(beams+1);
        for(loop=[1 : beams ])
        {
                translate([timber_length/(beams+1) + (loop*part_x),0,timber_width]) cubeI([timber_length, timber_width,z-timber_length*2]);
        }
    }
}

module backWallStruts(x,z,number_x, braces=1)
{
    echo("backWallStruts(x,z,number_x, braces)",x,z,number_x, braces);
        for(base_x = [0 : number_x-1])
            {
                translate([base_x*(x/number_x),0,0]) verticalStruts(x/number_x,z, braces = braces);
            }
}

module sideWallStruts(x,z,number_x, braces=1)
{
    echo("sideWallStruts(x,z,number_x, braces)",x,z,number_x, braces);
    rotate([0,0,90]) backWallStruts(x,z,number_x, braces);
}

module prism(l, w, h)
{
    polyhedron(
        points=[[0,0,0], [l,0,0], [l,w,0], [0,w,0], [0,w,h], [l,w,h]],
        faces=[[0,1,2,3],[5,4,3,2],[0,4,5,1],[0,3,4],[5,2,1]]
        );
}

module cadding(x, board_height, board_width_b, board_width_s, degrees)
{
    translate([0, -board_width_b, 0]) color("Peru",0.6) rotate([-degrees,0,0])
    {
        cube([x, board_width_b - board_width_s, board_height]);
        translate([0,- board_width_s,0]) prism(x, board_width_s, board_height);
    }
}

module caddings(x, z, board_height = 150, board_width_b = 11, board_width_s = 6, overlap = 25, degrees=3)
{
    panel_uplift = board_height - overlap;
    panels = round(-0.5+z/panel_uplift);
    for(p = [0 : panels-1])
    {
        translate([0,0,panel_uplift * p])
         cadding(x, board_height, board_width_b, board_width_s, degrees);
    }
}

module claddingBackWall(x,z)
{
    translate([x,0,0]) rotate([0,0,180]) caddings(x, z);
}

module claddingLeftSideWall(x,z)
{
    opp = z-shed_front_back;
    adj = x - base_timber;
    hyp = sqrt(opp*opp+adj*adj);
    theta = atan(opp/adj);
    echo("Degrees: ", theta);
    translate([0,x,0]) rotate([0,0,270]) difference()
    {
        caddings(x, z);
      translate([0,-opp/2,z-opp]) rotate([0,-theta,0]) cube([x+10,opp,opp]);
    }
}

module claddingRightSideWall(x,z)
{
    opp = z-shed_front_back;
    adj = x - base_timber;
    hyp = sqrt(opp*opp+adj*adj);
    theta = atan(opp/adj);
    echo("Degrees: ", theta);
    translate([0,0,0]) rotate([0,0,90]) difference()
    {
        caddings(x, z);
        translate([0,-opp/2,z]) rotate([0,theta,0]) cube([x+10,opp,opp]);
    }
}

module claddingFrontWall(x,z, space_to_window_w, space_to_window_h, window_w, window_h, door_w, door_h)
{
    translate([0,0,0]) rotate([0,0,0]) difference()
    {
        caddings(x, z);
        translate([space_to_window_w,-100,space_to_window_h]) rotate([0,0,0]) cube([window_w,200,window_h]);
        translate([space_to_window_w*2+window_w,-100,0]) rotate([0,0,0]) cube([door_w,200,door_h]);
    }
}

module frontWallStruts(x,z, space_to_window_w, space_to_window_h, window_w, window_h, door_w, door_h, braces)
{
    echo("frontWallStruts(x,z,space_to_window_w, space_to_window_h, window_w, window_h, door_w, braces)", x,z, space_to_window_w, space_to_window_h, window_w, window_h, door_w, door_h, braces);
    translate([0,0,0]) verticalStruts(space_to_window_w,space_to_window_h+window_h, braces = braces);
    translate([space_to_window_w,0,0]) verticalStruts(window_w,space_to_window_h, braces = braces, beams=1);
    translate([0,0,space_to_window_h+window_h]) verticalStruts(space_to_window_w/2,z-(space_to_window_h+window_h), braces = braces);
    translate([space_to_window_w/2,0,space_to_window_h+window_h]) verticalStruts(window_w+space_to_window_w,z-(space_to_window_h+window_h), braces = braces, beams=2);
    translate([space_to_window_w+window_w,0,0]) verticalStruts(space_to_window_w,space_to_window_h+window_h, braces = braces);
    if(door_h-(space_to_window_h+window_h>0))  translate([space_to_window_w/2+window_w+space_to_window_w,0,space_to_window_h+window_h]) verticalStruts(space_to_window_w/2,door_h-(space_to_window_h+window_h), braces = braces);
    translate([space_to_window_w/2+window_w+space_to_window_w,0,door_h]) verticalStruts(x-(space_to_window_w/2+window_w+space_to_window_w),z-door_h, braces = braces, beams=2);
    translate([space_to_window_w+window_w+space_to_window_w+door_w,0,0]) verticalStruts(x-(space_to_window_w+window_w+space_to_window_w+door_w),door_h, braces = braces);
}

module roofJoist(y,timber_width=75,timber_length=47)
{
    cubeI([timber_width,y,timber_length]);
}

module roofJoists(x,y,number_x,timber_width=75,timber_length=47)
{
    x_inside = x - timber_length*2 - timber_width;
    x_inside_part = x_inside / number_x;
    for(base_x = [0 : number_x])
       {
        translate([timber_length+(x_inside_part*base_x),0,-timber_length])  roofJoist(y);
       }
}

module roof(x,y,z_front,z_back, panel_slf_w = 1220, panel_slf_l = 2440, panel_slf_t = base_sheet_t)
{
    opp = z_front-z_back;
    adj = y - base_timber;
    hyp = sqrt(opp*opp+adj*adj);
    theta = atan(opp/adj);
    overhang = (panel_slf_l-hyp)/2;
    overhang_flat = cos(theta) * overhang;
    overhang_up = sin(theta) * overhang;

    // Want it to line up with front of timber truct not back
    height_adjust = base_timber * tan(theta);
    echo("overhang", overhang, opp, hyp, theta, height_adjust, overhang_flat, overhang_up);
    translate([-overhang_flat,-overhang_flat,z_front+base_timber+panel_slf_t+height_adjust+overhang_up]) rotate([-theta,0,0])
    {
        shedLowerFloor(x+overhang_flat*2,panel_slf_l);
        translate([overhang,overhang,0]) roofJoists(x,y,6 );
    }
}

module restOfGarden(offset_x=-381, offset_y=+381 + shed_width)
{
    garden_width= 5947.4;
    garden_length_shed_space = 2440;
    translate([offset_x, offset_y,0])
    {
        color("Green") cube([garden_width, 10, 1830]);
        translate([0,-garden_length_shed_space-381,0]) color("Green") cube([10, garden_length_shed_space+381, 1830]);
        translate([garden_width,-garden_length_shed_space,0]) color("Green") cube([10, garden_length_shed_space, 1830]);
    }
    translate([-381,-3507.5-2592.5-381,0]) color("Green") cube([10, +3507.5+2592.5+381, 1830]);
//    translate([garden_width-381,-3507.5-2592.5-381,0]) color("Green") cube([10, +3507.5+2592.5+381, 1830]);

    translate([-381+0.1,-3507.5-381,0]) color("red") cube([3202.5, 3507.5, 400]);
    translate([-381+0.1,-3507.5-2592.5-381,0]) color("red") cube([4727.5,2592.5, 400]);

    }

if($t>0.05)
    shedBase(shed_length, shed_width, number_x=5,number_y=1, braces=1);

if($t>0.10)
    translate([0,0,base_timber]) floorMembrane(shed_length, shed_width, thickness = 1);

if($t>0.15)
    translate([0,0,base_timber+1]) shedLowerFloor(shed_length, shed_width);

if($t>0.20)
    translate([0,shed_width-base_timber,base_timber+1+base_sheet_t])  backWallStruts(shed_length, shed_front_back, 5);
if($t>0.25)
    translate([base_timber,base_timber,base_timber+1+base_sheet_t])  sideWallStruts(shed_width-base_timber*2,shed_front_back,2, braces=1);

if($t>0.30)
    translate([shed_length,base_timber,base_timber+1+base_sheet_t])  sideWallStruts(shed_width-base_timber*2,shed_front_back,2, braces=1);

if($t>0.35)
    translate([0,0,base_timber+1+base_sheet_t])  frontWallStruts(shed_length,shed_front_height, space_to_window_w=1830/2, space_to_window_h=2030-610, window_w=1830, window_h=610, door_w=870, door_h=2030, braces=0);

if($t>0.40)
    roof(shed_length, shed_width,shed_front_height,shed_front_back);

if($t>0.45)
    translate([0,shed_width+0.5,base_timber])
        breatherMembrane(shed_length, shed_front_back);

if($t>0.50)
    translate([-0.5,0,base_timber])
        breatherMembraneSide(shed_width, shed_front_height);

//shedBaseSection(shed_length/6,shed_width, braces = 1);
if($t>0.65)
    translate([0,shed_width+3,base_timber])
        claddingBackWall(shed_length, shed_front_back);

if($t>0.70)
    translate([-3,0,base_timber])
        claddingLeftSideWall(shed_width, shed_front_height);

if($t>0.75)
    translate([shed_length+3,0,base_timber])
        claddingRightSideWall(shed_width, shed_front_height);

if($t>0.80)
    translate([-3,0,base_timber])
        claddingFrontWall(shed_length,shed_front_height,  space_to_window_w=1830/2, space_to_window_h=2030-610, window_w=1830, window_h=610, door_w=870, door_h=2030);

//restOfGarden();

shed_length = 5184;
shed_width = 1829+305/2;
shed_front_height = 2400;
shed_front_back = 2135;
base_timber = 47;
base_sheet_t = 18;

module cubeI(size,center=false)
{
    echo("Dim: ",size);
    cube(size, center=center);
}

module shedBaseSection(x,y,timber_width=75,timber_length=47, braces=0)
{
    echo("shedBaseSection: x,y,timber_width,timber_length, braces ", x,y,timber_width,timber_length, braces);
    echo("http://www.wickes.co.uk/Wickes-Sawn-Kiln-Dried-47X75-x-3000mm-Single/p/107186#tab-DynamicDeliveryWebTab_content");
    cubeI([x,timber_width,timber_length]);
    translate([0,y-timber_width,0]) cubeI([x,timber_width,timber_length]);
    translate([0,timber_width,0]) cubeI([timber_width,y-timber_width*2,timber_length]);
    translate([x-timber_width,timber_width,0]) cubeI([timber_width,y-timber_width*2,timber_length]);
    if(braces > 0)
    {
        middle_y = y - timber_width*2;
        part_y = middle_y/(braces+1);
        for(loop=[1 : braces ])
        {
                translate([timber_width,timber_width/(braces+1) + (loop*part_y),0]) cubeI([x-timber_width*2, timber_width,timber_length]);
        }
    }
}

module shedBase(x,y,number_x,number_y, braces=1)
{
    for(base_y = [0 : number_y-1])
    {
        for(base_x = [0 : number_x-1])
            {
                translate([base_x*(x/number_x),base_y*(y/number_y),0]) shedBaseSection(x/number_x,y/number_y, braces = braces);
            }
    }
}

module floorMembrane(x,y,thickness,color="Black", overlap=10)
{
    echo("x,y,thickness=1,color, overlap=1",x,y,thickness,color, overlap);
    echo("http://www.screwfix.com/p/dmp-damp-proof-membrane-black-1000ga-3-x-4m/50464#product_additional_details_container");
    translate([-overlap,-overlap,0]) color(color, 1) cube([x+overlap*2,y+overlap*2, thickness]);
}

//module shedLowerFloor(x,y, panel_slf_w = 607, panel_slf_l =   1829, panel_slf_t =   5.5, panel_min_dimentions = 100)
module shedLowerFloor(x,y, panel_slf_w = 1220, panel_slf_l = 2440, panel_slf_t = base_sheet_t, panel_min_dimentions = 100)
{
//    echo("http://www.wickes.co.uk/Wickes-Non-Structural-Hardwood-Plywood-5-5x607x1829mm/p/111196");
    echo("http://www.wickes.co.uk/Wickes-General-Purpose-OSB3-Board-18x1220x2440mm/p/110517");
    x_panels = round(-0.5+x/panel_slf_w);
    y_panels = round(-0.5+y/panel_slf_l);
    echo(y_panels);
    if(y_panels>0) // Panel is Not longer than width
    {
        for(base_y = [0 : y_panels-1]) //Truncate not round
        {
            for(base_x = [0 : x_panels-1])
            {
                translate([base_x*panel_slf_w,base_y*panel_slf_l,0]) cubeI([panel_slf_w, panel_slf_l,panel_slf_t]);
                if(y-(panel_slf_l*y_panels) > panel_min_dimentions)
                translate([base_x*panel_slf_w,y_panels*panel_slf_l,0]) cubeI([panel_slf_w, y-(panel_slf_l*y_panels),panel_slf_t]);
                }
            if(x-(panel_slf_w*x_panels) > panel_min_dimentions)
                translate([(x_panels)*panel_slf_w,(y_panels-1)*panel_slf_l,0]) cubeI([x-(panel_slf_w*x_panels), panel_slf_l,panel_slf_t]);
            if(y-(panel_slf_l*y_panels) > panel_min_dimentions)
                translate([(x_panels)*panel_slf_w,y_panels*panel_slf_l,0]) cubeI([x-(panel_slf_w*x_panels), y-(panel_slf_l*y_panels),panel_slf_t]);
        }
    }
    else // Panel is longer than width
    {
     for(base_x = [0 : x_panels-1])
     {
        translate([base_x*panel_slf_w,0,0]) cubeI([panel_slf_w, y,panel_slf_t]);
     }
     if(x-(panel_slf_w*x_panels) > panel_min_dimentions)
                translate([(x_panels)*panel_slf_w,0,0]) cubeI([x-(panel_slf_w*x_panels), y,panel_slf_t]);
    }
}

module verticalStruts(x,z,timber_width=47,timber_length=47, braces=0, beams=0)
{
//    echo("shedBaseSection(x,z,timber_width,timber_length, braces",x,z,timber_width,timber_length, braces);
//    echo("Walls - http://www.wickes.co.uk/Wickes-Sawn-Kiln-Dried-47-x-47-x-2400mm-Pack-6/p/107114");
    cubeI([x,timber_width,timber_length]);
    translate([0,0,z-timber_width]) cubeI([x,timber_width,timber_length]);
    translate([0,0,timber_width]) cubeI([timber_width,timber_length,z-timber_width*2]);
    translate([x-timber_width,0,timber_width]) cubeI([timber_width,timber_length,z-timber_width*2]);
    if(braces > 0)
    {
        middle_z = z - timber_width*2;
        part_z = middle_z/(braces+1);
        for(loop=[1 : braces ])
        {
                translate([timber_width,0,timber_width/(braces+1) + (loop*part_z)]) cubeI([x-timber_width*2, timber_width,timber_length]);
        }
    }
    if(beams> 0)
    {
        middle_x = x - timber_length*2;
        part_x = middle_x/(beams+1);
        for(loop=[1 : beams ])
        {
                translate([timber_length/(beams+1) + (loop*part_x),0,timber_width]) cubeI([timber_length, timber_width,z-timber_length*2]);
        }
    }
}

module backWallStruts(x,z,number_x, braces=1)
{
    echo("backWallStruts(x,z,number_x, braces)",x,z,number_x, braces);
        for(base_x = [0 : number_x-1])
            {
                translate([base_x*(x/number_x),0,0]) verticalStruts(x/number_x,z, braces = braces);
            }
}

module sideWallStruts(x,z,number_x, braces=1)
{
    echo("sideWallStruts(x,z,number_x, braces)",x,z,number_x, braces);
    rotate([0,0,90]) backWallStruts(x,z,number_x, braces);
}

module prism(l, w, h)
{
    polyhedron(
        points=[[0,0,0], [l,0,0], [l,w,0], [0,w,0], [0,w,h], [l,w,h]],
        faces=[[0,1,2,3],[5,4,3,2],[0,4,5,1],[0,3,4],[5,2,1]]
        );
}

module caddings(x, z, board_height = 150, board_width_b = 11, board_width_s = 6, overlap = 25, degrees=3)
{
    panels = round(-0.5+z/board_height);
    panel_uplift = board_height - overlap;
    for(p = [0 : panels-1])
    {
        translate([0,0,panel_uplift * p])
         cadding(x, board_height, board_width_b, board_width_s, degrees);
    }
}

module cadding(x, board_height, board_width_b, board_width_s, degrees)
{
    translate([0, -board_width_b, 0]) color("Peru",0.6) rotate([-degrees,0,0])
    {
        cube([x, board_width_b - board_width_s, board_height]);
        translate([0,- board_width_s,0]) prism(x, board_width_s, board_height);
    }
}

module frontWallStruts(x,z, space_to_window_w, space_to_window_h, window_w, window_h, door_w, door_h, braces)
{
    echo("frontWallStruts(x,z,space_to_window_w, space_to_window_h, window_w, window_h, door_w, braces)", x,z, space_to_window_w, space_to_window_h, window_w, window_h, door_w, door_h, braces);
    translate([0,0,0]) verticalStruts(space_to_window_w,space_to_window_h+window_h, braces = braces);
    translate([space_to_window_w,0,0]) verticalStruts(window_w,space_to_window_h, braces = braces, beams=1);
    translate([0,0,space_to_window_h+window_h]) verticalStruts(space_to_window_w/2,z-(space_to_window_h+window_h), braces = braces);
    translate([space_to_window_w/2,0,space_to_window_h+window_h]) verticalStruts(window_w+space_to_window_w,z-(space_to_window_h+window_h), braces = braces, beams=2);
    translate([space_to_window_w+window_w,0,0]) verticalStruts(space_to_window_w,space_to_window_h+window_h, braces = braces);
    if(door_h-(space_to_window_h+window_h>0))  translate([space_to_window_w/2+window_w+space_to_window_w,0,space_to_window_h+window_h]) verticalStruts(space_to_window_w/2,door_h-(space_to_window_h+window_h), braces = braces);
    translate([space_to_window_w/2+window_w+space_to_window_w,0,door_h]) verticalStruts(x-(space_to_window_w/2+window_w+space_to_window_w),z-door_h, braces = braces, beams=2);
    translate([space_to_window_w+window_w+space_to_window_w+door_w,0,0]) verticalStruts(x-(space_to_window_w+window_w+space_to_window_w+door_w),door_h, braces = braces);
}

module roofJoist(y,timber_width=75,timber_length=47)
{
    cubeI([timber_width,y,timber_length]);
}

module roofJoists(x,y,number_x,timber_width=75,timber_length=47)
{
    x_inside = x - timber_length*2 - timber_width;
    x_inside_part = x_inside / number_x;
    for(base_x = [0 : number_x])
       {
        translate([timber_length+(x_inside_part*base_x),0,-timber_length])  roofJoist(y);
       }
}

module roof(x,y,z_front,z_back, panel_slf_w = 1220, panel_slf_l = 2440, panel_slf_t = base_sheet_t)
{
    opp = z_front-z_back;
    adj = y - base_timber;
    hyp = sqrt(opp*opp+adj*adj);
    theta = atan(opp/adj);
    overhang = (panel_slf_l-hyp)/2;
    overhang_flat = cos(theta) * overhang;
    overhang_up = sin(theta) * overhang;

    // Want it to line up with front of timber truct not back
    height_adjust = base_timber * tan(theta);
    echo("overhang", overhang, opp, hyp, theta, height_adjust, overhang_flat, overhang_up);
    translate([-overhang_flat,-overhang_flat,z_front+base_timber+panel_slf_t+height_adjust+overhang_up]) rotate([-theta,0,0])
    {
        shedLowerFloor(x+overhang_flat*2,panel_slf_l);
        translate([overhang,overhang,0]) roofJoists(x,y,6 );
    }
}

if($t>0.1)
    shedBase(shed_length, shed_width, number_x=5,number_y=1, braces=1);

if($t>0.2)
    translate([0,0,base_timber]) floorMembrane(shed_length, shed_width, thickness = 1);

if($t>0.3)
    translate([0,0,base_timber+1]) shedLowerFloor(shed_length, shed_width);

if($t>0.4)
    translate([0,shed_width-base_timber,base_timber+1+base_sheet_t])  backWallStruts(shed_length, shed_front_back, 5);
if($t>0.5)
    translate([base_timber,base_timber,base_timber+1+base_sheet_t])  sideWallStruts(shed_width-base_timber*2,shed_front_back,2, braces=1);

if($t>0.6)
    translate([shed_length,base_timber,base_timber+1+base_sheet_t])  sideWallStruts(shed_width-base_timber*2,shed_front_back,2, braces=1);

if($t>0.7)
    translate([0,0,base_timber+1+base_sheet_t])  frontWallStruts(shed_length,shed_front_height, space_to_window_w=1830/2, space_to_window_h=2030-610, window_w=1830, window_h=610, door_w=870, , door_h=2030, braces=0);

if($t>0.8)
    roof(shed_length, shed_width,shed_front_height,shed_front_back);

//shedBaseSection(shed_length/6,shed_width, braces = 1);

!caddings(2000, 500);

//! cadding(2000);


main: use linear_extrusion builtin preview mode

include <config.scad>
use <thing_libutils/screws.scad>
use <thing_libutils/shapes.scad>;
use <thing_libutils/misc.scad>;
use <thing_libutils/transforms.scad>;
use <thing_libutils/attach.scad>;
use <thing_libutils/linear-extrusion.scad>;
use <thing_libutils/timing-belts.scad>;

use <x-end.scad>
include <x-end.h>

use <x-carriage.scad>
include <x-carriage.h>

use <y-motor-mount.scad>
include <y-motor-mount.h>

use <y-carriage-bearing-mount.scad>
include <y-carriage-bearing-mount.h>

use <y-carriage-belt-clamp.scad>
include <y-carriage-belt-clamp.h>

use <y-idler.scad>
include <y-idler.h>

/*use <z-motor-mount.scad>*/
/*include <z-motor-mount.h>*/

use <gantry-upper-connector.scad>
use <gantry-lower-connector.scad>

use <rod-clamps.scad>
include <rod-clamps.h>

/*use <psu.scad>*/
/*include <psu.h>*/

/*use <lcd2004.scad>*/
/*use <power-panel-iec320.scad>*/

// x carriage
axis_range_x_ = main_width/2 + zmotor_mount_rod_offset_x - xaxis_end_width(true) - x_carriage_w/2 - .5*mm;
axis_range_x = [-1,1] *  axis_range_x_;
axis_printrange_x = [-1/2, 1/2] * printbed_size[0];
axis_x_parked = [false, true];

axis_range_y=[0*mm,200*mm];
axis_pos_y = axis_range_y[0];
axis_range_z=[90*mm,353*mm];
axis_pos_z = axis_range_z[0];

echo(str("Axis range X: " , axis_range_x[0], " ", axis_range_x[1]," mm"));
echo(str("Axis range Y: " , axis_range_y[0], " ", axis_range_y[1]," mm"));
echo(str("Axis range Z: " , axis_range_z[0], " ", axis_range_z[1]," mm"));

echo(str("Build area Z: " , axis_range_z[1]-axis_range_z[0] , " mm"));

home_offset_x0 = axis_range_x[0] - axis_printrange_x[0] + v_x(extruder_filapath_offset)[0];
echo(str("Home offset X0: " , home_offset_x0));
echo(str("Tool offset X: " , axis_range_x_+x_carriage_w/2));

module x_axis()
{
    // x axis
    translate([0,0,axis_pos_z])
    {
        zrod_offset = zmotor_mount_rod_offset_x;
        for(z=[-1,1])
        for(z=xaxis_beltpath_z_offsets)
        translate([-main_width/2-zrod_offset+xaxis_end_motor_offset[0], xaxis_zaxis_distance_y, z])
        rotate(90*X)
        belt_path(
            len=main_width+2*(zrod_offset)+xaxis_end_motor_offset[0],
            belt_width=xaxis_belt_width,
            belt=xaxis_belt,
            pulley_d=xaxis_pulley_inner_d,
            orient=X, align=X);

        for(x=[0:len(axis_range_x)-1])
        {
            /*#cubea(size=[x_carriage_w,10,100]);*/

            pos = axis_x_parked[x] ? axis_range_x[x] : -axis_printrange_x[x];

            translate([pos,0,0])
            mirror([x==0?0:1,0,0])
            attach(xaxis_carriage_conn, [[0,-xaxis_zaxis_distance_y,0],N])
            {
                x_carriage_withmounts(show_vitamins=true, beltpath_offset=x==0?-1:1);

                x_carriage_extruder(show_vitamins=true, with_sensormount=x==0);
            }
        }

        // x smooth rods
        color(color_rods)
        for(z=[-1,1])
            translate([xaxis_rod_offset_x,xaxis_zaxis_distance_y,z*(xaxis_rod_distance/2)])
                cylindera(h=xaxis_rod_l,d=xaxis_rod_d+.1, orient=X);

        for(x=[-1,1])
        {
            translate([x*(main_width/2), 0, 0])
            translate([0, xaxis_zaxis_distance_y, 0])
            translate([x*zmotor_mount_rod_offset_x, 0, 0])
            {
                mirror([max(0,x),0,0])
                xaxis_end(with_motor=true, beltpath_index=max(0,x), show_nut=true, show_motor=true, show_nut=true);
            }
        }
    }
}

module y_axis()
{
    // y axis
    color(color_part)
    attach([[yaxis_belt_path_offset_x,main_depth/2,yaxis_motor_offset_z], [0,-1,0]], yaxis_motor_mount_conn)
    {
        yaxis_motor_mount(show_motor=true);
    }

    extrusion_idler_conn = [[yaxis_belt_path_offset_x, -main_depth/2-extrusion_size, -extrusion_size+yaxis_idler_offset_z], [0,-1,0]];
    attach(extrusion_idler_conn, yaxis_idler_conn)
    {
        color(color_part)
        yaxis_idler();

        attach(yaxis_idler_conn_pulleyblock, yaxis_idler_pulleyblock_conn)
        {
            color(color_part)
            yaxis_idler_pulleyblock(show_pulley=true);
        }
    }

    translate([yaxis_belt_path_offset_x,0,yaxis_belt_path_offset_z])
    {
        translate([0,main_depth/2-yaxis_motor_offset_x,0])
        {
            rotate(90*Y)
            belt_path(
                len=main_depth-yaxis_motor_offset_x-yaxis_idler_pulley_offset_y,
                belt_width=yaxis_belt_width,
                pulley_d=yaxis_pulley_inner_d,
                belt=yaxis_belt,
                align=-Y, orient=Y);
        }
    }

    color(color_part)
    translate([0,-axis_pos_y,get(LinearBearingInnerDiameter, yaxis_bearing)/2])
    attach(yaxis_carriage_bearing_mount_conn_bearing, yaxis_belt_mount_conn)
    {
        yaxis_belt_holder();
    }

    translate([0,0,get(LinearBearingInnerDiameter, yaxis_bearing)/2])
    {
        // y smooth rod clamps to frame
        for(x=[-1,1])
        for(y=[-1,1])
        {
            attach([[x*(yaxis_rod_distance/2),y*(main_depth/2+extrusion_size/2),0],Z],mount_rod_clamp_conn_rod)
            {
                mount_rod_clamp_full(rod_d=zaxis_rod_d, thick=4, width=extrusion_size, thread=zmotor_mount_clamp_thread, orient=Y);
            }
        }

        // y smooth rods
        for(x=[-1,1])
        translate([x*(yaxis_rod_distance/2), 0, 0])
        {
            color(color_rods)
            cylindera(h=yaxis_rod_l,d=yaxis_rod_d, orient=Y);

            for(y=[-1,1])
            {
                attach([[0,y*(yaxis_bearing_distance_y/2)-axis_pos_y,0],[0,0,-1]], yaxis_carriage_bearing_mount_conn_bearing)
                {
                    yaxis_carriage_bearing_mount();
                    %yaxis_carriage_bearing_mount(part="vit");
                }
            }
        }

        attach(yaxis_carriage_bearing_mount_conn_bearing, [[0,axis_pos_y,0],Z])
        {
            yaxis_carriage_size_inner = [32*mm,32*mm,0];

            // y axis plate "arms"
            for(x=[-1,1])
            for(y=[-1,1])
            hull()
            {
                translate([x*yaxis_carriage_size[0]/2, y*yaxis_carriage_size[1]/2, 0])
                {
                    cylindera(d=10*mm, h=yaxis_carriage_size[2], align=[-x,-y,1]);
                }
                translate([x*(yaxis_carriage_size[0]/2-yaxis_carriage_size_inner[0]/2), y*(yaxis_carriage_size[1]/2-yaxis_carriage_size_inner[1]/2*mm), 0])
                {
                    cylindera(d=10*mm, h=yaxis_carriage_size[2], align=[-x,-y,1]);
                }
            }

            // y axis plate
            cubea(yaxis_carriage_size - yaxis_carriage_size_inner, align=Z);

            translate([0,0,10*mm])
                cubea(printbed_size, align=Z);
        }
    }

}

module z_axis()
{
    // z axis
    for(x=[-1,1])
    {
        translate([x*(main_width/2),0,main_height])
        {
            mirror([x==-1?1:0,0,0])
            {
                color(color_part)
                translate([zmotor_mount_rod_offset_x, 0, extrusion_size/2])
                {
                    mount_rod_clamp_half(
                            rod_d=zaxis_rod_d,
                            screw_dist=zmotor_mount_clamp_dist,
                            thick=5,
                            base_thick=5,
                            width=zmotor_mount_thickness_h,
                            thread=zmotor_mount_clamp_thread);
                }
            }
        }

        translate([x*(main_width/2), 0, 0])
        {
            translate([0,0,zaxis_motor_offset_z])
                mirror([x==-1?1:0,0,0])
                {
                    color(color_part)
                    zaxis_motor_mount(show_motor=true);

                    color(color_part)
                    translate([zmotor_mount_rod_offset_x, 0, zmotor_mount_thickness_h/2])
                    {
                        mount_rod_clamp_half(
                                rod_d=zaxis_rod_d,
                                screw_dist=zmotor_mount_clamp_dist,
                                thick=5,
                                base_thick=5,
                                width=zmotor_mount_thickness_h,
                                thread=zmotor_mount_clamp_thread);
                    }
                }

            // z smooth rods
            translate([x*(zmotor_mount_rod_offset_x),0,0])
            {
                // z rods
                color(color_rods)
                translate([0,0,zaxis_motor_offset_z-80*mm])
                    cylindera(h=zaxis_rod_l,d=zaxis_rod_d, align=Z);

            }
        }
    }

}

module gantry_upper()
{
    color(color_extrusion)
    for(y=[-1,1])
    translate([0,y*(main_upper_dist_y/2),0])
    {
        for(x=[-1,1])
        translate([x*(main_width/2), 0, 0])
        {
            linear_extrusion(h=main_height, align=[-x,0,1], orient=Z);
        }

        translate([0, 0, main_height])
        {
            linear_extrusion(h=main_upper_width, align=Z, orient=X);
        }
    }

    // upper gantry connectors
    for(x=[-1,1])
    {
        translate([x*(main_width/2),0,main_height])
        {
            mirror([x==-1?1:0,0,0])
            {
                color(color_gantry_connectors)
                    gantry_upper_connector();
            }

        }
    }
}

module gantry_lower()
{
    color(color_extrusion)
    for(z=[-1,1])
    {
        translate([0,0,z*-main_lower_dist_z/2])
        {
            for(y=[-1,1])
            translate([0, y*(main_depth/2), 0])
            {
                linear_extrusion(h=main_width, align=[0,y,-1], orient=X);
            }

            for(x=[-1,1])
            translate([x*(main_width/2), 0, 0])
            {
                cubea([extrusion_size, main_depth, extrusion_size], align=[-x,0,-1]);
            }
        }

    }

    // lower gantry connectors
    for(x=[-1,1])
    for(y=[-1,1])
    {
        translate([x*(main_width/2),y*(main_depth/2),-extrusion_size/2])
        {
            mirror([x==1?0:-1,0,0])
            mirror([0,y==1?1:0,0])
            {
                color(color_gantry_connectors)
                    gantry_lower_connector();
            }

        }
    }
}

module enclosure()
{
    // inner
    w=56*cm;
    h=56*cm;
    d=58*cm;
    wallthick=2*cm;
    backthick=0.5*cm;

    translate([0, 0, h/2+wallthick])
    {
        // left/right walls
        for(x=[-1,1])
        translate([x*w/2,0,0])
        cubea([wallthick,d,h], align=[x, 0, 0]);

        // back plate
        translate([0,d/2,0])
        cubea([w,backthick,h], align=[0,-1, 0]);

        // top/bottom plate
        for(z=[-1,1])
        translate([0,0,z*h/2])
        cubea([w+wallthick*2,d,wallthick], align=[0,0, z]);
    }
}

module main()
{
    translate([0,0,-main_lower_dist_z/2])
    gantry_lower();

    translate(-zaxis_rod_offset)
    gantry_upper();

    x_axis();
    y_axis();
    z_axis();

    translate([-75*mm,0,0])
    translate([0,main_depth/2,0])
    translate([0,extrusion_size,0])
    translate([0,0,-main_lower_dist_z/2-extrusion_size/2])
    power_panel_iec320(orient=[0,-1,0], align=Y);

    if(!$preview_mode)
    {
        translate([0,-main_depth/2,0])
        translate([0,-extrusion_size/2,0])
        translate([0,0,-main_lower_dist_z/2])
        mount_lcd2004(show_gantry=true);

        for(x=[-1,1])
        translate([x*(main_width/2-extrusion_size),main_depth/2,-main_lower_dist_z-extrusion_size])
        {
            translate([-x*psu_a_w/2, -psu_a_d/2, psu_a_h/2])
            {
                psu_a();

                mirror([x==-1?1:0,0,0])
                    translate([0, -psu_a_screw_dist_y/2, 0])
                    psu_a_extrusion_bracket_side();

                translate([0, psu_a_screw_dist_y/2, 0])
                    psu_a_extrusion_bracket_back();
            }
        }

        translate([0,-main_depth/2,-main_lower_dist_z-extrusion_size])
            translate([-100,100,0])
            rotate([0,0,270])
            import("stl/RAMPS1_4.STL");
    }
}

feet_height = 8*mm;
translate([0, 0, main_lower_dist_z+extrusion_size*2+feet_height])
main();

/*psu();*/

/*%enclosure();*/

include <config.scad>
use <thing_libutils/screws.scad>
use <thing_libutils/shapes.scad>;
use <thing_libutils/misc.scad>;
use <thing_libutils/transforms.scad>;
use <thing_libutils/attach.scad>;
use <thing_libutils/linear-extrusion.scad>;
use <thing_libutils/timing-belts.scad>;

use <x-end.scad>
include <x-end.h>

use <x-carriage.scad>
include <x-carriage.h>

use <y-motor-mount.scad>
include <y-motor-mount.h>

use <y-carriage-bearing-mount.scad>
include <y-carriage-bearing-mount.h>

use <y-carriage-belt-clamp.scad>
include <y-carriage-belt-clamp.h>

use <y-idler.scad>
include <y-idler.h>

/*use <z-motor-mount.scad>*/
/*include <z-motor-mount.h>*/

use <gantry-upper-connector.scad>
use <gantry-lower-connector.scad>

use <rod-clamps.scad>
include <rod-clamps.h>

/*use <psu.scad>*/
/*include <psu.h>*/

/*use <lcd2004.scad>*/
/*use <power-panel-iec320.scad>*/

// x carriage
axis_range_x_ = main_width/2 + zmotor_mount_rod_offset_x - xaxis_end_width(true) - x_carriage_w/2 - .5*mm;
axis_range_x = [-1,1] *  axis_range_x_;
axis_printrange_x = [-1/2, 1/2] * printbed_size[0];
axis_x_parked = [false, true];

axis_range_y=[0*mm,200*mm];
axis_pos_y = axis_range_y[0];
axis_range_z=[90*mm,353*mm];
axis_pos_z = axis_range_z[0];

echo(str("Axis range X: " , axis_range_x[0], " ", axis_range_x[1]," mm"));
echo(str("Axis range Y: " , axis_range_y[0], " ", axis_range_y[1]," mm"));
echo(str("Axis range Z: " , axis_range_z[0], " ", axis_range_z[1]," mm"));

echo(str("Build area Z: " , axis_range_z[1]-axis_range_z[0] , " mm"));

home_offset_x0 = axis_range_x[0] - axis_printrange_x[0] + v_x(extruder_filapath_offset)[0];
echo(str("Home offset X0: " , home_offset_x0));
echo(str("Tool offset X: " , axis_range_x_+x_carriage_w/2));

module x_axis()
{
    // x axis
    translate([0,0,axis_pos_z])
    {
        zrod_offset = zmotor_mount_rod_offset_x;
        for(z=[-1,1])
        for(z=xaxis_beltpath_z_offsets)
        translate([-main_width/2-zrod_offset+xaxis_end_motor_offset[0], xaxis_zaxis_distance_y, z])
        rotate(90*X)
        belt_path(
            len=main_width+2*(zrod_offset)+xaxis_end_motor_offset[0],
            belt_width=xaxis_belt_width,
            belt=xaxis_belt,
            pulley_d=xaxis_pulley_inner_d,
            orient=X, align=X);

        for(x=[0:len(axis_range_x)-1])
        {
            /*#cubea(size=[x_carriage_w,10,100]);*/

            pos = axis_x_parked[x] ? axis_range_x[x] : -axis_printrange_x[x];

            translate([pos,0,0])
            mirror([x==0?0:1,0,0])
            attach(xaxis_carriage_conn, [[0,-xaxis_zaxis_distance_y,0],N])
            {
                x_carriage_withmounts(show_vitamins=true, beltpath_offset=x==0?-1:1);

                x_carriage_extruder(show_vitamins=true, with_sensormount=x==0);
            }
        }

        // x smooth rods
        color(color_rods)
        for(z=[-1,1])
            translate([xaxis_rod_offset_x,xaxis_zaxis_distance_y,z*(xaxis_rod_distance/2)])
                cylindera(h=xaxis_rod_l,d=xaxis_rod_d+.1, orient=X);

        for(x=[-1,1])
        {
            translate([x*(main_width/2), 0, 0])
            translate([0, xaxis_zaxis_distance_y, 0])
            translate([x*zmotor_mount_rod_offset_x, 0, 0])
            {
                mirror([max(0,x),0,0])
                xaxis_end(with_motor=true, beltpath_index=max(0,x), show_nut=true, show_motor=true, show_nut=true);
            }
        }
    }
}

module y_axis()
{
    // y axis
    color(color_part)
    attach([[yaxis_belt_path_offset_x,main_depth/2,yaxis_motor_offset_z], [0,-1,0]], yaxis_motor_mount_conn)
    {
        yaxis_motor_mount(show_motor=true);
    }

    extrusion_idler_conn = [[yaxis_belt_path_offset_x, -main_depth/2-extrusion_size, -extrusion_size+yaxis_idler_offset_z], [0,-1,0]];
    attach(extrusion_idler_conn, yaxis_idler_conn)
    {
        color(color_part)
        yaxis_idler();

        attach(yaxis_idler_conn_pulleyblock, yaxis_idler_pulleyblock_conn)
        {
            color(color_part)
            yaxis_idler_pulleyblock(show_pulley=true);
        }
    }

    translate([yaxis_belt_path_offset_x,0,yaxis_belt_path_offset_z])
    {
        translate([0,main_depth/2-yaxis_motor_offset_x,0])
        {
            rotate(90*Y)
            belt_path(
                len=main_depth-yaxis_motor_offset_x-yaxis_idler_pulley_offset_y,
                belt_width=yaxis_belt_width,
                pulley_d=yaxis_pulley_inner_d,
                belt=yaxis_belt,
                align=-Y, orient=Y);
        }
    }

    color(color_part)
    translate([0,-axis_pos_y,get(LinearBearingInnerDiameter, yaxis_bearing)/2])
    attach(yaxis_carriage_bearing_mount_conn_bearing, yaxis_belt_mount_conn)
    {
        yaxis_belt_holder();
    }

    translate([0,0,get(LinearBearingInnerDiameter, yaxis_bearing)/2])
    {
        // y smooth rod clamps to frame
        for(x=[-1,1])
        for(y=[-1,1])
        {
            attach([[x*(yaxis_rod_distance/2),y*(main_depth/2+extrusion_size/2),0],Z],mount_rod_clamp_conn_rod)
            {
                mount_rod_clamp_full(rod_d=zaxis_rod_d, thick=4, width=extrusion_size, thread=zmotor_mount_clamp_thread, orient=Y);
            }
        }

        // y smooth rods
        for(x=[-1,1])
        translate([x*(yaxis_rod_distance/2), 0, 0])
        {
            color(color_rods)
            cylindera(h=yaxis_rod_l,d=yaxis_rod_d, orient=Y);

            for(y=[-1,1])
            {
                attach([[0,y*(yaxis_bearing_distance_y/2)-axis_pos_y,0],[0,0,-1]], yaxis_carriage_bearing_mount_conn_bearing)
                {
                    yaxis_carriage_bearing_mount();
                    %yaxis_carriage_bearing_mount(part="vit");
                }
            }
        }

        attach(yaxis_carriage_bearing_mount_conn_bearing, [[0,axis_pos_y,0],Z])
        {
            yaxis_carriage_size_inner = [32*mm,32*mm,0];

            // y axis plate "arms"
            for(x=[-1,1])
            for(y=[-1,1])
            hull()
            {
                translate([x*yaxis_carriage_size[0]/2, y*yaxis_carriage_size[1]/2, 0])
                {
                    cylindera(d=10*mm, h=yaxis_carriage_size[2], align=[-x,-y,1]);
                }
                translate([x*(yaxis_carriage_size[0]/2-yaxis_carriage_size_inner[0]/2), y*(yaxis_carriage_size[1]/2-yaxis_carriage_size_inner[1]/2*mm), 0])
                {
                    cylindera(d=10*mm, h=yaxis_carriage_size[2], align=[-x,-y,1]);
                }
            }

            // y axis plate
            cubea(yaxis_carriage_size - yaxis_carriage_size_inner, align=Z);

            translate([0,0,10*mm])
                cubea(printbed_size, align=Z);
        }
    }

}

module z_axis()
{
    // z axis
    for(x=[-1,1])
    {
        translate([x*(main_width/2),0,main_height])
        {
            mirror([x==-1?1:0,0,0])
            {
                color(color_part)
                translate([zmotor_mount_rod_offset_x, 0, extrusion_size/2])
                {
                    mount_rod_clamp_half(
                            rod_d=zaxis_rod_d,
                            screw_dist=zmotor_mount_clamp_dist,
                            thick=5,
                            base_thick=5,
                            width=zmotor_mount_thickness_h,
                            thread=zmotor_mount_clamp_thread);
                }
            }
        }

        translate([x*(main_width/2), 0, 0])
        {
            translate([0,0,zaxis_motor_offset_z])
                mirror([x==-1?1:0,0,0])
                {
                    color(color_part)
                    zaxis_motor_mount(show_motor=true);

                    color(color_part)
                    translate([zmotor_mount_rod_offset_x, 0, zmotor_mount_thickness_h/2])
                    {
                        mount_rod_clamp_half(
                                rod_d=zaxis_rod_d,
                                screw_dist=zmotor_mount_clamp_dist,
                                thick=5,
                                base_thick=5,
                                width=zmotor_mount_thickness_h,
                                thread=zmotor_mount_clamp_thread);
                    }
                }

            // z smooth rods
            translate([x*(zmotor_mount_rod_offset_x),0,0])
            {
                // z rods
                color(color_rods)
                translate([0,0,zaxis_motor_offset_z-80*mm])
                    cylindera(h=zaxis_rod_l,d=zaxis_rod_d, align=Z);

            }
        }
    }

}

module gantry_upper()
{
    color(color_extrusion)
    for(y=[-1,1])
    translate([0,y*(main_upper_dist_y/2),0])
    {
        for(x=[-1,1])
        translate([x*(main_width/2), 0, 0])
        {
            if($preview_mode)
            {
                cubea(size=[extrusion_size, extrusion_size, main_height], align=[-x,0,1]);
            }
            else
            {
                linear_extrusion(h=main_height, align=[-x,0,1], orient=Z);
            }
        }

        translate([0, 0, main_height])
        {
            if($preview_mode)
            {
                cubea(size=[main_upper_width, extrusion_size, extrusion_size], align=Z);
            }
            else
            {
                linear_extrusion(h=main_upper_width, align=Z, orient=X);
            }
        }
    }

    // upper gantry connectors
    for(x=[-1,1])
    {
        translate([x*(main_width/2),0,main_height])
        {
            mirror([x==-1?1:0,0,0])
            {
                color(color_gantry_connectors)
                    gantry_upper_connector();
            }

        }
    }
}

module gantry_lower()
{
    color(color_extrusion)
    for(z=[-1,1])
    {
        translate([0,0,z*-main_lower_dist_z/2])
        {
            for(y=[-1,1])
            translate([0, y*(main_depth/2), 0])
            {
                if($preview_mode)
                {
                    cubea([main_width, extrusion_size, extrusion_size], align=[0,y,-1]);
                }
                else
                {
                    linear_extrusion(h=main_width, align=[0,y,-1], orient=X);
                }
            }

            for(x=[-1,1])
            translate([x*(main_width/2), 0, 0])
            {
                if($preview_mode)
                {
                    cubea([extrusion_size, main_depth, extrusion_size], align=[-x,0,-1]);
                }
                else
                {
                    linear_extrusion(h=main_depth, align=[-x,0,-1], orient=Y);
                }
            }
        }

    }

    // lower gantry connectors
    for(x=[-1,1])
    for(y=[-1,1])
    {
        translate([x*(main_width/2),y*(main_depth/2),-extrusion_size/2])
        {
            mirror([x==1?0:-1,0,0])
            mirror([0,y==1?1:0,0])
            {
                color(color_gantry_connectors)
                    gantry_lower_connector();
            }

        }
    }
}

module enclosure()
{
    // inner
    w=56*cm;
    h=56*cm;
    d=58*cm;
    wallthick=2*cm;
    backthick=0.5*cm;

    translate([0, 0, h/2+wallthick])
    {
        // left/right walls
        for(x=[-1,1])
        translate([x*w/2,0,0])
        cubea([wallthick,d,h], align=[x, 0, 0]);

        // back plate
        translate([0,d/2,0])
        cubea([w,backthick,h], align=[0,-1, 0]);

        // top/bottom plate
        for(z=[-1,1])
        translate([0,0,z*h/2])
        cubea([w+wallthick*2,d,wallthick], align=[0,0, z]);
    }
}

module main()
{
    translate([0,0,-main_lower_dist_z/2])
    gantry_lower();

    translate(-zaxis_rod_offset)
    gantry_upper();

    x_axis();
    y_axis();
    z_axis();

    translate([-75*mm,0,0])
    translate([0,main_depth/2,0])
    translate([0,extrusion_size,0])
    translate([0,0,-main_lower_dist_z/2-extrusion_size/2])
    power_panel_iec320(orient=[0,-1,0], align=Y);

    if(!$preview_mode)
    {
        translate([0,-main_depth/2,0])
        translate([0,-extrusion_size/2,0])
        translate([0,0,-main_lower_dist_z/2])
        mount_lcd2004(show_gantry=true);

        for(x=[-1,1])
        translate([x*(main_width/2-extrusion_size),main_depth/2,-main_lower_dist_z-extrusion_size])
        {
            translate([-x*psu_a_w/2, -psu_a_d/2, psu_a_h/2])
            {
                psu_a();

                mirror([x==-1?1:0,0,0])
                    translate([0, -psu_a_screw_dist_y/2, 0])
                    psu_a_extrusion_bracket_side();

                translate([0, psu_a_screw_dist_y/2, 0])
                    psu_a_extrusion_bracket_back();
            }
        }

        translate([0,-main_depth/2,-main_lower_dist_z-extrusion_size])
            translate([-100,100,0])
            rotate([0,0,270])
            import("stl/RAMPS1_4.STL");
    }
}

feet_height = 8*mm;
translate([0, 0, main_lower_dist_z+extrusion_size*2+feet_height])
main();

/*psu();*/

/*%enclosure();*/