store custom data in drawing

store custom data in drawing

Anonymous
Not applicable
2,060 Views
5 Replies
Message 1 of 6

store custom data in drawing

Anonymous
Not applicable

Hello,

when I need to store some data in the drawing, I am usually using DBDictionary for that. But what is the way to store custom class object?

0 Likes
2,061 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

Besides using DBDictonary, you can also look into Autodesk.AutoCAD.ApplicationServices.Document.UserData property, which is an HashTable.

 

Kean had a 2-part article on this long time ago:

 

http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_1.html

http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_2.html

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

Anonymous
Not applicable

I know its possible because we have a program inhouse that does store data in the file not more than a few KB extra.  Though I have no clue how its done, new to this.

0 Likes
Message 4 of 6

Anonymous
Not applicable

I came across these articles, but that property only stores data for one session, i need them to be persistent.

0 Likes
Message 5 of 6

Anonymous
Not applicable

I don't know what kind of data you want to store.

But please look at my example. With this function i create a new drawing and add an xrecord with my specified data.

 

[CommandMethod("NewDoc")]
        public void NewDoc()
        {
            Database acDbNewDoc = new Database();
            
            // get the editor object
            Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;

            using (Transaction tr = acDbNewDoc.TransactionManager.StartTransaction())
            {
                try
                {
                    // open the NOD for read
                    DBDictionary nod = tr.GetObject(acDbNewDoc.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
                    try
                    {
                        // check to see if our entry is in there, excpetion will be thrown if not so process that
                        // condition in the catch
                        ObjectId entryId = nod.GetAt(ACMEDATA);

                        // if we are here, then all is ok
                        // print the data
                        //ed.WriteMessage("This entity already has data...");
                        // ok extract the xrecord
                        Xrecord myXrecord = default(Xrecord);
                        // read it from the NOD dictionary
                        myXrecord = tr.GetObject(entryId, OpenMode.ForRead) as Xrecord;

                        // now print out the values
                        // Get data from Xrecord
                        ResultBuffer resBuf = myXrecord.Data;
                        TypedValue[] resbufvalue = resBuf.AsArray();
                        acadApp.ShowAlertDialog(string.Format("{0}, {1}, {2}", resbufvalue[0].Value, resbufvalue[1].Value, resbufvalue[2].Value));
                    }
                    catch
                    {
                        // upgrade to write status
                        nod.UpgradeOpen();

                        // create a new XRecord
                        Xrecord myXrecord = new Xrecord();
                        // create the resbuf list
                        ResultBuffer data = new ResultBuffer(new TypedValue((int)DxfCode.Text, "ACMEDATA1"),
                                                             new TypedValue((int)DxfCode.Text, "ACMEDATA2"),
                                                             new TypedValue((int)DxfCode.Text, "ACMEDATA3"));
                        // now add it to the xrecord
                        myXrecord.Data = data;

                        // create the entry
                        nod.SetAt(_dictionaryName, myXrecord);
                        // tell the transaction about the newly created xrecord
                        tr.AddNewlyCreatedDBObject(myXrecord, true);
                    }
                    // all ok, commit it
                    tr.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    // a problem occurred, lets print it
                    ed.WriteMessage("a problem occurred because " + ex.Message);
                }
                finally
                {
                    // whatever happens we must dispose the transaction
                }
                // Save the document
                acDbNewDoc.SaveAs(@"c:\temp\XrecDWG.dwg", DwgVersion.Current);
            }
        }

        public void ReadDocData(object sender, DocumentCollectionEventArgs e)
        {
            Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction tr = ed.Document.Database.TransactionManager.StartTransaction())
            {
                try
                {
                    // open the NOD for read
                    DBDictionary nod = tr.GetObject(ed.Document.Database.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;

                    // check to see if our entry is in there, excpetion will be thrown if not so process that
                    // condition in the catch
                    ObjectId entryId = nod.GetAt("ACMEDATA");

                    // if we are here, then all is ok
                    // print the data
                    // ok extract the xrecord
                    Xrecord myXrecord = default(Xrecord);
                    // read it from the NOD dictionary
                    myXrecord = tr.GetObject(entryId, OpenMode.ForRead) as Xrecord;

                    // now print out the values
                    // Get data from Xrecord
                    ResultBuffer resBuf = myXrecord.Data;
                    TypedValue[] resbufvalue = resBuf.AsArray();

                    ed.WriteMessage(string.Format("\n{0}, {1}, {2}", resbufvalue[0].Value, resbufvalue[1].Value, resbufvalue[2].Value));
                }
                catch
                {
                    ed.WriteMessage("\nThis is drawing doesn't have ACME data");
                }
            }
        }

 

 

0 Likes
Message 6 of 6

Anonymous
Not applicable

yeah, that's what i use, when i want to store texts and numbers, but i have a class containing several properties, including ArrayList and that ArrayList is made of custom objects too

I tried serialization, but the problem is, that AutoCAD classes like Point3d are not serializable.

0 Likes