AutoCAD .Net 使用扩展字典存储自定义数据

来源:互联网 发布:淘宝服装平铺拍摄 编辑:程序博客网 时间:2024/06/05 03:46

每个 AutoCAD 数据库元素对象(DBObject)都可以使用扩展字典来存储自定义数据。
通常这一机制用来为图元对象(比如:线、圆等)存储非图形数据。
以下示例代码实现:
AddXRecordToEntity: 让用户选择一图元,然后在图元的扩展字典中存储自定义数据。
GetXRecordFromEntity: 让用户选择一图元,读取图元的扩展字典中存储的自定义数据。

[CommandMethod("AddXRecordToEntity")]public void AddXRecordToEntity(){    Document doc = Application.DocumentManager.MdiActiveDocument;    Database db = doc.Database;    PromptEntityResult per = doc.Editor.GetEntity("\nSelect an entity: ");    if (per.Status != PromptStatus.OK)    {        return;    }    using (Transaction tr = db.TransactionManager.StartTransaction())    {        Entity entity = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Entity;        if (entity.ExtensionDictionary == ObjectId.Null)        {            entity.CreateExtensionDictionary();        }        DBDictionary xDict = tr.GetObject(entity.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;        if (!xDict.Contains("CAXDEV"))        {            xDict.UpgradeOpen();            Xrecord xRec = new Xrecord();            ResultBuffer rb = new ResultBuffer();            rb.Add(new TypedValue((int)DxfCode.Text, "Hello www.caxdev.com"));            rb.Add(new TypedValue((int)DxfCode.Int32, 123));            rb.Add(new TypedValue((int)DxfCode.Real, 1.2345));            xRec.Data = rb;            xDict.SetAt("CAXDEV", xRec);            tr.AddNewlyCreatedDBObject(xRec, true);        }        tr.Commit();    }}[CommandMethod("GetXRecordFromEntity")]public void GetXRecordFromEntity(){    Document doc = Application.DocumentManager.MdiActiveDocument;    Database db = doc.Database;    PromptEntityResult per = doc.Editor.GetEntity("\nSelect an entity: ");    if (per.Status != PromptStatus.OK)    {        return;    }    using (Transaction tr = db.TransactionManager.StartTransaction())    {        Entity entity = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;        if (entity.ExtensionDictionary != ObjectId.Null)        {            DBDictionary xDict = tr.GetObject(entity.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;            if (xDict.Contains("CAXDEV"))            {                ObjectId xRecId = xDict.GetAt("CAXDEV");                Xrecord xRec = tr.GetObject(xRecId, OpenMode.ForRead) as Xrecord;                if (xRec == null)                    return;                foreach (TypedValue tv in xRec.Data)                {                    doc.Editor.WriteMessage("\nTypeCode: {0}; Value: {1}", tv.TypeCode, tv.Value);                }            }        }    }}

参考文章:

Adding extension dictionary
Using .NET in the Land of NOD

原创粉丝点击