Convert text to dot

Is there a way to convert text to dots?

Hi Bill,

i don’t think there is a command for it, but you could use below python script:

import Rhino
import scriptcontext 
import rhinoscriptsyntax as rs

def TextToDot():
    ids = rs.GetObjects("Select text objects", 512, True, True, False)
    if not ids: return
    
    for text_obj in [rs.coercerhinoobject(id, True, True) for id in ids]:
        if isinstance(text_obj, Rhino.DocObjects.TextObject): 
            bbox = rs.BoundingBox(text_obj, text_obj.TextGeometry.Plane)
            if bbox:
                text = text_obj.TextGeometry.Text
                point = (bbox[0] + bbox[6]) * 0.5
                rs.AddTextDot(text, point)
                rs.DeleteObject(text_obj)
    
if __name__=="__main__":
    TextToDot()

c.

Thanks clement!
Would it possible to use the text insertion point for the dot?
Bill

Hi Bill, of course:

import Rhino
import scriptcontext 
import rhinoscriptsyntax as rs

def TextToDot():
    ids = rs.GetObjects("Select text objects", 512, True, True, False)
    if not ids: return
    
    for text_obj in [rs.coercerhinoobject(id, True, True) for id in ids]:
        if isinstance(text_obj, Rhino.DocObjects.TextObject): 
            text = text_obj.TextGeometry.Text
            rs.AddTextDot(text, text_obj.TextGeometry.Plane.Origin)
            rs.DeleteObject(text_obj)
    
if __name__=="__main__":
    TextToDot()

c,

Wow that was quick! Thanks clement.

Hi clement,
Works like a charm in Rhino 5, but it looks like McNeel & Associates still have some work to do on the version 6 WIP. In the WIP the dots are not created at the insertion point. They are bang on in Rhino 5.

Hope one of the developers sees this.

I appreciate your help,
Bill

Hi Bill,

thanks for the report, i see that both scripts work in the WIP but the insertion point used in the second script seems to be at the upper left. In V5 the text insertion point is the lower left instead.

If you want to have a quick workaround, you might try this until V6 got fixed:

import Rhino
import scriptcontext 
import rhinoscriptsyntax as rs

def TextToDot():
    ids = rs.GetObjects("Select text objects", 512, True, True, False)
    if not ids: return
    
    for text_obj in [rs.coercerhinoobject(id, True, True) for id in ids]:
        if isinstance(text_obj, Rhino.DocObjects.TextObject): 
            bbox = rs.BoundingBox(text_obj, text_obj.TextGeometry.Plane)
            if bbox:
                text = text_obj.TextGeometry.Text
                point = bbox[0]
                rs.AddTextDot(text, point)
                rs.DeleteObject(text_obj)
    
if __name__=="__main__":
    TextToDot()

I am thinking of adding some command line options where to position the Dot after the conversion.

@pascal, i hope you see that, will this be corrected in V6 ?

c.

FWIW, I’m not seeing that here.
To the left is the result of your second script; to the right the result of the third.

And if I use a different style for the text:

@wim, thanks for the extensive test.

If you turn points on for a text object in Rhino WIP, is the insertion point in the lower left or upper left ? In my case i can see it in the upper left (WIP):

and using same font and text size it appears lower left in V5:

I guess the best would be to explode the text into curves and then get the bounding box from it. I have to try this and then see if the results are more consistent within both versions of Rhino.

c.

Neither.
Remember that in Rhino 6, text adheres to a style. It’s the style that dictates where this point will be. In my base case (but I also tried one other style), it is in the lower center.

@wim, the text i’ve been testing with is the same in V5 and V6. I’ve copy/pasted it from V5 into V6. The results are different, even the bounding box for the same text after exploding is slightly different.

Sample file with extra layers for each Rhino version:

TextComparison.3dm (48.5 KB)

And a script which allows to control the dot position @bisenberger:

TextToDot.py (1.7 KB)

c.

Hi clement,
My original source data was a text file with a list of coordinates. I imported the text file into a program that created points with text label. I exported the results as an AutoCAD dwg file. The dwg file is what was testing in Rhino. I have attached the dwg file and text file for you to check out.

test.zip (8.5 KB)

Thanks for the script that allows to control dot position. I will download and test now.

@bisenberger, testing with your dwg file, i get the converted Dot positioned over the points in the dwg file if i choose “InsertionPoint” in the script. I get the same results in V5 and V6, running in Top viewport.

c.

@clement,
Worked in V5 and V6 for me too :smile:
Thanks a bunch.

1 Like

The text string might be the same but the text object is not. A copy - paste from RH5 into RH6 converts it into rich text and gets the style of the current style assigned to it.

Copy-Paste of RH5 [top] text and bounding box rectangle into RH6 [bottom]:

So…

… that doesn’t surprise me.
@lowell?

Hi clement,
Don’t want to impose, but if your up to it. Now I want to go the other way, dots to text, so that the strings created from the dots, are the dot label, followed by the x,y,z coordinate; e.g.; label,x,y,z.
Bill

Hi Bill,

you could use _ConvertDots command with _Output=Text and handle all other options like text height and alignment. Then on the resulting text objects apply below script to append the insertion point coordinate:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    
    msg = "Select text objects to append insertion point coordinate"
    ids = rs.GetObjects(msg, rs.filter.annotation, True, True, True)
    if not ids: return
    
    ndigits = 3
    
    for text_obj in [rs.coercerhinoobject(id, True, True) for id in ids]:
        if isinstance(text_obj, Rhino.DocObjects.TextObject):
            text = text_obj.TextGeometry.Text
            point = text_obj.TextGeometry.Plane.Origin
            x,y,z = map(lambda n: round(n, ndigits) + 0, point)
            text_obj.TextGeometry.Text = "{} {}, {}, {}".format(text, x, y, z)
            text_obj.CommitChanges()
        
    scriptcontext.doc.Views.Redraw()
    
DoSomething()

_
c.

Hi clement,
I wasn’t very clear. Instead of converting to text, I meant to create a comma delineated text file with each line of the text file having the label,x,y,z of the dot. Sorry about that.

Hi Bill,

try below, you can either copy to clipboard or save from the dialog:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    
    ids = rs.GetObjects("Select dots", rs.filter.textdot, True, True, True)
    if not ids: return
    
    ndigits = 3
    output = ""
    
    for dot_obj in [rs.coercerhinoobject(id, True, True) for id in ids]:
        text = dot_obj.Geometry.Text
        point = dot_obj.Geometry.Point
        x,y,z = map(lambda n: round(n, ndigits) + 0, point)
        output += ("{}, {}, {}, {}\n".format(text, x, y, z))
    
    if len(output) != 0:
        Rhino.UI.Dialogs.ShowTextDialog(output, "Title")
    
DoSomething()

_
c.

Hi clement,
It prompted me to import, but I want to export existing dot labels and coordinates to a text file for use in another program. That program can read a comma delineated text file formatted with name,x,y,z. I’ve tried using the Rhino export to .txt, but it appears to only export points and just the x,y,z,r,g,b values.