You are on page 1of 7

Per-document data in AutoCAD .

NET applications - Part 2 - Through the Interface


http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html[21/01/2014 16:49:43]
Translate this page Spanish
Microsoft

Translator
K E A N W A L M S L E Y

About the Author
Kean on Google+
J A N U A R Y 2 0 1 4
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
P O S T I N D E X
R E V E R S E I N D E X
Per-document data in AutoCAD .NET applications - Part 1 | Main | A quick
Visual Studio tip - automatically implement interfaces in C#
October 19, 2006
Per-document data in AutoCAD .NET applications - Part 2
This entry completes the series of posts about per-document data. Here are the
previous entries:
Some background to AutoCAD's MDI implementation and per-document data
Per-document data in ObjectARX
Per-document data in AutoCAD .NET applications - Part 1
Document.UserData
Now let's take a look at a second technique in .NET for storing transient (non-
persisted) data with an AutoCAD document, the UserData property. The managed
framework for AutoCAD associates a hash table with each document, which can
be accessed using the UserData property. Hash tables are a great way to store
and access data quickly: each object you store in a hash table is associated with
a particular key, or lookup value. You then use this key to get at the data you've
stored in the hash table.
The UserData property returns an object of the standard .NET class,
System.Collections.Hashtable, so its advised to look on MSDN for further
examples of usage.
I've written some code to demonstrate how you might store and access per-
document data in the UserData property. The below C#code declares and
implements a simple class called MyData to store custom data, and then two
commands that store data in and use data from the UserData hash table.
The inc command checks whether theres an object under a particular
key, and if not, it creates and adds a MyData object. It then goes on to
increment the integer value stored in that object (and therefore in the
hash table)
The bogus command has some fun with the hash table, storing a
bogus object (a Point2D object) in the place where the "inc" command
expects to find a MyData object. This can only be run on a fresh
drawing one that has not had inc executed otherwise there will
already be an object stored in the hash table
Here's the code:
usi ng Aut odesk. Aut oCAD. Appl i cat i onSer vi ces;
usi ng Aut odesk. Aut oCAD. Runt i me;
usi ng Aut odesk. Aut oCAD. Edi t or I nput ;
usi ng Aut odesk. Aut oCAD. Geomet r y;
usi ng Syst em. Col l ect i ons;
[ assembl y: CommandCl ass( t ypeof ( CommandCl asses. User Dat aCl ass) ) ]
1 0 R A N D O M
P O S T S
F E E D / S H A R E
Subscribe via RSS
ShareThis
676 Like
S E A R C H

Drawing transient graphics
appropriately in AutoCAD
within multiple paperspace
viewports using .NET
Announcing DevBlogs
from ADN
Smoothly transitioning
between 3D AutoCAD
views using .NET - Part 2
Septembers Plugin of the
Month live on Autodesk
Labs: TransTips
Creating an AutoCAD
block using .NET
Inserting a specific Excel
sheet as an AutoCAD
table using .NET
Going West
Looking for API
developers to work with
Project Draw
This one made me smile...
Sectioning an AutoCAD
solid using F#
C A T E G O R I E S
3D printing 3ds Max
AJ AX Android Annotation
scaling Arduino
ASP.NET Async AU
Augmented Reality
AutoCAD
AutoCAD
.NET AutoCAD OEM
Autodesk Autodesk
Exchange AutoLISP /
Visual LISP Azure Batch
Spanish

Per-document data in AutoCAD .NET applications - Part 2 - Through the Interface


http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html[21/01/2014 16:49:43]
A U T O D E S K B L O G S
AutoCAD Insider
Between the Lines
It's Alive in the Lab
Mod the Machine
The Building Coder
It's All J ust Ones and Zeros
Dances with Elephants
AutoCAD DevBlog
Cloud and Mobile DevBlog
AEC DevBlog
Manufacturing DevBlog
Infrastructure Modeling DevBlog
Around the Corner
More on autodesk.com...
namespace CommandCl asses
{
publ i c cl ass User Dat aCl ass
{
/ / Speci f y a key under whi ch we want
/ / t o st or e our cust om dat a
const st r i ng myKey = " AsdkDat a" ;
/ / Def i ne a cl ass f or our cust om dat a
publ i c cl ass MyDat a
{
publ i c i nt count er ;
publ i c MyDat a( )
{
count er = 0;
}
}
[ Aut odesk. Aut oCAD. Runt i me. CommandMet hod( " i nc" ) ]
publ i c st at i c voi d i ncr ement ( )
{
Document doc =
Appl i cat i on. Document Manager . Mdi Act i veDocument ;
Edi t or ed = doc. Edi t or ;
Hasht abl e ud = doc. User Dat a;
MyDat a md;
md = ud[ myKey] as MyDat a;
i f ( md == nul l )
{
obj ect obj = ud[ myKey] ;
i f ( obj == nul l )
{
/ / MyDat a obj ect not f ound - f i r st t i me r un
md = new MyDat a( ) ;
ud. Add( myKey, md) ;
}
el se
{
/ / Found somet hi ng di f f er ent i nst ead
ed. Wr i t eMessage(
" Found an obj ect of t ype \ " " +
obj . Get Type( ) . ToSt r i ng( ) +
" \ " i nst ead of MyDat a. " ) ;
}
}
i f ( md ! = nul l )
{
ed. Wr i t eMessage( " \ nCount er val ue i s: " +
md. count er ++) ;
}
}
[ Aut odesk. Aut oCAD. Runt i me. CommandMet hod( " bogus" ) ]
publ i c st at i c voi d addBogus( )
{
Document doc =
Appl i cat i on. Document Manager . Mdi Act i veDocument ;
Edi t or ed = doc. Edi t or ;
Hasht abl e ud = doc. User Dat a;
Poi nt 2d pt = new Poi nt 2d( ) ;
t r y
{
processing Blocks Books
Civil 3D Commands
Concurrent programming
Conferences Core
Console Custom objects
Database Debugging
Design DesignScript
DevCasts DevTV
Dimensions Documents
Draw order Drawing
structure DWF Dynamic
Dynamic Blocks Excel
F# Facebook Fields
Fractals Freewheel
Games Geometry
Graphics system
Hatches HTML
Installation Instructables
Interviews Inventor iOS
IronPython IronRuby
J ava J avaScript J igs
J SON Kinect Leap
Motion LINQ Mac
Mobile Morgan Netduino
Notification / Events
Object properties
ObjectARX Overrules
Personal Plotting Plugin
of the Month Point
clouds Purge Python
Raspberry Pi RealDWG
Reality capture REST
Retro computing Revit
Robotics Ruby Runtime
SaaS Security
Selection Social media
Solid modeling Tables
Tinkercad Training
Transactions Translation
Travel Unity3D User
interface Visual Basic &
VBA Visual Studio
Web/Tech Weblogs
WinRT WPF XML
R E C E N T
P O S T S
Previewing geometry as it
streams into AutoCAD
using .NET
Leaving Israel
Impressions of Tel Aviv
Forcing AutoCAD object
snapping using .NET
Update on purging DGN
linestyles from AutoCAD
drawings using .NET
AU 2013 classes available
on-demand
Attack of the robots
Happy 2014
Merry Christmas!
Moving text in an
AutoCAD block using
.NET Part 3
R E C E N T
C O M M E N T S
Kean Walmsley on Taking
a screenshot of a user-
selected portion of a
Per-document data in AutoCAD .NET applications - Part 2 - Through the Interface
http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html[21/01/2014 16:49:43]
Comments
ud. Add( myKey, pt ) ;
}
cat ch
{
ed. Wr i t eMessage(
" \ nCoul d not add bogus obj ect at \ " " +
myKey +
" \ " , must be somet hi ng t her e al r eady. " ) ;
}
}
}
}
Now let's take a look at that running:
[From first drawing...]
Command: i nc
Count er val ue i s: 0
Command: i nc
Count er val ue i s: 1
Command: i nc
Count er val ue i s: 2
Command: i nc
Count er val ue i s: 3
Command: new
[From second drawing...]
Command: i nc
Count er val ue i s: 0
Command: i nc
Count er val ue i s: 1
Command: i nc
Count er val ue i s: 2
Command: bogus
Coul d not add bogus obj ect at " AsdkDat a" , must be somet hi ng t her e
al r eady.
Command: new
[From third drawing...]
Command: bogus
Command: i nc
Found an obj ect of t ype " Aut odesk. Aut oCAD. Geomet r y. Poi nt 2d" i nst ead of
MyDat a.
Posted at 04:42 PM in AutoCAD, AutoCAD .NET | Permalink
TrackBack
TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200d834bdaf1c53ef
Listed below are links to weblogs that reference Per-document data in AutoCAD
.NET applications - Part 2:
Thanks for your great article,Kean.
For C++,you give the Falsely MDI-Aware Version
example.
csharpbird said...
drawing using .NET
Dominik Hepp on Taking
a screenshot of a user-
selected portion of a
drawing using .NET
Kean Walmsley on First
impressions of Kinect for
Windows 2.0 pre-release
Andrew on First
impressions of Kinect for
Windows 2.0 pre-release
Kean Walmsley on
Adding to AutoCADs
Application Menu and
Quick Access Toolbar
using .NET
Dharmarajan on Adding to
AutoCADs Application
Menu and Quick Access
Toolbar using .NET
Kean Walmsley on
Updating a specific
attribute inside an
AutoCAD drawing using
.NET
Kean Walmsley on
AutoCAD DGN Hotfix now
available
Greg Robinson on
Gathering points defining
3D AutoCAD geometry
using .NET
Kean Walmsley on
AutoCAD 2014 for
developers
A R C H I V E S
J anuary 2014 (8)
December 2013 (9)
November 2013 (13)
October 2013 (13)
September 2013 (13)
August 2013 (14)
J uly 2013 (14)
J une 2013 (12)
May 2013 (14)
April 2013 (13)
March 2013 (12)
February 2013 (12)
J anuary 2013 (15)
December 2012 (12)
November 2012 (13)
October 2012 (14)
September 2012 (12)
August 2012 (13)
J uly 2012 (9)
J une 2012 (13)
Per-document data in AutoCAD .NET applications - Part 2 - Through the Interface
http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html[21/01/2014 16:49:43]
Reply October 19, 2006 at 07:46 PM
What about .NET?
Reply October 19, 2006 at 10:44 PM
Thanks, csharpbird.
If you take a look at the previous entry (Part 1), you can
see the "glob" command implementation... this could be
considered a "Falsely MDI-Aware" command, if the
command was supposed to be considering the counter
variable to be local for each document.
Kean said...
Reply October 19, 2006 at 11:54 PM
Thanks, Kean! Your's last three articles is very useful!
Alexander Rivilis said...
Reply October 20, 2006 at 01:38 PM
Great couple of articles Kean.
This is always a subject developers are unaware of
when developing ObjectARX applications and it would
happen with .NET too.
Thanks, Fernando.
Fernando Malard said...
Reply May 16, 2008 at 07:09 PM
hi kean,
are you aware of the difference between static and
instance variables in an AutoCAD .NET application
command class? If you create static variables (and a
static command method), they exist only once. If you
create instance variables (and a non-static command
method), a new instance of the class is instantiated for
each AutoCAD document. This automatically generates
and manages per-document data, with no need for any
work at all from your side. I have a nice sample
demonstrating this, if you like.
cheers
jeremy
jeremy said...
Hi J eremy,
Yes, I am - the previous post covered that.
The reason I used this approach in another recent post
is actually that sometimes the class instances you
Kean said...
More...
Per-document data in AutoCAD .NET applications - Part 2 - Through the Interface
http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html[21/01/2014 16:49:43]
Reply May 17, 2008 at 10:56 AM
receive automatically are not the ones you expect -
particularly when implementing event handlers
(presumably it's because you're in the application
context rather than being in a particular document's
context, depending). So I actually find UserData to be
more predictable and therefore dependable.
Cheers,
Kean
Reply June 08, 2009 at 05:46 PM
Hi,Kean
You've said
"I've written some code to demonstrate how you might
store and access per-document data in the UserData
property"
Where can I find these code? Thank u.
hhhwjb
hhhwjb said...
Reply June 08, 2009 at 05:48 PM
hhhwjb - it's the C#code in this post.
Kean
Kean Walmsley said...
Reply June 09, 2009 at 02:38 AM
Hi,Kean
Thank u for ur answer.
I test this code, when I close the document,the
UserData lost. How can I save the UserData together
with my documentation?
Thank u in advance.
hhhwjb said...
Reply June 09, 2009 at 08:13 AM
This example does not persist data in the DWG file: it
just associates it with the document for runtime access.
If you need to persist data then you'll probably want to
use XRecords to do so (search this blog to find
examples).
Kean
Kean Walmsley said...
Hi Kean,
I came across this article when i was
Matus said in reply to Kean Walmsley...
Per-document data in AutoCAD .NET applications - Part 2 - Through the Interface
http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html[21/01/2014 16:49:43]
Comment below or sign in with Typepad Facebook Twitter Google+
and more...
Reply January 13, 2011 at 04:55 PM
searching for a way to store custom classes
in AutoCAD drawing.
I thought i found the answear until i realised,
that this data is not persistent.
On the other hand XRecord can only store
ResultBuffer type in his Data property and
DBDictionary can only store classes derived
from the DBObject class. Or am I wrong?
What is the best way to store custom classes
id drawings persistently?
Matus
Reply January 13, 2011 at 05:06 PM
Hi Matus,
Regarding your last question (which may also
respond to your others), It depends:
Xdata for smallish amounts (meaning up to
about 4K, as a rule of thumb... there's a
shared 16K limit per chunk of Xdata, if I recall
correctly).
Xrecords in extension dictionaries (or centrally
in the named objects dictionary) for larger
amounts.
ResultBuffers are actually reasonably flexible,
if you implement them properly.
Regards,
Kean
Kean Walmsley said in reply to Matus...
Reply June 09, 2009 at 01:43 PM
Thank u very much.
hhhwjb said...
Reply December 14, 2012 at 03:13 PM
Thanks Kean, it is still relevant in 2012.
Regards
Craig
Craig Nicholas said...
Per-document data in AutoCAD .NET applications - Part 2 - Through the Interface
http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html[21/01/2014 16:49:43]
(You can use HTML tags like <b> <i> and <ul> to style your text. URLs
automatically linked.)
Email address is not displayed with comment.

Name
Email Address
Web Site URL
Post Preview

You might also like