Have you extended your object data recently?31 Jul, 2001 By: Bill Fane
After a day of skiing at Whistler/Blackcomb, Captain LearnCurve uploaded the data from his pocket-sized GPS satellite positioning receiver and ran it through the analysis program. Any day that produces 39,750 vertical feet and 51.9 linear miles of skiing can't be all bad. Speaking of data, are you going to finish your extended object data story? As we saw last time (April 2001), with a bit of AutoLISP programming you can attach your own data to any object in an AutoCAD drawing. It is your data. AutoCAD and all other programs ignore it, and most users don't even know it's there.
Attaching extended data is a simple five-step process:
- Register your application.
- Collect and format the data to be attached.
- Get the entity data for the object to which you want to attach your data.
- Append your data to the object's data.
- Update the object's data.
Once your data is attached, you can retrieve it with an even simpler
- Register your application.
- Use AutoLISP's (entget) function, but pass your application name along with the entity name.
April's "Learning Curve" covered a few simple manual operations. We typed in AutoLISP expressions one at a time at the Command prompt to attach and retrieve a single piece of data.
This time, let's get a little fancier and create a small program that attaches two pieces of data to any selected object. We'll break the program code into five chunks that match the five steps outlined earlier. If you type the program into Notepad, you need to enter only the code (as contained within parentheses) and not the explanations.
Five easy steps
Start with the following lines of AutoLISP code:
|(setq A-N "CADALYST")|
|(regapp A-N); application name|
If this is to be your data, the first step is to tell AutoCAD who you are so other applications won't mess with your data. You need to register only once per session, so you might want to have your program do it automatically as it loads. The example uses the (setq) function to set a variable to hold the name. We'll need the name several more times, so this makes it more convenient to retrieve.
Collect and format data. Next, define the command to attach data:
(defun c:ATTACH () ; command to ATTACH data
The following asks you to supply two text strings at the Command prompt.
Now that you have the data, you need to format
it properly and set it up as a list to be appended to the object data.
Note the indentation of the parenthesesextended data consists of
a series of lists nested within lists, at least four levels deep. We'll
hold our extended data in a variable called EXDATA.
Level 1: You must hold the extended data in a master list so it
can eventually be appended to the object data list:
Level 2: The master list holds a sublist:
The first item in the sublist is 3, which is the DXF group code for extended data.
Level 3: Next comes a sub-sublist of your specific data:
The first item in it is the application name. All extended data from all applications ends up in a single 3 group, so you must identify which data is yours.
Level 4: Finally, you come to the first item of your specific
data. You must create a dotted pair list that consists of a suitable group
code followed by your data (I'll explain suitable in a moment):
(cons 1000 NEWDATA-1)
In this example, the data is passed to the pair list in the form of a variable set previously, when you input the data.
Level 4 again: Here is the next item of your data, once again
set up as a dotted-pair list:
(cons 1000 NEWDATA-2)
Once all the data is paired up, add four right parentheses to close things
) ) ) )
Get the object entity data. This section uses normal AutoLISP
techniques to retrieve the standard AutoCAD data for an object:
You can also use regular functions and techniques such as (ssget), (entlast), (ent-next), and so on.
Append data. Nothing magic here just a normal AutoLISP operation
that joins your extended data list to the existing object data list.
Update object data. Again, no magic: simply update the object
data in the drawing database so it now includes your extended data:
|) ; and close off our (defun)|
And that's all there is to it! This AutoLISP code collects two pieces of information and attaches them to a selected object in the drawing. Obviously, a "real" program includes things such as proper error handlers, local variables, and comments, but this example shows the basic principles involved.
Okay, now let's get your extended data back. As we saw earlier, the usual (entget) function works only if you tell it to get your data by passing it your application name.
The simple command Get, shown in the code box above, asks you to select an object and then returns the extended data from it. Once again, the code does not include error trapping for instances such as "Nothing selected" or "Selected object does not have extended data."The routine is so simple that its comments should be enough to explain it.
As in our earlier example, the routine returns the full 3 group as a
list of nested lists held in the variable E_DATA.
|(1000 . "hello")|
|(1000 . "sailor!")|
You can now use the usual AutoLISP techniques that involve functions such as (car), (cdr), (cadr), and (nth) to extract specific values from this list.
|Group codes for extended data|
As mentioned earlier, you must set up extended data as a dotted pair with "suitable" group codes. The good news is that extended data has its own unique set of group codes, all in the 1000 series (see "Group codes for extended data" above).
Each type of data should be dot-paired with its appropriate group code. Once you retrieve the extended data (group 3), you can use the (assoc) function to find a specific item within it.
The bad news is that extended data must use only the listed group codes. If you have more than one piece of data with the same type, then sequence counts, and you must keep track of what does what. Our example program attached two text strings. They were both group 1000.
When retrieving data, you can use the AutoLISP function (nth) to pick a specific item out of the extended data.
And there you have ita brief introduction to the wonderful world of extended object data.
And now for something completely different
Computer programming languages usually involve some form of if-then logic statement, and this normally includes Boolean operations. A typical example runs something like: "If your nose runs and your feet smell, then you are built upside-down."