AutoCAD

Add Blocks and Update a List of Materials (AutoLISP Solutions AutoCAD Tutorial)

1 Sep, 2007 By: Tony Hotchkiss

Program automatically populates blocks with part numbers, quantity, vendor, and description.


Ryan Casper of Indiana e-mailed a request to add blocks to a drawing. After the blocks are added, an equipment list located at the top right of the drawing will be updated automatically with vendor part number, quantity, vendor, and a description. The vendor part number, vendor, and description are attached to the blocks as attributes that are constant and invisible. The equipment list already has item numbers, and some of the item numbers have been filled in with existing equipment. The program will automatically calculate the quantity of each item. All equipment symbols (blocks) are to be stored in an external drawing.

The AutoLISP Solution is ADD-BLOCKS.LSP and ADD-BLOCKS.DCL. The routine does all of the above, and it is flexible enough to handle blocks being added in any order and for the drawing to be revisited later if you want to add new blocks or modify existing blocks.

Get the Code!
Download the ADD-BLOCKS.LSP and ADD-BLOCKS.DCL files from Cadalyst's CAD Tips site. Save the file in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then use the browser to select the file. Casper wants to have the program available on startup, so you also can add the LSP file to the Startup Suite so it will be loaded automatically when any drawing is opened.

How to Use the ADD-BLOCKS.LSP Code
To start the ADD-BLOCKS.LSP program, enter ADB and you will see the Symbols Drawing File dialog box.

 

figure
The Symbols Drawing File dialog box.

Browse to find the drawing that contains the blocks to be inserted and then you will see the Block List dialog box.

 

figure
The Block List dialog box.

This list contains all blocks from the symbols file and any blocks that were already in the current drawing. Select any block, and you will be prompted for an insertion position. After inserting any block, you will be prompted to insert the marker block that contains the next available item (mark) number. A partial equipment list displays some available (unassigned) mark numbers.

 

figure
The equipment list with available rows.

AutoCAD will display some typical blocks with their marker (item) numbers in octagonal frames. Note that the numbers correspond to the available rows of the previous equipment list.

 

figure
Typical blocks added to the drawing with their marker numbers.

When you insert a block, you will be prompted to insert the same block again. If you answer No, you will be asked if you want to insert a new block. For either prompt, you may press Enter or the spacebar to accept the default Yes response, or you may enter N to decline the invitation to insert further blocks. After all required blocks are inserted, the equipment list is updated with vendor part number, quantity, vendor, and a description for each of the inserted blocks. For test purposes, I created an updated equipment list with blocks. The updated information text in lowercase doesn't represent actual parts as shown.

 

figure
An updated equipment list with rows filled in from the inserted blocks.

Programming Notes
After my usual error-handler and system-variable management functions, the program starts with the Add-Block function shown here:

 

(defun add-block ()
(vl-load-com)
(setq *thisdrawing* (vla-get-activedocument
(vlax-get-acad-object)
) ;_ end of vla-get-activedocument
*modelspace* (vla-get-ModelSpace *thisdrawing*)
) ;_ end of setq
(vl-cmdf "UCS" "World")
(vl-cmdf "ZOOM" "Extents")
; available equ't list marks and locations
(setq numlist (make-num-list))
(setq *mark-bname-list* nil)
(insert-block numlist) ; multi-inserts blocks and markers
(get-attribs) ; obtain attribute list
(add-data) ; fill in electrical equipment list
(vl-cmdf "UCS" "Previous")
) ;_ end-of add-block

Here I chose to set the UCS (user coordinate system) to the World view to get coordinates without resorting to the coordinate translation functions of AutoLISP. Note that I set the UCS back to its previous setting at the end of the function. I also set the zoom to Extents, because at various times during execution I use a crossing method of selecting entities with two points. If the two points are not on the screen, the selection set returns nil, so I used this convenience several times during the program.

Add-Block calls Make-Num-List to make a list of all available equipment list rows. It is followed by a call to Insert-Block that uses the list of numbers obtained from Make-Num-List. Insert-Block starts by using the standard GetFiled function to get the symbol drawing and then VLA-InsertBlock is used to insert the symbols drawing into the model space of the current drawing. A while loop then inserts new blocks into the drawing. The new blocks come from a dialog box, which is managed by a call to Get-BName from within the while loop:

 

(defun get-bname (markertxt)
(setq bnameindex nil)
(setq doit 4)
(setq ad_id (load_dialog "add-blocks.dcl"))
(while (>= doit 2)
(if (not (new_dialog "getbname" ad_id))
(exit)
) ;_ end of if
(init-bname)
(action_tile "bname" "(setq bnameindex (atoi $value))")
(action_tile
"accept"
; this creates global *bname*
"(get-bname-data) (done_dialog 1)"
) ;_ end of action_tile
(setq doit (start_dialog))
(cond
((= doit 1)
(do-insert markertxt)
)
) ;_ end of cond
) ;_ end of while
(unload_dialog ad_id)
) ;_ end of get-bname

Note that Get-BName calls Do-Insert, which is the main function for actually inserting the blocks and markers into the drawing.

After all the blocks are inserted, the Add-Block calls Get-Attribs, which makes a list of the entire block attribute values and their positions in the equipment list. Finally, Add-Data is called to add the information into the equipment list in the correct places.

As always, I look forward to receiving your comments and requests for "AutoLISP Solutions." Contact me using the links below.


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