Joseph Romero
Joseph Romero

Reputation: 1

AutoCAD LISP where I can select the Polyline which bounds the selected Text

I work with lots of rectangles(POLYLINE) with an ID name(TEXT) inside them. I need to separate each Recatngles by Layer depending on their ID name. So to make things a bit easier, I want to be able to select the Polyline using the Text inside each Rectangles. Can this be done by a LISP?

I don't know anything about programming so I need help from the gurus here.

Thank you.

Upvotes: 0

Views: 33

Answers (1)

Lee Mac
Lee Mac

Reputation: 16015

The short answer is yes, it's possible.

There are various ways to achieve this, but the success of the algorithm that you opt to implement will depend somewhat on the typical structure of your drawings (e.g. how congested the drawing is or whether polylines have concave sections which contain text from other polylines etc.).

For each of the methods I describe below, you'll first need to use ssget to obtain a set of all candidate polylines; you can then use various techniques to associate each polyline in the set with a given text object.

Method 1: Crossing/Window Polygon Selection

This is likely the easiest method to tackle the problem, but has some drawbacks where performance is concerned.

The general algorithm is as follows:

  • For every polyline in the set:
    • Obtain the set of vertices defining the polyline (DXF group 10 entries in the DXF data).
    • Zoom the drawing area to a window which encloses all vertices (to do this, you can calculate the min & max X & Y coordinates in the set of vertices so as to obtain a rectangular window which encloses all points) - for this you can use either a command call (command "_.zoom" "_w" ...) or the COM ZoomWindow method of the Application class (vla-zoomwindow (vlax-get-acad-object) ...).
    • Use the ssget function with a WP (Window Polygon) or CP (Crossing Polygon) mode string and the set of polyline vertices along with an appropriate filter list targeting TEXT entities (and perhaps other properties, such as layer) to obtain a set of text objects residing within the polyline.
    • Perform a Zoom > Previous to restore the previous view - (command "_.zoom" "_p" ...) or the COM ZoomPrevious method of the Application class (vla-zoomprevious (vlax-get-acad-object) ...).

Method 2: Ray-Casting

An alternative method is to cast a ray (an infinite line) from the insertion point or alignment point of the text object and check the number of times that it intersects a polyline in the set - you can use the COM IntersectWith method to achieve this.

If the number of intersections is odd, the text object resides within the polyline - though, there are a few caveats to this method, e.g. accounting for if the ray exactly passes through the vertex of a concave edge, which could result in a false positive/negative.

Method 3: Bounds Checking

A straightforward approach might be to check whether the insertion point or alignment point of the text object simply falls within the rectangular bounds of the polyline, and, if so, associated the text to the polyline.

The advantage of this method is the speed of the check (simply checking whether the X & Y coordinate values fall within a given range), but the method could yield false positives if, for example, the polyline has exaggerated concave sections which contain text that is to be associated with other polylines.

Upvotes: 0

Related Questions