C# AutoCAD2010 编写扩展方法,包括扩展记录的添加、读取、删除、修改
时间: 2023-08-12 19:07:24 浏览: 171
CAD-CASS下实体的扩展属性XDATA.rar_CAD属性_Cass_c# xdata_cad 扩展属性_cadcass编
扩展方法是C#中一种特殊的静态方法,用于向现有类型添加新的方法,而不必继承或修改原始类型。在AutoCAD中,我们可以通过扩展方法来添加用于操作扩展记录的方法。下面是一个示例代码,展示如何编写扩展方法来添加、读取、删除、修改扩展记录。
首先,我们需要定义一个静态类来包含扩展方法:
```
public static class ExtensionMethods
{
// 添加扩展记录
public static void AddXRecord(this DBObject obj, string dictName, string recordName, ResultBuffer data)
{
Database db = obj.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 获取对象的扩展字典
DBDictionary dict = obj.ExtensionDictionary;
if (dict == null)
{
dict = new DBDictionary();
obj.ExtensionDictionary = dict;
trans.AddNewlyCreatedDBObject(dict, true);
}
// 获取扩展字典中的扩展记录
Xrecord xrec = new Xrecord();
xrec.Data = data;
ObjectId xrecId = dict.SetAt(recordName, xrec);
// 提交事务
trans.Commit();
}
}
// 读取扩展记录
public static ResultBuffer GetXRecord(this DBObject obj, string dictName, string recordName)
{
Database db = obj.Database;
ResultBuffer data = null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 获取对象的扩展字典
DBDictionary dict = obj.ExtensionDictionary;
if (dict != null && dict.Contains(recordName))
{
// 获取扩展记录
Xrecord xrec = (Xrecord)trans.GetObject(dict.GetAt(recordName), OpenMode.ForRead);
data = xrec.Data;
}
// 提交事务
trans.Commit();
}
return data;
}
// 删除扩展记录
public static bool RemoveXRecord(this DBObject obj, string dictName, string recordName)
{
Database db = obj.Database;
bool success = false;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 获取对象的扩展字典
DBDictionary dict = obj.ExtensionDictionary;
if (dict != null && dict.Contains(recordName))
{
// 删除扩展记录
success = dict.Remove(recordName);
}
// 提交事务
trans.Commit();
}
return success;
}
// 修改扩展记录
public static bool ModifyXRecord(this DBObject obj, string dictName, string recordName, ResultBuffer newData)
{
Database db = obj.Database;
bool success = false;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 获取对象的扩展字典
DBDictionary dict = obj.ExtensionDictionary;
if (dict != null && dict.Contains(recordName))
{
// 获取扩展记录
Xrecord xrec = (Xrecord)trans.GetObject(dict.GetAt(recordName), OpenMode.ForWrite);
xrec.Data = newData;
success = true;
}
// 提交事务
trans.Commit();
}
return success;
}
}
```
接下来,我们可以在AutoCAD中使用这些扩展方法来操作扩展记录。例如:
```
// 获取当前图形中的第一个实体
Entity ent = null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (ObjectId id in btr)
{
ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
break;
}
trans.Commit();
}
// 添加扩展记录
ResultBuffer data = new ResultBuffer(new TypedValue(5003, "MyData"));
ent.AddXRecord("MyDict", "MyRecord", data);
// 读取扩展记录
ResultBuffer data2 = ent.GetXRecord("MyDict", "MyRecord");
// 删除扩展记录
bool success = ent.RemoveXRecord("MyDict", "MyRecord");
// 修改扩展记录
ResultBuffer newData = new ResultBuffer(new TypedValue(5003, "MyNewData"));
success = ent.ModifyXRecord("MyDict", "MyRecord", newData);
```
这样,我们就可以使用扩展方法来方便地操作扩展记录了。注意,扩展方法只能用于实例对象,不能用于静态对象。
阅读全文