AutoCAD

Purge Block Attributes

1 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.

 

Overall Performance
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.

 

Overall Performance
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 key down while you select the last drawing in the range, just as you would in any Windows application. To select a random group of drawings, hold the key down while you make your selection. You may also use any combination of selection methods to combine a range with random selections.

PROGRAMMING NOTES
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
(repeat 24
(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))))
(if ss
(progn
(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
(entdel en)
(entmake newlist)
) ;_ end of repeat
) ;_ end of progn
) ;_ end of if
) ;_ end of do-purge

LAST WORDS
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.


About the Author: Tony Hotchkiss


AutoCAD Tips!

Lynn Allen

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter



Poll
Which file format do you use most often for CAD drawing/model exchange?
Native format
PDF
3D PDF
DWF
STEP or IGES
JT
IFC
Other
Submit Vote