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
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.
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