Exit Full View
ZipUp

/Words/Label.foocad

Label

Simple labels, which I glue to the inside of component organisers bins etc.

I don't like paper-based labels, as they are easily damanged / get dirty. I've tried Dymo, but they peal off too easily, and look ugly.

@Custom strings is a multi-line string, with each line creating a label. You can create a multi-line label by adding \n within a line.

The default custom values create a minimal background, which is just large enough to keep all the pieces together.

The default font-size is large enough to be easily printable using a 0.6mm nozzle.

FooCAD Source Code
import uk.co.nickthecoder.foocad.smartextrusion.v1.*
import static uk.co.nickthecoder.foocad.smartextrusion.v1.SmartExtrusion.*

class Label : Model, PostProcessor {

    @Custom( lines=4 )
    var strings = """
Example
"""

    @Custom
    var backgroundThickness = 0.4

    @Custom
    var raisedThickness = 0.6

    @Custom
    var margins = Margins2d( -1.5, -1, 0, -1 )

    @Custom( about="Offset the text on the background" )
    var outline = 0.75

    @Custom
    var style = TextStyle( DEFAULT_FONT, 8 ).hAlign( HAlignment.CENTER )

    @Custom( about="Include a M600 command after background has been printed" )
    var changeFilament = true

    @Custom( about="Additional spacing between each line of a single label" )
    var lineSpacing = -1

    @Custom
    var radius = 1

    meth label( string : String ) : Shape3d {
        
        val lineHeight = Text("Xy", style).size.y
        val standardHeight = Text("X", style).toPolygon().size.y

        var text : Shape2d = Square(0)
        var background : Shape2d = Square(0)
        var prevRect : Shape2d = Square( 0 )

        var thinnestWidth : double = text.size.x
        var y = 0
        for (line in string.split( "\n" ) ) {
            if (line.isNotBlank()) {
                val lineText = Text( line, style ).toPolygon()
                    .frontTo( y )
                y -= lineHeight + lineSpacing
                val lineWidth = lineText.size.x
                if (lineWidth < thinnestWidth) {
                    thinnestWidth = lineWidth
                }
                val rect = Square( lineText.size.x + margins.x, standardHeight + margins.y )
                    .leftTo(lineText.left - margins.left )
                    .backTo( lineText.back + margins.back )
                    .roundAllCorners( radius )
                val join = Square( rect.size.x - radius*2, 0.1 ).leftTo( rect.left+radius ).backTo( rect.back ) hull
                    Square( prevRect.size.x - radius*2, 0.1 ).leftTo( prevRect.left+radius ).frontTo( prevRect.front )
                background += rect + join// prevRect hull rect
                prevRect = rect
                text += lineText
            }
        }

        val background3d = (background + text.offset(outline))
            .extrude( backgroundThickness )
            .color("DarkGrey")

        val text3d = text.smartExtrude( backgroundThickness + raisedThickness )

        return background3d + text3d
    }

    override fun build() : Shape3d {
        //return label( "Mill" )
        var result : Shape3d = Cube(0)
        for ( string in strings.split("\n") ) {
            if ( string.isNotBlank() ) {
                val multiLine = string.replaceAll( "\\\\n", "\n" )
                result += label( multiLine ).backTo( result.front - 1 )
            }
        }
        return result
    }

    override meth postProcess(gcode: GCode) {
        println( "ChangeColor? $changeFilament" )
        if (changeFilament) {
            println( "Changing color" )
            gcode.findHeight( backgroundThickness ).insertAfter( "; Change Colour\nM600" )
        }
    }

    override meth postProcess(pieceName : String, gcode: GCode) {
        println( "ChangeColor? $changeFilament" )
        postProcess( gcode )
    }

}