.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with WblockCloneObject - AutoCAD 2014, vb.net

3 REPLIES 3
Reply
Message 1 of 4
RodWing
911 Views, 3 Replies

Problem with WblockCloneObject - AutoCAD 2014, vb.net

I'm using Civil 3D 2014 to return an objectID collection of polylines from the active document. Once I have the collection I am passing it along with the document database to the function below. The function opens an existing document and then performs a WblockCloneObject of the polylines into the newly opened document.

 

This function works about have of the the time. The other half I get an Access Violation when executing the WblockCloneObject statement. I've included the link to the Autodesk source document from which I adapted this function.

 

Anyone have any ideas or suggestions?

 

Thanks,

Rod

 

 

 ''''''''''''''''''''''''''''''''''''''''''
    ' Adapted from: http://docs.autodesk.com/ACD/2013/ENU/index.html?url=files/GUID-E02A8AAF-61FF-4C72-8960-0AEEBBEC2594...
    '
    ' objIDCol is created from Civil 3D Corridor.Baseline.FeatureLineMap.FeatureLineCollection.FeatureLine.ExportAsPolyline3dCollection()
    '
    Public Function CopyObjectsToNewFile(ByVal outFileName As String, ByRef docSourceDB As Database, ByRef objIDCol As Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection) As Boolean
        Dim docMgr As DocumentCollection = Application.DocumentManager
        Dim docDest As Document
        Dim docDestDB As Database

        CopyObjectsToNewFile = False

        If IsNothing(docSourceDB) Then
            MsgBox("Source document database is nothing")
            Exit Function
        End If

        docDest = DocumentCollectionExtension.Open(docMgr, outFileName, False)
        If IsNothing(docDest) Then
            MsgBox("Cannot open " & outFileName)
            Exit Function
        End If

        docDestDB = docDest.Database

        '' Lock the new document
        Using acLckDoc As DocumentLock = docDest.LockDocument()

            '' Start a transaction in the new database
            Using acTrans = docDest.TransactionManager.StartTransaction()

                '' Open the Block table for read
                Dim acBlkTblNewDoc As BlockTable
                acBlkTblNewDoc = acTrans.GetObject(docDestDB.BlockTableId, _
                                                   OpenMode.ForRead)

                '' Open the Block table record Model space for read
                Dim acBlkTblRecNewDoc As BlockTableRecord
                acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace), _
                                                      OpenMode.ForRead)

                '' Clone the objects to the new database
                Dim acIdMap As IdMapping = New IdMapping()

                docSourceDB.WblockCloneObjects(objIDCol, acBlkTblRecNewDoc.ObjectId, acIdMap, _
                                           DuplicateRecordCloning.Ignore, False)


                '' Save the copied objects to the database
                acTrans.Commit()
            End Using

            '' Unlock the document
        End Using

        CopyObjectsToNewFile = True
    End Function
    '
    ''''''''''''''''''''''''''''''''''''''''''

 

3 REPLIES 3
Message 2 of 4
Balaji_Ram
in reply to: RodWing

Hi Rod,

 

I have tried the following code snippet and it worked ok in AutoCAD 2014. I do not have Civil3D installed in my system, so haven't tried it in that.

 

Can you please try this code snippet in Civil3D and see if the crash is reproducible ? The entities to be cloned to the new database can be selected when prompted. 

 

        Imports Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension

        <CommandMethod("CopyObjectsBetweenDatabases", CommandFlags.Session Or CommandFlags.UsePickSet)> _
        Public Sub CopyObjectsBetweenDatabases()
            Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()

            '' Get the current document and database
            Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim acCurDb As Database = acDoc.Database
            Dim acEd As Editor = acDoc.Editor

            Dim selectionRes As PromptSelectionResult
            selectionRes = acEd.SelectImplied

            ' If there's no pickfirst set available...
            If selectionRes.Status.Equals(PromptStatus.Error) Then
                ' Ask the user to select entities
                Dim selectionOpts As PromptSelectionOptions = New PromptSelectionOptions
                selectionOpts.MessageForAdding = vbLf & "Select entities to copy : "
                selectionRes = acEd.GetSelection(selectionOpts)
            End If

            ' If the user has not cancelled...
            If selectionRes.Status.Equals(PromptStatus.OK) Then
                Dim SelSet As SelectionSet = selectionRes.Value()

                ' Add all the objects to copy to the new document
                For Each ID As ObjectId In SelSet.GetObjectIds
                    acObjIdColl.Add(ID)
                Next
            End If

            If acObjIdColl.Count = 0 Then
                Return
            End If

            '' Change the file and path to match a drawing template on your workstation
            Dim sLocalRoot As String = Application.GetSystemVariable("LOCALROOTPREFIX")
            Dim sTemplatePath As String = sLocalRoot + "Template\acad.dwt"

            '' Create a new drawing to copy the objects to
            Dim acDocMgr As DocumentCollection = Application.DocumentManager
            Dim acNewDoc As Document = acDocMgr.Add(sTemplatePath)
            Dim acDbNewDoc As Database = acNewDoc.Database

            '' Lock the new document
            Using acLckDoc As DocumentLock = acNewDoc.LockDocument()

                '' Start a transaction in the new database
                Using acTrans = acDbNewDoc.TransactionManager.StartTransaction()

                    '' Open the Block table for read
                    Dim acBlkTblNewDoc As BlockTable
                    acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId, _
                                                       OpenMode.ForRead)

                    '' Open the Block table record Model space for read
                    Dim acBlkTblRecNewDoc As BlockTableRecord
                    acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace), _
                                                          OpenMode.ForRead)

                    '' Clone the objects to the new database
                    Dim acIdMap As IdMapping = New IdMapping()
                    acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap, _
                                               DuplicateRecordCloning.Ignore, False)

                    '' Save the copied objects to the database
                    acTrans.Commit()
                End Using

                '' Unlock the document
            End Using

            '' Set the new document current
            acDocMgr.MdiActiveDocument = acNewDoc
        End Sub

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Message 3 of 4
RodWing
in reply to: Balaji_Ram

Thanks Balaji,

 

This does not work for me. I've inserted the snippet from the first problem I'm running in to. The acDocMgr.add method raises an error. There is no Add member of Autodesk.AutoCAD.ApplicationServices.DocumentCollection.

 

        '' Create a new drawing to copy the objects to
        Dim acDocMgr As DocumentCollection = Application.DocumentManager
        Dim acNewDoc As Document = acDocMgr.add(sTemplatePath)

 

Message 4 of 4
Balaji_Ram
in reply to: RodWing

Hi Rod,

 

Does your code have this import statement ?

 

"Imports Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension"

 

I have attached the sample project that works ok in AutoCAD 2014.

Please try this and let me know if you still have the crash.

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost