AutoCAD Civil 3D Customization

AutoCAD Civil 3D Customization

Reply
Active Contributor
RodWing
Posts: 27
Registered: ‎04-18-2008
Message 1 of 3 (105 Views)

Showing a Form makes objects locked.

105 Views, 2 Replies
03-21-2012 03:30 PM

I checked off the solution on my previous post too soon. This really shouldn't be that difficult, but this has still got me stumped.

 

I've posted another code sample below.

 

When I use the sub StartMacro1 to launch my app everything appears to work fine. Using StartMacro2 displays a form with a single button whose click event calls the ProcessAllAlignments function. Calling the function via the button's click event  causes the trans.GetObject(objID, OpenMode.ForWrite) statement to throw a Locked expection.

 

Any ideas why that is happening?

 

I would also like to perform a trans.Commit() after each alignment object is updated rather than at the end of the For loop. I get an access violation and Civil 3D crashes on me when I try to do that.

 

Thanks.

Rod

 

Public Sub StartMacro1()
ProcessAllAlignments()
End Sub

Public Sub StartMacro2()
myForm.Show()
End Sub

 

Public Function ProcessAllAlignments() As Boolean
Dim objCol As Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection
Dim objID As Autodesk.AutoCAD.DatabaseServices.ObjectId

Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database

 

ProcessAllAlignments = False

 

Try
objCol = CivilApplication.ActiveDocument.GetAlignmentIds()
Using trans As Transaction = db.TransactionManager.StartTransaction
For Each objID In objCol
Dim algNet As Alignment = trans.GetObject(objID, OpenMode.ForWrite)

 

If algNet.SuperelevationCriticalStations.Count > 0 Then
If Not UpdateSuper(algNet) Then
Exit Function
End If
End If
Next

 

trans.Commit()
trans.Dispose()
End Using

 

ProcessAllAlignments = True
Catch ex As Exception
MsgBox(ex.Message, vbExclamation)
End Try
End Function

 

*Expert Elite*
Jeff_M
Posts: 4,300
Registered: ‎07-22-2003
Message 2 of 3 (97 Views)

Re: Showing a Form makes objects locked.

03-21-2012 10:06 PM in reply to: RodWing

If you are using a modeless form then you will need to lock the document, this will prevent the eLockViolation. I don't use VB enough to be able to post sample code for you.

 

If you wish to commit after each alignment change, you will need to start a new transaction for each one.

 

 

Jeff_M, also a frequent Swamper
Mentor
MarkPendergraft
Posts: 375
Registered: ‎06-27-2005
Message 3 of 3 (70 Views)

Re: Showing a Form makes objects locked.

04-02-2012 09:16 AM in reply to: RodWing
    Public Sub StartMacro1()
        ProcessAllAlignments()
    End Sub

    Public Sub StartMacro2()
        myForm.Show()
    End Sub



    Public Function ProcessAllAlignments() As Boolean
        Dim objCol As Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection
        Dim objID As Autodesk.AutoCAD.DatabaseServices.ObjectId

        Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database

        ProcessAllAlignments = False

        Try
            objCol = CivilApplication.ActiveDocument.GetAlignmentIds()
            Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument


                Using trans As Transaction = db.TransactionManager.StartTransaction
                    For Each objID In objCol
                        Dim algNet As Alignment = trans.GetObject(objID, OpenMode.ForWrite)



                        If algNet.SuperelevationCriticalStations.Count > 0 Then
                            If Not UpdateSuper(algNet) Then
                                Exit Function
                            End If
                        End If
                    Next



                    trans.Commit()
                    trans.Dispose()
                End Using
            End Using

            ProcessAllAlignments = True
        Catch ex As Exception
            MsgBox(ex.Message, vbExclamation)
        End Try
    End Function

 

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.