Purge Block Attributes1 Feb, 2003 By: Tony Hotchkiss
AutoLISP Solutions: Learn how to use AutoLISP to make changes in multiple documents.
Thom Beauchamp e-mailed a request to take a set of 10 or so drawings, each with 2,200 or more attributed blocks, and purge all of the attributes, leaving the graphics for the blocks intact. The AutoLISP Solution is PURGE-ATTRIBS.LSP and its dialog-box control counterpart PURGE-ATTRIBS.DCL. The purge routine automatically removes every attribute from every block reference. The only user interaction is to select the drawings to be purged from a dialog box list.
HOW TO USE PURGE-ATTRIBS.LSP
Download the LSP and the DCL files from Cadalyst's CAD Tips site and save them in the AutoCAD support directory. From the AutoCAD tools menu, choose Load Applications, or enter Appload at the AutoCAD command prompt. In the Load/Unload Applications dialog box, select the PURGE-ATTRIBS.LSP file from the support directory where you installed it, then click Close. After you load the program, AutoCAD prompts you to enter PA to start.
When the program starts, you see the dialog box shown in figure 1.
Figure 1. purge-attribs.lsp presents this dialog box and a warning to set SDI to one and LISPINIT to zero.
A note on the dialog box reads:
Note: Single Document Interface only.
You must set system variables SDI = 1,
and LISPINIT = 0 If these are not set,
press the Cancel button and set them.
All of the AutoLISP Solutions programs that use multiple drawings need to operate on one drawing at a time, so it is essential to set SDI to 1 to turn off the multiple-drawing interface. This in turn requires that the AutoLISP functions and variables remain active between drawings, and that requires that you set LISPINIT to 0 (zero).
The dialog box has a pop-up list of drives with the label Look in Drive. You can choose any drive that is active on your computer or to which your computer is connected. The default drive is C, and a list of folders in that drive appears in the Drawing Folders list box. You may select a folder from the list by using the scroll bar if necessary to navigate to the folder that contains your drawings. Any AutoCAD drawing files in a selected folder are displayed in the Drawings list box on the right side. You may select one or more of the drawings, as shown in figure 2.
Figure 2. purge-attribs.lsp runs on all the drawings you select using the standard Windows pick functions.
To select a single drawing, just click it. To select a range of drawings, click the first drawing in the range and then hold the
The program uses some Visual LISP functions and therefore works only in AutoCAD 2000 or later versions. The program starts with my error handler and system variable management functions. The dialog box driver function is (purge-attribs), and this calls the (init-dwg) function to initialize the default values displayed in the dialog box.
An interesting function, (make-drivelist) finds the active disk drives on the computer, starting with the C drive, and returns a list of drives. I've used (make-drivelist) in the past because it has the Visual LISP function (vl-file-size), which returns an integer showing the size of its only argument filename. If filename is a directory, (vl-file-size) returns 0, so you can determine if a drive exists, as shown in the following code fragment:
(setq drivelist nil
code (ascii "C")
) ;_ setq
(setq str (strcat (chr code) ":/"))
(if (vl-file-size str)
(setq drivelist (append drivelist (list str)))
) ;_ if
(setq code (1+ code))
) ;_ repeat
The last function called by purge-attribs is (do-mdwgs), shorthand for do multiple drawings. Its single argument is a list of the full pathnames of the user-selected drawings. (do-mdwgs) contains a repeat loop that opens each drawing in turn from the drawing list and calls (do-purge) in each drawing. (do-purge) is the function that removes all of the attributes from each block in the drawing. If you simply want to remove all attributes from the current drawing, you can run this function by typing (do-purge) at the Command line. (do-purge) collects a selection set of all insert entities that have attributes attached to them. If there is such a selection set, then for each entity in the set, the function changes the flag that signals whether attributes are present to "No attributes" and creates a new entity to replace the attributed entity, which is deleted. The code for (do-purge) is:
(defun do-purge (/ en elist newitem olditem newlist)
(setq ss (ssget "X" '((0 . "INSERT") (66 . 1))))
(setq i (- 1))
(repeat (sslength ss)
(setq en (ssname ss (setq i (1+ i)))
elist (entget en)
newitem (cons 66 0)
olditem (assoc 66 elist)
newlist (subst newitem olditem elist)
) ;_ end of setq
) ;_ end of repeat
) ;_ end of progn
) ;_ end of if
) ;_ end of do-purge
The total length of PURGE-ATTRIBS.LSP and PURGE-ATTRIBS.DCL combined is about ten pages. One of those pages is used for the error handler and system variable functions. As you can see, the function that removes all attributes from a drawing, (do-purge), is less than half a page. The rest of the program is concerned entirely with providing a dialog box interface that supports multiple drawings. As usual, good programming, and keep those requests coming in.
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!