I was playing around in Rhino one day and thought it would be cool to be able to connect points to their nearest neighbours. I figured the best way to do so was to write a python script that would find and draw lines between nearest points. Easier said than done…
My first (failed) iteration, I guess, attempted to retrieve points and find closest points in a group. Needless to say, it didn’t work for a number of reasons:

```import rhinoscriptsyntax as rs import Rhino import scriptcontext import math```
```#Get points to draw lines between pt_group = rs.GetObjects("Select 5 or more points", filter=1, minimum_count=5)```
```#prints number of points for reference print "number of points =",len(pt_group) pt_sum = len(pt_group) for i in range(pt_sum): pt_origin = rs.PointCoordinates(pt_group[i]) pt_get = rs.PointClosestObject(pt_group[i], pt_group) print type(pt_origin) print type(pt_get[1])Curve([pt_origin, pt_get[1]])```

My second (failed) iteration involved a bit more more effort. I even tried annotating the script. Funny thing is I essentially wound up at the same spot – trying to figure out how to evaluate the closest points to any given point.

```import rhinoscriptsyntax as rs import Rhino import scriptcontext import math```

```def CullPts(origin, list, group_count, return_count): #this function takes a list of points and returns the closest #ones to the point of origin. for i in range (group_count): remove_point = list.index(group_count[i]) print remove_point # list.remove(remove_point) # line_draw = rs.PointClosestObject(origin, list) # rs.AddLine(origin, line_draw[1])```

```#Get points to draw lines between pt_group = rs.GetObjects("Select 5 or more points", filter=1, minimum_count=5)```

```#prints number of points for reference print "number of points =",len(pt_group) pt_sum = len(pt_group)```

```#evaluates closest points for each in the list pt_group for i in range (pt_sum): pt_origin = rs.PointCoordinates(pt_group[i]) CullPts(pt_origin, pt_group, pt_sum, 3)```

My third and relatively successful attempt solved the issues I was having in the first attempts. There were a couple of things at helped. First, actually figuring out how I was going to evaluate distances to points (by drawing lines and evaluating the lengths of those lines and deleting the longest ones) and actually finishing a for loop.

```import rhinoscriptsyntax as rs import Rhino```

```#user input select points getPts = rs.GetObjectsEx(message="Select Points", filter=1, select=True) chooseLines = rs.GetInteger(message="How many lines do you want to keep from origin points?") while chooseLines > len(getPts): print "Not enough origin points!" chooseLines = rs.GetInteger(message="Re-enter lines to keep")```

```#convert system.guid into point coordinates ptList = [x[3] for x in getPts]```

```for index in xrange(len(ptList)): #gets point coordinates print ptList[index] #prints point coordinates```

```#draw lines between points proxLines = [] lengthLines = [] for i in xrange(len(ptList)): print i proxLines = [] lengthLines = [] for j in xrange(len(ptList)): if ptList[i] != ptList [j]: drawLine = rs.AddLine(ptList[i], ptList[j]) getLength = rs.CurveLength(drawLine) proxLines.append(drawLine) lengthLines.append(getLength) while len(proxLines) > chooseLines: maxLen = lengthLines.index(max(lengthLines)) # print maxLen #prints index number of largest line # print len(proxLines) #prints length of index of the list that contains the drawn lines rs.DeleteObject(proxLines[maxLen]) del proxLines[maxLen] del lengthLines[maxLen]```

There are still two things I want to do to have this script perform the way I want. First, finding if there’s already a short line connected to a given point and drawing the next shortest line instead. The other addition is adding a function that evaluates duplicate lines and removes one of them. Rhino has a default command called “Seldup” that selects duplicates for manual deletion, but it’d be nice to have that incorporated into the script.