Scripting TextObject Command

Hi,

Looking for a way to script the TextObject command in Rhino 7 using Python. Can anyone help with this. I like the ability to end up with curves and utilize the spacing function.

Eric

Here is a sample… It’s complicated, as there are a lot of options. Just for the sample I threw in some user inputs, you can hard code those or get user input for other options using the same basic setup. I set the initial spacing value to 0, I was surprised, you can even enter a negative number for that.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#note: for V7 only
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def AddTextObject():
    txt_str=rs.StringBox("Enter text string",title="Enter Text")
    if not txt_str: return
    
    tol=sc.doc.ModelAbsoluteTolerance
    ht=rs.GetReal("Text height?",minimum=tol)
    if ht is None: return
    
    sp=rs.GetReal("Text spacing?",0)
    if sp is None: return
    
    pt=rs.GetPoint("Pick point to place text")
    if not pt: return
    
    comm_str='! _-TextObject _Height={} _Rotation=0 _Font "Arial"'.format(ht)
    comm_str+=' _Italic=_No _Bold=_No _HorizontalAlignment=_Left'
    comm_str+=' _VerticalAlignment=_Top _CreateGeometry=_Curves _GroupOutput=_Yes'
    comm_str+=' _AllowSingleStrokeFonts=No _LowerCaseAsSmallCaps=_No'
    comm_str+=' _AddSpacing=_Yes Spacing={}'.format(sp)
    comm_str+=' _UseTextCenterToPosition=_No "{}" {}'.format(txt_str,pt)
    
    rs.EnableRedraw(False)
    rs.Command(comm_str,False)
    
AddTextObject()

Helvetosaur,

Thank you for putting that together for me! One additional thing I want to do:

Your function returns to me a set of grouped curves, which is fine. I want to go a step further. As an example: The word “FLOOR” would consist of 8 separate curves. I want to put the geometry into a list so the letter F & L are two list items followed by the letters that have two curves in them (O,O & R) as nested lists so it keeps the letter geometry grouping. Does that make sense? I would have a list of 5 letters. First and second items have a single curve each, and the last 3 have 2 curves each.

I have some code that I got from a TextEntity example that gets me a single TextEntity consisting of the five letters. Is there a way to iterate through the TextEntity and break it up into five text entities, each with a single letter in a list? Then I could iterate through the list of single character TextEntities and use CreateCurves to get my curves. I would then have the list of curves as described above and I can then play around with spacing of each individual letter, etc.

I’m doing this as a work around since Rhino doesn’t support text kerning.

Any help would be greatly appreciated.

text_entity = TextEntity()
text_entity.Plane = Plane.WorldXY
text_entity.Text = "FLOOR"
text_entity.Justification = TextJustification.BottomLeft
text_entity.TextHeight = 0.625
text_entity.FontIndex = doc.Fonts.FindOrCreate("Helvetica Medium", False, False)
textcurves = text_entity.CreateCurves(text_entity.DimensionStyle , True)

I’ve taken this a step further and think I can work with this for now. Thanks.

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

def CreateLineOfText(text , font , h , space = 0.0):
    
    text_entity = Rhino.Geometry.TextEntity()
    text_entity.Plane = Rhino.Geometry.Plane.WorldXY
    text_entity.Text = text
    text_entity.Justification = Rhino.Geometry.TextJustification.BottomLeft
    text_entity.TextHeight = float(h)
    text_entity.FontIndex = sc.doc.Fonts.FindOrCreate(font, False, False)
    text_entity.SetBold(True)
    textcurves = text_entity.CreateCurves(text_entity.DimensionStyle , False ,spacing = space)
    for i in textcurves:
        sc.doc.Objects.AddCurve(i)
        sc.doc.Views.Redraw()
    return textcurves

retCrvs = CreateLineOfText("FLOOR","Helvetica Medium",.625,.1)

I am having a slight issue with the textobject command. The finished height of the resulting curves is slightly higher then what is being input. If you manually create some text you will find that these same settings when applied to a textobject output as curves is slightly different. It seems to be a scaling thing because you can scale the curves down to fit the manually outputted text and they end up being the same. Any thoughts?

Eric

Hi @eric.bunn,

Fonts, unfortunately, are not as precise as just modeling. If you need the curves created from text to fix neatly inside of a bounding box, then you’ll need to scale.

– Dale

Thanks Dale. I’ve already written the scaling in so I’ll leave it at that. Thanks for the reply.

Eric

Please send large CAD files to http://dropbox.yousendit.com/EricBunn-MayhillPDDC