I am an amateur in Rhino and has very minimal Python exposure. I would like to measure the closest distance (2D horizontal distance) of multiple points (from a csv with XYZ) to a polyline, but I have hundreds of points. I prefer using Python in Rhino, as I am used to using it but not really an expert to write a new code.
Ex. CSV with Point X,Y,Z, import to Rhino, measure closest distance of each point to a polyline in DXF, and then output the distance as a new column in the csv for each point. I am only interested in the 2D horizontal distance, so Z doesn’t really matter.
Ultimately, end goal is to look at the distribution of distance from the points to the polyline. I would appreciate any assistance. Thank you in advance.
Since I seem to understand you want to learn rather than the script done for you. I will provide you with the documentation needed and a breakdown of the problem.
If you need further help tag me and I’ll guide you through it
You could do something like this, once you import your curve into Rhino:
import csv
import rhinoscriptsyntax as rs
rfilename = "C:\\yourpath\\testPnts.csv" #file name to read
wfilename = "C:\\yourpath\\wtestPnts.csv" #file name to write
def CSVlist():
pnts = []
if not rfilename: return
with open(rfilename) as csvfile:
reader = csv.reader(csvfile)
for row in reader:
x = float(row[0])
y = float(row[1])
z = float(0)
pnts.append(rs.CreatePoint(x,y,z))
return pnts
pnts = CSVlist()
crv = rs.GetCurveObject()[0] #pick curve
cpram = [rs.CurveClosestPoint(crv,i) for i in pnts] #parameter of closest points on curve
cpnts = [rs.EvaluateCurve(crv,i) for i in cpram] #points on curve
dist = [rs.Distance(i,j) for i,j in zip(pnts,cpnts)]
with open(wfilename, "wb") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',')
for point, d in zip(pnts,dist):
csvwriter.writerow([point[0], point[1], point[2], d])
print "Points written sucessfully to file"