c#查看扩展属性及添加扩展属性
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Cad_图元扩展属性读写
{
public static class Class1
{
//查看属性
[CommandMethod(“sx”)]
public static void Tst()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptEntityOptions opt = new PromptEntityOptions(“\n 请选择实体:”);
PromptEntityResult res = ed.GetEntity(opt);
if (res.Status == PromptStatus.OK)
{
Transaction tran = doc.TransactionManager.StartTransaction();
using (tran)
{
DBObject obj = tran.GetObject(res.ObjectId, OpenMode.ForRead);
ResultBuffer rb = obj.XData;
if (rb == null)
{
ed.WriteMessage("\n 该图元没有实体属性!");
}
else
{
StringBuilder Properties=new StringBuilder();//这里为了更好的展示用
foreach (TypedValue tv in rb)
{
switch (tv.TypeCode)
{
case 1001:
ed.WriteMessage("\n类型:{0};字段名称:{1}",
tv.TypeCode,//dxf组码
tv.Value//值
);
#region 存储属性
string Property_Field = string.Format("\n类型:{0};字段名称:{1}",
tv.TypeCode,//dxf组码
tv.Value//值
);
Properties.AppendLine(Property_Field);
#endregion
break;
case 1000:
ed.WriteMessage("\n类型:{0};字段内容:{1}",
tv.TypeCode,
tv.Value
);
#region 存储属性
string Property_Content = string.Format("\n类型:{0};字段内容:{1}",
tv.TypeCode,//dxf组码
tv.Value//值
);
Properties.AppendLine(Property_Content);
#endregion
break;
}
}
rb.Dispose();
global::System.Windows.Forms.MessageBox.Show(Properties.ToString());
}
}
}
}
//添加属性
[CommandMethod("add")]
public static void SetXData()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptEntityOptions opt = new PromptEntityOptions("\n 请选择实体:");
PromptEntityResult res = ed.GetEntity(opt);
if (res.Status == PromptStatus.OK)
{
PromptResult Fied_Name = ed.GetString("\n请输入字段名称:");
PromptResult Fied_Content = ed.GetString("\n请输内容:");
Transaction tran = doc.TransactionManager.StartTransaction();
using (tran)
{
DBObject obj = tran.GetObject(res.ObjectId, OpenMode.ForWrite);
if (obj.ObjectId.AddProperty(Fied_Name.StringResult, Fied_Content.StringResult))
{
ed.WriteMessage("属性添加成功!");
}
else
{
ed.WriteMessage("添加失败!");
}
tran.Commit();
}
}
}
//向实体添加扩展属性
public static bool AddProperty(this ObjectId EntityId, string FieldName, string Value = "")
{
bool returnValue = false;
TypedValue[] Properties = new TypedValue[]{
new TypedValue(1001, FieldName),
new TypedValue((int)DxfCode.ExtendedDataAsciiString, Value),
//new TypedValue((int)DxfCode.ExtendedDataAsciiString, "测试")//可以添加多行内容
};
ResultBuffer rb = new ResultBuffer(Properties); try
{
using (Database db = HostApplicationServices.WorkingDatabase)
{
using (Transaction tran = db.TransactionManager.StartTransaction())
{
RegAppTable rat = (RegAppTable)tran.GetObject(db.RegAppTableId, OpenMode.ForWrite);
RegAppTableRecord ratr;
ObjectId ratrid = ObjectId.Null;
if (rat.Has(FieldName))
{
ratrid = rat[FieldName];
}
else
{
ratr = new RegAppTableRecord();
ratr.Name = FieldName;
ratrid = rat.Add(ratr);
tran.AddNewlyCreatedDBObject(ratr, true);
}
Entity en;
en = (Entity)tran.GetObject(EntityId, OpenMode.ForWrite);
en.XData = rb;
tran.Commit();
returnValue = true;
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
returnValue = false;
}
return returnValue;
}
}
}