//cad调用命令
[CommandMethod("ChangeColor")]
public void ChangeColor()
{
//获取操作的cad文档
Document doc=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//获取Cad中document中的数据
Database db = doc.Database;
//进行编辑操作,与cad操作交互
Editor ed = doc.Editor;
try
{
//提示用户选择对象
ObjectId id = ed.GetEntity("\n 请选择要改变颜色的对象").ObjectId;
//开启事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//以写的方式打开对对象
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
ent.ColorIndex = 200;
trans.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
switch (ex.ErrorStatus)
{
case ErrorStatus.InvalidIndex:
ed.WriteMessage("\n输入的颜色值有误");
break;
case ErrorStatus.InvalidObjectId:
ed.WriteMessage("\n请选择对象");
break;
default:
ed.WriteMessage(ex.ErrorStatus.ToString());
break;
}
}
}