cadalyst
AutoCAD

Autolisp Solutions: Import Blocks and Attributes

6 Nov, 2006 By: Tony Hotchkiss

An update to this column's popular block insertion command.


James Anderson of Atlanta, Georgia, requested an update of my block insertion program from 2005 (INS-BLK). Anderson wants to insert multiple copies of the same block with different attribute values for each insertion by using data from a delimited file from a spreadsheet or database file. He also wants to control the scale of the blocks and would like the option of manually picking the insertion point. In addition, the blocks should be inserted in either model space or in a paper space layout. This latter request makes a difference to the Visual LISP method INSERTBLOCK because the model or paper space designation is a required part of that method. Finally, Anderson requested that the program work with blocks with any number of attributes.

INSBLK3.LSP and INSBLK3.DCL have all of the above features, in addition to a dialog box that controls:

  1. The block or symbols location (current or external drawing)
  2. Import file delimiter (comma or semicolon)
  3. Insertion point method (automatic or manual)
  4. Model or paper space
  5. Block scale

These choices are shown on the Import Blocks and Attributes dialog box below.

 

figure
The Import Blocks and Attributes dialog box.

Anderson suggested the default values shown in the dialog box, although you can easily change them (as you can see in the Programming Notes section).

Get the Code
Download the INSBLK3.LSP and INSBLK3.DCL files from Cadalyst's CAD Tips site and save them in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then select the INSBLK3.LSP file from where you stored it.

How to Use INSBLK3.LSP
To start the program, enter IB3 at the Command prompt and the Import Blocks and Attributes dialog box displays. Check the selections for Symbols location, file delimiter, insertion point, model or paper space and the block scale. If the symbol(s) are in an external file, you'll see the Symbols drawing file dialog box.

 

figure
If the symbols are in an external file, the Symbols drawing file dialog box appears.

Locate the drawing that contains the blocks (symbols) and click Open to insert it into the current drawing. If your blocks are in the current drawing, you won't see the Symbols drawing file dialog box. In either case, the Import file dialog box appears.

 

figure
The Import file dialog box.

The import file may be either comma delimited or semicolon delimited, and is typically created by saving an Excel spreadsheet or an Access database file in the delimited form. An example of a spreadsheet file that Anderson supplied is shown below.

 

figure
Excel spreadsheet data showing the attribute fields 5 through 11.

The restrictions on the delimited data file are that the first four fields are reserved for the block name and the x, y and z components of the insertion point. The number of fields used for attributes must be at least as many as the actual number of attributes for each block. The number of attributes used by the program is extracted directly from the inserted block, so you may use several blocks, each with a different number of attributes, or you can use a series of insertions of the same block, as was done in the examples shown here.

The final step is to indicate the location of each block insertion, or if you automatically place the blocks by using the point locations of fields 2 through 4 of the data file, the blocks and attributes will be placed in the drawing in either model space or layout paper space. In this example, the result is shown in an AutoCAD 2007 drawing layout.

 

figure
Attributes inserted in paper space.

Programming Notes
The program starts with an error handler and my system variable management functions SETV, RSETV, SETTING and RESETTING. These functions are followed by the dialog box handler INSBLK3, shown here:

 

(defun insblk3 (/ dcl_id insdata)
(vl-load-com)
(setq *thisdrawing* (vla-get-activedocument
(vlax-get-acad-object)
) ;_ end of vla-get-activedocument
*modelspace* (vla-get-ModelSpace *thisdrawing*)
*paperspace* (vla-get-PaperSpace *thisdrawing*)
) ;_ end of setq
(setq dcl_id (load_dialog "insblk3.dcl"))
(if (not (new_dialog "insblk3" dcl_id))
(exit)
) ;_ end of if
(init)
(action_tile
"accept"
"(setq insdata (get-insblk-data))(done_dialog 1)"
) ;_ end of action_tile
(setq doit (start_dialog))
(unload_dialog dcl_id)
(if (> doit 0)
(do-insert insdata)
) ;_ end of if
) ;_ end of insblk3

The first few lines set up some parameters for using Visual LISP functions and accessing model or paper space, before the dialog box is loaded, initialized and set to get the data that is passed to the DO-INSERT function. Initialization is performed by the INIT function, and if you want to set your own initial conditions for the dialog box, you may edit the INIT function accordingly.

How to Set the Dialog Box Default Values

The INIT function is as follows:

 

(defun init ()
(set_tile "Delimiter" "Comma")
(set_tile "Location" "curdwg")
(set_tile "Inspttype" "Manual")
(set_tile "Space" "Paper")
(set_tile "Scale" "0.0003")
) ;_ end of init

The initial values are shown as Comma, curdwg, Manual, Paper and 0.0003 as shown. You may change these values by using a text editor such as NOTEPAD. Do not use a word processor for the changes, because word processors add characters that are not recognized by Visual LISP. The choices you have are as follows:

 

Default valueAlternative value
CommaScolon
curdwgextdwg
ManualAuto
PaperModel
0.0003Any number

With these values, you can set the default delimiter to comma or semicolon, the default symbol location to current drawing or external drawing, the insertion point method to manual or automatic, block placement to paper or model space, and the scale of the blocks to any scale number. You may change any of the values, but remember to keep the double quotes! The only allowed values are those that are shown in the above table, and you must enter the values exactly as shown, with capital and lowercase letters.

Program Major Functions
The DO-INSERT function tests for the location of the block symbols and inserts an external drawing if necessary. It also tests for the delimiter type before calling the DO-DELIM-FILE function that reads the comma or semicolon delimited file line by line. Tests are made for automatic or manual insertion point and for model or paper space. For each line of data, the block and attributes are placed in the current drawing using the following code:

 

 (setq BRefObj (vla-InsertBlock
*space* inspt bname bscale bscale bscale 0)
;_ end of vla-InsertBlock
) ;_ end of setq
(setq attribs (vla-getAttributes BRefObj))
(setq sarr (vlax-variant-value attribs))
(setq num (vlax-safearray-get-u-bound sarr 1))
(repeat (1+ num)
(setq
attrefobj (vlax-safearray-get-element sarr (setq j (1+ j)))
) ;_ end of setq
(setq val (parse str Dcode (setq i (1+ i))))
(vla-put-TextString attrefobj val)
) ;_ end of repeat

Here the block is inserted with the VLA-INSERTBLOCK method; note how the number of attributes is determined before the repeat loop is executed so that the correct number of attributes is used to match each inserted block. A PARSE function is called to take care of the number of characters in each string separated by the delimiter before inserting the attribute using the VLA-PUT-TEXTSTRING function.

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


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!