Have you extended your object data recently?

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
two-step process:

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.

(setq NEWDATA-1

(getstring "Enter some words: "
T)

NEWDATA-2

(getstring "Enter some more words: "
T)

)

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 parenthesesextended 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.(setq EXDATA

Level 1: You must hold the extended data in a master list so it
can eventually be appended to the object data list:(list

Level 2: The master list holds a sublist:

(list

-3

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:

(list

A-N

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
off.) ) ) )

Get the object entity data. This section uses normal AutoLISP
techniques to retrieve the standard AutoCAD data for an object:

(setq ENTDATA

(entget

(car

(entsel)

)

)

)

You can also use regular functions and techniques such as (ssget), (entlast),
(ent-next), and so on.

Update object data. Again, no magic: simply update the object
data in the drawing database so it now includes your extended data:

(entmod NEWENT)

) ; 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.

GET.LSP

(defun c:GET ()

; new command.

(setq ENTDATA

; create a variable. . .

(entget

; to hold all the data.

(car (entsel))

; from a selected

(list A-N)

; entget needs

; application name

)

)

(setq E_DATA ; create a variable to

; hold. . .

(assoc

; the list. . .

-3

; of the "-3" group.
. .

ENTDATA

; in the retrieved

)

)

)

Get back!
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.

(-3

("CADALYST"

(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

1000

ASCII string (up to 255 bytes long) in extended
data

1003

Extended data layer name

1004

Chunk of bytes (up to 127 bytes long) in
extended data

1005

Entity handle in extended data; text string
of up to 16 hexadecimal digits

1010

A point in extended data

1011

A 3D world space position in extended data

1012

A 3D world space displacement in extended
data

1013

A 3D world space direction in extended data

1040

Extended data floating-point value

1041

Extended data distance value

1042

Extended data scale factor

1070

Extended data 16-bit signed integer

1071

Extended data 32-bit signed long

Groupies, anyone?
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 ita 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."

About the Author: Bill Fane

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!Follow Lynn on Twitter