Insert Blocks from a Database

31 Oct, 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.
drawing before the program executes or access it from the Insblk dialog box during execution. Figure 1 shows the dialog box with a choice for Symbols Location, where you can select the current drawing or an external drawing. The dialog box also shows the Import file format with field type choices of Delimited or Fixed Width. If you select Delimited, the delimiter can be Comma or Semicolon. When you select Fixed Width, you can enter a field width or use the default 10.

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.

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 Code
Download 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 or visit his web site.
The program begins as usual with an error handler and system variable management functions. The (insblk) function initializes the dialog box and gets any entered data by calls to (init), (set-fieldtype), and (get-insblk-data), respectively. The function (do-insert) uses the information gathered from the dialog-box inputs to load the external drawing that contains all of the blocks. If this symbols drawing has already been loaded, this step is bypassed because a search of the drawing block table tests for the existence of a block with the external drawing name. The Visual LISP function (VL-FILENAME-BASE) strips any prefix or file extension from the external drawing name for the purpose of this block table search.

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.

About the Author: Tony Hotchkiss

More News and Resources from Cadalyst Partners

For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX.  Visit the Equipped Mold Designer here!

For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP.  Visit the Equipped Architect here!