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

VB.NET Adding multiple dwg files to a new drawing

0 REPLIES 0
Reply
Message 1 of 1
david_jamesM529C
222 Views, 0 Replies

VB.NET Adding multiple dwg files to a new drawing

I wonder if someone can help with the following:

Scenario: I have a folder with multiple .dwg files in it, (up to 40 in some cases). They need to be added to a new drawing file as layers with the end result being as in screenshot titled "ACad drawing grid view". I have code gleaned from this forum to add layers to the file but it it doesn't work. I am not sure whether I should be adding layers or blocks or whether is can be achieved at all using the API.

The code currently iterates through the .dwg files in the folder and

option 1 - extracts the layers from the drawing and runs a procedure to add them to the new drawing (result is nothing is added)

Private Shared Function GetLayersFromDrawing(ByVal sourceFile As String, curDoc As Document) As IList(Of LayerTableRecord)
If String.IsNullOrEmpty(sourceFile) Then Throw New ArgumentNullException("sourceFile")
If Not File.Exists(sourceFile) Then Throw New FileNotFoundException(sourceFile)
If curDoc Is Nothing Then Throw New ArgumentNullException("MdiActiveDocument")
Dim sourceLayers = New List(Of LayerTableRecord)()
Dim layer As LayerTableRecord

Try
Dim sourceDb = New Database(True, False)
sourceDb.ReadDwgFile(sourceFile, FileOpenMode.OpenForReadAndAllShare, False, Nothing)

Using trans = sourceDb.TransactionManager.StartOpenCloseTransaction()
Dim layerTable = CType(trans.GetObject(sourceDb.LayerTableId, OpenMode.ForRead), LayerTable)

For Each layerId In layerTable
layer = CType(trans.GetObject(layerId, OpenMode.ForRead), LayerTableRecord)
If layer.Name <> "0" Then
sourceLayers.Add(layer)
End If
Next

trans.Commit()
End Using

Catch acex As Autodesk.AutoCAD.Runtime.Exception
System.Diagnostics.Debug.Write(acex.Message)
'logger.LogErr("OPES.ERR", ex.Message, ex.StackTrace.ToString)
End Try

Return sourceLayers
End Function

 

Private Shared Sub AddLayersToCurrentDrawing(ByVal layerList As IList(Of LayerTableRecord), ByRef curDoc As Document, ByRef curDb As Database)
If layerList Is Nothing Then Throw New ArgumentNullException("layerList")
If layerList.Count = 0 Then Throw New ArgumentOutOfRangeException("layerList")
'Dim curDoc = Application.DocumentManager.MdiActiveDocument
If curDoc Is Nothing Then Throw New ArgumentNullException("MdiActiveDocument")
'Dim curDb = curDoc.Database

Try

Using trans = curDb.TransactionManager.StartOpenCloseTransaction()
Dim layerTable = CType(trans.GetObject(curDb.LayerTableId, OpenMode.ForWrite), LayerTable)

For Each layer In layerList
If layerTable.Has(layer.Name) Then Continue For
layerTable.Add(layer)
trans.AddNewlyCreatedDBObject(layer, True)
Next

trans.Commit()
End Using

Catch ex As Autodesk.AutoCAD.Runtime.Exception
System.Diagnostics.Debug.Write(ex.Message)
curDoc.Editor.WriteMessage(ex.Message)
End Try
End Sub

 

option 2 - extracts blocks and adds them to the new drawing file, (result is one set of layers is added, appears to be a random file in the folder, see "ACad block import result").

Public Sub ImportBlocks(doc As Document, srcFile As String)
Dim destDb As Database = doc.Database
Dim sourceDb As Database = New Database(False, True)

Try
sourceDb.ReadDwgFile(srcFile, System.IO.FileShare.Read, True, "")
Dim blockIds As ObjectIdCollection = New ObjectIdCollection()
Dim tm = sourceDb.TransactionManager

Using myT = tm.StartOpenCloseTransaction()
Dim bt As BlockTable = CType(myT.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, False), BlockTable)

For Each btrId As ObjectId In bt
Dim btr As BlockTableRecord = CType(myT.GetObject(btrId, OpenMode.ForRead, False), BlockTableRecord)
If Not btr.IsAnonymous AndAlso Not btr.IsLayout Then blockIds.Add(btrId)
btr.Dispose()
Next
End Using

Dim mapping = New IdMapping()
sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, False)

Catch ex As Autodesk.AutoCAD.Runtime.Exception
logger.LogErr("OPES.ERR", ex.Message)
End Try

sourceDb.Dispose()
End Sub

 

I hope I've explained this well enough. Would be grateful of some help here.

 

Many thanks.

Labels (4)
0 REPLIES 0

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report