In addition try this code, limited tested
you have to add check if file exist, if layout name
is not from your layout list etc:
<CommandMethod("clayout", CommandFlags.Session)> _
Public Sub testCopyLayoutFromTemplate()
CopyLayoutFromTemplate("C:\Test\Temp2.dwt", "A")
End Sub
Public Sub CopyLayoutFromTemplate(ByVal filepath As String, ByVal layoutname As String)
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim curdb As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim loc As DocumentLock = doc.LockDocument()
Using loc
Dim tr As Transaction = doc.TransactionManager.StartTransaction
Using tr
Try
Dim mLayoutmgr As LayoutManager = LayoutManager.Current
Dim newlayoutname As String = layoutname
Dim layoutId As ObjectId = ObjectId.Null
Dim btrId As ObjectId = ObjectId.Null
layoutId = mLayoutmgr.CreateLayout(newlayoutname)
Dim newlayout As Layout = DirectCast(tr.GetObject(layoutId, OpenMode.ForWrite), Layout)
Dim layoutbtr As BlockTableRecord = DirectCast(tr.GetObject(newlayout.BlockTableRecordId, OpenMode.ForWrite), BlockTableRecord)
btrId = layoutbtr.ObjectId
Dim btr As BlockTableRecord = CType(tr.GetObject(curdb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
Dim exdb As Database = New Database(False, True)
Using exdb
Using extr As Transaction = exdb.TransactionManager.StartTransaction
exdb.ReadDwgFile(filepath, System.IO.FileShare.Read, False, "")
Dim layoutdict As DBDictionary = DirectCast(exdb.LayoutDictionaryId.GetObject(OpenMode.ForRead), DBDictionary)
Dim layout As Layout = DirectCast(layoutdict.GetAt(layoutname).GetObject(OpenMode.ForRead), Layout)
Dim exbtr As BlockTableRecord = DirectCast(extr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead), BlockTableRecord)
Dim objcoll As ObjectIdCollection = New ObjectIdCollection
For Each id As ObjectId In exbtr
objcoll.Add(id)
Next
exdb.WblockCloneObjects(objcoll, btrId, New IdMapping, DuplicateRecordCloning.Ignore, False)
End Using
End Using
mLayoutmgr.CurrentLayout = newlayoutname
tr.Commit()
ed.Regen()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.ToString & vbCr & ex.Message)
End Try
End Using
End Using
End Sub
~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919