.NET

Reply
Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 11 of 20 (423 Views)

Re: Save some variables into DWG file - how? C#.

09-12-2012 11:20 PM in reply to: _gile

Do you have some example of code for XRecords and DBDictionary?

 

I'm searching, but it hard to find what I need.

*Expert Elite*
_gile
Posts: 2,115
Registered: ‎04-29-2006
Message 12 of 20 (418 Views)

Re: Save some variables into DWG file - how? C#.

09-12-2012 11:55 PM in reply to: GrzesiekGP

Here're two little example to set and get xrecord data (ResultBuffer as for Xdata) in a named dictionary child of the root NamedObjectsDictionary (NOD).

        /// <summary>
        /// Add or edit a Xrecord data in a named dictionary (the dictionary and xrecord are created if not already exist)
        /// </summary>
        /// <param name="dictName">The dictionary name</param>
        /// <param name="key">the xrecord key</param>
        /// <param name="resbuf">the xrecord data</param>
        public void SetXrecord(string dictName, string key, ResultBuffer resbuf)
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary NOD = 
                    (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary dict;
                if (NOD.Contains(dictName))
                {
                    dict = (DBDictionary)tr.GetObject(NOD.GetAt(dictName), OpenMode.ForWrite);
                }
                else
                {
                    dict = new DBDictionary();
                    NOD.UpgradeOpen();
                    NOD.SetAt(dictName, dict);
                    tr.AddNewlyCreatedDBObject(dict, true);
                }
                Xrecord xRec = new Xrecord();
                xRec.Data = resbuf;
                dict.SetAt(key, xRec);
                tr.AddNewlyCreatedDBObject(xRec, true);
                tr.Commit();
            }
        }

        /// <summary>
        /// Gets an xrecord data in a named dictionary
        /// </summary>
        /// <param name="dictName">The dictionary name</param>
        /// <param name="key">The xrecord key</param>
        /// <returns>The xrecord data or null if the dictionary or the xrecord do not exist</returns>
        public ResultBuffer GetXrecord(string dictName, string key)
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary NOD = 
                    (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                if (!NOD.Contains(dictName))
                    return null;
                DBDictionary dict = tr.GetObject(NOD.GetAt(dictName), OpenMode.ForRead) as DBDictionary;
                if (dict == null || !dict.Contains(key))
                    return null;
                Xrecord xRec = tr.GetObject(dict.GetAt(key), OpenMode.ForRead) as Xrecord;
                if (xRec == null)
                    return null;
                return xRec.Data;
            }
        }

 

 

 

Gilles Chanteau
Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 13 of 20 (413 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 12:09 AM in reply to: _gile

Thank you very much for help.

 

Please tell me one more thinkg. Am I able to get specified TypedValue from ResultBuffer?

If not I suppose that the best way would be to put all values into dictionary<int,string>. Am I right?

*Expert Elite*
_gile
Posts: 2,115
Registered: ‎04-29-2006
Message 14 of 20 (408 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 12:20 AM in reply to: GrzesiekGP

As for Xdata you have to use a ResultBuffer to set data in a Xrecord and the Xrecord.Data property is a ResultBuffer.

Assuming your xrecord contains a single data of type String, you can get the data this way:

 

ResultBuffer resbuf = GetXrecord(theDictName, theXrecKey);

string theFirstData =(string) resbuf.AsArray()[0].Value;

Gilles Chanteau
Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 15 of 20 (404 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 12:53 AM in reply to: _gile

Thank you for the help :smileyhappy:

Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 16 of 20 (395 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 02:58 AM in reply to: _gile

Please let me know one more thing.

 

Am I able to get all keys from my dictionary?

 

I would to allow user to select from which Key in my dictionary user want to see his variables.

 

In example:

 

SetXrecord("myDict", "keysFirst", rb);

SetXrecord("myDict", "keysSecond", rb);

SetXrecord("myDict", "keysThird", rb);

 

And now I want to list: keysFirst,keysSecond,keysThird.

 

Ok, I've got it:

 List<string> _keys = new List<string>();
                foreach (DBDictionaryEntry entry in dict)
                {
                    _keys.Add(entry.Key);
                }
                return string.Join(",", _keys.ToArray());

 

 

Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 17 of 20 (380 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 11:15 AM in reply to: GrzesiekGP

One more to do :smileyhappy: Removing keys :smileytongue:

 

I have this code:

 

public static void RemoveXrecord(string dictName, string key)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            DocumentLock dl = doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true);
            using (dl)
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                    if (!NOD.Contains(dictName))
                        return;
                    DBDictionary dict = tr.GetObject(NOD.GetAt(dictName), OpenMode.ForWrite) as DBDictionary;
                    if (dict == null || !dict.Contains(key))
                        return;
                    dict.Remove(key);
                }
            }
        }

 But key still exist after removing :smileysad:

*Expert Elite*
_gile
Posts: 2,115
Registered: ‎04-29-2006
Message 18 of 20 (376 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 11:51 AM in reply to: GrzesiekGP

It looks like you forgot to call: tr.Commit();

Gilles Chanteau
Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 19 of 20 (371 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 11:56 AM in reply to: _gile

Lol, I'm blind :smileytongue:

 

Thank you!

Valued Contributor
RichardCammeray
Posts: 56
Registered: ‎12-08-2010
Message 20 of 20 (353 Views)

Re: Save some variables into DWG file - how? C#.

09-13-2012 04:15 PM in reply to: GrzesiekGP

Once you work with Dictionaries very useful is ArxDbd utility for testing.

http://otb.manusoft.com/2009/09/arxdbg-utility.htm

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!