TheSwamp

Code Red => .NET => Topic started by: Andrey Bushman on November 12, 2014, 08:08:55 AM

Title: DBObject.Erase Method
Post by: Andrey Bushman on November 12, 2014, 08:08:55 AM
DBObject.Erase method has the overloaded version (with the boolean parameter):

Code - C#: [Select]
  1. public void Erase();
  2. public void Erase([MarshalAs(UnmanagedType.U1)] bool erasing);
Quote from: SDK text
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)?

Best regards.
Title: Re: DBObject.Erase Method
Post by: Jeff H on November 12, 2014, 09:42:12 AM

Quote
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.

I think passing in false sets the erase bit back to false and will be filed out the database.
Title: Re: DBObject.Erase Method
Post by: n.yuan on November 12, 2014, 09:53:58 AM
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:

Code: [Select]
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();
            }
        }
    }
}