Search code examples
.netvb.netautocad-pluginobjectarx

How do I properly delete ACAD DBObjects?


Found this out on my own after finding some stuff online. Took me a while but for anyone who wants to know how to do it see below. Also, the code I posted had a big problem with it acText was never declared in it. I was also setting it equal to acadObj incorrectly. It needed to be set on a case to case basis. Below is the delete code I have. It has something to do with opening the working database editor.

Public Sub deleteDBObject(ByRef dbObj As DBObject)
    Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
    Dim db As Database = HostApplicationServices.WorkingDatabase
    Dim tm As Transaction = db.TransactionManager.StartTransaction()
    Try
        Dim ent As Entity = CType(tm.GetObject(dbObj.Id, OpenMode.ForWrite), Entity)
        ent.Erase()
        ent = Nothing
        dbObj = Nothing
        tm.Commit()
    Catch
    Finally
        tm.Dispose()
    End Try
    Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen()
End Sub

If Anyone knows how to do it a better way or if they can explain why this works that would be hot.


Solution

  • Sorry, but I don't understand. Since you already have a DBObject(dbObj), if it is a database resident object, call its Erase method (dbObj.Erase()); if it is not, just leave it alone and gc of .NET Framework will handle it.