Exit Full View
Up

/Torch/VapeTorch.foocad

VapeTorch
FooCAD Source Code
import uk.co.nickthecoder.foocad.smartextrusion.v1.*
import static uk.co.nickthecoder.foocad.smartextrusion.v1.SmartExtrusion.*
import static uk.co.nickthecoder.foocad.chamferedextrude.v1.ChamferedExtrude.*
import static uk.co.nickthecoder.foocad.arrange.v1.Arrange.*
class VapeTorch : Model {

    var batterySize = Vector2( 40, 14 )

    var chargerSize = Vector3( 18, 29, 4 )

    var clearance = 0.1
    var thickness = 1.2
    var lipSize = 3
    var usbSize = Vector2( 10, 3.5 )
    var switchBodySize = Vector2( 11, 6 )
    var switchHeadSize = Vector3( 3.5, 3.5, 6 )

    fun battery() = Circle( batterySize.y/2 ).extrude( batterySize.x ).previewOnly()
    fun chargerModule() : Shape3d {
        val board = Square( chargerSize.x + 0.1, chargerSize.y ).centerX().extrude(2)
        val usb = Cube( 6, 6, 3 ).centerX().frontTo( board.front - 2 ).bottomTo( board.top )
        return (board + usb).color("Green")
    }
        
    fun profile() = Square( chargerSize.x, chargerSize.z ).centerX() hull
            Circle( batterySize.y / 2 ).frontTo( chargerSize.z )

    @Piece
    fun chargerEnd() : Shape3d {
        val profile = profile()
        val height = Math.max( chargerSize.y, batterySize.x )  + thickness * 2 + lipSize + clearance
        val chamfer = thickness * 0.8

        val case = ExtrusionBuilder().apply {
            crossSection( profile.offset(thickness - chamfer) )
            forward( chamfer )
            crossSection( chamfer )
            forward( height + thickness - chamfer )
            crossSection()
            crossSection( - thickness )
            forward( -height )
            crossSection()
        }.build()

        val battery = battery().translateY(5)
            .bottomTo( thickness )
            .frontTo( chargerSize.z )

        val module = chargerModule().rotateX(90).mirrorY()
            .translateZ( thickness + 0 )
            .previewOnly()
        val endStop = Cube( case.size.x, thickness + 2, thickness ).centerX()
            .bottomTo( chargerSize.y + thickness )
            .frontTo( case.front )
            .color("Orange")
            
        val usbHole = Square( usbSize ).centerX().translateY(0.5)
            .extrude(4)
            .bottomTo(-1)

        val batteryRing = (
            Circle( thickness + batterySize.y / 2 ) -
            Circle( batterySize.y / 2 ) -
            Square( batterySize.y ).centerX().translateY(batterySize.y* 0.38)
        ).extrude( 15 )
        val batteryShelf = (batteryRing - Cube( 100 ).centerX().rotateX(-45))
            .rotateZ(180)
            .translateY( chargerSize.z + batterySize.y/2 )
            .translateZ(10)

        return case - usbHole + endStop + batteryShelf
    }

    @Piece
    fun ledEnd() : Shape3d {
        val profile = profile()
        val chamfer = thickness * 0.8
        val height = 10
    
        val case = ExtrusionBuilder().apply {
            crossSection( profile.offset(thickness - chamfer) )
            forward( chamfer )
            crossSection( chamfer )
            forward( height + thickness - chamfer )
            crossSection()
            crossSection( - thickness - clearance )

            forward( lipSize )
            crossSection()
            crossSection( -thickness )
            forward( -lipSize - thickness )
            crossSection()
            forward( - thickness )
            crossSection( thickness + clearance )

            forward( -height + thickness*2 )
            crossSection()
        }.build()

        val ledHeight = 8
        val ledRadius = 2.5
        val offset = chargerSize.z + batterySize.y/2 + 2

        val ledHole = Cylinder( ledHeight+1, ledRadius )
            .bottomTo(-0.1)
            .centerYTo( offset )

        val ledCase = Cylinder( ledHeight, ledRadius + thickness )
            .centerYTo( offset )

        val switchHole = Cube( switchBodySize.x, switchBodySize.y, thickness*3 )
            .center()
            .centerYTo( switchBodySize.y / 2 + thickness )

        return case + ledCase - ledHole.color("Red") - switchHole
    }

    @Piece
    fun switchKnob() : Shape3d {
        val block = (
                profile().offset( thickness ) intersection
                Square( 100, 14 ).center()
            )
            .toPolygon()
            .roundAllCorners(2)
            .smartExtrude( switchHeadSize.z + 1 )
                .top( Fillet( 1.5 ) )

        val hole = Square( switchHeadSize.x, switchHeadSize.y )
            .center()
            .centerYTo( switchBodySize.y / 2 )
            .translateX( switchHeadSize.x / 2 )
            .chamferedExtrude( switchHeadSize.z, -0.6, 0 )

        return block - hole
    }

    @Piece
    fun all() = arrangeX( 2, chargerEnd(), ledEnd(), switchKnob() )

    override fun build() : Shape3d {
        val chargerEnd = chargerEnd()
        val ledEnd = ledEnd().mirrorZ().bottomTo( chargerEnd.top - lipSize+clearance )
        val battery = battery().backTo( chargerEnd.back-thickness ).bottomTo( thickness * 2 )
    
        return chargerEnd + ledEnd + battery
    }

}