Closest distance of multiple points to a polyline

Hi All,

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.

I’d love to help you, however this seems like a full on plugin.
Let’s break this down into smaller tasks so that It is easier to tackle :

  1. You want to be able to read data from the CSV → 13.1. csv — CSV File Reading and Writing — IronPython 2.7.2b1 documentation
  2. You want to be able to add points based on the data from the CSV →
  3. You want to be able to calculate the distance between a point and a curve →
  4. You want to be able to export (add) some data on CSV → 13.1. csv — CSV File Reading and Writing — IronPython 2.7.2b1 documentation

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

1 Like

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)
    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"

Its partially based on the code found here: Rhino - How to read and write a CSV files

1 Like