Jump to content

Selections Sort .... all type of order


X11start

Recommended Posts

I would like to make a lisp that orders me the selected objects, in various ways:
I'll give an example with numbers, but they could be polylines or any other object.

   1   2   3

   4   5   6

   7   8   9

 

I would like to be able to select them with "Windows" and give them e.g. the Left-Right and top-down mode (as you read a text):
1 2 3 4 5 6 7 8 9

but also other ways e.g. from bottom to top and from right to left:
9 6 3 8 5 2 7 4 1

or from top to bottom and from left to right:
1 4 7 2 5 8 3 6 9

I Attached the lisp that I did and that runs only from left to right and from top to bottom...
but I think you can find solutions faster than mine!

 

My lisp is not complete: LISTA1  output contains the coordinates of the element + the selection set... a I just have to do SSADD to complete the command and create selection set final.

numbers.gif

SelSort.lsp ORDINE.lsp

  • Like 1
Link to comment
Share on other sites

On occasion when I've needed to select objects in a particular order I'll use the fence option. 

Not as quick as what you're looking for but faster than picking each one individually.

Link to comment
Share on other sites

  1. Build a selection set with ssget
  2. use a point of some type that all entity's have. (usually #10)
  3. build a list with the entity name and point.
  4. process the list using the point
  5. after sorting process entity's
(if (setq SS (ssget "_:L")) ;#1
    (progn
      (setq tlst (mapcar '(lambda (x) (list (cdr (assoc 10 (entget x))) x)) (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS))))) ;#2 & 3
      (setq tlst
            (mapcar 'cadr ;after sort strip point and only leave entity name
                    (vl-sort tlst ;#4 
                             '(lambda (a b)
                                (if (equal (cadr (car a)) (cadr (car b)) 1e-6) ;if y are equal
                                  (< (caar a) (caar b)) ;true sort left most first
                                  (> (cadr (car a)) (cadr (car b))) ;false sort highest first
                                )
                              )
                    )
            )
      )
    )
  )
  (foreach ent tlst

 

This method has some handy caps. Entity's need to be in a grid and basically a straight line. 

example. if #2 is higher then 1 it will be first in the list.

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

Thank you all for your help, especially to MHUPP: your lisp is definitely more effective than mine!

 

Now on this basis, I will try to make sure that you can get the other 3 types of sorting (to the remaining 4 I follow the advice of STEVEN P... I use REVERSE!)

Link to comment
Share on other sites

2 hours ago, X11start said:

Thank you all for your help, especially to MHUPP: your lisp is definitely more effective than mine!

Now on this basis, I will try to make sure that you can get the other 3 types of sorting (to the remaining 4 I follow the advice of STEVEN P... I use REVERSE!)

 

or you can just switch how it sorts.

 

123
456
789

if statment for y sorts by row
if statment for x sorts by column
switch the > < signs to get the order you want

;sorts 321654987
(if (equal (cadr (car a)) (cadr (car b)) 2) ;if y are equal within 2 
  (> (caar a) (caar b)) 
  (< (cadr (car a)) (cadr (car b)))
)

;sorts 963852741
(if (equal (caar a) (caar b) 2) ;if x are equal within 2
  (< (cadr (car a)) (cadr (car b))) 
  (> (caar a) (caar b)) 
)

;sorts 147258369
(if (equal (caar a) (caar b) 2) 
  (> (cadr (car a)) (cadr (car b))) 
  (< (caar a) (caar b)) 
)

 

  • Like 1
Link to comment
Share on other sites

;
;  (load "Selsort")
;  (selsort 2)
;  ... select object ...
;
;  Verify:
;  command -> Delete
;  (nth 0 tlst) -> delete text "1"
;
;  command -> Delete
;  (nth 1 tlst) -> delete text "4"
;
; ...
;  command -> Delete
;  (nth 8 tlst) -> delete text "9"
;
; If TIPO > 4 TLST is reverse.
;
;
;  1   2   3
;  4   5   6
;  7   8   9

(defun SelSort (tipo / flag)

  (setq SS (ssget))
  (setq	tlst				; Crea una lista contenente le coord. x e y + il set di selezione
	 (mapcar '(lambda (x) (list (cdr (assoc 10 (entget x))) x))
		 (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
	 )
  )					;#2 & 3
;
  (if (> tipo 4)			; se Tipo > 4 lo riporta ai primi 4 ma mette un Flag
    (setq tipo (- tipo 4) flag T)
  )
;
  (if (= tipo 1)			; Sx-Dx & Up-Down -> 1-2-3-4-5-6-7-8-9
    (setq tlst
	   (mapcar
	     'cadr			;dopo aver ordinato il punto della striscia e lasciare solo il nome dell'entita'  
	     (vl-sort tlst		;#4 
		      '(lambda (a b)
			 (if (equal (cadr (car a)) (cadr (car b)) 1e-6)
					;se y sono uguali 
			   (< (caar a) (caar b))
					;Se true: ordina prima a sinistra 
			   (> (cadr (car a)) (cadr (car b)))
					;Se false: ordina prima il piu' alto 
			 )
		       )
	     )
	   )
    )
  )
;
  (if (= tipo 2)			; Up-Down & Sx-Dx -> 1-4-7-2-5-8-3-6-9
    (setq tlst
	   (mapcar
	     'cadr			;dopo aver ordinato il punto della striscia e lasciare solo il nome dell'entita'  
	     (vl-sort tlst		;#4 
		      '(lambda (a b)
			 (if (equal (car (car a)) (car (car b)) 1e-6)
					;se x sono uguali
			   (> (cadr (car a)) (cadr (car b)))
					;Se true: ordina prima il piu' alto 
			   (< (caar a) (caar b))
					;Se false: ordina prima a sinistra 
			 )
		       )
	     )
	   )
    )
  )
;
  (if (= tipo 3)			; Dx-Sx & Up-Down -> 3-2-1-6-5-4-9-8-7
    (setq tlst
	   (mapcar
	     'cadr			;dopo aver ordinato il punto della striscia e lasciare solo il nome dell'entita'  
	     (vl-sort tlst		;#4 
		      '(lambda (a b)
			 (if (equal (cadr (car a)) (cadr (car b)) 1e-6)
					;se y sono uguali 
			   (> (caar a) (caar b))
					;Se true: ordina prima a Destra
			   (> (cadr (car a)) (cadr (car b)))
					;Se false: ordina prima il piu' alto 
			 )
		       )
	     )
	   )
    )
  )
;
  (if (= tipo 4)			; Up-Down & Sx-Dx -> 3-6-9-2-5-8-1-4-7
    (setq tlst
	   (mapcar
	     'cadr			;dopo aver ordinato il punto della striscia e lasciare solo il nome dell'entita'  
	     (vl-sort tlst		;#4 
		      '(lambda (a b)
			 (if (equal (car (car a)) (car (car b)) 1e-6)
					;se x sono uguali
			   (> (cadr (car a)) (cadr (car b)))
					;Se true: ordina prima il piu' alto 
			   (> (caar a) (caar b))
					;Se false: ordina prima a Destra
			 )
		       )
	     )
	   )
    )
  )
;
  (if flag				; se Flag -> True Inverte la lista
    (setq tlst (reverse tlst))
  )
;
)

I expanded the code of MHUPP... this is the result!

 

thank you very much!

Edited by X11start
  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...