<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Selection Set Sorting base on X axis in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107725#M119893</link>
    <description>&lt;P&gt;Oops. &amp;nbsp;I just saw where I &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;forgot a quote before *POLYLINE.&lt;/FONT&gt;&lt;/STRONG&gt; &amp;nbsp;We old farts are so forgetful. &amp;nbsp;@Booker280Z would not make such a mistake.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wish me luck tomorrow. &amp;nbsp;I'm off to flatter NJDEP into accepting my methodology for a flood plain analysis. &amp;nbsp;I feel confident as it uses my own1993 software. :]&lt;/P&gt;</description>
    <pubDate>Thu, 25 May 2017 01:28:57 GMT</pubDate>
    <dc:creator>john.uhden</dc:creator>
    <dc:date>2017-05-25T01:28:57Z</dc:date>
    <item>
      <title>Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7101127#M119889</link>
      <description>&lt;P&gt;Hi Sirs,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm just new on using autolisp and still learning so please help me on the dilemma i'm facing.&lt;/P&gt;&lt;P&gt;Basically i want to sort a selection set of polyline (rectangle) under the layer of "REF" basing on it's X axis. I copied bits of info around the net but i can't get it work the way i want it to.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is where i copied some of it.&lt;BR /&gt;[URL="&lt;A href="http://forums.augi.com/showthread.php?137837-Sort-Selectionset-by-X-coord&amp;quot;]http://forums.augi.com/showthread.php?137837-Sort-Selectionset-by-X-coord[/URL" target="_blank"&gt;http://forums.augi.com/showthread.php?137837-Sort-Selectionset-by-X-coord"]http://forums.augi.com/showthread.php?137837-Sort-Selectionset-by-X-coord[/URL&lt;/A&gt;]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;(setq plt_set (ssget '((8 . "A3 REF")))) ;select layer "a3 ref"
  (setq i (getint "\nEnter page no.: ") ;input page number
	count 1
	count1 (sslength plt_set)
	ss6 (ssadd)
	ss4 (list (ssname plt_set (1- count)))
	)

  (while (&amp;lt;= (1+ count) count1)        ;SORT LIST BY X COORD LOW TO HIGH
    (setq ss3 (list (ssname plt_set count)))
    (setq ss4 (append ss4 ss3))
    (setq count (1+ count))
  ) ;_ end of while
  
  (vl-sort ss4
	 (function
	   (lambda (a b) (&amp;gt; ;; or maybe &amp;gt; depending on the order you want
			   (cadr (assoc 10 (entget a)))
			   (cadr (assoc 10 (entget b)))
			   )
	     )
	   )
	 )
  (foreach ss5 (reverse ss4)
    (setq ss6 (ssadd ss5 ss6))
    )&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is still sorting by the sequence of when it was created.&lt;/P&gt;&lt;P&gt;Hoping for your kind help Cad Masters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;BR /&gt;Ryan&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 07:58:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7101127#M119889</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-05-23T07:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7101272#M119890</link>
      <description>&lt;P&gt;Hi Ryan and welcome to the Forums!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's hard for me to get the logic of your variables naming. I've took a liberty and took another code from the link you've posted -&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/514089"&gt;@alanjt_&lt;/a&gt;&amp;nbsp;'s code look much better and I've fixed the minor typo it had.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;(defun c:Test ( / _SortSSByXValue ss)
  
  ; alanjt
  ; http://forums.augi.com/showthread.php?137837-Sort-Selectionset-by-X-coord%22]http://forums.augi.com/showthread.php?137837-Sort-Selectionset-by-X-coord[/URL
  (defun _SortSSByXValue (ss / lst i e add)
    (if (eq (type ss) 'PICKSET)
      (progn
        (repeat (setq i (sslength ss))
          (setq lst (cons (cons (setq e (ssname ss (setq i (1- i))))
                                (cadr (assoc 10 (entget e))))
                          lst)))
        (setq add (ssadd))
        (foreach e (vl-sort lst (function (lambda (a b) (&amp;lt; (cdr a) (cdr b)))))
          (ssadd (car e) add))
        (if (&amp;gt; (sslength add) 0)
          add))))
  
  ; -----------------------------------------------------------
  
  (if (setq ss (ssget '((0 . "LWPOLYLINE") (8 . "A3 REF"))))
    (setq ss (_SortSSByXValue ss))))&lt;/PRE&gt;
&lt;P&gt;BUT be aware that a rectangle is by a definition a LWPOLYLINE which EACH vertex has code 10 - so the code is sorting the ss by the FIRST vertex of each polyline (rectangle).&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 09:05:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7101272#M119890</guid>
      <dc:creator>ВeekeeCZ</dc:creator>
      <dc:date>2017-05-23T09:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7101432#M119891</link>
      <description>&lt;P&gt;I got my code working as i would like. the vl-sort part is the problem, i didnt store it in a new variable so. " (setq ss99 (vl-sort ...)" this solves the prob.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you for your response.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 10:38:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7101432#M119891</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-05-23T10:38:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107715#M119892</link>
      <description>&lt;P&gt;But be careful. &amp;nbsp;Are you sure they are lightweights? &amp;nbsp;Is the first vertex always to the left?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Shooting from the hip (as they say in the old western movies, before Clint Eastwood)... IOW, not tested at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;(defun @group (old n / item new)
  (while old
    (while (&amp;lt; (length item) n)
      (setq item (cons (car old) item) old (cdr old))
    )
    (setq new (cons (reverse item) new) item nil)
  )
  (reverse new)
)
(defun @SortPolyLR ( / ss i n obj coords objlist)
  (and
    (setq ss (ssget '((0 . *POLYLINE"))))
    (repeat (setq i (sslength ss))
      (setq obj (vlax-ename-&amp;gt;vla-object (ssname (setq i (1- i)))))
      (setq coords (vlax-get Obj 'Coordinates))
      (setq n (if (= (vlax-get Obj 'ObjectName) "AcDbPolyline") 2 3))
      (setq coords (@group coords n))
      (setq objlist (cons (cons (apply 'min (mapcar 'car coords)) obj) objlist))
    )
    (setq objlist (vl-sort '(lambda (a b)(&amp;lt; (car a)(car b))) objlist))
  )
  (mapcar 'cdr objlist)
)&lt;/PRE&gt;
&lt;P&gt;@SortPolyLR should return a list of selected polyline objects (LW, heavy, or 3D) sorted in ascending X value from their left-most vertex.&lt;/P&gt;
&lt;P&gt;No, it does not account for bulges out to the left, but could be written to use getboundingbox instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am also pleased that it is the first use of the new function&amp;nbsp;@group, unless someone has just beaten me to it.&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2017 01:12:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107715#M119892</guid>
      <dc:creator>john.uhden</dc:creator>
      <dc:date>2017-05-25T01:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107725#M119893</link>
      <description>&lt;P&gt;Oops. &amp;nbsp;I just saw where I &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;forgot a quote before *POLYLINE.&lt;/FONT&gt;&lt;/STRONG&gt; &amp;nbsp;We old farts are so forgetful. &amp;nbsp;@Booker280Z would not make such a mistake.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wish me luck tomorrow. &amp;nbsp;I'm off to flatter NJDEP into accepting my methodology for a flood plain analysis. &amp;nbsp;I feel confident as it uses my own1993 software. :]&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2017 01:28:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107725#M119893</guid>
      <dc:creator>john.uhden</dc:creator>
      <dc:date>2017-05-25T01:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107727#M119894</link>
      <description>&lt;P&gt;Yes, I also forgot (in red)...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;(setq obj (vlax-ename-&amp;gt;vla-object (ssname &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;ss&lt;/FONT&gt;&lt;/STRONG&gt; (setq i (1- i)))))&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2017 01:32:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107727#M119894</guid>
      <dc:creator>john.uhden</dc:creator>
      <dc:date>2017-05-25T01:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107757#M119895</link>
      <description>&lt;P&gt;Yet another possibility&lt;/P&gt;
&lt;PRE&gt;(defun c:somefunc  (/)
 (setq ss1 (ssadd))
 (mapcar '(lambda (x) (ssadd (car x) ss1))
         (vl-sort (mapcar '(lambda (x)
                            (cons x
                                  (vl-sort (cdr (reverse (vl-remove-if-not 'listp (mapcar 'cdr (entget x)))))
                                           '(lambda (x y) (&amp;lt; (cadr x) (cadr y))))))
                          (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "lwpolyline") (8 . "REF")))))))
                  '(lambda (x y) (&amp;lt; (cadadr x) (cadadr y)))))
 ss1)&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 May 2017 02:05:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7107757#M119895</guid>
      <dc:creator>Ranjit_Singh</dc:creator>
      <dc:date>2017-05-25T02:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Selection Set Sorting base on X axis</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7421187#M119896</link>
      <description>&lt;P&gt;Here is my little utility i use for sorting selection sets.&amp;nbsp; The reason for some of the alternative features is to being able to call it from VBA (in which case, i later on developed my own sort function within VBA to reduce LISP call from within VBA code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;; ssSort
;           ss:  AutoLISP pickset, or a selection set name -- if nil, it uses the previous selection set
;          xyz: 'X 'Y 'Z (or "x" "y" "z"... or 0 1 2)
;          dir:  +1 to +2 ascending, -1 to -2 descending... where pos/neg 1 is min and 2 is max (1.5 mid)
; RETURN VALUE: the sorted pickset
;
; examples:  (ssSort mySelSet 'X 1.5)  -- sorts selection set 'mySelSet' by center X ascending
;            (ssSort nil 'Y -1)        -- sorts the previous selection set by minimum Y descending

(defun ssSort (ss xyz dir /  
                RetVal objSelSet i ent obj entMin entMax c valMin valMax val presort sorted objs item)
  (setq RetVal nil)
  (if (not ss)
    (setq ss (ssget "P"))
  )
  (if (= (type ss) 'STR)
    (if (vl-Catch-All-Error-p (setq objSelSet (vl-Catch-All-Apply 'vla-item (list (vla-get-SelectionSets (vla-get-ActiveDocument (vlax-get-acad-object))) ss))))
      (progn
        (princ (strcat "\nUnable to find selection set '" ss "."))
      )
      (progn
        (setq ss (ssadd))
        (vlax-for obj objSelSet
          (setq ss (ssadd (vlax-vla-object-&amp;gt;ename obj) ss))
        )
      )
    )
  )
    
  (cond
   ((or (= xyz 0) (= xyz 'X) (= xyz "X") (= xyz "x")) (setq c 0))
   ((or (= xyz 1) (= xyz 'Y) (= xyz "Y") (= xyz "y")) (setq c 1))
   ((or (= xyz 2) (= xyz 'Z) (= xyz "Z") (= xyz "z")) (setq c 2))
  )
    
  (if (and ss (= (type ss) 'PICKSET) (&amp;gt;= (abs dir) 1) (&amp;lt;= (abs dir) 2))
    (progn
      (setq i -1)
      (repeat (sslength ss)
        (setq ent (ssname ss (setq i (1+ i)))
              obj (vlax-ename-&amp;gt;vla-object ent)
        )
        (vla-getBoundingBox obj 'entMin 'entmax)
        (if (and entMin entMax)
          (setq valMin (vlax-safearray-get-element entMin c)
                valMax (vlax-safearray-get-element entMax c)
                val (+ valMin (* (- valMax valMin) (- (abs dir) 1)))
                presort (cons (cons ent val) presort)
          )
        )
      )
      (if presort
        (progn
          (setq sorted (vl-sort presort (function (lambda (e1 e2) (&amp;lt; (cdr (if (&amp;gt; dir 0) e1 e2)) (cdr (if (&amp;gt; dir 0) e2 e1)))))))
          (setq i 0)
          (setq Retval (ssadd))
          (foreach item sorted
            (setq RetVal (ssadd (car item) RetVal))
          )
          (if objSelSet
            (progn
              (vla-Clear objSelSet)
              (setq objs (vlax-make-safearray vlax-vbObject (cons 0 (- (sslength ss) 1)))
                    i -1
              )
              (foreach item sorted
                (vlax-safearray-put-element objs (setq i (1+ i)) (vlax-ename-&amp;gt;vla-object (car item)))
              )
              (vla-AddItems objSelSet objs)
            )   
          )   
        )
      )
    )
  )
  RetVal
)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 18:43:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-sorting-base-on-x-axis/m-p/7421187#M119896</guid>
      <dc:creator>lando7189</dc:creator>
      <dc:date>2017-09-29T18:43:56Z</dc:date>
    </item>
  </channel>
</rss>

