Processing Entities within a Selection Set with autolisp in AutoCAD

Once we have created a selection set, the next step is to access each object in the set and apply the rest of the routine to it. But, we cannot use the (foreach) function we used earlier. Selection sets are a special data type called a pickset and need specific functions to access the data. The function to process the elements of a selection set is (ssname).

Explanation: the (ssname) function

(ssname selection_set position)

The selection_set is a selection set and the position is the location of the object you want to process within the set.

  • The index position numbering begins at 0.

Example

Command: (setq EN1 (ssname SS1 0))
<Entity name: 60000014>

This gets the first object in the selection set.

Command: (setq EN2 (ssname SS1 1))
<Entity name: 60000028>

This gets the second object in the selection set.

  • In most cases you will let the code do the numbering for you. See the practice below.

PRACTICE

In this practice you will use the AutoLISP selection set tools to update the (changeblock) function created earlier so that users may select more than one block at a time. Test the routine in BLOX.DWG. Estimated time for completion: 10 minutes.

Solution

(defun C:CHANGEBLOCK ()
(setq NEWBLOCKNAME (getstring “\nNew block name: “))
(if (/= NEWBLOCKNAME “”)
(progn
                  (setq SS (ssget))
                  (setq POSITION 0)
                  (while (setq ENAME (ssname SS POSITION))
        (setq OLDELIST (entget ENAME)
OLDBLOCKNAMELIST (assoc 2 OLDELIST)
NEWBLOCKNAMELIST (cons 2 NEWBLOCKNAME)
NEWELIST (subst NEWBLOCKNAMELIST
OLDBLOCKNAMELIST
OLDELIST)
) ;_ end of setq
(entmod NEWELIST)
       (setq POSITION (1+ POSITION))
              ) ;_ end of while
              (setq SS nil)
              ) ;_ end of progn
(prompt “\nA block name was not entered.”)
) ;_ end of if
(princ)
) ;_ end of defun

  • A complete solution to this exercise is on your class disk as CHANGEBLOCK-B.LSP
See also  why AutoLISP in AutoCAD?

Modular Code for Selection Sets (Optional)

You may want to create a modular routine that will work whenever you need to process entities within a selection set. Here is an example called SSMOD. It uses another modular routine called SETDXF, which is also included below.

;;;SETDXF – isolates data associated with a certain
;;; DXF code and automatically updates the entity
;;;SETDXF requires three arguments:
;;; CODE- the DXF code number of the type
;;; of information you wish to change
;;; NEWVALUE The new value of the changed
;;; information
;;; ENAME – the ename of the entity to be changed

(defun SETDXF (CODE NEWVALUE ENAME)
      (entmod
              (subst
                     (cons CODE NEWVALUE)
                     (assoc CODE (entget ENAME))
                     (entget ENAME)
               )
      )
)

;;;SSMOD – modifies each entity in a selection set
;;; based on a new value for a given DXF code
;;;SSMOD requires three arguments:
;;;SS – The selection set to modify
;;;CODE – the DXF code number of the type
;;;of information you wish to change
;;;NEWVALUE – The new value of the changed information
;;;Two other variables, POSITION and ENAME, do not
;;; require input from the user.

(defun SSMOD (SS CODE NEWVALUE / POSITION ENAME)
      (setq POSITION 0)
      (while (setq ENAME (ssname SS POSITION))
             (setdxf CODE NEWVALUE ENAME)
             (setq POSITION (1+ POSITION))
       ) ;_ end of while
) ;_ end of defun

  • A complete solution to this exercise is on your class disk as SSMOD-A.LSP
See also  Executing AutoCAD Commands through AutoLISP
Back to top button