Insert Blocks from a Database1 Nov, 2003 By: Tony Hotchkiss
AutoLISP Solutions: AutoLISP routine draws on database listing to place blocks.
Sean Serin e-mailed from Switzerland to request a routine that inserts premade blocks into a drawing sheet using information that comes from a Microsoft Access database. The solution is INSBLK.LSP and its dialog-box counterpart, INSBLK.DCL. The program inserts blocks from any comma- or semicolon-delimited file, or from a fixed-width-field format file. You can export all of these file types from Microsoft Access. The fields are assumed to be in the order of the local block name, then the x, y, and z coordinates, and then any other information such as a description and a unique ID number. The routine uses only the first four fields from the database, although the delimiter, if any, should follow the fourth field. If a fifth field isn't included, a fixed-field-width format is suitable.
The blocks are assumed to exist in a single drawing but not displayed in the drawing. You can insert the drawing into the current
Figure 1. Symbols can reside in the current drawing or an external one.
If the symbols reside in an external file, a Symbols Drawing File dialog box displays (figure 2). You can browse to locate the AutoCAD drawing that contains the blocks. If the symbols are already in the current drawing, the Symbols Drawing File dialog does not display. In either case, the Import File dialog box appears (figure 3), and you can specify a delimited or fixed-width database file.
Figure 2. Symbols Drawing File dialog box lets you navigate to the drawing that holds your blocks.
Figure 3. Import File dialog box is where you select the database file of blocks and locations.
Selecting the OK button automatically inserts the blocks named in the database at the positions given by the x, y, z coordinates. Figure 4 is an example of some blocks (symbols) inserted by INSBLK.LSP.
Figure 4. Selection of blocks inserted by INSBLK.LSP.
For this example, I used the Microsoft Access database shown in figure 5, from which I exported the comma-delimited file shown in the listing of figure 6.
Figure 5. This Microsoft Access database was used to create the example shown in figure 4.
Figure 6. Comma-delimited file exported from Access database.
HOW TO USE INSBLK.LSP
Download the LSP and 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 INSBLK.LSP from the directory where you installed it, click the Load button, and then click Close. AutoCAD prompts you to enter IB to start. You now see the dialog box in figure 1, where you select the symbol locations and the import file format. Click on the OK button to automatically insert the blocks.
I wrote and tested the code using Visual LISP in AutoCAD 2004. It should work in any version back to Release 14 because the only Visual LISP function in the code doesn't need any later version of AutoCAD.
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.
The (do-insert) function also opens the database import file and checks whether the field type is fixed-width or delimited. If the field type is delimited, the delimiter (comma or semicolon) is assigned to a variable to be used in a call to the (do-delim-file) function. Otherwise, the routine calls the (do-fixed-file) function to deal with the fixed-field-width files. (do-fixed-file) uses a (while) loop to read each line of the import file. It simply assigns each field in turn to the block name and the x, y, and z coordinates by making use of the standard (substr) function to step through the string that has been read one field at a time. For each of the lines read, the (command) function calls the AutoCAD Insert command to insert the block name at the correct location.
The (do-delim-file) function does the same job of using a (while) loop to read each line of the import file, except that a call to a (parse) function separates the fields with the appropriate delimiter. The (parse) function checks each character of a string. If it finds a delimiter (comma or semicolon, depending on the file type), it then tests the location of the field to return the block name or x,y,z coordinate. The number of the field controls a (repeat) loop, inside which a (while) loop tests for the delimiter.
As usual, good programming, and keep those requests coming in.
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!