AutoCAD.Net prohibits deletion of primitives

Preventing primitives from being deleted by the user can be achieved through ObjectOverrule.
The following code:
1. The command AddEraseOverrule creates two primitives and prohibits deletion.
2. Command RemoveEraseOverrule to remove deletion restrictions.

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 overrides the Erase method.
In this method, deletion of primitives is prohibited by throwing exceptions instead of deleting them.

2. SetIdFilter ensures that the rule is valid only for the primitives of a particular ObjectId.
ObjectOverrule also has a method, SetXDataFilter, that can be used to set up to be valid only for objects that have a specific XData.

Reference article:
Prevent deletion/erasing of entity

Keywords: Database

Added by dasding on Sat, 30 May 2020 19:53:25 +0300