Technology Industry
Industry: Email Alert RSS FeedInsert blocks from a database: AutoLISP routine draws on database listing to place blocks - Autolisp solutions: reader-requested AutoLISP routines
CADalyst, Nov, 2003 by Tony Hotchkiss
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.
Most RecentTechnology Articles
The blocks are assumed to exist in a single drawing but not displayed in the drawing. You can insert the drawing into the current 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.
[FIGURE 1 OMITTED]
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.
[FIGURES 2-3 OMITTED]
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. 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.
[FIGURES 4-6 OMITTED]
HOW TO USE INSBLK.LSP
Download the LSP and DCL files from www.cadalyst.com (see box above) 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.
PROGRAMMING NOTES
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.
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.
CXO UnpluggedSmart Business interviews on BNET
Brought to you by CBS MoneyWatch.com
- Best- and Worst-Paid College Degrees
- 6 Things You Should Never Do on Twitter or Facebook
- How Much Sleep Do You Really Need?
- 6 Big Myths about Gas Mileage
- 5 Rules for Immediate Annuities
- Death in the Family: 12 Things to Do Now
- Dumbest Things You Do With Your Money
- 6 Online Networking Mistakes to Avoid
- 401(k) Mistakes to Avoid
- 5 Economic Scenarios to Keep You Up at Night
- The Real ‘Best Places to Retire’
- Best Credit Cards for You
- 12 Tough Questions to Ask Your Parents
- The Real ‘Best Colleges’
- Home Buyer Tax Credit: How to Cash In
- Why You Shouldn't Bash Cash
- 8 Phony 'Bargains' and Better Alternatives
- Danger: 3 Debit Card Scams to Avoid
- 6 Myths About Gas Mileage
- 29 Fees We Hate Most
- Quick and Easy Ways to Boost Returns
- Best Stocks to Buy Now
- Lower Your Taxes: 10 Moves to Make Now
- New Jobs: 8 Lessons from Real-Life Career Switchers
- The New Job Market: Who Wins and Who Loses?
- Health Care Reform's Public Option: Everything You Need to Know
- Volunteer Work When Unemployed: Should You Work for Free?
- Whose Recovery Is This?
- Long-Term-Care Insurance: 4 Biggest Risks to Avoid
Content provided in partnership with
Most Recent Technology Articles
Most Recent Technology Publications
Most Popular Technology Articles
- BizRate to monitor in-store customer satisfaction for Office Depot stores - Market Intelligence
- Speed control of separately excited DC motor
- Failed businesses in Japan: a study of how different companies have failed, and tips on how to succeed, in the Japanese market
- Effects of creative, educational drama activities on developing oral skills in primary school children
- Political stability and economic growth in Asia



