The AutoLisp Intermediate Tutorial


Selection Set Functions

Functions  -    ssadd      ssdel     sslength     ssname      Example Programs


ssadd - This function does one of three things depending on how many arguments you send with it. 

                If you use the ssadd function with no arguments it will return a new selection set with no members.

                       Example :    (setq myNewSet(ssadd))

                If you use ssadd with a name of an entity it will return a new selection set with only that one member.  

                       Example :    (setq myNewSet(ssadd entityName))

                If you use ssadd with a name of an entity and name of an selection set, it will add that entity to the set.

                       Example :    (setq myOldSet(ssadd entityName selectionSetName))

          And return the selection set with the entity added to the set.     

                Syntax : (ssadd)  (ssadd entityName)  (ssadd entityName SelectionSetName)

                       Where entityName is a valid entity name returned by ssname or entsel.

                       Where selectionSetName is a valid name of a selection set returned by setq.

               Returns the selection set with the new entity added to it.

                      Example: (<Selection set: 1>


           Example:  Let's build a function that allows me to add items to a selection set until I stop.

           (defun C:BldSet()                     ;defin a program name

      (setq mySet(ssadd))                  ;build an empty selection set

      (while(/= (setq enS(entsel)) nil)   ;loop until I pick nothing or press enter.

        (setq en(car enS))                  ;get the entity name of the selected item

        (setq mySet(ssadd en mySet))       ;add the entity to the selection set

      )

     )

           Now at the Command prompt type:

           Command: move<enter>

           Command: !mySet<enter>             ;don't forget the exclamation point in front of the variable name.

           Did everything get selected to be moved?  Cool.  Move individual items around and try it again.   Did everything get selected again?  Cool.  Why? Because AutoLisp finds them by their entity name, not their location.                          

 

ssdel - This function does the opposite of ssadd.   SSDEL removes an entity from a selection set.

                  Syntax   :  (ssdel entityName selectionSetName)

                       Where entityName is a valid entity name returned by ssname or entsel.

                       Where selectionSetName is a valid name of a selection set returned by setq.

               Returns a new selection set without the entity.

               Assuming en1 is a valid entity and en2 is not. 

               Assuming ss1 is a valid selection set and ss2 is not.

               Assuming en3 is a valid entity name but is not part of any selection set.

             (ssdel en1 ss1)                   ;returns selection set without entity en.

             (ssdel en1 ss2)                   ;returns ERROR - Bad Argument Type

             (ssdel en2 ss1)                   ;returns ERROR - Bad Argument Type 

             (ssdel en2 ss2)                   ;returns ERROR - Bad Argument Type 

             (ssdel en3 ss1)                    ;returns nil 

                Note: If you filter correctly using the ssget "X" function, you shouldn't need ssdel.

           

sslength - This function returns the number of entities inside a selection set.

                 Syntax   :  (sslength selectionSetName)

                        Where selectionSetName is a valid name of a selection set returned by setq.

               Returns a real number if the number is greater than 32,767, otherwise it returns an integer.

                             The number, real or integer, represents the quantity of items inside the selection set.

               Assuming ss1 is a valid selection set containing 15 entities.

               Assuming ss2 is a valid selection set containing 1 entity.

               Assuming ss3 is a valid selection set containing no entities.

               Assuming ss4 is a valid selection set containing 40,000 entities.

               Assuming ss5 is not a valid selection set.

             (sslength ss1)                     ;returns 15 as an integer

             (sslength ss2)                     ;returns 1 as an integer

             (sslength ss3)                     ;returns 0 as an integer

             (sslength ss4)                     ;returns 40000.0 as a real number

             (sslength ss5)                     ;returns ERROR - Bad Argument Type

 

ssname - This function returns an entity name contained inside a selection set.

                 Syntax   :  (ssname selectionSetName nthItem)

                        Where selectionSetName is a valid name of a selection set returned by setq.

                        Where nthItem is an integer representing an index into the set.

                         Note:   nthItem needs to be a real if the set contains more than 32,767 items.

               Returns an entity's name on success and nil on error.

               Assuming ss1 is a valid selection set containing 15 entities.

               Assuming ss2 is a valid selection set containing 40,000 entities.

               Assuming ss3 is not a valid selection set.

             (ssname ss1 0)                     ;returns the first entity in the selection set.

             (ssname ss1 3)                     ;returns the fourth entity.

             (ssname ss1 24)                    ;returns nil

             (ssname ss1 -3)                    ;returns nil

             (ssname ss2 34555)                 ;returns ERROR - Bad Argument Type  (needs real)

             (ssname ss2 34555.0)               ;returns the 34555th entity

          

End of Selection Set Functions


Example Program 1:

I will write a program that prints the layer name of every entity in a selection set.

(defun C:pLay()                                 ;define a program name

  (if (setq eset(ssget)                         ;use the ssget function to select entities

    (progn                                      ;use progn since there will be more than 1 statement

      (setq cntr 0)                             ;set the cntr to the first item in the set

      (while (< cntr (sslength eset))              ;while cntr is less than the length of the set

             ;note: the length is one more than the index of items since the first item is zero.  In other words, to get to the                ;first item in a selection set consisting of one item you would use (ssname eset 0) not (ssname eset 1).

        (setq en(ssname eset cntr))                    ;get the entity name of the item indexed with cntr

        (setq enlist(entget en))                       ;get the dxf group codes of the enitity

        (setq layName(cdr(assoc 8 enlist)))      ;get the layer name

                   (princ "\n ")                            ;print "\n " will cause a new line to be printed

        (princ layName)                            ;print the layer name to the command line

        (setq cntr(+ cntr 1))                      ;increment the counter

      )                                            ;close the while statement

    )                                              ;close the progn on the if statement

    (princ "\n Error - No entities selected.") ;print a message on the else statement

          ; note: the if statement can be a " if then " statement or a " if then else" statement

  )                                             ;close the if statement

)                                               ;close the program


Note: I personally do not find any of these functions useful except for the ssname and sslength functions.

If you use your ssget "X" filters well enough there is no reason for the ssadd or ssdel statements.

There are other selection set functions that are even less useful such as   ssmemb, sssetfirst, & ssgetfirst.

When you loop through your selection set you can check an entities DXF group codes and decide whether the entity is one to either skip or work on.  So, you can avoid all of these useless functions.  Don't get me wrong.  I'm not saying they are completely useless and you shouldn't use them.  I'm saying why bother.


AutoLisp Intermediate Tutorial

AutoLisp Tutorial Home