Parametrics-Driven Drawings

30 Jun, 2003 By: Bill Kramer

The other day a question was put to me: can one drive an AutoCAD drawing from an Excel spreadsheet? The caller wondered if this was feasible. The answer was a resounding "Yes!" The concept has been behind AutoCAD customization since CADENCE magazine first hit the streets back in 1986. Using a parameter-based drawing system is still one of the most powerful tools to rapidly design and draft standard things. Although the concept remains the same, the options have greatly improved.

Parameter-Based Drawing Systems
What is a parameter-based drawing system exactly? Sometimes called "parametric systems," these programs draw parts (or all) of a drawing based on a given set of values, called "parameters." They are applied against algorithms that create points, scalars, and angles for geometry definitions. Think of a simple graphic such as a rectangle. AutoCAD defines a rectangle as being four points stored in a lightweight polyline. But you may think of the rectangle as having a center point, a width, and a height. The basic operation of a parameter-based solution, in this case, would be to take the three values you know (center, width, height) and convert them into the four points needed to draw a lightweight polyline in AutoCAD.

Most parametric systems are a bit more complex in nature. Still, they all contain the basic elements of a program: an input step, a processing step, and an output step. The processing step involves calculations, which can be done with any of the programming languages available for AutoCAD (such as Visual LISP and VBA). Any modern programming language that can talk to an ActiveX server, such as AutoCAD, can also be used. The ActiveX server in AutoCAD allows a program to define new entity objects and manipulate existing ones inside a drawing. This is one of the output options for a parametric system. Some of the other output options are to create a DXF file, send commands to AutoCAD, and to directly interface with the software using tools such as Visual LISP, VBA, Object ARX, and DBX.

So how do you obtain the input data? Originally the only way was to ask the AutoCAD user to supply a parameter or to read the data in the file. Today you can obtain it from another server program (such as Access or Excel), get data from other AutoCAD graphic and non-graphic objects, or even read it in from a source on another computer using Internet protocols (see Table 1 for a list of potential input options and their effectiveness).

Table 1. Parametric System Input Options
Type of InputLanguage
 Visual LISPVBAObjectARXActiveX Client
Command InputBest ChoiceGoodGoodMaybe
Graphic SelectionBest ChoiceGoodGoodMaybe
Dialog BoxOkayBest ChoiceGoodMaybe
ActiveX ServerOkayBest ChoiceGoodShould
Excel SpreadsheetOkayBest ChoiceGoodShould
Access DatabaseOkayBest ChoiceGoodShould
Text Data FileGoodGoodGoodMaybe
Binary Data FileNoLimited OptionsYesMaybe
Another ComputerNot easyNot easyYes, not easyMaybe

From Table 1 we can see that virtually any of the standard programming options can be used to accomplish the input processing. Some are better than others at certain tasks. Let me explain some of the entries. When I mark "Best Choice," I mean all the programming choices will work; however, I would select the one marked as the best choice. For example, Visual LISP is the best choice for input from the command line or graphic selection, because the coding required to accomplish these in Visual LISP is significantly less than what the other methods require. The reverse is true when dialog boxes are involved; Visual LISP dialog boxes take a lot longer to layout and design than the VBA dialog boxes.

Listing 1. Code List

(defun C:CLIN (/ EL P1 AA LL)
 (setq EL (entsel "\nPick a line: "))
 (if EL (progn ;;Operator make a selection?
   (setq EL (entget (car EL))) ;;entity list
   (if (and EL ;;valid entity and a line?
    (= (cdr (assoc 0 EL)) "LINE"))
 ;;Get parameters from LINE object
 (setqP1 (cdr (assoc 10 EL))
LL (distance P1
(cdr (assoc 11 EL)))
AA (angle P1
  (cdr (assoc 11 EL)))
 (Circles_Along_Line P1 LL AA)
(defun Circles_Along_Line (P1 LL AA / DD CRObj)
 DD 0.0 ;;initialize running dist
  CRObj ;;create circle make list
   '((0 . "CIRCLE")
    (8 . "0")
    (10 0.0 0.0 0.0)
    (40 . 0.5)
    (210 0.0 0.0 1.0)
 ;;Loop while running dist < line length
 (while ( < DD LL)
 (setq CRObj ;;update circle point
 (subst (cons 10 P1)
 (assoc 10 CRObj)
 (entmake CRObj) ;;add circle
 ;;update location and distance
 (setq P1 (polar P1 AA 3.0)
 DD (+ DD 3.0)

The Table advises against using Visual LISP to handle binary data files; Visual LISP does not process binary data-only ASCII text files. It can process binary data one byte at a time. This is a very cumbersome process; I recommend you use another tool instead. When I encountered these sorts of problems in the past, I resorted to using Object ARX (and the ADS version that predated ARX) to create a new function that could be called from LISP. This does require the Object ARX SDK from Autodesk and the proper compiler to create the ARX file, so I don't recommend this unless you are a highly experienced AutoCAD customization programmer.

The next step is to process the input into a format that can be used in the creation or modification of the graphics. To create a new graphic object, you must have what's required to define it. For the most part, the data required will be the basic geometric properties. For example, a circle needs the center point and the radius, and a line needs the two ending points.

Let's build a simple parameter-based system and investigate it. Suppose we need to draw holes at regularly spaced intervals along a line, as shown in Figure 1. Let's also assume that each hole is one drawing unit in diameter and spaced no less than three drawing units apart. These rules would be known to the programmer and can thus be incorporated directly into the programming logic.

Figure 1. These circles were created using parameters (see LISP listing).

The input data required are those values not known at the time of development. We know the spacing requirement and the size requirement. What we do not know is the data for the line: where it starts and stops, how long it is, and what the inclination is. The line data must come from the user or through some other input scheme and then get converted into data useful to the application.

So where does the line data come from? That is up to the application, but we can suppose three possible sources. First is from the user selection of a line graphic in the drawing. The user draws and line, then runs the parameter-driven routine, which gets the data needed directly from the AutoCAD LINE object. The second alternative is for the data points to come from another input source, such as a data file or a spreadsheet. And the last source might be a calculation method that computes the location of a line that may or may not be drawn.

Given the line data points, we can compute the length and inclination angle of the line. With those numbers, a loop can be started to draw circles at varying center points along the line, each spaced by the distance involved. Another output method might be to simply use the AutoCAD edit command MEASURE after defining a block for the circle to be inserted.

For this application, which language is the simplest to use? A lot depends on the input data source. If the primary input is to locate a LINE object in the drawing, then Visual LISP is the best solution. But if the data is coming from a design program already written in Visual BASIC, then that language will be the better choice.

When coding a parameter-based solution, there are some concepts you should apply. The first is modular programming. Structure your program so that a change in something, such as the input system, does not hinder the computation and output. As an example, a function set has been provided in the Code List in Listing 1. This set contains two functions. C:CLIN is the command line function and where the set starts. The user is asked to select a line object from which the parameters for the base point (P1), angle (AA), and length of the line (LL) are determined. These parameters are then passed to the function (CIRCLES_ALONG _LINE), which draws the circles along the linear path. The functions are made separate so it is easy to change the source of the data and just call the circles along a line routine again. This represents a modular solution to the problem. The strategy applies to all programming languages and not just the LISP example shown (LISP was chosen because it is the most succinct language that demonstrates this process).

Parametrics is a powerful concept that can be used to gain a lot of productivity in AutoCAD. The only ingredients you need are the creativity to come up with a process and the time needed to program it. Until next time, keep on programming!

About the Author: Bill Kramer

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!