Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Shortest LWpolyline from a selection

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
hamza_itani
781 Views, 5 Replies

Shortest LWpolyline from a selection

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

5 REPLIES 5
Message 2 of 6
dbhunia
in reply to: hamza_itani

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

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 3 of 6
Kent1Cooper
in reply to: dbhunia


@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.]

 

Kent Cooper, AIA
Message 4 of 6

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

 

Message 5 of 6
ronjonp
in reply to: hamza_itani

Solved HERE too...

Message 6 of 6
Kent1Cooper
in reply to: Kent1Cooper


@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))))
Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost