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;
}
}

Leave a Reply