Hello,
could someone show me a very simple code-example, how to attach an extension dictionary to an AutoCAD Object und add a XRecord?
I will be grateful to you, if you would write your own examle and not copy one from the internet.
Best regards,
Hi,
Here're some extension methods from my library (but you shouldn't copy them as there're now on the internet).
Those methods have to be called from within an active transaction.
using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.Linq; namespace Gile.Extensions { public static class Extensions { public static T GetObject<T>(this ObjectId id) where T : DBObject { return (T)id.GetObject(OpenMode.ForRead); }
public static T GetObject<T>(this ObjectId id, OpenMode mode) where T : DBObject
{
return (T)id.GetObject(mode);
} public static void SetXrecordData(this DBDictionary dict, string key, params TypedValue[] values) { dict.SetXrecordData(key, new ResultBuffer(values)); } public static void SetXrecordData(this DBDictionary dict, string key, ResultBuffer data) { Xrecord xrec; if (dict.Contains(key)) { xrec = ((ObjectId)dict[key]).GetObject<Xrecord>(OpenMode.ForWrite); } else { dict.UpgradeOpen(); xrec = new Xrecord(); dict.SetAt(key, xrec); dict.Database.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(xrec, true); } xrec.Data = data; } public static DBDictionary GetOrCreateExtensionDictionary(this DBObject obj) { if (obj.ExtensionDictionary == ObjectId.Null) { obj.UpgradeOpen(); obj.CreateExtensionDictionary(); } return obj.ExtensionDictionary.GetObject<DBDictionary>(); } public static void SetXDictionaryXrecordData(this DBObject obj, string key, params TypedValue[] values) { obj.SetXDictionaryXrecordData(key, new ResultBuffer(values)); } public static void SetXDictionaryXrecordData(this DBObject obj, string key, ResultBuffer data) { obj.GetOrCreateExtensionDictionary().SetXrecordData(key, data); } } }
If you were happy to use code already posted in public, then I'd also point you to the .NET Training Labs on www.autodesk.com/developautocad. (Note that this page is teporarily offline today - come back tomorrow if you get a page not found error.