After I manually select multiple LWpolylines, I need a command to find and select the shortest (closed) LWpolyline from my current selection, I need only one polyline to be selected at a time, even if multiple polylines share the same length.
Thanks
Solved! Go to Solution.
Solved by dbhunia. Go to Solution.
Try this....
(defun c:fspl (/ ss i e d lst add) (vl-load-com) (setq ss (ssget ":L" '((0 . "LWPOLYLINE")(70 . 1)))) (repeat (setq i (sslength ss)) (setq e (ssname ss (setq i (1- i))) d (vlax-curve-getdistatparam e (vlax-curve-getendparam e)) ) (setq lst (cons (list d e) lst)) ) (setq lst (vl-sort lst '(lambda (e1 e2) (< (car e1) (car e2))))) (setq add (ssadd)) (ssadd (nth 1 (nth 0 lst)) add) (sssetfirst nil add) (princ) )
@dbhunia wrote:
....
....
(setq ss (ssget ":L" '((0 . "LWPOLYLINE")(70 . 1)))) ....
I would point out that you must not have tried that including any closed Polylines with linetype generation enabled, because their 70-code entry will be (70 . 129) and will not be "seen" in that selection.
But also, it can be done without building a whole list of sub-lists pairing entity names with lengths, and then sorting that by the lengths. It can just step through the selection, and if the current one's length is the shortest length found so far, make that the one to be selected/highlighted/gripped. Lightly tested:
(vl-load-com); if needed
(defun C:SCPL (/ ss len n pl pllen shortest); = Shortest Closed PolyLine (if (setq ss "_:L" (ssget '((0 . "LWPOLYLINE") (-4 . "&") (70 . 1))))
;; finds closed Polylines with or without linetype generation enabled (progn (setq len 1e10); initial reference, assuming none billions of units long (repeat (setq n (sslength ss)) (setq pl (ssname ss (setq n (1- n))); the current Polyline pllen (vlax-curve-getDistAtParam pl (vlax-curve-getEndParam pl)); its length len (min pllen len); whichever is shorter ); setq (if (= pllen len) (setq shortest (ssadd pl))); shortest? make this the one ); repeat (sssetfirst nil shortest); select/highlight/grip result ); progn ); if (princ) ); defun
[Depending on what you want to do about the shortest one, you might omit the restriction to only those on unlocked Layers -- the "_:L" in the (ssget) function. If you only want to know which it is, or how long it is, or what Layer it's on, or something, but don't want to do anything to it, it wouldn't matter whether its Layer is locked.]
Try the MinOf utility from www.cadstudio.cz/freeware - its selects minimum/maximum of any object property.
Vladimir Michl, www.cadstudio.cz www.cadforum.cz
@Kent1Cooper wrote:....
.... (if (setq ss "_:L" (ssget '((0 . "LWPOLYLINE") (-4 . "&") (70 . 1))))
....[.... you might omit the restriction to only those on unlocked Layers -- the "_:L" in the (ssget) function. ....]
Whoops -- I had omitted that initially [and tested without it], then decided to put it back in, but put it in the wrong place. If desired, it should be here:
(if (setq ss (ssget "_:L" '((0 . "LWPOLYLINE") (-4 . "&") (70 . 1))))