Boolean indicating if object is to be erased or unerased.I not understand why need the overloaded version. Is the same Erase() and Erase(true)? When I must to call Erase(false)?
This does not remove the object from the database, nor from memory. However, if the erase bit is set when the database is saved or output to DXF, the object is not filed out and thus is not present the next time the file is opened.
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();
}
}
}
}