本文为大家分享了AutoCAD .Net禁止图元被删除的具体代码,供大家参考,具体内容如下
禁止图元被用户删除可以通过 ObjectOverrule 实现。
以下代码:
1、命令 AddEraseOverrule 创建两个图元并禁止被删除。
2、命令 RemoveEraseOverrule 移除删除限制。
static EraseOverrule eraseRule = null;
public class EraseOverrule : ObjectOverrule
{
public override void Erase(DBObject dbObject, bool erasing)
{
throw new Autodesk.AutoCAD.Runtime.Exception(
Autodesk.AutoCAD.Runtime.ErrorStatus.NotApplicable);
//base.Erase(dbObject, erasing);
}
}
[CommandMethod("AddEraseOverrule")]
public static void AddEraseOverrule()
{
if (eraseRule == null)
{
eraseRule = new EraseOverrule();
Overrule.AddOverrule(RXObject.GetClass(typeof(Entity)),
eraseRule, true);
Overrule.Overruling = true;
eraseRule.SetIdFilter(CreateEntities());
}
}
[CommandMethod("RemoveEraseOverrule")]
public static void RemoveEraseOverrule()
{
if (eraseRule != null)
{
Overrule.Overruling = false;
Overrule.RemoveOverrule(RXObject.GetClass(typeof(Entity)),
eraseRule);
eraseRule.Dispose();
eraseRule = null;
}
}
private static ObjectId[] CreateEntities()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
ObjectId[] oids = new ObjectId[2];
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord space = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
//
Line line = new Line();
line.StartPoint = new Point3d(0, 0, 0);
line.EndPoint = new Point3d(100, 100, 0);
space.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
oids[0] = line.ObjectId;
//
Circle circle = new Circle();
circle.Center = new Point3d(0, 0, 0);
circle.Radius = 50;
space.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
oids[1] = circle.ObjectId;
tr.Commit();
}
return oids;
}
1、类 EraseOverrule 继承自 ObjectOverrule,并重写了 Erase 方法。
在该方法中,通过抛异常而不是执行删除操作,禁止图元被删除。
2、SetIdFilter 确保该规则只对特定 ObjectId 的图元有效。
ObjectOverrule 还有方法 SetXDataFilter,可以用来设置只对拥有特定 XData 的对象有效。
参考文章: Prevent deletion/erasing of entity
您可能感兴趣的文章:点击图片,AJAX删除后台图片文件的实现代码(asp.net)