I'm an experienced developer with VBA but am now moving everything to .NET, which I'm new to concerning AutoCAD (as of this week new) so this should be a pretty easy stuff that I can't get to work. I built a function that should return a reference to an object that I want to change some properties to, lets say the layer for example. The problem is, the object returns all right, I can read the ObjectId or anything else, but if I try to change the layer an eNotOpenForWrite error pops up and AutoCAD crashes. Why is this happening if I am opening it in a ForWrite mode? Am I perhaps writing a whole lot of nonesense in the GetEntity function that appears to work but really doesn't? I've tried this with existing objects, objects I draw at the moment, and objects created from code... no positive result. Could anyone point me in the right direction? Thanks.
Note: Both routines are in different projects each on it's own class, acadDB and acadTrans are previously declared in the class that GetEntity belongs to cause other functions use them as well. objModelLayout is an instance of the class that contains GetEntity. I am using Visual Studio 2005 and AutoCAD 2006.
Public Function GetEntity(ByVal Prompt As String) As Entity
Dim optEntityOptions As New PromptEntityOptions(vbCrLf & Prompt)
Dim presEntity As PromptEntityResult
Dim dbobjObject As DBObject = Nothing
Dim Command As Editor
Command = acadApp.DocumentManager.MdiActiveDocument.Editor
presEntity = Command.GetEntity(optEntityOptions)
acadDB = HostApplicationServices.WorkingDatabase
acadTrans = acadDB.TransactionManager.StartTransaction()
Try
dbobjObject = acadTrans.GetObject(presEntity.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
acadTrans.Commit()
Catch
acadTrans.Abort()
Finally
If Not IsNothing(dbobjObject) Then
GetEntity = CType(dbobjObject, Entity)
Else
GetEntity = Nothing
End If
If Not IsNothing(acadTrans) Then acadTrans.Dispose()
If Not IsNothing(acadDB) Then acadDB.Dispose()
End Try
End Function
_
Public Sub TestGetObject()
Dim acEntity As Entity
Dim Command As Editor
Command = acadApp.DocumentManager.MdiActiveDocument.Editor
Try
acEntity = objModelLayout.GetEntity("Select object to change its layer: ")
Catch ex As Exception
Exit Sub
End Try
Try
Command.WriteMessage(acEntity.ObjectId.ToString)
acEntity.Layer = "Layer1"
Catch ex As Exception
MsgBox("Layer Missing!")
End Try
End Sub