Prevent deletion/erasing of entity

By Virupaksha Aithal

One way to avoid erasing of entity is to use object overrule. With object overrule, you can override the “Erase” functionality and stop users from erasing the object.

Below example avoids erasing of only those entities which has xdata with “ADS” application name. To add a xdata with “ADS” application name, refer DevBlog Using .NET API to Add and Remove XData @ http://adndevblog.typepad.com/autocad/2012/04/using-net-api-to-add-and-remove-xdata-.html#tp .

To use the code, first create few entities and add Xdata with “ADS” application name. Run command “eraseOverrule”, which adds an object overrule. The call to “SetXDataFilter()” will make sure overrule is effective only for the entities which has “ADS” application name in xdata.  Now try to deleted entities with xdata, the object overrule callback “Erase” will be called from which code return the “NotApplicable” value. This will force the AutoCAD not delete the entity.

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("eraseOverrule")]
static public void eraseOverrule()
{
    if (eraseRule == null)
 {
     eraseRule = new EraseOverrule();
 
     Overrule.AddOverrule(RXObject.GetClass(typeof(Entity)),
                                            eraseRule, true);
     Overrule.Overruling = true;
     eraseRule.SetXDataFilter("ADS");
 }
 else
 {
     Overrule.Overruling = false;
     Overrule.RemoveOverrule(RXObject.GetClass(typeof(Entity)),
                                                  eraseRule);
     eraseRule.Dispose();
     eraseRule = null;
 }
}

Comments

2 responses to “Prevent deletion/erasing of entity”

  1. Hi
    Throwing an exception destroys the Undo mechanism. Any remedy?

  2. This will be a little bit delayed answer, but maybe it will be helpful to someone else.
    Just add “if (dbObject.IsUndoing) return;” at the begining of the Erase method to run undo mechanism properly.
    public override void Erase(DBObject dbObject, bool erasing)
    {
    if (dbObject.IsUndoing) return;
    throw new Autodesk.AutoCAD.Runtime.Exception(
    Autodesk.AutoCAD.Runtime.ErrorStatus.NotApplicable);
    //base.Erase(dbObject, erasing);
    }

Leave a Reply to cincirCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading