Grouping Points from Sublist by Cluster

I began working off of the “Group points by Cluster” post, grouping my points based off their proximity worked great. However, I have begun grouping my points beforehand by what clash detection group they belonged to.

My ultimate end goal is to be able to group my clash points by test > by element id > by proximity, but right now I just want to be able to group them by proximity.

This is the Python Script I have been using:
#Copyright(c) 2015, Konrad Sobon
# @arch_laboratory, http://archi-lab.net
#join/group curves
# Changed to work with points by Einar Raknes, 2016

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


def groupPoints(point_list,ignore_distance): 
    grouped_points = [] #A list  that contains point groups
    queue = set() #A queue set (contains only unique items)
    while point_list: 
	    point_group = [] #A list that contains points that are within the ignore_distance
	    queue.add(point_list.pop()) # Move the last point from the point_list to our queue 
	    while queue:
		    current_point = queue.pop() #Get the last item in the queue and remove it from the queue.
		    point_group.append(current_point) #Add the current point to the point_group
		    for potential_match in point_list: 
			    distance = current_point.DistanceTo(potential_match) 
			    if distance <= ignore_distance:
				    queue.add(potential_match) 
		    point_list = [item for item in point_list if item not in queue] #removes points in queue from point_list

	    grouped_points.append(point_group)

    return grouped_points 

OUT = groupPoints(IN[0],IN[1])

4Excel Reader Create Points to SORT5.dyn (187.0 KB)
CLASHFILEALL2.xlsx (16.3 KB)

1 Like

You’re providing the node with a 2D list when it will only work for 1D lists:

The particular line the error is referencing is:

queue.add(point_list.pop())

In this case, queue is a set. When point_list.pop() is called, it removes and returns the last item in point_list. Since point_list contains two lists, it is returning a list, which it then tries to add to the set (invalid operation) and then raises an exception. If the structure of your input list is important, you can modify the script to instead do separate calls of the groupPoints() function, providing it one 1D list at a time.

@cgartland Thanks for the response! How would I go about doing separate calls?

Thanks,

For example, the last line is:

OUT = groupPoints(IN[0],IN[1])

You can replace this with something like:

all_points = IN[0]
threshold = IN[1]
OUT = [groupPoints(sublist, threshold) for sublist in all_points]

Which will yield:

image

I should note that this modification will only work for lists nested 1 level deep (e.g. [[A, B], [C, D]])

Thank you so much! That worked! :smiley:

1 Like