I have a section of code that creates a new layout and sets if current. It works in 2014 AutoCAD but I cannot get it to work in 2016 AutoCAD. I combed through this several times and cannot figure out why it keeps erring out. It catches the error at the line "LayoutManager.Current.CurrentLayout = acLayout2.LayoutName"
Any help would be greatly appreciated.
Code:
Public Sub CreateLayout(ByVal db As Database, ByVal ed As Editor, ByVal doc As Document, ByVal LayName As String, ByVal myPSxrefs As ArrayList)
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Using layouts As DBDictionary = DirectCast(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite), DBDictionary)
' Add a number at the end of layout name if already exists
Dim i As Integer = 0
Dim OriginalName As String = LayName
Dim x As Double = 25.4 * CDbl(TB_X.Text)
Dim y As Double = 25.4 * CDbl(TB_Y.Text)
Dim myPlotOrigin As Point2d = New Point2d(y, x)
' Create and activate the new layout
Using myLockDoc As DocumentLock = doc.LockDocument()
Dim acLayoutMgr As LayoutManager = LayoutManager.Current
Dim layoutId As ObjectId = acLayoutMgr.CreateLayout(LayName)
Dim acLayout2 As Layout = tr.GetObject(layoutId,
OpenMode.ForRead)
If acLayout2.TabSelected = False Then
LayoutManager.Current.CurrentLayout = acLayout2.LayoutName
End If
'Plot Settings
'' Get the PlotInfo from the layout
Using acLayout As Layout = DirectCast(tr.GetObject(layoutId, OpenMode.ForWrite), Layout)
Dim acPlInfo As PlotInfo = New PlotInfo()
acPlInfo.Layout = acLayout.ObjectId
'' Get a copy of the PlotSettings from the layout
Dim acPlSet As PlotSettings = New PlotSettings(acLayout.ModelType)
acPlSet.CopyFrom(acLayout)
'' Update some properties of the PlotSettings object
Dim acPlSetVdr As PlotSettingsValidator = PlotSettingsValidator.Current
Dim myCTB As String = "LBI-Full.ctb"
Dim device As String = "Bluebeam PDF"
acPlSetVdr.SetCurrentStyleSheet(acPlSet, myCTB)
acPlSetVdr.SetPlotConfigurationName(acPlSet, device, myMediaCanonical)
If CB_SheetRotation.Text = "LANDSCAPE" Then
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees090)
End If
If CB_SheetRotation.Text = "PORTRAIT" Then
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees000)
End If
If CB_SheetRotation.Text = "LANDSCAPE(Upside Down)" Then
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees180)
End If
If CB_SheetRotation.Text = "PORTRAIT(Upside Down)" Then
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees270)
End If
acPlSetVdr.SetPlotType(acPlSet, DatabaseServices.PlotType.Layout)
acPlSetVdr.SetZoomToPaperOnUpdate(acPlSet, True)
acPlSetVdr.SetPlotOrigin(acPlSet, myPlotOrigin)
acPlSetVdr.SetUseStandardScale(acPlSet, True)
If myPSxrefs.Count > 0 Then
attachXrefsTypes(myPSxrefs, "TTBL")
End If
ed.Regen()
'' Update the layout
acLayout.CopyFrom(acPlSet)
End Using
End Using
tr.Commit()
End Using
Catch ex As System.Exception
Dim Msg As String = "Error in function Create Layout"
Dim Title As String = "Operation cancelled"
MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
MsgBox(ex.Message & Err.Number)
tr.Abort()
End Try
End Using
End Sub