Exit Full View
Up

/Tools/Screw Tidy Insert/ScrewTidyRoundedInsert.foocad

ScrewTidyRoundedInsert
FooCAD Source Code
 
class ScrewTidyRoundedInsert : Model {

    @Custom
    var thickness = 0.5

    @Custom
    var radius = 3

    @Custom
    var taper = 2

    @Custom
    var chamferBase = 2
        
    @Custom
    var chamferSlope = 2

    @Custom
    var x = 1

    @Custom
    var y = 1

    // Experimental ! Keep at 0
    var feetHeight = 0

    fun insert( width : int, depth : int, height : int ) : Shape3d {

        val top = Square( width, depth )
            .center().roundAllCorners( radius )

        val mid = Square( width - taper, depth - taper )
            .center().roundAllCorners( radius )

        val bottom = Square(
            width - taper - chamferBase * 2 / chamferSlope,
            depth - taper - chamferBase * 2 / chamferSlope
        ).center().roundAllCorners( radius )

        val insert = ExtrusionBuilder().apply {
            crossSection( bottom )
            if (chamferBase > 0) {
                forward(chamferBase)
                crossSection( mid )
            }
            forward( height - chamferBase )
            crossSection( top )

            // Now head down to make it hollow

            crossSection( top.offset(-thickness) )
            forward( -height + thickness + chamferBase )

            if (chamferBase > 0 ) {
                crossSection( mid.offset( -thickness ) )
                forward( -chamferBase )
            }
            crossSection( bottom.offset(-thickness) )

        }.build()

        return if (feetHeight <= 0) {
            insert

        } else {
            // What I'm hoping for is to build 4 feet on the bed, and then
            // bidge between the shorter distance between the feet in 2 layers,
            // then bridge between the longer distances in 2 layers, and then
            // fingers crossed, the bottom of the box will bridge!
            val layerHeight = 0.2
            val feetWidth = 2

            val outline = bottom -
                bottom.offset(-feetWidth)

            val feetEndsProfile = outline -
                Square( bottom.size.x+feetWidth*2, bottom.size.y-10 ).center()
            val feetProfile = feetEndsProfile -
                Square( bottom.size.x-10, bottom.size.y+feetWidth*2 ).center()

            val feet = feetProfile.extrude( feetHeight ) +
                feetEndsProfile.extrude(layerHeight*4).translateZ( feetHeight - layerHeight*4 ) + 
                outline.extrude(layerHeight*2).translateZ( feetHeight - layerHeight*2 )

            feet + insert.translateZ(feetHeight)
        }
    }

    override fun build() : Shape3d {
        return insert( x * 39, y * 54, 44.5 ) // Cube( 100,1,100).center()
    }

}