# When a Line Is Not Just a Line

31 Aug, 2003 By: Bill Kramer

What makes a CAD drawing special? If you create engineering documents for a living, then you already know the answer to this question--CAD drawings have smarts. Suppose you are drawing two lines that need to be parallel. To do this in a CAD system is easy. Draw the first line and then use an offset to create the parallel line. This is not as easy in a pixel-based drawing system, such as the Paint program found in Windows. The use of vectors allows the CAD system to reuse the data from previous graphics. This permits for drafting productivity aids such as locating a point at the end or middle of a line or drawing a line perpendicular to another graphic. For many, this alone represents a powerful tool; however, a good CAD system, such as AutoCAD, can go far beyond this in terms of drawing smarts.

Graphics in smart CAD environments are stored as vectors, and the basic point and parameter information is also stored. By themselves vectors provide a lot of information. If you know the start and end points of a line, then you can calculate the distance between the ends, the angle from one point to another, and so forth. Over the past few years, the trend in CAD has been towards the use of objects. Graphic entities are objects that carry the basic parameters (properties) and a host of useful extensions based on those properties, commonly known as methods. Methods are functions that do something with the object. An example is the intersectWith method that works with most of the common AutoCAD graphic objects. Given two suitable objects, the intersectWith method returns the point(s) where the objects intersect. By suitable objects I mean standard AutoCAD geometry objects such as lines, arcs, circles, ellipses, and so on.

Thus, having a vector-based graphic system does allow for the creation of precise drawings. And, by using an object approach, new graphic elements can be introduced to give the drawing system more flexibility. An example in AutoCAD is the ellipse. In the "ancient days" we had to draw a circle (or arc), convert it to a block, then insert it with a variable x,y scale to produce an ellipse. Now we have an ellipse object. The ellipse object makes manipulating an elliptical graphic much easier and more powerful in that AutoCAD knows it is an ellipse (as do our applications) and not just a circle in a block reference.

But we can go beyond that in a power CAD system such as AutoCAD. When writing an application, we can select to attach additional data to an object that does not appear in the drawing but is useful in the context of our application. For example, when a line is not just a line. If a line represents the edge of something, such as a wall, then it may have additional properties of interest to an application. The line may contain data related to mass, moment of inertia, material options, and a lot more. The amount of information that a line may carry is only limited by your imagination.

Let's take a look at the various ways in which data can be attached to drawings inside AutoCAD. It is important to realize that these options were not introduced all at once, but over time as AutoCAD evolved. As a result, some of the options are more powerful than others. But that does not imply that the less powerful are no longer useful. Some are simpler to use and may be suitable for your application, while others are more complex and provide you with very advanced capabilities.

Drawing-Based Data
We will start by looking at the type of data that can be attached to a drawing beyond what is typically found in AutoCAD. Drawing-based data may be considered something that is unique for each drawing or that is used many times in a single drawing. The most frequently used method of attaching data to a drawing is to use attributes as part of a block.

The most frequently found application of attributes is the title block of the drawing. Virtually every engineering document contains a title block. One way to save time when creating documents of this nature is to have a library of title blocks ready to go. Since each title block will have variable text information (the majority of which is visible and plotted as part of the document), the attribute system works very well. In this case the attribute data can be considered unique to each drawing as there really should only be one title block per drawing in most cases.

Data that appears in a drawing multiple times is often demonstrated in a bill of materials. The bill of materials can often be found as a table with variable text in a drawing. Each line in the table is a block with attributes. Not only does the attribute input editor (a dialog box) make it easier to manipulate the data, the extract system for attributes is a perfect way to get that data out of the drawing and into a text file for further processing by other systems.

Attributes can be visible or invisible; however, they must always be associated with a block insert. There is another way to define invisible drawing data that is not attached to anything, and that is to use what is known as a dictionary. A dictionary object is a non-graphic entity object in AutoCAD containing references to other objects. These other objects can be graphical, or they can be non-graphical objects known as XRECORDS. An example of a graphics-based dictionary is "ACAD_GROUP". Here you will find all the groups defined in a drawing.

Accessing the dictionary objects in Visual LISP is very easy. Use NamedObjDict to obtain a reference to the dictionary object, and then use the "DictXXX" subrs found in Visual LISP. You can search for a specific dictionary using DICTSEARCH or simply walk through each of the dictionaries using DICTNEXT. Each of these subrs returns an entity list containing the dictionary references. That is, you will get a list containing the entity names of the entity objects related through the dictionary system. Now, it is not a simple list of names but is instead an association list where each of the names has a group code of 330 (or 340, if the entity has been deleted but not purged from the drawing).

VBA programmers have an object route to the dictionaries from the AutoCAD object. There is a collection of dictionary references each pointing to a dictionary object. The dictionary object behaves like a collection for the objects in the dictionary but has additional properties and methods worth exploring. The objects in a dictionary can be graphical in nature or non-graphical XRECORD objects. XRECORD objects are great for storing information about the drawing that is not visible, unless your application makes it so. The only restriction for using an XRECORD is it must be the child of another object, typically a dictionary. Otherwise, you will not have an easy way to reference it again. This is why XRECORD objects are typically found in dictionaries. So they can be located again in the future. In general, I find manipulating dictionary objects to be easier using the object approach of VBA, since many of the methods provided are quite useful.

Entity-Based Data
Individual entity objects can have data attached to them in two ways. The first way is to include a reference to an extension dictionary object. An extension dictionary is simply a link from the graphic object to a dictionary object that can house as much data as your application needs. Multiple objects can all link to the same dictionary, forming a relationship between different objects in a drawing. This sort of relationship can be multidirectional in that a change to an object in the grouping may end up reflecting on another member in the group. Or, it could be informational, such as material properties. If you have the material specifics known in an application, you can associate those specifics with multiple objects in the drawing.

The second and older approach is to use extended data. Extended data has a limit to the amount of data you can attach to an object (albeit a large enough limit for most applications to do just fine). Extended data attaches directly to the object as additional group codes in the entity list. You must use the application name when retrieving the entity data list in order to view these additional group codes in Visual LISP. In VBA the entity objects contain a method known as getXdata that will return an array of group codes and variant data that can be used to retrieve the data attached to an object.

When Changes Happen
In order to have graphic objects react to changes to other graphic objects in a drawing, you must consider when you desire this to take place. You have two choices. The first is to batch up the changes and perform them all at once in the stream of your application. This assumes that the changes taking place are under the control of your program and not just AutoCAD editor commands invoked by the user. When the potential for changes by the user exists, you must check for them at every opportunity in order to keep the application working properly.

The second way to change related objects is to work with reactor objects, and that can be tricky when you are first learning the ways of reactors. But once you have a few simple concepts under control, the use of reactors is a very elegant solution to having related objects stay related when one of them is changed. The first thing to know about reactors is that you must control the flow through the reactor. This is best illustrated with a simple example.

Suppose you have two related graphic objects: a circle and a line. If the radius of the circle is changed, you want the end point of the line to move. But, if the center of the circle changes, you want the entire line to move. When the line is moved, the circle should move. And, if the end point of the line that is on the circle changes, the radius of the circle should change to match. What will happen if the line or circle are changed inside the program code? As the first object is changed, the reactor will run. If that reactor changes the related object, then the reactor for the related object starts. Now, should that reactor change the original object, the reactor for it will run again and the process will repeat over and over. Obviously this is not an acceptable situation. The way to keep this from happening is to use flags in your programs indicating that a particular process is considered busy. Set a flag in the first reactor indicating that it is active. When the related object is changed and its reactor starts, that flag can be tested to indicate no change is desired and to simply return immediately.

Summary
CAD Drawings can be a lot smarter than scanned raster images, and some CAD drawings are a lot smarter than others. Just how smart your drawings can be is up to you and your imagination. The tools exist in AutoCAD to make drawings that can carry a tremendous amount of information. There are XRECORD objects tied into DICTIONARY objects as well as the extended data features of the entity objects themselves. To learn to use them to their full extent you need to keep on programmin'.

# 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!