I’m trying to group individual lines of text based on their proximity. We often receive parcel information as single lines of text and I want to convert them to a single Mtext object.
This is what I have so far.
I have no clue how comparing 576 true/false values to a list of 24 strings consistently groups these correctly, but I’ll take it. After the List.UniqueItems I’m usually left with some duplicate though incomplete lists. I’ve been searching for a node-based way to combine the lists that have at least one matching entry. So far nothing has made a solution click for me.
If I can get the lists cleaned up I should be able to reorder the text and create a single Mtext.
Thank you
When exporting a workspace, zoom in so you can read the node titles. It appears you are doing a cross (cartesian) product of all the text locations distance, then selecting on distance and culling the repeats. This could get very gnarly very quickly as you will experience exponential growth
You could use List.GroupBySimilarity nodes on the Y and then the X, adjust the tolerance to suit. Finally flip the Y values of the group so selection is top down (largest to smallest). I have used rectangles here but as long as you can get a point in space you can group by cluster
Note: pay attention to the list levels.
Hi,
an example of group / clustering using the DBSCAN implementation (with SciPy; no need for extra libraries).
import sys
import clr
# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *
import statistics
import math
import numpy as np
import random
from scipy.spatial import cKDTree
adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
def dbscan2(x_pts, eps, min_samples, max_samples=None):
x_pts = np.asarray(x_pts)
n = x_pts.shape[0]
tree = cKDTree(x_pts)
neighbors = tree.query_ball_point(x_pts, eps)
labels = np.full(n, -2, dtype=int) # -2 = unvisited
cluster_id = 0
for i in range(n):
if labels[i] != -2:
continue
# Check if i can actually start a cluster
if len(neighbors[i]) < min_samples:
labels[i] = -1 # Noise
continue
# Attempt to grow a cluster
temp_cluster = [i]
labels[i] = cluster_id
# Use a local stack for expansion
stack = [nb for nb in neighbors[i] if nb != i]
while stack and (max_samples is None or len(temp_cluster) < max_samples):
j = stack.pop(0) # BFS-style expansion
if labels[j] == -2 or labels[j] == -1:
labels[j] = cluster_id
temp_cluster.append(j)
# if the new point is a core point, add its neighbors to the stack
if len(neighbors[j]) >= min_samples:
for nb in neighbors[j]:
if labels[nb] == -2:
stack.append(nb)
# VALIDATION: If the cluster ended up smaller than min_samples
if len(temp_cluster) < min_samples:
for idx in temp_cluster:
labels[idx] = -1 # Revert to noise
else:
cluster_id += 1
return labels
def group_by_dbscan(elements, n_min=2, n_max=4, min_distace=0.1, max_distace=0.6):
#
results = []
#
min_samples = n_min # min element by cluster
#
for eps in np.arange(min_distace, max_distace,0.1):
#
elements = np.array([e for e in elements])
inputs_points = [e.Location for e in elements]
array_pt = np.array([[p.X, p.Y] for p in inputs_points])
#
groups = []
labels = dbscan2(array_pt, eps, min_samples, max_samples=n_max)
cluster_indices = {c: np.where(labels==c)[0].tolist()
for c in np.unique(labels) if c>=0}
noise_indices = np.where(labels==-1)[0].tolist()
# color results
for lab, idxs in cluster_indices.items():
sub_elems = np.take(elements, idxs, axis=0)
groups.append(sub_elems)
if groups:
total = sum(len(i) for i in groups)
mean_grp = statistics.mean(len(i) for i in groups)
#
results.append([total, mean_grp, eps, groups])
# find best result
best_result = sorted(results, key = lambda x : (x[0], x[1]), reverse=True)[0][-1] if results else []
# re-order sub-group texts by Y
best_result = [sorted(grp, key = lambda txt : txt.Location.Y, reverse=True) for grp in best_result]
return best_result
texts = IN[0]
groups = group_by_dbscan(texts)
OUT = groups