Anda di halaman 1dari 12

Extend AutoCAD entity data | Cadalyst

Seite 1 von 4

Print

AutoCAD

Extend AutoCAD entity data


1 Apr, 2001
By: Bill Fane

It 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.

http://www.cadalyst.com/cad/autocad/extend-autocad-entity-data-4772?print=1

06.12.2016

Extend AutoCAD entity data | Cadalyst

Seite 2 von 4

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 casesensitive. 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.

http://www.cadalyst.com/cad/autocad/extend-autocad-entity-data-4772?print=1

06.12.2016

Extend AutoCAD entity data | Cadalyst

Seite 3 von 4

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.

About the Author: Bill Fane


E-mail Bill Fane
About Bill Fane
See contents by Bill Fane

AutoCAD Tips!
In her easy-to-follow, friendly
style, long-time Cadalyst
contributing editor and
Autodesk Technical Evangelist
Lynn Allen guides you through
a new feature or time-saving
trick in every episode of her popular
AutoCAD video tips. Subscribe to the free
Cadalyst Video Picks newsletter and we'll
notify you every time a new video tip is
published. All exclusively from Cadalyst!
Follow Lynn on Twitter

Poll
Are you responsible for any CAD management
duties (conducting training, implementing
software, establishing standards, etc.)?

http://www.cadalyst.com/cad/autocad/extend-autocad-entity-data-4772?print=1

Yes: I am a full-time CAD manager


Yes: CAD management is part of my job
description

06.12.2016

Have you extended your object data recently? | Cadalyst

Seite 1 von 5

Print

AutoCAD

Have you extended your object data recently?


1 Aug, 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 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)

http://www.cadalyst.com/cad/autocad/have-you-extended-your-object-data-recently-4771?print=1

06.12.2016

Have you extended your object data recently? | Cadalyst

Seite 2 von 5

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

)
)

http://www.cadalyst.com/cad/autocad/have-you-extended-your-object-data-recently-4771?print=1

06.12.2016

Have you extended your object data recently? | Cadalyst

Seite 3 von 5

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.
(setq NEWENT
(append ENT DATA
EXDATA)

)
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

(entget

; create a variable. . .

; 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

http://www.cadalyst.com/cad/autocad/have-you-extended-your-object-data-recently-4771?print=1

06.12.2016

Have you extended your object data recently? | Cadalyst

Seite 4 von 5

)
)
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.
Groupies, anyone?
Group codes for extended data
As mentioned earlier, you must set up extended data as a dotted pair with 1000 ASCII string (up to 255
"suitable" group codes. The good news is that extended data has its own
bytes long) in extended
unique set of group codes, all in the 1000 series (see "Group codes for
data
extended data" above).
1003 Extended data layer
name
Each type of data should be dot-paired with its appropriate group code.
1004 Chunk of bytes (up to
Once you retrieve the extended data (group 3), you can use the (assoc)
127 bytes long) in
function to find a specific item within it.
extended data
The bad news is that extended data must use only the listed group codes. 1005 Entity handle in extended
data; text string of up to
If you have more than one piece of data with the same type, then
16 hexadecimal digits
sequence counts, and you must keep track of what does what. Our
1010 A point in extended data
example program attached two text strings. They were both group 1000.
1011 A 3D world space
position in extended data
When retrieving data, you can use the AutoLISP function (nth) to pick a
1012 A 3D world space
specific item out of the extended data.
displacement in
And there you have ita brief introduction to the wonderful world of
extended data
extended object data.
1013 A 3D world space
direction in extended
And now for something completely different
data
Computer programming languages usually involve some form of if-then
1040 Extended data floatinglogic statement, and this normally includes Boolean operations. A typical
point value
example runs something like: "If your nose runs and your feet smell, then
1041 Extended data distance
you are built upside-down."
value
1042 Extended data scale
factor
1070 Extended data 16-bit
signed integer
1071 Extended data 32-bit
signed long

http://www.cadalyst.com/cad/autocad/have-you-extended-your-object-data-recently-4771?print=1

06.12.2016

Working with Extended Data in VBA and AutoLISP | Cadalyst

Seite 1 von 6

Print

AutoCAD

Working with Extended Data in VBA and


AutoLISP
1 Jan, 2000
By: Bill Kramer
Extended data is nongraphic information that is attached to AutoCAD entity objects in your drawing. It is very
useful when you are writing programs that need additional information on top of the data already present in
an object. For example, suppose you wanted to draw a fence line on a map and along with the data points
you wanted to include information about the last time the fence section was repaired or updated. Extended
data would work well for this application.
This month we are going to explore extended data manipulated by a VBA program as compared to one
manipulated in AutoLISP. There are some important differences in the way you go about such an activity
depending on the language you select. They are summarized in the fact that in VBA we manipulate objects
while in AutoLISP we manipulate entity data lists. Using objects for such manipulations is much easier to
fathom than navigating convoluted entity data lists as will be seen. The only potential uncomfortable part for
beginners involves the use of variant arrays in VBA. But once that concept is under control, the rest is easy.
Extended data is stored in an object using group codes just as with all other data found in entity objects. From
the AutoLISP and DXF programming perspective, this is the normal way entity objects are accessed. That is,
each component in an entity object has a group
code that indicates what kind of data follows. Table 1
But for VBA programmers, the use of group
Common Extended Data Group Codes
codes may be new since the normal properties
of the entity objects are directly accessed as Code
Represents
Data type
needed. For example, if you have a line object 1000
Application name
Application name
open, you access the starting point using the 1001
Text value
String
StartPoint property of the object. There are no 1005
Entity handle string String
group codes involved in VBA applications
1010
Data point X
Real
development except for the extended data.
1020
Data point Y
Real
Table 1 lists the most common group codes
1030
Data point Z
Real
used for extended data elements.
1040
Number
Real
1041
Scaled distance value Real
Group Codes
1070
Integer
Integer value
Group codes can be repeated over and over
again in the extended data. Should your
application require five real numbers, then five different instances of the 1040 group code would be used. The
group codes, their order and their associated data values are completely controlled by your application. In
fact, the only restriction you will face in the use of group codes is that each data object can only hold up to 16
kilobytes of extended data. That's a lot of information and most applications will not even come close to this
requirement. If they do, then another data storage strategy is strongly recommended.
Multiple applications can also store extended data in a single entity object. Your program may store data and
so may others at the same time. And AutoCAD does not mix them up. The way in which AutoCAD keeps the
information intact is actually your responsibility.
Using a common name can lead to confusion where the extended data is concerned.
Using a unique name, your data is differentiated from that of other programs. The extended data name is of
your own design and should be unique for your application.
One way to obtain a unique name for your extended data is to search through the registered applications with
a given name and see if it has been used. In AutoLISP you accomplish this using the (TBLSEARCH) subr, as
shown in Listing 1 (All listings are at the bottom of this page). The function New_AppID has one argument-a
seed name as devised by your application. Use any name you want. Based on internal storage and searching

http://www.cadalyst.com/cad/autocad/working-with-extended-data-vba-and-autolisp-4818?print=1

06.12.2016

Working with Extended Data in VBA and AutoLISP | Cadalyst

Seite 2 von 6

requirements in AutoCAD, a registered application name should not be too long, so I recommend that you
keep the name to about 15 or 20 characters in length. Of course it can be longer, but why use up the space
for a longer name?
Listing 2 shows the same in VBA. The extended data application names are stored in a collection named
RegisteredApplications. In VBA a DO loop is used to access the collection until a new name is found. Item()
is used inside the loop with the key name (a string) containing the seed string and the incrementing counter,
just as in the AutoLISP code. After the function finds a new application name, it is added to the collection. The
addition of the name to the collection is not required in VBA as it is in AutoLISP. VBA will add a new name the
first time it is used as part of an entity object update.
After an application name has been determined, your program can now add extended data to AutoCAD entity
objects. At first glance, the approach is a bit confusing. Since the extended data is of your own design and
AutoCAD has no way of knowing what you may want to store, general-purpose storage containers have been
devised for the purpose.
In AutoLISP, all extended data is appended to objects using a -3 group code in the entity list. Within the group
code you can find all of the extended data that has been added to the object. It is a nested list within the
entity list and thus the confusion for many beginning AutoLISP programmers. Sometimes the issue is cleared
up a little when looking at what is required when adding extended data.
Adding extended data to an object in AutoLISP is simply a matter of appending it to an existing (or new) entity
data list. The extended data is stored in a list with the group codes appropriate to the data type. The following
is a simple example of a string, a real number and an integer stored in an extended data list format.
((1001 . "String")
(1040 . 1.234)
(1070 . 12))
The data appears in the same style as a regular entity data list. The only differences are that the group codes
used are in the 1000 series, and the data itself is of your own design. But this list represents just the data
itself. To add it to an entity data list, you must add the application name to the front. If our registered
application name is "EXAMPLE", then the list will look similar to the following:
("EXAMPLE"
(1001 . "String")
(1040 . 1.234)
(1070 . 12))
Now you add the group code for extended data. There can be any number (up to the memory limits of
extended data) of Xdata lists contained inside the -3 group code set. Each must have a unique name as the
first member of the list. And each name must be registered first using (REGAPP) so that AutoCAD recognizes
it as valid extended data.
(-3
("EXAMPLE"
(1001 . "String")
(1040 . 1.234)
(1070 . 12)))
The extended data list with group code -3 is then appended to the end of an entity data list that is in turn
written to the drawing database. Appending the list can be accomplished through the use of the (APPEND)
subr with an existing entity data list and then writing the data to the drawing with (ENTMOD). New entities can
have the extended data list included as part of the new entity data list that is written to the drawing database
using the (ENTMAKE) subr. Because of the nested lists involved, many novice AutoLISP programmers tend
to shy away from the use of extended data or use utility routines that shield them from the nasty details. (Note
that extended data utility routines for AutoLISP programmers can be obtained from the Web site listed at the
end of this column.)
By comparison, the addition of extended data in VBA involves a different approach. In VBA, the extended
data is added to an object after it has been added to the drawing database. The entity is created first to obtain
an entity object that can be used to reference the method available for storing the extended data. All
AutoCAD drawing objects support the method SetXData. This method allows a program to affix extended
data to an existing drawing object. Listing 3 demonstrates how the SetXData method works with an existing
object supplied as the type AcadEntity. In this simple example, an entity object is supplied along with three
data elements (a string, a real number and an integer). The object's SetXData method is used to write the
three data elements to the drawing database after they are placed into a variant array along with an
application name ("MyData"in the example listing).

http://www.cadalyst.com/cad/autocad/working-with-extended-data-vba-and-autolisp-4818?print=1

06.12.2016

Working with Extended Data in VBA and AutoLISP | Cadalyst

Seite 3 von 6

Integer and Variant Arrays


Two arrays are required for the SetXData method. The first array contains only integer values that are the
matching group codes for the data being attached. The other array is a variant array that contains the data
itself. Thus to add a string one must put a 1001 group code into the integer array and the string value into the
variant array at the same position.
At the head of the variant/integer array combination is the application name stored with a 1000 group code.
Without this value, the extended data does not have a valid registered application name and will not be
added. The function shown in Listing 2 can be used to obtain a unique name for your extended data, or you
can simply make one up as shown in Listing 3. One item of note is that you do not have to register the
application name being used in the extended data when using VBA. All you need to do is use it, and it will be
added to the registered names for you automatically. But if you want to guarantee that the name you are
using is your own, registering it first is the only way to go. Otherwise you could be carelessly overwriting
someone else's extended data!
Variant arrays are used in the writing and retrieving of extended data in VBA. Variants allow for multiple data
storage types all referenced with a common name. This is much like lists in AutoLISP. The member of a list or
variant array can be of any supported data type. When the SetXData member is used with a valid entity
object, the extended data is written to the object in the drawing database. Internally the system converts the
data in the variant array using the group codes supplied in the integer array to store the data in proper
containers. Although the data type is found in the variant data elements themselves, your application may
want to treat the values differently and thus the group codes are required to further classify the data. For
example, group code 1040 signifies a normal real number, but you use group code 1041 when you want the
number to be automatically updated whenever the object is scaled. When AutoCAD sees a group code 1041
being added as extended data, it sets up the scaling operation for us thereby relieving our application of the
burden.
Getting extended data from an object requires the use of the registered application name. Since more than
one application may attach data to an object, the data you are interested in must be specifically requested by
name. In AutoLISP this is achieved in the (ENTGET) subr with the inclusion of a list of application names
following the entity name to get. To illustrate, the following expression is supplied with an entity name (EN)
and will return an entity list (stored in EL) with extended data that has the application name "EXAMPLE".
(setq EL
(entget EN
,("EXAMPLE")))
The application name is stored in a list so that multiple applications can be retrieved in a single statement.
Extended data is then found using the -3 group code in the entity data list. The (cdr (assoc -3 EL)) expression
will return the extended data list. If multiple extended data lists are being used, the (ASSOC) subr can then
be used to get them from within the extended data set. If only one set of extended data is requested, the
(ASSOC) can be used, but the common approach is to use composite primitives (CAR) and (CDR) instead.
Using the example I started earlier in this column, the variable XD is set to the extended data found in the
entity list EL.
(setq XD
(cdr
(assoc -3 EL)))
The above code results in the following when used with our previous example data.
(("EXAMPLE"
(1001 . "String")
(1040 . 1.234)
(1070 . 12)))
The extended data can now be further reduced to something more useful. Taking the (CAR) of this list will
return the "EXAMPLE" extended data and then taking the (CDR) will result in just the extended data without
the application name "EXAMPLE" at the beginning. The next expression places just the extended data into
the list XD.
(setq XD
(cdar XD))
((1001 . "String")
(1040 . 1.234)
(1070 . 12))

http://www.cadalyst.com/cad/autocad/working-with-extended-data-vba-and-autolisp-4818?print=1

06.12.2016

Working with Extended Data in VBA and AutoLISP | Cadalyst

Seite 4 von 6

This is why retrieving extended data looks complicated in AutoLISP. The operations just outlined are often
bunched together into a single expression, such as the following:
(setq XD
(cdadr
(assoc -3 EL)))
The composite primitive (CDAR) was expanded to include the (CDR) used to remove the -3 group code
obtained from the (ASSOC) subr. Although elegant in the conservative usage of code, this sort of data
manipulation can be complicated to follow in the source code. That is where VBA is superior. The access of
extended data from an object in VBA is accomplished with the GetXData method. Given the application name
as the first parameter to GetXData, the method will fill in two variants with array data. These variant arrays
contain the group code integers and the data values respectively. The data will be returned in the same order
as it was placed into the entity object.
Listing 4 contains a portion of code that will ask the operator to select an entity and will then retrieve the
extended data for an application named "MyData". (Again, a more complete example using a dialog box to
display extended data can be found at the Web site listed at the end of the column.)
Retrieving Extended Data in VBA
Retrieval of extended data in VBA relies heavily on variant data types. The values in XdT and XdV from
Listing 4 can be retrieved using LBound and UBound. LBound(XdT) will return the index of the first entry in
the array; UBound (XdT) will result in the last index in the array. An incrementing counter can be used to loop
from one value to the next and obtain the extended data elements.
Both AutoLISP and VBA provide generic methods for obtaining extended data since AutoCAD cannot
possibly know what your intentions may be when developing an application that uses them. Some
applications may even use multiple registered application names to help keep the extended data separated
based on the demands of various interrelated components. As an example, a beam centerline object may
contain details about the cross section of the beam as a set of extended data and information about the
current loading on the beam as another. That way the application interested in only the cross section can
obtain that information directly without having to wade through the loading data and so forth.
It is the generic nature of extended data that scares many people away until they find a need for it. But it is
also that same nature that gives extended data the power and flexibility needed to be used in the engineering
and architectural computer graphics arena. Your applications and those of other developers can utilize this
tool set to build intelligent objects. The next step is into reactors that are tied to these objects so that your
application runs as they are manipulated. Using extended data to hold the data and reactors to maintain the
integrity of the overall system greatly enhances the way your applications will work with the users. Until next
time, keep on programming!
(All code along with more complete examples can be found at www.autocode.com, the author's primary Web
site.)
Listing 1.
Find a Unique Application Name(defun New_AppID (
SeedName ;String name for application
/
Cnt ;incrementing counter
)
(setq CNT 1) ;;initialize counter
(while (tblsearch "APPID"
(strcat SeedName (itoa Cnt)))
;;Found one, increment counter
(setq Cnt (1+ Cnt)))
;;register the application
(regapp (strcat SeedName (itoa Cnt)))
;;return name used
(strcat SeedName (itoa Cnt))
Listing 2.
Add New Application in VBAPrivate Function NewAppID( _
Seed As String) _
As String
Dim I As Integer

http://www.cadalyst.com/cad/autocad/working-with-extended-data-vba-and-autolisp-4818?print=1

06.12.2016

Working with Extended Data in VBA and AutoLISP | Cadalyst

Seite 5 von 6

I = 0
On Error Resume Next
With ThisDrawing.RegisteredApplications
Do
I = I + 1
Err.Clear
.Item(Seed & Str(I))
Loop Until Err.Number <> 0
.Add (Seed & Str(I))
End With
NewAppID = Seed & Str(I)
End Function
Listing 3.
Adding Xdata in VBAPrivate Sub XDataAdd( _
Obj As AcadEntity, _
Item1 As String, _
Item2 As Double, _
Item3 As Integer _
)
Dim TypArray(0 To 3) As Integer
Dim ValueArray(0 To 3) As Variant
TypArray(0) = 1001 'Application name
ValueArray(0) = "MyData"
'
TypArray(1) = 1000 'string item
ValueArray(1) = Item1
'
TypArray(2) = 1040 'real number
ValueArray(2) = Item2
'
TypArray(3) = 1070 'integer number
ValueArray(3) = Item3
'
Obj.SetXData TypArray, ValueArray
End Sub
Listing 4.
Retrieve Xdata in VBADim Ob As AcadEntity 'Entity object
Dim PP As Variant 'Selection point
Dim XdT As Variant 'Group codes
Dim XdV As Variant 'Data values
On Error Resume Next
ThisDrawing.Utility.GetEntity Ob, PP, "Select"
If Err.Number = 0 Then
Ob.GetXData "MyData", XdT, XdV
' process continues==

About the Author: Bill Kramer


About Bill Kramer
See contents by Bill Kramer

AutoCAD Tips!
http://www.cadalyst.com/cad/autocad/working-with-extended-data-vba-and-autolisp-4818?print=1

06.12.2016

Anda mungkin juga menyukai