Write Point Name and Coordinate Value to .txt File

Dear All,

In Python, I tried to write points in Rhino with their Objectname and Coordinates as a list to a .txt File. However, I don’t know how to combine the objectname string with the coordinate string value.

Maybe somebody knows how to:

Combine objectname with coordinate value in a .txt List?
How to insert delimiter signs/spaces such as a Tab?
How to round decimal values to the next integer?
How to control indents of a Python script? I noticed, when I move the file.write line one space to the right. I got a longer list with multiple values.(please see: Text-write-change indent(Multiple-texts).txt)
Test-Write.Text-Name-Coordinate.txt (303 Bytes)
Text-write-change indent(Multiple-texts).txt (880 Bytes)
Test-write.point-names-coordinate.3dm (21.3 KB)
Text-Write.py (714 Bytes)

Thank you very much for your time.

Kind regards,

Jaap Bijlstra

You might want to read up a little on how text strings are concatenated in Python. Also perhaps how indenting within a python script works - that is essential for making things work correctly.

Here is a small bit of sample code with a few comments:
(note I did not add any error checking or anything like that)

import rhinoscriptsyntax as rs

#Get the points to export
objectIds = rs.GetObjects("Select Points",1, True, True)

#Get the filename to create
filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
filename = rs.SaveFileName("Save point coordinates as", filter)


delim="\t"
#for a tab delimiter it could also be chr(09)
#for a space delimiter it would just be delim=" "
#for a semicolon delimiter it would just be delim=";" etc.

#create an empty string to collect all data
to_write=""

with open(filename, "w") as file:
    for id in objectIds:
        #get point coords - result is a list of 3 numbers
        point_coords = rs.PointCoordinates(id)
        #to round to 3 places:
        rd=[round(n,3) for n in point_coords]
        #convert point coordinates to string with delimiters
        #several ways to do this, I like to use the format (substitution) method
        pt_str="{}{}{}{}{}".format(rd[0],delim,rd[1],delim,rd[2])
        #get object name
        obj_name = rs.ObjectName(id)
        #name can be None if object has not been named, give it a default name
        if not obj_name:
            obj_name="Unnamed"
        #add the current line to the existing string plus a newline character
        to_write+=obj_name+delim+pt_str+"\n"
    #write the whole file (one long string) at once
    file.write(to_write)
1 Like

Thank you Mitch for your time!

I overworked the script and it works now! I still need to learn more about indenting in a python script.

Wish you a nice day.

Regards,

Jaap Bijlstra