Per-document Data in AutoCAD .NET Applications - Part 2 - Through the Interface_GOOD

download Per-document Data in AutoCAD .NET Applications - Part 2 - Through the Interface_GOOD

of 7

Transcript of Per-document Data in AutoCAD .NET Applications - Part 2 - Through the Interface_GOOD

  • 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 quickVisual Studio tip - automatically implement interfaces in C#

    October 19, 2006

    Per-document data in AutoCAD .NET applications - Part 2This entry completes the series of posts about per-document data. Here are theprevious entries:

    Some background to AutoCAD's MDI implementation and per-document dataPer-document data in ObjectARXPer-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 managedframework for AutoCAD associates a hash table with each document, which canbe accessed using the UserData property. Hash tables are a great way to storeand access data quickly: each object you store in a hash table is associated witha particular key, or lookup value. You then use this key to get at the data you'vestored 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 furtherexamples 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 andimplements a simple class called MyData to store custom data, and then twocommands that store data in and use data from the UserData hash table.

    The inc command checks whether theres an object under a particularkey, and if not, it creates and adds a MyData object. It then goes on toincrement the integer value stored in that object (and therefore in thehash table)

    The bogus command has some fun with the hash table, storing abogus object (a Point2D object) in the place where the "inc" commandexpects to find a MyData object. This can only be run on a freshdrawing one that has not had inc executed otherwise there willalready be an object stored in the hash table

    Here's the code:

    using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.Geometry;using System.Collections;

    [assembly: CommandClass(typeof(CommandClasses.UserDataClass))]

    1 0 R A N D O MP O S T S

    F E E D / S H A R E

    Subscribe via RSS

    ShareThis

    676Like

    S E A R C H

    Drawing transient graphicsappropriately in AutoCADwithin multiple paperspaceviewports using .NET

    Announcing DevBlogsfrom ADN

    Smoothly transitioningbetween 3D AutoCADviews using .NET - Part 2

    Septembers Plugin of theMonth live on AutodeskLabs: TransTips

    Creating an AutoCADblock using .NET

    Inserting a specific Excelsheet as an AutoCADtable using .NET

    Going West

    Looking for APIdevelopers to work withProject Draw

    This one made me smile...

    Sectioning an AutoCADsolid using F#

    C A T E G O R I E S

    3D printing 3ds MaxAJAX Android Annotationscaling ArduinoASP.NET Async AUAugmented Reality

    AutoCADAutoCAD.NET AutoCAD OEMAutodesk AutodeskExchange AutoLISP /Visual LISP Azure Batch

  • 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 Just 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 CommandClasses{ public class UserDataClass { // Specify a key under which we want // to store our custom data const string myKey = "AsdkData";

    // Define a class for our custom data public class MyData { public int counter; public MyData() { counter = 0; } }

    [Autodesk.AutoCAD.Runtime.CommandMethod("inc")] public static void increment() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Hashtable ud = doc.UserData; MyData md;

    md = ud[myKey] as MyData; if (md == null) { object obj = ud[myKey]; if (obj == null) { // MyData object not found - first time run md = new MyData(); ud.Add(myKey, md); } else { // Found something different instead ed.WriteMessage( "Found an object of type \"" + obj.GetType().ToString() + "\" instead of MyData."); } } if (md != null) { ed.WriteMessage("\nCounter value is: " + md.counter++); } } [Autodesk.AutoCAD.Runtime.CommandMethod("bogus")] public static void addBogus() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Hashtable ud = doc.UserData; Point2d pt = new Point2d(); try {

    processing Blocks BooksCivil 3D CommandsConcurrent programmingConferences CoreConsole Custom objectsDatabase DebuggingDesign DesignScriptDevCasts DevTVDimensions DocumentsDraw order Drawingstructure DWF DynamicDynamic Blocks ExcelF# Facebook FieldsFractals FreewheelGames GeometryGraphics systemHatches HTMLInstallation InstructablesInterviews Inventor iOSIronPython IronRubyJava JavaScript JigsJSON Kinect LeapMotion LINQ MacMobile Morgan NetduinoNotification / EventsObject propertiesObjectARX OverrulesPersonal Plotting Pluginof the Month Pointclouds Purge PythonRaspberry Pi RealDWGReality capture RESTRetro computing RevitRobotics Ruby RuntimeSaaS SecuritySelection Social mediaSolid modeling TablesTinkercad TrainingTransactions TranslationTravel Unity3D Userinterface Visual Basic &VBA Visual StudioWeb/Tech WeblogsWinRT WPF XML

    R E C E N TP O S T S

    Previewing geometry as itstreams into AutoCADusing .NET

    Leaving Israel

    Impressions of Tel Aviv

    Forcing AutoCAD objectsnapping using .NET

    Update on purging DGNlinestyles from AutoCADdrawings using .NET

    AU 2013 classes availableon-demand

    Attack of the robots

    Happy 2014

    Merry Christmas!

    Moving text in anAutoCAD block using.NET Part 3

    R E C E N TC O M M E N T S

    Kean Walmsley on Takinga 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); } catch { ed.WriteMessage( "\nCould not add bogus object at \"" + myKey + "\", must be something there already."); } } }}

    Now let's take a look at that running:

    [From first drawing...]

    Command: incCounter value is: 0Command: incCounter value is: 1Command: incCounter value is: 2Command: incCounter value is: 3Command: new

    [From second drawing...]

    Command: incCounter value is: 0Command: incCounter value is: 1Command: incCounter value is: 2Command: bogusCould not add bogus object at "AsdkData", must be something therealready.Command: new

    [From third drawing...]

    Command: bogusCommand: incFound an object of type "Autodesk.AutoCAD.Geometry.Point2d" instead ofMyData.

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

    csharpbird said...

    drawing using .NET

    Dominik Hepp on Takinga screenshot of a user-selected portion of adrawing using .NET

    Kean Walmsley on Firstimpressions of Kinect forWindows 2.0 pre-release

    Andrew on Firstimpressions of Kinect forWindows 2.0 pre-release

    Kean Walmsley onAdding to AutoCADsApplication Menu andQuick Access Toolbarusing .NET

    Dharmarajan on Adding toAutoCADs ApplicationMenu and Quick AccessToolbar using .NET

    Kean Walmsley onUpdating a specificattribute inside anAutoCAD drawing using.NET

    Kean Walmsley onAutoCAD DGN Hotfix nowavailable

    Greg Robinson onGathering points defining3D AutoCAD geometryusing .NET

    Kean Walmsley onAutoCAD 2014 fordevelopers

    A R C H I V E S

    January 2014 (8)

    December 2013 (9)

    November 2013 (13)

    October 2013 (13)

    September 2013 (13)

    August 2013 (14)

    July 2013 (14)

    June 2013 (12)

    May 2013 (14)

    April 2013 (13)

    March 2013 (12)

    February 2013 (12)

    January 2013 (15)

    December 2012 (12)

    November 2012 (13)

    October 2012 (14)

    September 2012 (12)

    August 2012 (13)

    July 2012 (9)

    June 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 cansee the "glob" command implementation... this could beconsidered a "Falsely MDI-Aware" command, if thecommand was supposed to be considering the countervariable 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 ofwhen developing ObjectARX applications and it wouldhappen 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 andinstance variables in an AutoCAD .NET applicationcommand class? If you create static variables (and astatic command method), they exist only once. If youcreate instance variables (and a non-static commandmethod), a new instance of the class is instantiated foreach AutoCAD document. This automatically generatesand manages per-document data, with no need for anywork at all from your side. I have a nice sampledemonstrating this, if you like.

    cheers

    jeremy

    jeremy said...

    Hi Jeremy,

    Yes, I am - the previous post covered that.

    The reason I used this approach in another recent postis 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 applicationcontext rather than being in a particular document'scontext, depending). So I actually find UserData to bemore predictable and therefore dependable.

    Cheers,

    Kean

    Reply June 08, 2009 at 05:46 PM

    Hi,KeanYou've said"I've written some code to demonstrate how you mightstore and access per-document data in the UserDataproperty"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,KeanThank u for ur answer.I test this code, when I close the document,theUserData lost. How can I save the UserData togetherwith 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: itjust associates it with the document for runtime access.If you need to persist data then you'll probably want touse XRecords to do so (search this blog to findexamples).

    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 classesin AutoCAD drawing.I thought i found the answear until i realised,that this data is not persistent.On the other hand XRecord can only storeResultBuffer type in his Data property andDBDictionary can only store classes derivedfrom the DBObject class. Or am I wrong?

    What is the best way to store custom classesid drawings persistently?

    Matus

    Reply January 13, 2011 at 05:06 PM

    Hi Matus,

    Regarding your last question (which may alsorespond to your others), It depends:

    Xdata for smallish amounts (meaning up toabout 4K, as a rule of thumb... there's ashared 16K limit per chunk of Xdata, if I recallcorrectly).

    Xrecords in extension dictionaries (or centrallyin the named objects dictionary) for largeramounts.

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

    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 and to style your text. URLsautomatically linked.)

    Email address is not displayed with comment.

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

    Jkb2N1bWVudF9kYXRfMi5odG1sAA==: MSTWMenu: es

    hlbWU9bGlnaHQmaGVpZ2h0PTIxAA==: form0: lsd: AVpC_5owhref: http://through-the-interface.typepad.com/action: likenobootload: iframe_referer: http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.htmlref: button0: lsd_(1): AVpC_5owhref_(1): http://through-the-interface.typepad.com/action_(1): likenobootload_(1): iframe_referer_(1): http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.htmlref_(1):

    Jkb2N1bWVudF9kYXRfMi5odG1sAA==: form1: filter.q: input3:

    I9JTIzMjMyMzIzJndpZHRoPTQ3OQA=: form1: comment: author: Nameemail: Email Addressurl: Web Site URLpost: preview: