Creating a layout with portrait orientation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to create a .net command that will create a layout. I have found a great example by Kean (https://www.keanw.com/2015/11/creating-an-autocad-layout-with-custom-plot-and-viewport-settings-usin...
Also found a few other posts that touch on this, but with no solutions.
The code below works as far as creating the layout, but it is always "landscape" where I need portrait. Ive tried different media names, setting plot rotation, rotating the layout, and nothing will produce a portrait orientation.
It seems you can't change much of anything in a layout object as most properties are read-only, so you must use a PlotSettingsValidator to set these properties, but it has nothing to set orientation.
In over 35 years of using AutoCAD, I've never understood AutoCAD's logic (or lack thereof) when in come to plotting: different paper sizes that have an inherent orientation, then an additional orientation setting, and a plot rotation to boot. Lots of things that seem to conflict with each other.
<CommandMethod("CL")> _
Public Sub CreateLayout()
Dim doc = Application.DocumentManager.MdiActiveDocument
If (doc Is Nothing) Then
Return
End If
Dim strLayout As String = "Sht01"
Dim db = doc.Database
Dim ed = doc.Editor
Dim ext = New Extents2d
Dim tr = db.TransactionManager.StartTransaction
Dim lm As LayoutManager = LayoutManager.Current
Dim id = lm.GetLayoutId(strLayout)
' If it doesn't exist, we create it
If Not id.IsValid Then
id = lm.CreateLayout(strLayout)
End If
Dim strMsg As String = ""
lm.CurrentLayout = strLayout
Dim lay = CType(tr.GetObject(id, OpenMode.ForWrite), Layout)
Dim pageSize As String = "ANSI_A_(8.50_x_11.00_Inches)"
'Dim pageSize As String = "ANSI_A_(11.00_x_8.50_Inches)"
Dim styleSheet As String = "monochrome.ctb"
Dim device As String = "AutoCAD PDF (High Quality Print).pc3"
'Dim device As String = "DWF6 ePlot.pc3"
Dim ps = New PlotSettings(lay.ModelType)
ps.CopyFrom(lay)
Dim psv = PlotSettingsValidator.Current
' Set the device
Dim devs = psv.GetPlotDeviceList
If devs.Contains(device) Then
psv.SetPlotConfigurationName(ps, device, Nothing)
psv.RefreshLists(ps)
End If
' Set the media name/size
Dim mns = psv.GetCanonicalMediaNameList(ps)
If mns.Contains(pageSize) Then
psv.SetCanonicalMediaName(ps, pageSize)
End If
' Set the pen settings
Dim ssl = psv.GetPlotStyleSheetList
If ssl.Contains(styleSheet) Then
psv.SetCurrentStyleSheet(ps, styleSheet)
End If
psv.SetPlotRotation(ps, PlotRotation.Degrees090)
'psv.SetPlotRotation(ps, PlotRotation.Degrees000)
' Copy the PlotSettings data back to the Layout
Dim upgraded = False
If Not lay.IsWriteEnabled Then
lay.UpgradeOpen()
upgraded = True
End If
lay.CopyFrom(ps)
'lay.PlotPaperSize.RotateBy(90, New Point2d(0, 0))
If upgraded Then
lay.DowngradeOpen()
End If
Dim vpNum As Integer = 2
Dim vpIds = lay.GetViewports
Dim vp As Viewport = Nothing
For Each vpId As ObjectId In vpIds
Dim vp2 = CType(tr.GetObject(vpId, OpenMode.ForWrite), Viewport)
If ((Not (vp2) Is Nothing) AndAlso (vp2.Number = vpNum)) Then
'Viewport found
vp = vp2
Exit For
End If
Next
If (vp Is Nothing) Then
'Viewport doesn't exist, so create one
Dim btr = CType(tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite), BlockTableRecord)
vp = New Viewport
' Add it to the database
btr.AppendEntity(vp)
tr.AddNewlyCreatedDBObject(vp, True)
' Turn it - and its grid - on
vp.On = True
vp.GridOn = True
End If
vp.Height = 10.5
vp.Width = 8.0
vp.StandardScale = StandardScaleType.Scale1To1
tr.Commit()
End Sub