以往的工作过程中,没有使用过删除属性的操作。上周一,遇到了这个问题。在网友的帮助下,终于解决。代码很简单,只是想不到啊。。。
正解代码为:
ResultBuffer rbDel = new ResultBuffer(
new TypedValue((int)DxfCode.ExtendedDataRegAppName, strAppName));
ent.XData = rbDel;
其中strAppName为需要删除的属性名称。
上述代码的含义为:定义一个需要删除的类型(typedvalue)对其只写入需要删除的属性名称,不予赋属性值。
用创建的typedvalue构造resultbuffer rb
将rb赋给实体的XData。即可删除xdata里面指定的属性名称。
下面是自己写的一个方法。用来删除实体属性
/// <summary>
/// 删除属性
/// </summary>
/// <param name="oid">实体主键</param>
/// <param name="arrayDelAppName">单个属性名称</param>
/// <returns>返回删除成功或失败的标记</returns>
public static bool DelAttribute(ObjectId oid, string strAppName)
{
bool blnSuccess = false;
try
{
using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
TypedValue tv = new TypedValue((int)DxfCode.ExtendedDataRegAppName, strAppName);
ResultBuffer rb = new ResultBuffer(tv);
Entity entTemp = (Entity)trans.GetObject(oid, OpenMode.ForWrite);
entTemp.XData = rb;
entTemp.Dispose();
trans.Commit();
blnSuccess = true;
}
}
catch (System.Exception ex)
{
return false;
}
return blnSuccess;
}
#region 删除属性
/// <summary>
/// 删除属性
/// </summary>
/// <param name="oid">实体主键</param>
/// <param name="arrayDelAppName">属性名称</param>
/// <returns>返回删除成功或失败的标记</returns>
private static bool DelAttribute(ObjectId oid, ArrayList arrayDelAppName)
{
bool blnSuccess = false;
try
{
using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
int intNum = arrayDelAppName.Count;
//StringBuilder sb = new StringBuilder();
//sb.Append("测试");
TypedValue[] tvs = new TypedValue[arrayDelAppName.Count];
for (int i = 0; i < arrayDelAppName.Count; i++)
{
tvs[i] = new TypedValue((int)DxfCode.ExtendedDataRegAppName, arrayDelAppName[i]);
}
ResultBuffer rb = new ResultBuffer(tvs);
Entity entTemp = (Entity)trans.GetObject(oid, OpenMode.ForWrite);
entTemp.XData = rb;
entTemp.Dispose();
trans.Commit();
blnSuccess = true;
}
}
catch (System.Exception ex)
{
return false;
}
return blnSuccess;
}
#endregion
/// <summary>
/// 删除属性
/// </summary>
/// <param name="oid">实体主键集合</param>
/// <param name="arrayDelAppName">属性名称</param>
/// <returns>返回删除成功或失败的标记</returns>
public static bool DelAttribute(ObjectIdCollection oidCollection, ArrayList arrayDelAppName)
{
bool blnSuccess = false;
try
{
using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
foreach (ObjectId oid in oidCollection)
{
int intNum = arrayDelAppName.Count;
TypedValue[] tvs = new TypedValue[arrayDelAppName.Count];
for (int i = 0; i < arrayDelAppName.Count; i++)
{
tvs[i] = new TypedValue((int)DxfCode.ExtendedDataRegAppName, arrayDelAppName[i]);
}
ResultBuffer rb = new ResultBuffer(tvs);
Entity entTemp = (Entity)trans.GetObject(oid, OpenMode.ForWrite);
entTemp.XData = rb;
entTemp.Dispose();
trans.Commit();
blnSuccess = true;
}
}
}
catch (System.Exception ex)
{
return false;
}
return blnSuccess;
}