.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Erase and entity

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
617 Views, 2 Replies

Erase and entity

I am trying to erase an entity, in this case a solid. When i run the code the no errors occur and i see the entity disapear, but then reapear a few seconds later. Below is my code

{code}
Public Shared Function DeleteObj(ByVal HandleStr As String) As Boolean

Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim dwg As Database = ed.Document.Database
Dim trans As Transaction = dwg.TransactionManager.StartTransaction()

'lock drawing
Dim dl As DocumentLock
dl = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()
Using (dl)
Try
'use handleto objectid
Dim hand As New Autodesk.AutoCAD.DatabaseServices.Handle(Convert.ToInt64(HandleStr))
Dim ObjectId As ObjectId
ObjectId = dwg.GetObjectId(False, hand, 0)
trans = dwg.TransactionManager.StartTransaction()
'get ent and erase it
Dim ent As Entity = trans.GetObject(ObjectId, OpenMode.ForWrite, True, True)
ent.Erase(True)
trans.Commit()
Catch ex As Exception
trans.Abort()
Return False
Finally
trans.Dispose()
End Try
End Using
Return True
End Function
{code}
2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: Anonymous

If you used the code exactly as you showed, then let guess the possible
source of the problem:

In your code you declared the Transaction:
...
Dim dwg As Database = ed.Document.Database
Dim trans As Transaction = dwg.TransactionManager.StartTransaction()
...

That means, you only decleared it, but also started it.

Then isn your Using...End Using block,

you did:

trans = dwg.TransactionManager.StartTransaction()

That is, you STARTED another Transaction, which is nested inside of the
first Transaction, and you did commited this second one and disposed it.

However, the first one is never been commited.

If it is pure .NET stuff, a object (Transaction) that declared in s code
group (Function in this case) should automaticallly go out of scope and
ready be gabage-collected without need to call Dispose() explicitly.
However, AutoCAD .NET API is not pure .NET stuff, and we need to make sure
some thing to be Dispose()-ed explicitly, such as Transaction (using
"Using...End Using" ensures it, so there is no need to call Dispose() inside
the "Using...End Using block, BTW).

One would say, since the variable "trans" has been asigned to the second
Transaction, the first one would have no reference pointing to it, thus it
would have been destroyed. Well, if it is COM object, it would be the case,
but with Acad .NET API object, which is kind of .NET wrapper on top of
ObjectARX object, I have no idea what kind of resources the Transaction
object involves, thus not sure what really happens when you start a
Transaction, then leave it alone withot either commited or rolled back or
without explicily disposed. I just guess this started/but never finished
Transaction might be the cause of your problem.

So, simply change declaration to

Dim trans As Transaction



wrote in message news:6265340@discussion.autodesk.com...
I am trying to erase an entity, in this case a solid. When i run the code
the no errors occur and i see the entity disapear, but then reapear a few
seconds later. Below is my code

{code}
Public Shared Function DeleteObj(ByVal HandleStr As String) As Boolean

Dim ed As Editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim dwg As Database = ed.Document.Database
Dim trans As Transaction = dwg.TransactionManager.StartTransaction()

'lock drawing
Dim dl As DocumentLock
dl =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()
Using (dl)
Try
'use handleto objectid
Dim hand As New
Autodesk.AutoCAD.DatabaseServices.Handle(Convert.ToInt64(HandleStr))
Dim ObjectId As ObjectId
ObjectId = dwg.GetObjectId(False, hand, 0)
trans = dwg.TransactionManager.StartTransaction()
'get ent and erase it
Dim ent As Entity = trans.GetObject(ObjectId,
OpenMode.ForWrite, True, True)
ent.Erase(True)
trans.Commit()
Catch ex As Exception
trans.Abort()
Return False
Finally
trans.Dispose()
End Try
End Using
Return True
End Function
{code}
Message 3 of 3
Anonymous
in reply to: Anonymous

Thanks Norman for the Great Explanation! Works great! It always helps to have another set of eyes look at your code.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost