@Ed__Jobe – That worked. It was a little confusing for a reason that’s obvious now. In my template file I created the layout with page setup and titleblock on the first layout “Layout1”. When running the code, I always used a new blank dwg to test with. The code would create a 2nd layout “Layout2”, but the template would always appear on the 1st layout: “Layout1”. Duh because is cloning “Layout1” from the template file. HAHA, so I renamed the layout in the template “TempLayoutC” which no one should use in their project dwgs. Then I added a line of code at the end to rename “TempLayoutC” to “Layout{Layout.Count}” Final code result is below.
PS - thanks to everyone who helped sort out this confusion!
#Region " Emp_NewLayoutC"
<CadRun.CommandMethod("Emp_NewLayoutC")>
Public Sub Emp_NewLayoutC()
' Layouts are sorted by taborder during the globals.GetLayouts sub during initiate
' Target is referenced to the dwg I'm currently working and want to add a new layout to - Any globals "g_" assume this too
' Temp/Template is referenced to, the layout of, or anything else pertaining to the template
Initiate()
'DB.WriteLine(vbNewLine & "Class1 | ServiceCommands | Emp_NewLayoutC")
Try
' Start transaction
Dim TargetTrans As CadDBS.Transaction = g_AcDatBas.TransactionManager.StartTransaction()
' Get the layout manager
Dim TargetLayoutMgr As CadDBS.LayoutManager = CadDBS.LayoutManager.Current
' Open in memory the titleblock dwt (Make sure this is in templates dir from options!)
Dim TempDb As CadDBS.Database = New CadDBS.Database(False, False)
TempDb.ReadDwgFile("TempFileName", CadDBS.FileOpenMode.OpenForReadAndAllShare, False, Nothing)
' Start a new Transaction
Dim TempTrans As CadDBS.Transaction = TempDb.TransactionManager.StartTransaction
' Get the layout from the template
Dim TempLayout As CadDBS.Layout
For Each entry As CadDBS.DBDictionaryEntry In CType(TempDb.LayoutDictionaryId.GetObject(CadDBS.OpenMode.ForRead), CadDBS.DBDictionary)
If entry.Key.Trim().ToUpper() <> "MODEL" Then
TempLayout = CType(TempTrans.GetObject(entry.Value, CadDBS.OpenMode.ForRead), CadDBS.Layout)
End If
Next
If TempLayout.ObjectId <> CadDBS.ObjectId.Null Then
' Create an id collection for the clone and add the layout
Dim OIC As New CadDBS.ObjectIdCollection
OIC.Add(TempLayout.ObjectId)
' Create id mapping
Dim IdMap As CadDBS.IdMapping = New CadDBS.IdMapping
' get the target database dict
Dim DbDict As CadDBS.DBDictionary = DirectCast(TempTrans.GetObject(g_AcDatBas.LayoutDictionaryId, CadDBS.OpenMode.ForRead, False, False), CadDBS.DBDictionary)
' If dict is open as ForRead, upgrade it to ForWrite. This also closes the object.
DbDict.UpgradeOpen()
' Target database clones from the template id collection
g_AcDatBas.WblockCloneObjects(OIC, g_AcDatBas.LayoutDictionaryId, IdMap, CadDBS.DuplicateRecordCloning.UnmangleName, False)
' Deep clone id pairs based on the mapping
Dim IPair As CadDBS.IdPair = IdMap.Lookup(TempLayout.ObjectId)
' Pair value is the cloned objects id
Dim NewObjId As CadDBS.ObjectId = IPair.Value
'LayoutMgr.CurrentLayout = Layt.LayoutName
DbDict.DecomposeForSave(CadDBS.DwgVersion.Current)
' Open the Layout
Dim Layt As CadDBS.Layout = TempTrans.GetObject(NewObjId, CadDBS.OpenMode.ForWrite)
' Move to the end of the tabs
Layt.TabOrder = TargetLayoutMgr.LayoutCount
' Add to the global list
g_Layouts.Add(Layt)
' Set the layout current if it is not already
If Layt.TabSelected = False Then : TargetLayoutMgr.CurrentLayout = Layt.LayoutName : End If
' Rename the tab
Layt.LayoutName = $"Layout{g_Layouts.Count}"
End If
' Save stuff from the template transaction
TempTrans.Commit()
' Regenerate the document
g_AcDoc.Editor.Regen()
' Save stuff from the target transaction
TargetTrans.Commit()
DB.WriteLine(vbNewLine)
Catch ex As Exception
Return
End Try
End Sub
#End Region
**Moderator edit: changed code window format to C#