The AutoLisp Intermediate Tutorial


Selecting Entities

Functions  -    entsel     ssget    Example Programs


entsel - This function prompts for the user to pick one entity.   

                       Syntax : (entsel)

               Returns the entity's name and the point selected in a list.

                      Example: (<Entity name: 3b40a08> (230.666 285.862 0.0))


               (car(entsel)) returns the entity name if entsel does not return nil..

               (cadr(entsel)) returns the point selected if entsel does not return nil..

                Note: Selecting an area in the drawing where there are no entities would return nil.  You will get an error                   if you try to get the car or cadr of nil as shown above.  The best thing to do is to save the return value of                   the entsel function, then check to see if it is a valid selection. 

                 Example:

                     (setq myEnt(entsel))           ; save the return value of the entsel function

         (if myEnt                      ; if the value of myEnt is not nil then do some stuff 

           (progn                                                 ; use the progn statement here for multiple statements

              ;do some stuff           ; put your code here

           )                                                            ; close the progn statement here

                         (alert "Nothing Selected.")  ; alert the user

         )                               ; close the if statement

 

ssget - This function prompts the user to select entities.

                  Syntax   :  (ssget)    ;prompts the user for a selection set.

                  Syntax   :  (ssget mode)    ;prompts the user for a selection set and sets mode to "W", "C", "L", and "P",                                     corresponding to the Window, Crossing, Last, and Previous selection methods. Another                                     optional mode value is "X", which selects the entire database.  There are other modes, but this                                     list contains the modes that will be used most often.

                           Syntax   :  (ssget "W" )    ;prompts the user for a selection set using the Window mode.

                           Syntax   :  (ssget "C" )    ;prompts the user for a selection set using the Crossing mode.

                           Syntax   :  (ssget "L" )    ;Selects the last entity selected.

                           Syntax   :  (ssget "P" )    ;Selects the previously selected entities.

                           Syntax   :  (ssget "X" )    ;Selects all entities in the drawing.

                  Syntax   :  (ssget filter_List)  or (ssget mode filter_List)

                 filter_List - an association list containing valid filters for entities corresponding to the Entity DXF Group                              Codes.     What did I just say?        Entity DXF Group Codes?   What the heck is that?                              Click here to find out. 

                           You can filter out enitites that do no match your criteria using the filter_List.  If you only                              wanted entities that were on layer "STR" then you could add a filter list that looks like this:

           (setq myFilter(list (cons 8 "STR"))) ;returns ((8 . "STR"))

           (ssget "X" myFilter)  ;returns a selection set containing only entities on layer "STR"

                        or

           (setq myFilter(list (cons 0 "LINE")(cons 8 "STR")))

                                ;returns ((0 . "LINE")(8 . "STR"))

           (ssget "X" myFilter) 

                               ;returns a selection set containing only line entities on layer "STR"

     

                  Returns a selection set of entity names.

End of Selecting Entities


Example Program 1:

(defun C:myProg()

  (if (setq myEnt(entsel))              ;start the if statement

   (progn                               ;going to have multiple statements

    (setq pt1(getpoint "\n Base Point: "))              ;store base point

    (setq pt2(getpoint "\n Displacement Point: "))     ;store displacement pt 

    (command "move" (car myEnt) "" pt1 pt2)             ;move entity

   )                                     ;close the progn

   (alert "Please select an entity!")    ;else alert the user of an error

  )                                      ;close the if statement

  (princ)                               ;clean exit (supresses echo)

)                                       ;close the program

Execution:

Command: (load "myProg")<enter>

Command: myProg<enter>

Command: Select Object: <pick>

Command: Base Point: <pick>

Command: Displacement Point: <pick>

Command:


Example Program 2:

(defun C:myProg2()

  (if (setq mySet(ssget "X" (list (cons 8 "STR")(cons 0 "CIRCLE")))) ;get set

   (progn                               ;going to have multiple statements

    (setq pt1(getpoint "\n Base Point: "))              ;store base point

    (setq pt2(getpoint "\n Displacement Point: "))     ;store displacement pt 

    (command "move" mySet "" pt1 pt2)   ;move all entities

   )                                     ;close the progn

   (alert "No entites Match Criteria!") ;else alert the user of an error

  )                                      ;close the if statement

  (princ)                               ;clean exit (supresses echo)

)                                       ;close the program

Execution:

Command: (load "myProg2")<enter>

Command: myProg2<enter>

Command: Base Point: <pick>

Command: Displacement Point: <pick>

Command:


AutoLisp Intermediate Tutorial

AutoLisp Tutorial Home