I'm looking for a macro that can detect distance between objects. Is there one that can do that? I want to be able to give a value and then have the script search and find any objects that are closer to each other than the value specified. Thanks
Joseph
jt123 said: I just need to detect whether any are too close or overlapping.
To clarify the requirement -- do you consider these two objects to be overlapping ?
If I calculate the bounding box of both objects, then the greatest X position of the left object exceeds the smallest Y position of the right object -- so the test would say that they overlap. But they are not actually touching at any point, and if the requirement is to allow the above situation then the test for overlap becomes a lot more complicated.
I see what your saying. I'm wanting to use this tool to check circle distances from each other. If it detects the circle object is less than the provided distance or overlapping, it can highlight it red like in the image below.
Will the individual objects always be circles?
That would make it easier, because we can take any two circles, calculate the distance between their centres (by pythagoras), subtract both of their radii and check if it exceeds the minimum distance. But if they were triangles or arbitrary shapes, then a very different procedure would be needed.
I think this will do it then ...
Dim ss As Shapes minimum_gap = 0.05 'value in inches, change as required Set ss = ActiveLayer.Shapes For i = 1 To ss.Count 'do every shape found If ss(i).Type = cdrEllipseShape Then For j = i + 1 To ss.Count 'compare it with every shape we haven't yet done If ss(j).Type = cdrEllipseShape Then distance_x = ss(j).CenterX - ss(i).CenterX distance_y = ss(j).CenterY - ss(i).CenterY distance = Sqr((distance_x * distance_x) + (distance_y * distance_y)) End If radius_i = ss(i).CenterX - ss(i).LeftX + ss(i).Outline.Width / 2 radius_j = ss(j).CenterX - ss(j).LeftX + ss(j).Outline.Width / 2 gap = distance - radius_i - radius_j If gap < minimum_gap Then ss(j).Fill.UniformColor.CMYKAssign 0, 100, 100, 0 End If Next j End If Next i
As currently written, it assumes all circles are on the active layer, so you can have other objects in the document without them being considered. It also presumes all the objects are circles
Cool, I'll give it a try. So the concept here is to set the distance within the code and "select" the objects I want to check, correct?
jt123 said:the circle may have a stroke thickness and the bounding box doesn't consider that
That is correct. Or at least, that is what it does -- "correct" behaviour would be for the bounding box to already include the outline.
Incidentally, the in X7 the outline has additional options to be inside or outside the object, so the "half thickness" is not necessarily correct there. Anybody wanting to adapt this for X7 should consider adding some condiitonal code that would add the correct value.