The method of AutoCAD. Net forbidding entity to be deleted


In this paper, we share the specific code of AutoCAD. Net forbidding the deletion of entities for your reference. The specific content is as follows

Forbidding elements from being deleted by users can be implemented through objectoverride.

The following codes:

1. Command adderaseoverrule to create two elements and prohibit them from being deleted.
2. Command removeeraseoverrule to remove the deletion limit.

static EraseOverrule eraseRule = null;

public class EraseOverrule : ObjectOverrule
 public override void Erase(DBObject dbObject, bool erasing)
  throw new Autodesk.AutoCAD.Runtime.Exception(
  //base.Erase(dbObject, erasing);

public static void AddEraseOverrule()
 if (eraseRule == null)
  eraseRule = new EraseOverrule();
        eraseRule, true);
  Overrule.Overruling = true;


public static void RemoveEraseOverrule()
 if (eraseRule != null)
  Overrule.Overruling = false;
  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);
  tr.AddNewlyCreatedDBObject(line, true);

  oids[0] = line.ObjectId;

  Circle circle = new Circle();
  circle.Center = new Point3d(0, 0, 0);
  circle.Radius = 50;
  tr.AddNewlyCreatedDBObject(circle, true);

  oids[1] = circle.ObjectId;


 return oids;

1. The class eraseoverrule inherits from objectoverrule and rewrites the erase method.
In this method, the element cannot be deleted by throwing exception instead of deleting.

2. Setidfilter ensures that the rule is only valid for specific objectid elements.
Objectoverride also has the method setxdatafilter, which can be used to set that only objects with a specific XDATA are valid.

Reference article: prevent deletion / erasing of entity

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.