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

时光怂恿深爱的人放手 提交于 2021-01-29 15:50:25

问题


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?


回答1:


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)
        )
    )
)


来源:https://stackoverflow.com/questions/61619896/autolisp-trying-to-select-lwpolyline-but-only-rectangles-how-do-i-do-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!