Skip to content

Add Text box method #2025

@RavinMaddHatter

Description

@RavinMaddHatter

I worked out a way to do text boxes. Happy to put in a PR if you have a place that this would be best in, This is a lazy method for doing text wrapping in the form of text boxes.

For example if you have a long string you want to put on the back of an object before printing. Rather then having to manually add the returns this will figure out where the line breaks would have to be and put them in.

I have not looked into your formating, but the example for a text box would look like this


def textbox(width,thickness,text,font,size,plane="XZ",halign="left"):
    lines = text.split("\n")
    endLines=[]
    finalText=cq.Workplane("XZ")
    for line in lines:
        curLen=0
        tempText = ""
        bb=cq.Workplane("XZ").text(line,size,thickness,font=font).val().BoundingBox()
        average_per_char=(bb.xmax - bb.xmin)/len(line)
        characterCutOff=int(width/average_per_char)
        for word in line.split(" "):
            if len(tempText)+len(word)>characterCutOff:
                endLines.append(tempText)
                tempText = word+" "
            else:
                tempText+=word+" "
        endLines.append(tempText)
    final_string = "\n".join(endLines)
    finalText=cq.Workplane(plane).text(final_string,size,thickness,font=font,halign=halign)
    return finalText
show_object(textbox(200,0.3,"this is a long string, sort of a runon sentence, the point is to get extra lines.","impact",20))

If it is something that would be value added, let me know. If not i will keep it in an external library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions