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

Order of selection set

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
saitoib
1147 Views, 11 Replies

Order of selection set

saitoib
Advocate
Advocate
Hi all.
 
The order of the selection set created by ssget may change even under the same conditions.
If I write a program that depends on the order of the selection set, it may not work as expected.
I think the value obtained by (ssname ss i) is the memory address of the variable.
Is it possible to sort by this value order?

Thank you.
Saitoib
0 Likes

Order of selection set

Hi all.
 
The order of the selection set created by ssget may change even under the same conditions.
If I write a program that depends on the order of the selection set, it may not work as expected.
I think the value obtained by (ssname ss i) is the memory address of the variable.
Is it possible to sort by this value order?

Thank you.
Saitoib
11 REPLIES 11
Message 2 of 12
Kent1Cooper
in reply to: saitoib

Kent1Cooper
Consultant
Consultant
Accepted solution

@saitoib wrote:
....
I think the value obtained by (ssname ss i) is the memory address of the variable.
Is it possible to sort by this value order? ....

The value obtained by (ssname ss i) would be an entity name.  I'm not on an AutoCAD computer right now, so I can't test whether those can be compared in a way that you could sort them by -- they sort of look numerical and/or like text, but they're not really numbers or text strings.  But entity names can be different from one session to another, so I would expect that you could get different results at different times from sorting the same group of entities, selected in the same way, and using the same sorting function.

 

But you could sort by handles:  (cdr (assoc 5 (entget (ssname ss i))))  Those don't change from one session to another.  And though they are not always sequential [there can be gaps], I think they only increase as objects are added.  If that's right, you should be able to sort things by their handles and always get drawing order.  But they're hexadecimal "numbers" [actually text strings in hexadecimal format], so they would need to be converted for sorting [there are converter functions around].

Kent Cooper, AIA
0 Likes


@saitoib wrote:
....
I think the value obtained by (ssname ss i) is the memory address of the variable.
Is it possible to sort by this value order? ....

The value obtained by (ssname ss i) would be an entity name.  I'm not on an AutoCAD computer right now, so I can't test whether those can be compared in a way that you could sort them by -- they sort of look numerical and/or like text, but they're not really numbers or text strings.  But entity names can be different from one session to another, so I would expect that you could get different results at different times from sorting the same group of entities, selected in the same way, and using the same sorting function.

 

But you could sort by handles:  (cdr (assoc 5 (entget (ssname ss i))))  Those don't change from one session to another.  And though they are not always sequential [there can be gaps], I think they only increase as objects are added.  If that's right, you should be able to sort things by their handles and always get drawing order.  But they're hexadecimal "numbers" [actually text strings in hexadecimal format], so they would need to be converted for sorting [there are converter functions around].

Kent Cooper, AIA
Message 3 of 12
devitg
in reply to: saitoib

devitg
Advisor
Advisor

@saitoib . As I can see , the SS,both by ssget or by activeselectionset  order,  is in reverse as it were add. 

Handle is in the same order it were add. 

 

;; Design by Gabo CALOS DE VIT from CORDOBA ARGENTINA
;;;    Copyleft 1995-2022 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM    
;;

 ; Hecho por  Gabo CALOS DE VIT de CORDOBA ARGENTINA
;;;    Copyleft 1995-2022 por Gabriel Calos De Vit 
;; DEVITG@GMAIL.COM 
;;;
;;************************************************************
;;;get-ss-order
(progn
;
;;-------------------------------------------------------------------------------------------------

;; hex2int.lsp
;; Hexadecimal to base-10 Integer converter [e.g. for entity handles]
;; Modified/simplified from Frank Oquendo's (basetodecimal) function.
;; Added negative-argument capability and argument validity controls.
;; Kent Cooper, January 2011

(DEFUN HEX2INT/HNDL$  (HNDL / RESULT NEG POWER TMP)
  (IF (/= (TYPE HNDL) 'STR)
    (PROGN
      (ALERT "Requires string argument.")
      (HEX2INT/HNDL$)
      ) ; end progn
    ) ; end if
  (SETQ
    HNDL   (STRCASE HNDL)
    RESULT 0
    ) ; end setq
  (IF (= (SUBSTR HNDL 1 1) "-")
    (SETQ NEG  T
          HNDL (SUBSTR HNDL 2))
    ) ; end if
  (IF (/= (VL-STRING-TRIM "0123456789ABCDEF" HNDL) "")
    (PROGN
      (ALERT "Invalid hexadecimal string.")
      (QUIT)
      ) ; end progn
    ) ; end if
  (REPEAT (SETQ POWER (STRLEN HNDL))
    (SETQ RESULT
                 (+
                   RESULT
                   (*
                     (-
                       (SETQ TMP (ASCII (SUBSTR HNDL 1 1)))
                       (IF (> TMP 64)
                         55
                         48)
                       ) ; end -
                     (EXPT 16 (SETQ POWER (1- POWER)))
                     ) ; end *
                   ) ; end +
          HNDL   (SUBSTR HNDL 2)
          ) ; end setq
    ) ; end while
  (IF NEG
    (- RESULT)
    RESULT)
  ) ; end defun - hex2int
;;*//*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*


;;************************************************************************************************
)
;;*//*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/


(DEFUN get-ss-order (/
ACAD-OBJ
ADOC
HND-$
HND-POINT
MODEL
OBJ
ORDER
POINT
SS
SS-0
SS-0-OBJ
SS-OBJ
SS-OBJ-0
UTILITY
                    )


(VL-LOAD-COM)
(SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT)) ;_ el programa ACAD 
(SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) ;_ el DWG que esta abierto-
(SETQ MODEL (VLA-GET-MODELSPACE ADOC))
(setq UTILITY (VLA-GET-UTILITY adoc))


  
  (setq ss (ssget "X" '((0 . "circle"))))
  (setq ss-0 (ssname ss 0))
  (setq ss-0-obj (VLAX-ENAME->VLA-OBJECT ss-0))
  (setq ss-obj (VLA-GET-ACTIVESELECTIONSET adoc))
  (setq ss-obj-0 (vla-item ss-obj 0)); just to compare is SS-ent , have the same order as activselectionset
  (setq order 0)
;;;(setq obj ss-obj-0)
  (VLAX-FOR
         obj
            ss-obj

    (setq point (VLAX-CURVE-GETPOINTATPARAM obj 0))
    (VLA-ADDTEXT model (itoa order) (VLAX-3D-POINT point) 200)
    (setq hnd-point (VLAX-CURVE-GETPOINTATPARAM obj pi))
    (setq
      hnd-$ (VLA-ADDTEXT model (HEX2INT/HNDL$ (VLA-GET-HANDLE obj)) (VLAX-3D-POINT hnd-point) 100)
    ) ;_  setq
    (vla-move
      hnd-$
      (VLAX-3D-POINT hnd-point)
      (vla-polarpoint UTILITY (VLAX-3D-POINT hnd-point) pi 100)
    ) ;_  vla-move
    (setq order (1+ order))
  ) ;_  VLAX-FOR




) ;end main defun

(DEFUN C:order# ()
  
  (get-ss-order)

  (prompt "Command Name :  C:order#" )
  (princ)

  )
;|«Visual LISP© Format Options»
(100 2 1 2 T " " 100 6 0 0 1 nil T nil T)
;*** DO NOT add text below the comment! ***|;

 

 

devitg_0-1652539456591.png

 

 

 

@saitoib . As I can see , the SS,both by ssget or by activeselectionset  order,  is in reverse as it were add. 

Handle is in the same order it were add. 

 

;; Design by Gabo CALOS DE VIT from CORDOBA ARGENTINA
;;;    Copyleft 1995-2022 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM    
;;

 ; Hecho por  Gabo CALOS DE VIT de CORDOBA ARGENTINA
;;;    Copyleft 1995-2022 por Gabriel Calos De Vit 
;; DEVITG@GMAIL.COM 
;;;
;;************************************************************
;;;get-ss-order
(progn
;
;;-------------------------------------------------------------------------------------------------

;; hex2int.lsp
;; Hexadecimal to base-10 Integer converter [e.g. for entity handles]
;; Modified/simplified from Frank Oquendo's (basetodecimal) function.
;; Added negative-argument capability and argument validity controls.
;; Kent Cooper, January 2011

(DEFUN HEX2INT/HNDL$  (HNDL / RESULT NEG POWER TMP)
  (IF (/= (TYPE HNDL) 'STR)
    (PROGN
      (ALERT "Requires string argument.")
      (HEX2INT/HNDL$)
      ) ; end progn
    ) ; end if
  (SETQ
    HNDL   (STRCASE HNDL)
    RESULT 0
    ) ; end setq
  (IF (= (SUBSTR HNDL 1 1) "-")
    (SETQ NEG  T
          HNDL (SUBSTR HNDL 2))
    ) ; end if
  (IF (/= (VL-STRING-TRIM "0123456789ABCDEF" HNDL) "")
    (PROGN
      (ALERT "Invalid hexadecimal string.")
      (QUIT)
      ) ; end progn
    ) ; end if
  (REPEAT (SETQ POWER (STRLEN HNDL))
    (SETQ RESULT
                 (+
                   RESULT
                   (*
                     (-
                       (SETQ TMP (ASCII (SUBSTR HNDL 1 1)))
                       (IF (> TMP 64)
                         55
                         48)
                       ) ; end -
                     (EXPT 16 (SETQ POWER (1- POWER)))
                     ) ; end *
                   ) ; end +
          HNDL   (SUBSTR HNDL 2)
          ) ; end setq
    ) ; end while
  (IF NEG
    (- RESULT)
    RESULT)
  ) ; end defun - hex2int
;;*//*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*


;;************************************************************************************************
)
;;*//*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/


(DEFUN get-ss-order (/
ACAD-OBJ
ADOC
HND-$
HND-POINT
MODEL
OBJ
ORDER
POINT
SS
SS-0
SS-0-OBJ
SS-OBJ
SS-OBJ-0
UTILITY
                    )


(VL-LOAD-COM)
(SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT)) ;_ el programa ACAD 
(SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) ;_ el DWG que esta abierto-
(SETQ MODEL (VLA-GET-MODELSPACE ADOC))
(setq UTILITY (VLA-GET-UTILITY adoc))


  
  (setq ss (ssget "X" '((0 . "circle"))))
  (setq ss-0 (ssname ss 0))
  (setq ss-0-obj (VLAX-ENAME->VLA-OBJECT ss-0))
  (setq ss-obj (VLA-GET-ACTIVESELECTIONSET adoc))
  (setq ss-obj-0 (vla-item ss-obj 0)); just to compare is SS-ent , have the same order as activselectionset
  (setq order 0)
;;;(setq obj ss-obj-0)
  (VLAX-FOR
         obj
            ss-obj

    (setq point (VLAX-CURVE-GETPOINTATPARAM obj 0))
    (VLA-ADDTEXT model (itoa order) (VLAX-3D-POINT point) 200)
    (setq hnd-point (VLAX-CURVE-GETPOINTATPARAM obj pi))
    (setq
      hnd-$ (VLA-ADDTEXT model (HEX2INT/HNDL$ (VLA-GET-HANDLE obj)) (VLAX-3D-POINT hnd-point) 100)
    ) ;_  setq
    (vla-move
      hnd-$
      (VLAX-3D-POINT hnd-point)
      (vla-polarpoint UTILITY (VLAX-3D-POINT hnd-point) pi 100)
    ) ;_  vla-move
    (setq order (1+ order))
  ) ;_  VLAX-FOR




) ;end main defun

(DEFUN C:order# ()
  
  (get-ss-order)

  (prompt "Command Name :  C:order#" )
  (princ)

  )
;|«Visual LISP© Format Options»
(100 2 1 2 T " " 100 6 0 0 1 nil T nil T)
;*** DO NOT add text below the comment! ***|;

 

 

devitg_0-1652539456591.png

 

 

 

Message 4 of 12
john.uhden
in reply to: Kent1Cooper

john.uhden
Mentor
Mentor

That's fine about sorting by handles, but what about the impact of spatial indexing (DRAWORDER)?

I think it may be a matter of perspective as to whether the chicken came before the egg.

Then again it does make sense to follow the old entnext system and To HELLo With draworder.

John F. Uhden

0 Likes

That's fine about sorting by handles, but what about the impact of spatial indexing (DRAWORDER)?

I think it may be a matter of perspective as to whether the chicken came before the egg.

Then again it does make sense to follow the old entnext system and To HELLo With draworder.

John F. Uhden

Message 5 of 12
devitg
in reply to: john.uhden

devitg
Advisor
Advisor

@john.uhden , Hi John, please clear me about the way handle could be changed by draworder 

And to use entnext, how to know which one is the first ent ?

 

 

0 Likes

@john.uhden , Hi John, please clear me about the way handle could be changed by draworder 

And to use entnext, how to know which one is the first ent ?

 

 

Message 6 of 12
Sea-Haven
in reply to: john.uhden

Sea-Haven
Mentor
Mentor

Just a quick dirty check draw order does not change the handle ID.

 

I think Kent is right with sort handle

((integervalueofhandle <Entity name: 23648c4d140>orHandle)(integervalueofhandle <Entity name: 23648c4d140>orHandle).......

Then no need to convert back to which entity.

 

 

0 Likes

Just a quick dirty check draw order does not change the handle ID.

 

I think Kent is right with sort handle

((integervalueofhandle <Entity name: 23648c4d140>orHandle)(integervalueofhandle <Entity name: 23648c4d140>orHandle).......

Then no need to convert back to which entity.

 

 

Message 7 of 12
saitoib
in reply to: saitoib

saitoib
Advocate
Advocate
in the meantime, my problem seems to be solved by using the DXF group code of 5 shape handles.
Thank you very much.
Saitoib
0 Likes

in the meantime, my problem seems to be solved by using the DXF group code of 5 shape handles.
Thank you very much.
Saitoib
Message 8 of 12
john.uhden
in reply to: devitg

john.uhden
Mentor
Mentor

@devitg 

Gabe, I didn't mean that draworder changes handles, but it can change the perception of what is newer vs. older.

(setq e (entnext)) gets you the first entity in the drawing.

Say you have a number of entities and you want to know the order of their creation...

(defun findorder (lst / e order)
;; where lst is a list of enames
  (setq e (entnext))
  (while (and e (< (length order)(length lst)))
    (if (vl-position e lst)
      (setq order (cons e order))
    )
    (setq e (entnext e))
  )
  (reverse order)
)

 

John F. Uhden

0 Likes

@devitg 

Gabe, I didn't mean that draworder changes handles, but it can change the perception of what is newer vs. older.

(setq e (entnext)) gets you the first entity in the drawing.

Say you have a number of entities and you want to know the order of their creation...

(defun findorder (lst / e order)
;; where lst is a list of enames
  (setq e (entnext))
  (while (and e (< (length order)(length lst)))
    (if (vl-position e lst)
      (setq order (cons e order))
    )
    (setq e (entnext e))
  )
  (reverse order)
)

 

John F. Uhden

Message 9 of 12
devitg
in reply to: john.uhden

devitg
Advisor
Advisor

@john.uhden , ok . I got it. 

Thanks

 

0 Likes

@john.uhden , ok . I got it. 

Thanks

 

Message 10 of 12
john.uhden
in reply to: devitg

john.uhden
Mentor
Mentor

@devitg 

Good.

Depending on how much cr@p is in your drawing, it may actually be faster than converting an ename to a handle and a handle to an integer and then sorting.  I haven't compared but I used it on a busy drawing and it was almost instant.

John F. Uhden

0 Likes

@devitg 

Good.

Depending on how much cr@p is in your drawing, it may actually be faster than converting an ename to a handle and a handle to an integer and then sorting.  I haven't compared but I used it on a busy drawing and it was almost instant.

John F. Uhden

Message 11 of 12
devitg
in reply to: john.uhden

devitg
Advisor
Advisor

@john.uhden if you use this defun 

 

 ;*****************************************************************

(DEFUN SS-->>ENT-LIST/ss  (SS) ;_01
  (Reverse(VL-REMOVE-IF-NOT '(LAMBDA (X) (= (TYPE X) 'ENAME)) (MAPCAR 'CADR (SSNAMEX SS))))

  )
;;;*************************************************************;;;

It return form first to last as been drawn . Because SSGET return the selectionset from Lasta to first 

 

 No need to calc anything . 

Any way I almost do not use this defun , I like to handle by VL-function  by  VLA-GET-ACTIVESELECTIONSET adoc

(defun get-all-circle-area-sum (/ ACAD-OBJ ADOC AREA CIRCLE-ENT-SS CIRCLE-OBJ-SS MODEL
                               )

  (VL-LOAD-COM)
  (SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT)) ;_ el programa ACAD 
  (SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) ;_ el DWG que esta abierto-
  (SETQ MODEL (VLA-GET-MODELSPACE ADOC))

  (setq circle-ent-ss (ssget "X" '((0 . "circle"))))

  (setq circle-obj-ss (VLA-GET-ACTIVESELECTIONSET adoc))

 ;then
  (setq area nil)
  (VLAX-FOR
         circle-obj
                   circle-obj-ss

    (setq area (+ area (VLA-GET-area circle-obj)))
  ) ;_  VLAX-FOR circle-obj 

)

No need to know sslength , no need to get a Index counter 

. It is my way . 

 

 

 

 

 

 

0 Likes

@john.uhden if you use this defun 

 

 ;*****************************************************************

(DEFUN SS-->>ENT-LIST/ss  (SS) ;_01
  (Reverse(VL-REMOVE-IF-NOT '(LAMBDA (X) (= (TYPE X) 'ENAME)) (MAPCAR 'CADR (SSNAMEX SS))))

  )
;;;*************************************************************;;;

It return form first to last as been drawn . Because SSGET return the selectionset from Lasta to first 

 

 No need to calc anything . 

Any way I almost do not use this defun , I like to handle by VL-function  by  VLA-GET-ACTIVESELECTIONSET adoc

(defun get-all-circle-area-sum (/ ACAD-OBJ ADOC AREA CIRCLE-ENT-SS CIRCLE-OBJ-SS MODEL
                               )

  (VL-LOAD-COM)
  (SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT)) ;_ el programa ACAD 
  (SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) ;_ el DWG que esta abierto-
  (SETQ MODEL (VLA-GET-MODELSPACE ADOC))

  (setq circle-ent-ss (ssget "X" '((0 . "circle"))))

  (setq circle-obj-ss (VLA-GET-ACTIVESELECTIONSET adoc))

 ;then
  (setq area nil)
  (VLAX-FOR
         circle-obj
                   circle-obj-ss

    (setq area (+ area (VLA-GET-area circle-obj)))
  ) ;_  VLAX-FOR circle-obj 

)

No need to know sslength , no need to get a Index counter 

. It is my way . 

 

 

 

 

 

 

Message 12 of 12
cadffm
in reply to: devitg

cadffm
Consultant
Consultant

@devitg  schrieb

It return form first to last as been drawn . Because SSGET return the selectionset from Lasta to first 

That statement isn't correct (I think you know it, but some  other readers today or in future not)

 

1. The order inside a selectionset is the order of selection

2. If you are using selection methods like windows windowpolygon cross crosspolygon box all...
   AutoCAD use the creation-order <this is what you are talking about, selection set created by these methods>,

   And this happen for each select while  selection.

 

Sample selectionset, created by 4 selections

[selectionset; [s1;picked:one object][s2;window: creation order][s3;faun:faundirection order][s4;cross:creation order]]

 

 >>"No need to calc anything . "

If you like to have the creation order and if you don't allow selectionmethods like

Multiple, Implie, faun, pick .. than it is right, in othewr cases (what are common) it is wrong.

Sebastian


@devitg  schrieb

It return form first to last as been drawn . Because SSGET return the selectionset from Lasta to first 

That statement isn't correct (I think you know it, but some  other readers today or in future not)

 

1. The order inside a selectionset is the order of selection

2. If you are using selection methods like windows windowpolygon cross crosspolygon box all...
   AutoCAD use the creation-order <this is what you are talking about, selection set created by these methods>,

   And this happen for each select while  selection.

 

Sample selectionset, created by 4 selections

[selectionset; [s1;picked:one object][s2;window: creation order][s3;faun:faundirection order][s4;cross:creation order]]

 

 >>"No need to calc anything . "

If you like to have the creation order and if you don't allow selectionmethods like

Multiple, Implie, faun, pick .. than it is right, in othewr cases (what are common) it is wrong.

Sebastian

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report