Python Rhino.Geometry Line list from script

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
import csv

a = [] # new. correct because global scope
def CSVlist():
    sc.doc = Rhino.RhinoDoc.ActiveDoc
    #prompt the user for a file to import
    filter = "CSV file (*.csv)|*.csv|*.txt|All Files (*.*)|*.*||"
    filename = rs.OpenFileName("Open Point File", filter)
    if not filename: return

    with open(filename) as csvfile:
        reader = csv.reader(csvfile)
        #a = [] # wrong because function scope
        for row in reader:

            p1 = Rhino.Geometry.Point3d(float(row[0]),
                                        float(row[1]),
                                        0)
            p2 = Rhino.Geometry.Point3d(float(row[2]),
                                        float(row[3]),
                                        0)
            """
            p1 = rs.AddPoint(float(row[0]),
                             float(row[1]),
                             0)
            p2 = rs.AddPoint(float(row[2]),
                             float(row[3]),
                             0)
            """
            x = Rhino.Geometry.Line(p1,p2)
            print(x)
            a.append(x)
            #print(rs.AddLine(p1,p2))
            #a.append(rs.AddLine(p1,p2))

########################################################

I am creating a list of lines and points from text. I want to output directly to line because it is easier than managing the creation of a polyline. I can output as a text fine with start points and end points of line segments, but the conversion from text to line is invalid. What to do?

text_to_line
##################

# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == "__main__" ):
    if x:
        CSVlist()

Edit: I have attached the Label and/or Line components to a parameter, for which the representation is null.

Edit2: Solved by bringing a to global scope

Hi,
Attach the line component to “a”, not to out?

That doesn’t work. the representation is null

Did you post the entire script? I remade it quickly here, but it works as expected. Make sure you aren’t overwriting a elsewhere again with an empty list.

Anyway, it’d be good if you didn’t re-use your input variable x as your temporary variable for line geometry.

import Rhino

lst=[
	["0.0", "0.0", "1.0", "0.0"],
	["0.0", "0.0", "0.0", "1.0"],
	["0.0", "1.0", "1.0", "1.0"],
	["0.0", "1.0", "0.0", "2.0"]
]

a = []
def CSVlist():
	reader = lst # simulate cvs reading
	for row in reader:
		p1 = Rhino.Geometry.Point3d(float(row[0]),
					    float(row[1]),
					    0)
		p2 = Rhino.Geometry.Point3d(float(row[2]),
					    float(row[3]),
					    0)
		line = Rhino.Geometry.Line(p1,p2)
		print line
		a.append(line)

if __name__=="__main__":
	if x:
		CSVlist()

All good man. See the edits for the solution. Thanks so much

1 Like