对于实体对象只有几何信息,如果你的形状表达的是某些有其他参数信息的对象,如表现建筑对象的梁,可能要附加材料信息,那么我们可以将这些附加的信息加入到几何对象的扩展数据中,目前可以给对象添加附加的数据有Xdata、Xrecord、以及DataTable,附着到对象的做法都差不多但是Xdata存储数据量有限,Xrecord储存及读取的时候条理性比较差,所以用DataTable作为扩展数据最为合适。添加扩展数据时必须注意的是对象必须添加到数据库中后才能添加扩展数据。- /////////////////////////////////////////////////////////////////////////////////////////////////
- //http://www.bimcad.org 数字建筑
- //深入浅出AutoCAD二次开发(李冠亿)
- /////////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(Sample.EntityData))]
- namespace Sample
- {
- class EntityData
- {
- [CommandMethod("WriteData")]
- public void AddData()
- {
- DataTable dt = new DataTable();
- dt.TableName = "ParameterTable";
- dt.AppendColumn(CellType.CharPtr, "Name");
- dt.AppendColumn(CellType.CharPtr, "Meterial");
- dt.AppendColumn(CellType.CharPtr, "Parameter");
- DataCellCollection Row = new DataCellCollection();
- DataCell Name = new DataCell();
- DataCell Meterial = new DataCell();
- DataCell Parameter = new DataCell();
- Name.SetString("工字钢");
- Meterial.SetString("Q235B");
- Parameter.SetString("200*200*32*25");
- Row.Add(Name);
- Row.Add(Meterial);
- Row.Add(Parameter);
- dt.AppendRow(Row, true);
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptEntityResult ent = ed.GetEntity("\n选择要写数据的对象");
- if (ent.Status == PromptStatus.OK)
- {
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Entity entity = (Entity)tr.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
- if (entity.ExtensionDictionary == new ObjectId())
- entity.CreateExtensionDictionary();
- DBDictionary extensionDic = (DBDictionary)tr.GetObject(entity.ExtensionDictionary, OpenMode.ForWrite, false);
- extensionDic.SetAt("ParameterTable", dt);
- tr.AddNewlyCreatedDBObject(dt, true);
- tr.Commit();
- }
- }
- }
- [CommandMethod("ReadData")]
- public void ReadData()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Entity entity = null;
- PromptEntityResult ent = ed.GetEntity("\n选择要读取数据的对象");
- if (ent.Status == PromptStatus.OK)
- {
- using (Transaction transaction = db.TransactionManager.StartTransaction())
- {
- entity = (Entity)transaction.GetObject(ent.ObjectId, OpenMode.ForRead, true);
- DBDictionary extensionDic = transaction.GetObject(entity.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;
- DataTable dt = transaction.GetObject(extensionDic.GetAt("ParameterTable"), OpenMode.ForRead) as DataTable;
- ed.WriteMessage("\n Name:" + dt.GetCellAt(0, 0).Value.ToString());
- ed.WriteMessage("\n Meterial:" + dt.GetCellAt(0, 1).Value.ToString());
- ed.WriteMessage("\n Parameter:" + dt.GetCellAt(0, 2).Value.ToString());
- transaction.Commit();
- }
- }
- }
- }
- }
复制代码运行”WriteData”命令选择要写扩展数据的对象:
再运行“ReadData“选择要读取扩展数据的对象,命令行中则显示对象的扩展数据:
|