Extend AutoCAD entity data
31 Mar, 2001 By: Bill FaneIt was a dark and stormy night. Captain LearnCurve's acute hearing picked up an anguished cry: Oh, woe is me! I have many pages of data that must match up to objects in my drawing! It takes forever to find the data for a given object! Whatever shall I do? Have you ever wanted to add some extra background information into an AutoCAD drawing? There are four basic ways to do this. The quickest and easiest way is to put notes on a separate Post-it layer. When you want to plot the drawing, simply freeze the layer so your notes don't plot. You can also put the notes on the Def-points layer because it never plots and cannot be made to do so. Starting with AutoCAD 2000, you can use the Layer dialog box to specify that a layer does not plot even though it is still visible and not frozen.
The bad news is that others can view and edit your notes. The notes also can't be associated with a specific object in the drawing.
The second way to add information to a drawing neatly overcomes these problems. You can use attributes attached to block definitions to associate your data with each insertion. The attribute definition can be visible or invisible under normal circumstances. You can use the Attdisp command to turn all attributes on or off, or make them revert to their normal mode as defined.The bad news is that you can attach attributes only to blocks and not to other object types. They are also visible and editable, or can easily be made so, to other users.
Link to a database file
The third way is probably the most powerful and versatile. You can establish
a link between objects in an AutoCAD drawing and records in a database
file. This link works both ways, so you can select an AutoCAD object and
retrieve the database information, or you can do database searches and
then find the matching AutoCAD objects.
A drawing can reference more than one database file, and many drawings can reference the same file. Other users, such as the accounting and purchasing departments, can continue to use and maintain the database information.
The bad news is that database links can be a little complex and difficult to set up, and the database files must be made available if you move or copy the drawing file.
Extended entity data
We now come to the fourth method. This is based on the fact that with
a bit of AutoLISP programming you can add up to 16KB of your data to any
AutoCAD object, including lines, circles, arcs, and blocks. You can even
add data to layer definitions and text or dimension styles. This is known
as extended entity data, and it is your data. AutoCAD studiously ignores
it, and only a suitable AutoLISP program can view or edit it.
Yes, you need to do some AutoLISP programming, or know someone who does. You can also use VBA, but this column focuses on AutoLISP.
Sample applications
But, what can I do with extended data?
I teach at an engineering institute. There have been concerns that a few of our students take the concept of "recycling" a little too seriously, so I wrote a routine that automatically tags every object in the drawing with its creation date and the user's login name. I can thus trace the genealogy of any suspect drawings.
This technique can be used to identify objects in your drawings if you suspect clients or competitors are "borrowing" and "recycling" your drawings, or portions thereof.
Two of my students wrote a routine for a local fire sprinkler company. The sprinkler system is drawn on its own layer superimposed on the building layout drawings. They are simple single-line drawings, with elbows drawn as arcs, and so on. Attached to each object is a series of extended data that includes the identification for each branch line, the diameter, the type and size of pipe or fitting, and the object handle of its neighbors.
The client can interrogate the extended data to identify all the pipes and fittings in a particular branch, find all the fittings of a particular size and type, calculate the total length of all the pipe of a specified size, and so on.
Five steps to success
Attaching extended data is a simple five-step process:
- register your application,
- collect and format the data to attach,
- get the entity data for the object to which you want to attach your data,
- append your data to the object's data, and
- update the object's data.
You can perform steps 1 through 3 in any order, as long as they come before 4 and then 5.
Now let's take a look at each of the five steps in sequence. Make sure your drawing has at least one object in it, such as a line or a circle. To experiment, you can simply type in each of the AutoLISP expressions at the Command prompt. I have formatted and indented them, but you can type in each expression as one long entry.
Register your application. No, don't call Autodesk or anyone else.
All you have to do is to tell AutoCAD who you are, so it and other applications
recognize your data and leave it alone. This task uses the (regapp) function:
(RegApp "CADALYST")
The name in quotes can be any unique name you want, so long as it does not contain spaces. It is not case-sensitive. You do this step only once per editing session.
Collect and format the data to be attached:
(setq EXDATA |
'((-3 ("CADALYST" |
(1000 . "Hello, Sailor!") |
)))) |
This creates a variable called EXDATA to hold your extended data, which is a string of text. Note how your application name is used again to let AutoCAD know that it is your data.
Get the entity data for the object to which you want to attach
your data:
(setq LASTENT (entget
(entlast)))
This simply gets the data for the youngest entity in the drawing and stores
it in a variable called LASTENT.
Append your data to the object's data:
(setq NEWENT(append LASTENT EXDATA))
As the code fragment implies, this simply appends or adds your data to the end of the existing data and saves the result into a variable called NEWENT.
Update the object's data:
(entmod NEWENT)
This modifies the entity definition on the drawing data so it now includes your data. If you now save the file to disk, this data remains attached to this object for as long as the drawing lives, unless you specifically delete or modify it. It does not show in the drawing itself, and does not appear if you use the List command to list the properties of an object. It is invisible to normal users.
Retrieve your data
Cool, but how do I see what it is?
No problem. This is a simple one- or two-step process.
First, if you save the drawing and then open it later, you must enter:
(RegApp "CADALYST")
again at the Command prompt. You don't need to do this if you are still
in the same editing session, but it won't hurt anything if you do. Next,
enter:
(cdr (cadadr (assoc 3 |
(entget (entlast) '("CADALYST")) |
))) |
Assuming you have not added anything to the drawing since you attached the data, this brings back and displays your data from the youngest object.
Astute AutoLISP programmers will note that this code segment consists basically of the normal (entget) function, except that we pass (entget) the application name as well as the entity name.
And that's it for the basic principles of extended entity data! Be sure to come back next time when we create and analyze an actual program to attach and retrieve several different types of data on a single entity.
And now for something completely different
The Dominican Republic is famous for its amber, as popularized
by Jurassic Park. A quick test tells you whether the beach peddler is
selling the real thinggenuine amber floats in seawater, while plastic
replicas sink.
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!