This will see if a layout with the supplied name exists if it does then the objectid of the layout is returned. If the layout does not exist then it is created and the objectid is then returned. Depending on what you are doing your function may not be shared. Make sure to regen in order for the layout to be displayed.
Public Shared Function CreateGetLayout(ByVal LayoutName As String) As ObjectId
' Lets get the active document
Dim oDoc As Document = Application.DocumentManager.MdiActiveDocument
' Lets get the database
Dim oDB As Database = oDoc.Database
' lock the document
Dim oLock As DocumentLock = oDoc.LockDocument
' Lets create a transaction so we can do some database work
Dim trans As Transaction = oDB.TransactionManager.StartTransaction
Try
' Lets get the layout dictionary
Dim oLayoutDict As DBDictionary = trans.GetObject(oDB.LayoutDictionaryId, OpenMode.ForRead)
Dim oLayout As Layout
' Lets see if a layout with that name exist.
If oLayoutDict.Contains(LayoutName) = True Then
' Get the layout object from the objectid which is the value of the entry
Return oLayoutDict.Item(LayoutName)
Else
' Create the new layout
oLayout = New Layout
' Set the layout name
oLayout.LayoutName = LayoutName
' Get the block table
Dim oBT As BlockTable = trans.GetObject(oDB.BlockTableId, OpenMode.ForRead)
' Create a new paperspace block table record
Dim oBTR As New BlockTableRecord
' Set the name of the block table record
oBTR.Name = "*Paper_Space"
' upgrade the open on the block table
oBT.UpgradeOpen()
' Add the block table record to the block table
Dim oBTRId As ObjectId = oBT.Add(oBTR)
' downgrade the open
oBT.DowngradeOpen()
' notify the transaction about the new object
trans.AddNewlyCreatedDBObject(oBTR, True)
' Add the layout to the layout dictionary associating the databse and block table record to the layout
oLayout.AddToLayoutDictionary(oDB, oBTRId)
' notify the transaction about the layout
trans.AddNewlyCreatedDBObject(oLayout, True)
' commit the changes
trans.Commit()
' return the id of the layout
Return oLayout.ObjectId
End If
Finally
' Remove the document lock
oLock.Dispose()
' Dispose of the transaction
trans.Dispose()
End Try
End Function