Search code examples
lispautocadautocad-pluginautolisp

AutoLisp trying to select LWPolyline BUT ONLY RECTANGLES. How do I do that?


So I'm trying to select all entities that are rectangles. I tried this (setq ss (ssget "X" '((0 . "RECTANG")))) , but it seems to select all polylines, including polygons. I tried with checking the vertices = 4 but then it also selects diamonds. How could I implement such a code?


Solution

  • I'd be highly surprised if your current code using the ssget filter list '((0 . "RECTANG")) was to select anything at all, as RECTANG is not a valid entity type for DXF group 0.


    In AutoCAD, the standard RECTANG command actually creates 2D polylines, whose entity type (DXF group 0) is LWPOLYLINE. As such, your ssget filter list should start:

    (ssget "_X" '((0 . "LWPOLYLINE")))
    

    We can filter this selection further to select only 2D polylines with 4 vertices using:

    (ssget "_X" '((0 . "LWPOLYLINE") (90 . 4)))
    

    And we can filter for closed polylines with 4 vertices using:

    (ssget "_X" '((0 . "LWPOLYLINE") (90 . 4) (-4 . "&=") (70 . 1)))
    

    However, it is not possible to narrow the selection any further than this using only the ssget filter list, and so to target rectangles specifically, you'll need to iterate over the selection returned by the above expression and remove any polylines which are not rectangular.

    You might write such a function in the following way:

    (defun selrectang ( / a b c d e i s )
        (if (setq s (ssget "_X" '((0 . "LWPOLYLINE") (90 . 4) (-4 . "&=") (70 . 1))))
            (progn
                (repeat (setq i (sslength s))
                    (setq i (1- i)
                          e (ssname s i)
                    )
                    (mapcar 'set '(a b c d) (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget e))))
                    (if
                        (not
                            (and
                                (equal (distance a b) (distance c d) 1e-8)
                                (equal (distance b c) (distance a d) 1e-8)
                                (equal (distance a c) (distance b d) 1e-8)
                            )
                        )
                        (ssdel e s)
                    )
                )
                (if (< 0 (sslength s)) s)
            )
        )
    )