.NET

Reply
Member
sheldon1874
Posts: 4
Registered: ‎10-30-2013
Message 1 of 5 (238 Views)

WblockCloneObjects - best way to change layer when using

238 Views, 4 Replies
10-30-2013 07:22 AM

Hi,

First post - so be gentle.

All I am after doing is to clone all contents of a layer in one dwg to another dwg on a different layer.  What I have so far is a csv file with layersFrom,layersTo info.  Active doc with the destination layers, a closed dwg with all the objects.

Using Wblockcloneobjects I can get all the objects copied across to the active doc no problem its just the change layer to destination bit I need.

Do commit the transaction then open then obj and then change?

Member
sheldon1874
Posts: 4
Registered: ‎10-30-2013
Message 2 of 5 (224 Views)

Re: WblockCloneObjects - best way to change layer when using

10-30-2013 09:21 AM in reply to: sheldon1874

Here is my code so far:

 

Public Function SwapLayerContents(ByVal acdocDest As Document, ByVal fname As String, ByVal strSourceLayer As String, ByVal strDestLayer As String, ByVal bCopyBlocks As Boolean) As Boolean
        Dim bDone = False
        Dim acDestDB As Database = acdocDest.Database
        Dim acSourceBlockTable As BlockTable
        Dim acSourceModelSpace As BlockTableRecord
        Dim acDestBlockTable As BlockTable
        Dim acDestModelSpace As BlockTableRecord
        Dim acSourceLayerTable As LayerTable
        Dim acDestLayerTable As LayerTable
        Dim acObjID_New As ObjectIdCollection = New ObjectIdCollection()
        Dim acIDMap As IdMapping = New IdMapping()


        Try
    'Lock Doc
            Using acDocLock As DocumentLock = acdocDest.LockDocument()
        'DBase
                Using acSourceDB As New Database(False, True)
        'Start Trans
                    Using acSourceTrans As Transaction = acSourceDB.TransactionManager.StartTransaction()
                        acSourceDB.ReadDwgFile(fname, IO.FileShare.Read, True, "")
            'Start Trans
                        Using acDestTrans As Transaction = acDestDB.TransactionManager.StartTransaction()
                            
                acSourceBlockTable = acSourceTrans.GetObject(acSourceDB.BlockTableId, OpenMode.ForRead)
                                acSourceModelSpace = acSourceTrans.GetObject(acSourceBlockTable(BlockTableRecord.ModelSpace), OpenMode.ForRead)
                                acSourceLayerTable = acSourceTrans.GetObject(acSourceDB.LayerTableId, OpenMode.ForRead)
                                acDestBlockTable = acDestTrans.GetObject(acDestDB.BlockTableId, OpenMode.ForWrite)

                            acDestModelSpace = acDestTrans.GetObject(acDestBlockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                           acDestLayerTable = acDestTrans.GetObject(acDestDB.LayerTableId, OpenMode.ForRead)
                'Test Layers
                                If acSourceLayerTable.Has(strSourceLayer) And acDestLayerTable.Has(strDestLayer) Then

                                For Each acObjID As ObjectId In acSourceModelSpace

                                    Dim acEnt As Entity = acSourceTrans.GetObject(acObjID, OpenMode.ForRead)

                                    If acEnt.Layer = strSourceLayer Then

                                        acObjID_New.Add(acObjID)

                                    End If

                                Next acObjID

                                Dim myMap As New IdMapping

                'SDK docs seem to suggest that mapping can ignore the layer table here
                                acSourceDB.WblockCloneObjects(acObjID_New, acDestModelSpace.ObjectId, myMap, DuplicateRecordCloning.Ignore, False)

                                acDestTrans.Commit()

                                bDone = True
                            Else
                                bDone = False
                            End If
                        End Using
                    End Using
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Error  " & ex.Message & vbCrLf & vbTab & ex.StackTrace, MsgBoxStyle.Critical)
            bDone = False
        End Try


        Return bDone
    End Function

<CommandMethod("swapLayer")> _
    Public Sub swapLayer()
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim fname As String = "c:\FilesBin\Source.dwg"

        ed.WriteMessage(vbCr & "Result = ++{0}--", SwapLayerContents(doc, fname, "SUR_BANK_BOTTOM", "SUR_BANKS", False))

    End Sub

Member
sheldon1874
Posts: 4
Registered: ‎10-30-2013
Message 3 of 5 (207 Views)

Re: WblockCloneObjects - best way to change layer when using

10-30-2013 01:22 PM in reply to: sheldon1874

The simple answer is:

 

Using acDBObj As DBObject = acSourceTrans.GetObject(acObjID, OpenMode.ForWrite)
                                        Dim acEnt As Entity = CType(acDBObj, Entity)
                                        If acEnt.Layer = strDestLayer Then

                                            acEnt.Layer = strDestLayer
                                            acObjIDs.Add(acObjID)

                                        End If
                                    End Using

 

Valued Mentor
fieldguy
Posts: 376
Registered: ‎03-31-2005
Message 4 of 5 (199 Views)

Re: WblockCloneObjects - best way to change layer when using

10-30-2013 02:36 PM in reply to: sheldon1874

Your code doesn't change the layer.

 

If acEnt.Layer = strDestLayer Then acEnt.Layer = strDestLayer
                                           

Member
sheldon1874
Posts: 4
Registered: ‎10-30-2013
Message 5 of 5 (193 Views)

Re: WblockCloneObjects - best way to change layer when using

10-30-2013 03:37 PM in reply to: fieldguy

Thanks for helping.

 

It actually does though, it looks wrong in the snip at the end of post.  There are different transactions on the go and the code does what I need at run-time.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

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