Converting texts to points with corresponding Z values

Good morning!

I’d always told myself I didn’t have to learn grasshopper but it seems as though the inevitable moment has arrived finally.

I had received a set of dwgs that supposedly carried topography data represented in points and lines with real-life height values. but when I opened them, I found out that the data were all texts in numbers sitting on the xy plane with no z values.

Is there a way to somehow extract points from each texts and move each points up by their corresponding values? I hope this question makes any sense.

I’ve been googling my way through but have been unlucky.

below is the screen shot of the texts from the top view. And I would like to thank you all in advance for your help and guidance!

Maybe you can share a file or something.

Will do!
q1.3dm (6.3 MB)

Yes this is certainly possible.
Try this:

import rhinoscriptsyntax as rs

def convert_texts_to_points():
    # Get all text objects in the document
    objects = rs.AllObjects()
    text_objects = [obj for obj in objects if rs.IsText(obj)]
    
    if not text_objects:
        print("No text objects found.")
        return

    for text_obj in text_objects:
        # Get the text string and insert point
        text_string = rs.TextObjectText(text_obj)
        insert_point = rs.TextObjectPlane(text_obj).Origin

        # Convert the text to a number
        try:
            height = float(text_string)
        except ValueError:
            # print(f"The text '{text_string}' does not contain a valid number. Skipping...")
            continue

        # Create a point object at the insert point with Z equal to the height
        point_location = (insert_point[0], insert_point[1], height)
        point_obj = rs.AddPoint(point_location)
        

convert_texts_to_points()

Thank you for your input! but I’m embarrassed to say that I have no clue what I’m looking… at :smiling_face_with_tear:

could you maybe tell me what rhinoscriptsyntax is? maybe I could start from there…

as a matter of fact, I’ve just found a page explaining the concept of it. I’ll learn about them apply your solution! Thank you so much!

To make this definition to work you will need to install the Human components.


TextObjects_to_Point_Coordinates.gh (13.3 KB)

P.S. Looks like 21 TextObjects are returning a <null> instead a value.

2 Likes

Thank you so much! I’ll take a look

you’re amazing… and I think some texts are letters so that might be why there are some null items. Thank you so much!

Well,
So here is my script:

import rhinoscriptsyntax as rs

warstwa = "MODEL::TEREN::TEREN-PUNKTY"

if __name__=="__main__":
    if rs.IsLayer(warstwa):
        rs.CurrentLayer(warstwa)
    else:
        rs.AddLayer(warstwa)
        rs.CurrentLayer(warstwa)
    tekst = rs.GetObject("click on a Z coordinate:", filter=512)
    if tekst:
        tekstZ = rs.DimensionText(tekst)
        if tekstZ:
            rzednaZ = float(tekstZ)
        punkt = rs.GetPoint("where to insert a point:")
        if punkt:
            rzednaX = punkt.X
            rzednaY = punkt.Y
            punkt = rs.AddPoint(rzednaX, rzednaY, rzednaZ)
            rs.HideObject(tekst)
            rs.ObjectLayer(punkt, warstwa)
        else:
            exit()
    else:
        exit()

Variables in polish, but I think these are quite easy. I prefer clicking on texts as they can have quite various applications.
Finally I have pinned the script to a button, of course.
Cheers, Jaro

1 Like

thank you so much!

I am not programmer, how I can use this script?

Hello,
Copy the script to a file, with “py” extension. Then just add a button, by right-clicking on any toolbar:


Then edit it, by adding a command and a tooltip:

Change the file path, of course. That’s all, more or less.
Cheers, Jaro

3 Likes

if you are on R8 and want to use standard components have a look at this as well:

for many of your text values, decimal separator is comma “,” for other you have a dot “.”

GH always wants the dot, otherwise this happens:

there are 31 texts which are all “space space comma space space”
those are circled in white and filtered out

2D_txt_to_3d_pt.gh (18.6 KB)

1 Like

See Rhino - How to Use Scripts and Plugins

2 Likes