Active Contributor
Posts: 38
Registered: ‎08-31-2006
Message 1 of 3 (75 Views)

Really basic select question

75 Views, 2 Replies
08-31-2006 12:50 PM
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()
dbobjObject = acadTrans.GetObject(presEntity.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)


If Not IsNothing(dbobjObject) Then
GetEntity = CType(dbobjObject, Entity)
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

acEntity = objModelLayout.GetEntity("Select object to change its layer: ")
Catch ex As Exception
Exit Sub
End Try

acEntity.Layer = "Layer1"
Catch ex As Exception
MsgBox("Layer Missing!")
End Try

End Sub
Distinguished Contributor
Posts: 1,691
Registered: ‎12-15-2003
Message 2 of 3 (75 Views)

Re: Really basic select question

08-31-2006 04:02 PM in reply to: ahmed.felix
You have committed and disposed of the transaction before you set the layer. Anything you do to the entity has to be done in the transaction.

Regards - Nathan
Active Contributor
Posts: 38
Registered: ‎08-31-2006
Message 3 of 3 (75 Views)

Re: Really basic select question

08-31-2006 05:51 PM in reply to: ahmed.felix
Thanks Nathan, there was a little more of work to do orginizing the transactions to get it running but what you said was pretty much it. I'm getting the hold of how this thing works.
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!