Exit Full View
Up

/Kobo/Old-Buttons.foocad

Old-Buttons
FooCAD Source Code
class Buttons : Model {
    
    @Custom
    var piece = "all"

    var slack = 0.5

    var thickness = 4
    var raised = 1

    var extra = 3
    val extraThickness = 1

    fun outline() : Shape2d {
        //println( SVGParser().parseFile("kobo.svg").shapes )
        return SVGParser().parseFile("kobo.svg").shapes["buttons"].center()
        //return Square( width - slack*2, height - slack*2 ).center()
        //    .roundAllCorners(radius-slack)
    }

    fun button( profile : Shape2d, middle : bool ) : Shape3d {
        return ExtrusionBuilder().apply {
            crossSection(profile.offset( extraThickness ))
            forward(extraThickness)
            if (!middle) crossSection( )
            crossSection( profile )
            forward( thickness - extraThickness )
            crossSection()
            forward( raised )
            crossSection( profile.offset( -raised / 2 ) )
        }.build()
    }
        

    fun explode( shape : Shape3d ) : Shape3d {
        return shape.translate(
            0.3*(shape.corner.x + shape.size.x/2),
            0.3*(shape.corner.y + shape.size.y/2),
            0
        )
    }

    fun exclusion( shape : Shape2d ) : Shape3d {
        return ExtrusionBuilder().apply {
            forward( -0.1 )
            crossSection( shape.offset( extraThickness*1.5 ) )
            forward( extraThickness*1.5 + 0.2 )
            crossSection( -extraThickness*1.5 )
        }.build().color( "Red" )
    }

    fun scale(buttonP : Shape2d) = buttonP.scale( 1.4,1.25 )

    override fun build() : Shape3d {
        
        val middleP = scale(Square( 6 ).center())

        val upP1 = PolygonBuilder().apply {
            moveTo( -3, 3 )
            lineTo( 3, 3 )
            lineTo( 2, 9 )
            lineTo( -2, 9 )
        }.build()
       
        val rightP1 = PolygonBuilder().apply {
            moveTo( 3, -3 )
            lineTo( 9, -2.5 )
            lineTo( 9, 2.5 )
            lineTo( 3, 3 )
        }.build()

        val upP = scale(upP1)
        val rightP = scale(rightP1)

        val middleExclusion = exclusion( middleP )

        val middle = button( middleP, true )
        val up = button( upP, false ) - middleExclusion.scaleX(4)
        val right = button( rightP, false ) - middleExclusion.scaleY(4)
        val down = up.mirrorY()
        val left = right.mirrorX()

        val center = middleP.offset(slack) +
            (upP.offset(slack).mirrorY().also()) +
            (rightP.offset(slack).mirrorX().also())

        val hole = center.extrude(40).center().color("Red")
        val cover = outline()
            .extrude( thickness - extraThickness )
            .translateZ(extraThickness+0.1)
            .color("Green") -
                hole
        val base = outline().offset(extra)
            .extrude( extraThickness )
            .color("LightGreen") -
            center.offset(extraThickness+0.2).extrude(extraThickness+1).translateZ(-0.5)
   
        
        // The tramlines were designed to make the button press easier, by
        // pressing along the middle of the switch, rather than across the
        // whole of the switch's surface.
        // Not sure if it works!
        val tramLines = (
            Cube( 100, 2, 0.6 ).centerX()
                .translateY(0.5).mirrorY().also().color("Blue") +
            Cube(2,100,0.6)
                .translateY(6).mirrorY().also()
                .translateX(0.5).mirrorX().also().color("LightBlue")
            ).translateZ(1000)

        val buttons = explode(middle) + 
            explode(up) + explode(right) +
            explode(left) + explode(down)

        return if (piece == "base") {
            base
        } else if (piece == "cover") {
            cover
        } else if (piece == "buttons" ) {
            buttons-tramLines
        } else if (piece == "center" ) {
            middle - tramLines
        } else if (piece == "up" ) {
            up - tramLines
        } else if (piece == "left" ) {
            left - tramLines
            
        } else {
            buttons-tramLines + 
                base.rotateX(180).translateY(35).toOriginZ() +
                cover.rotateY(180).translateX(35).toOriginZ()
        }

    }
}