AutoCAD Civil 3D Customization

Reply
Contributor
Posts: 19
Registered: ‎04-18-2008
Message 1 of 3 (101 Views)

Showing a Form makes objects locked.

101 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: 3,962
Registered: ‎07-22-2003
Message 2 of 3 (93 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
Posts: 375
Registered: ‎06-27-2005
Message 3 of 3 (66 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

 

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Civil 3D Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for AutoCAD Civil 3D helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Civil 3D

Twitter

Facebook

Blogs