Exit Full View
Up

/TableShelf.foocad

TableShelf
FooCAD Source Code
import static uk.co.nickthecoder.foocad.arrange.v1.Arrange.*
import static uk.co.nickthecoder.foocad.layout.v1.Layout2d.*
import static uk.co.nickthecoder.foocad.layout.v1.Layout3d.*

/**
    A shallow drawer for my Mum's over-bed table.
*/
class TableDrawer : Model {
    
    @Custom
    var size = Vector3( 150, 180, 30 )

    @Custom( about="Unused space, so that the draw can be fully extended, without it coming out" )
    var unused = 40

    @Custom( about="The thickness of the plywood base, plus a little extra slack" )
    var baseThickness = 6.2

    val thickness = 4
    val frontThickness = 2

    val chunk = 10
    val rabbitSize = Vector2( 8, 8 )
    val slack = Vector2(0.5, 0.5)
    val curveRadius = (size.z - thickness)/2

    fun rabbit() = Cube( rabbitSize.x + 1, size.y *2, rabbitSize.y )
            .leftTo( -size.x/2 - thickness - 1)
            .centerZTo( size.z / 2 )
            .centerY()
            .color("Red")


    fun curve() : Shape3d {
        val length = size.x - chunk*2 - slack.x
        val curve = Cube( length, curveRadius, curveRadius ).centerX() -
            Cylinder( length+2, curveRadius ).rotateY(90).toOrigin().centerX()

        return curve
    }

    fun front() : Shape3d {
        val front = Square( size.x + thickness*4 + slack.x*2, size.z )
            .roundAllCorners(2)
            .extrude( frontThickness )
            .rotateX(90)
            .centerX().backTo( -size.y/2 )

        val curve = curve().frontTo(front.back).bottomTo(baseThickness)

        return (front + curve).color( "Green" )
    }

    fun back() : Shape3d {
        val length = size.x - chunk*2 - slack.x
        val back = Square( length, size.z - thickness )
            .roundAllCorners(2)
            .extrude( frontThickness )
            .rotateX(90)
            .centerX().frontTo( size.y/2 )

        val curve = curve().mirrorY().backTo(back.front).bottomTo(baseThickness)

        return (back + curve).color( "Green" )
    }

    fun side() : Shape3d {
        val side = Cube( thickness, size.y, size.z - thickness - slack.y )
            .rightTo(0)
            .translateX( -size.x / 2 )
            .centerY()

        val forBase = Cube( chunk, size.y, size.z - baseThickness - thickness - slack.y )
            .bottomTo( baseThickness )
            .leftTo(side.right)
            .centerY()

        val holes = Cylinder( chunk, 1.5 ).sides(4)
            .centerXTo( forBase.middle.x )
            .spreadY( 3, size.y - 20 )
            .centerY()

        return (side + forBase).color("Orange") - rabbit() //- holes
    }

    fun runner() : Shape3d {
        val rabbit = rabbit()
        val inRabbit = Cube( rabbitSize.x, size.y, rabbitSize.y - slack.y*2 )
            .frontTo(-size.y/2)
            .centerZTo( rabbit.middle.z)
            .leftTo( -size.x / 2 - thickness )
        val side = Cube( thickness, inRabbit.size.y, size.z - inRabbit.bottom )
            .rightTo( inRabbit.left )
            .frontTo( inRabbit.front )
            .bottomTo( inRabbit.bottom )
        val top = Cube( chunk + thickness,  inRabbit.size.y, thickness )
            .topTo(side.top)
            .frontTo(side.front)
            .leftTo(side.right)
        return (inRabbit + side + top).color( "Blue" ).translateX(-slack.x)
    }

    @Piece
    fun printRunner() = runner().rotateY(-90).bottomTo(0)

    @Piece
    fun printSide() = side().rotateY(-90).bottomTo(0)

    @Piece
    fun printFront() = front().rotateX(90).bottomTo(0)

    @Piece
    fun printBack() = back().rotateX(-90).bottomTo(0)

    override fun build() : Shape3d {

        val open = 0// 120

        val base = Cube( size.x, size.y - unused, baseThickness )
            .centerX().frontTo(-size.y/2)
            .previewOnly()

        println( "Base = ${base.size}" )

        val side = side()

        val draw =  base + side.mirrorX().also() + front() + back() .translateY(-unused)
        return draw.translateY(-open) +
            runner().translateX(-0).mirrorX().also()
    }

    @Piece
    fun all() = arrangeX( 2,
        printSide(),
        printSide(),
        printRunner(),
        printRunner(),
        printFront().rotateZ(90),
        printBack().rotateZ(90)
    )
    
}