Jeff's answer is correct.
However, before you can call DBObject.Erase(false), you need to make sure you call correct overriden Transaction.GetObject() (e.g. with a third boolean argument passed in as "true").
Here is code showing that:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(EraseAndUnerase.MyCadCommands))]
namespace EraseAndUnerase
{
public class MyCadCommands
{
[CommandMethod("TestErase")]
public static void RunMyCadCommand()
{
Document dwg = CadApp.DocumentManager.MdiActiveDocument;
if (dwg == null) return;
Editor ed = dwg.Editor;
try
{
PromptSelectionResult res = ed.GetSelection();
if (res.Status == PromptStatus.OK)
{
ObjectId[] ids = res.Value.GetObjectIds();
EraseEntities(dwg, ids, true);
ed.UpdateScreen();
ed.GetString(
"\nAll selected entities have been erased.\nPress any key to unerase them...");
EraseEntities(dwg, ids, false);
ed.UpdateScreen();
}
}
catch (System.Exception ex)
{
ed.WriteMessage("\nError: {0}\n{1}", ex.Message, ex.StackTrace);
}
finally
{
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
}
private static void EraseEntities(Document dwg, ObjectId[] entIds, bool erasing)
{
using (var tran=dwg.TransactionManager.StartTransaction())
{
foreach (var id in entIds)
{
Entity ent;
if (erasing)
{
if (!id.IsErased)
{
ent = (Entity)tran.GetObject(id, OpenMode.ForWrite);
ent.Erase(); //or ent.Erase(true);
}
}
else
{
if (id.IsErased)
{
ent = (Entity)tran.GetObject(id, OpenMode.ForWrite, true);
ent.Erase(false);
}
}
}
tran.Commit();
}
}
}
}