Using newline in TextObject command

Hi,

I am trying to create a TextObject in Rhino6 programaticaly. The problem I encountered is that if the string I use contains a newline, Rhino thinks it is the end of the command and doesn’t give the expected result. Here is the script I use:

import rhinoscriptsyntax as rs
import datetime as dt

patient_name = "John Doe "
patient_number = "123456"
man_date = dt.date.today().strftime("%m/%y \n")

textString = man_date + patient_name + patient_number
TextSize = 3 
TextHeight = 1 

cmd = "_-TextObject " + "Height=" + str(TextSize) + " Font=Arial " + "Arial " + "Bold=No " + "CreateGeometry=" + "Solids " +  "AddSpacing=Yes " + "Spacing=" + "0.5 " + chr(34) + str(textString) + chr(34) + " 0,0,0 "

rs.Command(cmd, echo=False)
TextSolidBlock = rs.LastCreatedObjects()

The new line (\n) between man_date and patient_name causes the problem. Is there another way to insert a newline in the TextObject without it interpreting it as the end of the command?

Thank you,

Tal

Looks like your best bet is using rs.AddText

https://developer.rhino3d.com/api/RhinoScriptSyntax/#geometry-AddText

Hi Tal, I do not think there is a way, using the-TextObject command - I guess you need to create text in the script, explode to get curves, extrude… or be clever about making the lines yourself in the script with multiple calls to -TextObject…

-Pascal

Yep, that’s what I had to do in my scripts that create multiline text objects… Figure out the text height and line spacing and then offset the insertion points up or down for each line… It gets complicated, my convert text blocks to single stroke text objects (for engraving) is over 100 lines of code, and that’s where the fonts are hard coded (thus tested and measured).

That is what I ended up doing.

Thanks for the advice.

Maybe I’m not clever, but instead of doing hard to control multiple calls to -TextObject wouldn’t it be easier to do something like:

import rhinoscriptsyntax as rs

sometext = "aa\nbb\n\ncc"

rs.AddText(sometext, (0,0,0)) # add settings as necessary for font and alignment

Hi Nathan - Addtext() makes text - as text - the user needs to make text-shaped curves or solids,
= the TextObject command in Rhino (not Text… confusion wins!)

-Pascal

Ah, right. My bad.

-TextObject is possibly easier, but I just had to try. Comment and uncomment the corresponding lines to get curves or polysurfaces out of the text. And you’ll probably want to play more with dimstyles to use the fonts wanted etc.

import scriptcontext as sc
import Rhino.DocObjects as rdo
import Rhino.Geometry as rg

sometext = "".join(["useful text with number: "+str(i)+"\n" for i in range(0,3)])

ds = sc.doc.DimStyles.CurrentDimensionStyle

oa = rdo.ObjectAttributes()
te = rg.TextEntity()
te.Text = sometext
#texts = te.CreateCurves(ds, False, 1.0, 0.7)
texts = te.CreateExtrusions(ds, 1.0, 1.0, 0.7)
#texts = te.CreatePolySurfaces(ds, 1.0, 1.0, 0.7)

#addedobs = [sc.doc.Objects.AddCurve(text, oa) for text in texts]
addedobs = [sc.doc.Objects.AddExtrusion(text, oa) for text in texts]
#addedobs = [sc.doc.Objects.AddBrep(text, oa) for text in texts]
for obuid in addedobs: sc.doc.Objects.Select(obuid, True)

sc.doc.Views.Redraw()

Yeah, I started down that road but kept getting a crash if I set a break point to look at what was in ‘te’. Do you get that crash? Set a breakpoint someplace and expand the ‘te’ variable - here I get a boom every time. (reported)

-Pascal

Expanding indeed causes crash. I was too dumb to think of using breakpoints. I just used print(dir(te)). print is my first choice of investigation and debugging (: That, combined with the docs https://developer.rhino3d.com/api/RhinoCommon/html/Methods_T_Rhino_Geometry_TextEntity.htm

Could be that I’m wrong but you can explode the text to convert to curves and after extrude it…

Edit: ah, I see, Nathan made same approach… sorry, didn’t read his post before replying

You can, however if you need single-stroke text, this approach does not work - as plain Text does not support single-stroke fonts. Well, actually it does, but the display is wrong. However once exploded it does seem to look correct.

To make cnc path?

Or laser engraving or…

1 Like