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?
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
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
Your code doesn't change the layer.
If acEnt.Layer = strDestLayer Then acEnt.Layer = strDestLayer
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.