Remove Selected Attributes1 Jul, 2003 By: Tony Hotchkiss
AutoLISP Solutions: Pick the attributes you want to purge from multiple drawings.
Gail Rolland asked for a modification of a previous program,
PURGE-ATTRIBS.LSP (February 2003), so that it removes certain selected attributes, but not all of them. Regular readers may recall that in the earlier program the function to do the purging used just a few lines of code to eliminate the entire set of attributes. Selecting which attributes to remove is considerably more complicated, but with the routines to select multiple drawings already in place, I was able to add the necessary functions to complete the much more useful
The solution's two files,
PURGE-ATTRIBS-2.DCL, let you select not only a set of drawings, but also any common block reference, along with any number of attribute tags attached to the block reference. The program runs only in AutoCAD 2000 or later, including AutoCAD 2004.
HOW TO USE PURGE-ATTRIBS-2
Download the LSP and the DCL files 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-2.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 you start the routine, the dialog box in figure 1 appears, with a note warning you to set the system variables SDI and LISPINIT to 1 and 0, respectively.
Figure 1. Purge-Attribs-2 first presents this dialog box where you select the drawings to purge.
This is because all of the AutoLISP Solutions programs operate in a single-document environment.
The dialog box presents a pull-down list of drives, and you can choose any drive to which your computer is connected. The default drive is C. A list of folders in that drive appears in the Drawing Folders list box. Select a folder from the list by using the scroll bar, if necessary, to navigate to the folder that contains your drawings.
All AutoCAD drawing files in a selected folder are displayed on the Drawings list box at the right. To select a single drawing, just click it. To select a range of drawings, click the first drawing in the range, then hold the <Shift> 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
Click the OK button to display the Attributes dialog box (figure 2).
Figure 2. Attributes dialog box displays blocks on the left and attribute tags on the right.
The blocks in the first of the set of selected drawings appear on the left side of the dialog box, and the attribute tags associated with any selected block are shown on the right side. You can select any of the attribute tags to remove them from all of the drawings selected in the first dialog box.
A note on the Attributes dialog box explains that the attributes are not actually purged, but their values are removed so that the
Get the CodeDownload the code for this and all articles from Cadalyst's CAD Tips site. Or get the routines directly from author Tony Hotchkiss at CAD/CAM Technologies, 42 15th St., Buffalo, NY 14213, 716.883.8790, e-mail firstname.lastname@example.org or visit his web site.
Note that the routine looks for the block name you specify in all selected drawings. If the routine doesn't find the block in a drawing, it ignores that drawing, so the block doesn't actually need to be in all of the drawings. If the selected block appears several times in a drawing, the routine removes the selected attribute values from all instances of the block.
The program uses some Visual LISP functions and therefore works only in versions of AutoCAD 2000 or later. The program starts with my error handler and system variable management functions. The dialog box driver function, (purge-attribs-2), calls the (init-dwg) function to initialize the default values displayed in the dialog box.
The last function called by (purge-attribs-2) is (do-mdwgs), short for do multiple drawings. Its single argument is a list of the full pathnames of the user-selected drawings. (do-mdwgs) extracts the first drawing name from the list of drawings and calls the function (do-first-purge), which returns a list that contains the user-selected block reference and the selected attribute tags.
(do-first-purge) loads the dialog box from which you select a block and any required attribute tags. The dialog box is initialized with the (init) function, which gets a list of blocks and attribute tags from the open (first) drawing. The routine does this through calls to the (get-blist) and (get-taglist) functions, respectively. (get-blist) checks that block names are not already members of the block list to prevent duplicate names from appearing in the dialog box.
The (get-taglist) function sorts the attribute tag names before they are listed in the dialog box, so the tags may not appear in the same order as they appear on the drawing.
Successive calls to the (do-purge-2) function remove the attribute values from each drawing in turn. (do-purge-2) obtains a selection set of all Insert entities that have the name of the selected block and that also have attributes. The routine includes multiple instances of the same block in the selection set so that it can take their attribute values into account.
A (repeat) loop takes each entity in turn from the selection set and uses a (while) loop to step through each attribute entity. If the attribute entities match any of the tag names, the standard (subst), (entmod), and (entupd) functions replace the attribute values with a null string.
As usual, contact me with any comments or questions, and keep those requests coming in.
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor 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!