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

WblockCloneObjects into a layout

0 REPLIES 0
Reply
Message 1 of 1
MarkPendergraft
515 Views, 0 Replies

WblockCloneObjects into a layout

I am writing a program that wblockcloneobjects into a drawing. First it clones modelspace, then it checks to see if any layouts exist with the same name, if they don't exist, it clones the entire layout into the layout dictionary. If the layout already exists then i want to loop through the objects in the layout and use the wblockcloneobjects() method to insert them into the existing layout. My code runs without errors, yet the objects in the existing layouts do not come across.

i'm not sure what i'm doing wrong here, i am getting the correct objects from the source drawing, but they just aren't getting inserted into the new layout. i have tried many different methods of getting the id of the layout, and have even set it current using the layoutManager and then called SymbolUtilityServices.GetBlockPaperspaceId().

The idea here is to block an old Land Desktop drawing into a new Civil 3D drawing in order to have our styles from our drawing template show up in an old Land Desktop drawing.

The code for this program is as follows:

{code}

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Windows

Public Sub ConvertLDDdrawing()

'get the document manager
Dim dm As DocumentCollection = Application.DocumentManager
Dim sourceDb As New Database(False, True) 'create the source database here so we can dispose of it if something goes wrong
Dim newDb As Database

Try

'get the drawing to be inserted as a block from the user
Dim openFileDia As New Autodesk.AutoCAD.Windows.OpenFileDialog("Open LDD Drawing", "", "dwg", "Open LDD Drawing", OpenFileDialog.OpenFileDialogFlags.SearchPath)
Dim diagResult As System.Windows.Forms.DialogResult = openFileDia.ShowDialog()

'exit the sub if the dialog result returns a response other than ok
If Not diagResult = Windows.Forms.DialogResult.OK Then Exit Sub

Dim sourceDocName As String = openFileDia.Filename

'create a new doc from the template
'Dim newDoc As Document = dm.Add("C:\civil 3d project templates\mga.dwt")

'get the current database
newDb = HostApplicationServices.WorkingDatabase

'open the source database hidden from the end user
sourceDb.ReadDwgFile(sourceDocName, System.IO.FileShare.Read, True, "")

'create the object id collections which we will add to the new drawing
Dim msIds As New ObjectIdCollection()
Dim psIds As New ObjectIdCollection()

'create variables to hold a list of the layouts and their objectIds in the current drawing in case they are replicated
Dim newLayoutList As New System.Collections.Generic.List(Of String)
Dim newLayoutListIds As New ObjectIdCollection()
Dim sourceLayoutList As New System.Collections.Generic.List(Of String)
Dim sourceLayoutListIds As New ObjectIdCollection()

Dim layoutMan As LayoutManager = LayoutManager.Current

'open the source drawing's transaction manager and start a new transaction
Dim sourceTM As Autodesk.AutoCAD.DatabaseServices.TransactionManager = sourceDb.TransactionManager
Using sourceTrans As Transaction = sourceTM.StartTransaction

'get all the obejects in modelspace from the source drawing
Dim sourceMS As BlockTableRecord = sourceTrans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(sourceDb), OpenMode.ForRead, False)
For Each btrId As ObjectId In sourceMS
msIds.Add(btrId)
Next

'open the new drawings transaction manager, start a transaction and get the layouts currently in the drawing and their ids
Dim newTm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = newDb.TransactionManager
Using newTrans As Transaction = newTm.StartTransaction

'open the new drawings's layout dictionary
Dim newLayoutDbDict As DBDictionary = DirectCast(newTrans.GetObject(newDb.LayoutDictionaryId, OpenMode.ForRead, False), DBDictionary)
For Each layoutDictEnt As DictionaryEntry In newLayoutDbDict
newLayoutList.Add(layoutDictEnt.Key.ToString)
newLayoutListIds.Add(DirectCast(layoutDictEnt.Value, ObjectId))
Next

'open the source drawing's layout dictionary
Dim sourceLayoutDbDict As DBDictionary = DirectCast(sourceTrans.GetObject(sourceDb.LayoutDictionaryId, OpenMode.ForRead, False), DBDictionary)
For Each layoutDictEnt As DictionaryEntry In sourceLayoutDbDict
Dim layoutId As ObjectId = DirectCast(layoutDictEnt.Value, ObjectId)
Dim layout As Layout = DirectCast(sourceTrans.GetObject(layoutId, OpenMode.ForRead, False), Layout)
If layout.ModelType = False Then 'find out if the layout is the Model tab, and continue if it isnt
If newLayoutList.Contains(layout.LayoutName) Then

Dim layoutIndex As Integer
For i As Integer = 0 To newLayoutList.Count - 1
If newLayoutList(i) = layout.LayoutName Then
layoutIndex = i
Exit For
End If
Next

Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbCrLf + layout.LayoutName + " - Already Exists")

Dim layoutIds As New ObjectIdCollection()

Dim btr As BlockTableRecord = sourceTrans.GetObject(layout.BlockTableRecordId, OpenMode.ForRead, False)
For Each objId As ObjectId In btr
Dim obj As DBObject = sourceTrans.GetObject(objId, OpenMode.ForRead, False)
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbCrLf + obj.ToString)
If TypeOf (obj) Is MText Then
Dim mtext As MText = DirectCast(obj, MText)
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbCrLf + mtext.Contents + vbCrLf + vbCrLf)
mtext.Dispose()
End If
obj.Dispose()
layoutIds.Add(objId)
Next

Dim layoutIdMapping As New IdMapping()

Dim newlayout As Layout = DirectCast(newTrans.GetObject(newLayoutListIds.Item(layoutIndex), OpenMode.ForRead, False), Layout)

LayoutManager.Current.CurrentLayout = newlayout.LayoutName

sourceDb.WblockCloneObjects(layoutIds, newlayout.BlockTableRecordId, layoutIdMapping, DuplicateRecordCloning.Replace, False)

layoutIdMapping.Dispose()
newlayout.Dispose()
btr.Dispose()

Else
psIds.Add(layoutId)

End If
End If
layout.Dispose()
Next

End Using
End Using

' Copy blocks from source to destination database
Dim mapping As New IdMapping()

sourceDb.WblockCloneObjects(msIds, SymbolUtilityServices.GetBlockModelSpaceId(newDb), mapping, DuplicateRecordCloning.MangleName, False)

Dim mapping2 As New IdMapping()
sourceDb.WblockCloneObjects(psIds, newDb.LayoutDictionaryId, mapping2, DuplicateRecordCloning.MangleName, False)

dm.MdiActiveDocument.Editor.WriteMessage(vbCrLf)
dm.MdiActiveDocument.Editor.Regen()

Catch ex As Autodesk.AutoCAD.Runtime.Exception
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbCrLf + "Error converting drawing. Error message: " + ex.Message)
Finally
sourceDb.Dispose()
End Try
End Sub
{code} Edited by: MarkPendergraft on Apr 20, 2009 1:44 PM
0 REPLIES 0

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