Creating a layout with portrait orientation

Creating a layout with portrait orientation

bnording123
Explorer Explorer
385 Views
2 Replies
Message 1 of 3

Creating a layout with portrait orientation

bnording123
Explorer
Explorer

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

 

0 Likes
386 Views
2 Replies
Replies (2)
Message 2 of 3

Ed__Jobe
Mentor
Mentor

It looks like you have to add the newly created ps to the db for it to take effect. See this post. The code from Kean's post was part of an extension method, which was called in the context of a transaction.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 3

bnording123
Explorer
Explorer

Thank you very much for posting that link. While the code therein also had problem, it got me past my roadblock and I was able to get my command working just fine...  Or so I thought!

 

I found some simple code on the Autocad 2023 Customization guide: https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-5FA86EF3-DEFD-4256-BB1C-56DAC32BD868

which creates a layout with a default viewport and default plotsettings.

 

But, both Autodesk's code as well as mine have a problem in that freezing layers in the viewport does not work! Using any of the AutoCAD methods, or using code to freeze layers in the viewport will show up as VPfrozen in the layers dialog, but the layers are still visible.  It works properly with manually created layouts, but not with ones created by code. Been fighting with this for over a week now.

 

Below is the code from the customization guide that also exhibits the issue.

 

  <CommandMethod("CreateLayout")> _
  Public Shared Sub CreateLayout()
    ' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    ' Get the layout and plot settings of the named pagesetup
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
      ' Reference the Layout Manager
      Dim acLayoutMgr As LayoutManager = LayoutManager.Current

      ' Create the new layout with default settings
      Dim objID As ObjectId = acLayoutMgr.CreateLayout("newLayout")

      ' Open the layout
      Dim acLayout As Layout = acTrans.GetObject(objID, OpenMode.ForRead)

      ' Set the layout current if it is not already
      If acLayout.TabSelected = False Then
        acLayoutMgr.CurrentLayout = acLayout.LayoutName
      End If

      ' Output some information related to the layout object
      acDoc.Editor.WriteMessage(vbLf & "Tab Order: " & acLayout.TabOrder & _
                                vbLf & "Tab Selected: " & acLayout.TabSelected & _
                                vbLf & "Block Table Record ID: " & _
                                acLayout.BlockTableRecordId.ToString())

      ' Save the changes made
      acTrans.Commit()
    End Using
  End Sub

 

0 Likes