<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Command creates viewport, but VP doesn't exist until command runs second tim in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9194041#M20629</link>
    <description>&lt;P&gt;Here is a screencast demonstrating the behavior that I see:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="iframe-container" style="position: relative; height: 0; margin: 0; padding-bottom: 96.875%;"&gt;&lt;IFRAME width="640" height="620" style="position: absolute; left: 0; top: 0; width: 100%; height: 100%;" src="https://screencast.autodesk.com/Embed/Timeline/eedb48e0-e56b-47d6-9cb0-5fdcc1a422a6" frameborder="0" allowfullscreen="true" webkitallowfullscreen="true" scrolling="no"&gt;&lt;/IFRAME&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 10 Dec 2019 18:27:57 GMT</pubDate>
    <dc:creator>cwhetten</dc:creator>
    <dc:date>2019-12-10T18:27:57Z</dc:date>
    <item>
      <title>Command creates viewport, but VP doesn't exist until command runs second time</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9193996#M20628</link>
      <description>&lt;P&gt;I have created a custom command that creates a viewport and then goes on to do things with this viewport, but the problem I am having is that after creating the viewport, the additional steps fail because the new viewport doesn't seem to exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Strangely, if I execute the command again, immediately after the previous run finishes, and without doing anything else in between, the previously created viewport is detected and the additional steps can continue.&amp;nbsp; (It only creates the new viewport if one doesn't already exist.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the viewport is being created the first time, but it seems like the command needs to finish completely before the view is fully created...?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am new to the AutoCAD API, so I could be missing something fundamental, but I can't figure out what it is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is a simplified version of my code.&amp;nbsp; It doesn't include any of the additional steps beyond creating the viewport.&amp;nbsp; Instead it writes a message to the command line to report how many viewports it finds after the new one is created.&amp;nbsp; So, it should report 1 viewport, but the first time the command is executed, it reports 0 viewports found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to test the code, I have attached a drawing with a layout that has a circle on it (Layout 1).&amp;nbsp; Open the drawing, activate the layout, then run the command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me understand why the view is created, but doesn't seem to exist on the first run of my command?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports System.Linq

Namespace TestCommands
    'commands here are for debugging and testing purposes only
    Public Class Commands

        &amp;lt;CommandMethod("TESTPREPROCESS")&amp;gt;
        Public Shared Sub TestPreProcessCommand()
            TestPreProcessor()
        End Sub

    End Class

    Public Module PreProcessor
        Public Sub TestPreProcessor()
            'Find the used paperspace layout
            Dim usedLayouts As List(Of Layout) = FindUsedLayouts()
            For Each usedLayout As Layout In usedLayouts
                'Check if there is a viewport
                Dim viewportExists As Boolean = CheckForViewport(usedLayout)

                If viewportExists = False Then
                    'Create viewport
                    CreateViewport(usedLayout)
                End If
            Next
       End Sub
 
       Public Function CheckForViewport(chkLayout As Layout) As Boolean
            Dim docDatabase As Database = Application.DocumentManager.MdiActiveDocument.Database
            Dim viewIDs As ObjectIdCollection = chkLayout.GetViewports()
            Dim viewIDcount As Integer = 0
            For Each viewID As ObjectId In viewIDs
                'Database.GetViewports(False) should not contain the layout paperspace viewport
                'If the view ID item is found in the list, then it is not the paperspace viewport
                If (Not docDatabase.GetViewports(True).Contains(viewID)) Or docDatabase.GetViewports(False).Contains(viewID) Then
                    viewIDcount += 1
                End If
            Next
	    
	    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf &amp;amp; "Viewports found: {0}", viewIDcount.ToString)

            If viewIDcount &amp;gt; 0 Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Function FindUsedLayouts() As List(Of Layout)
            Dim autocadDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim docDatabase As Database = autocadDoc.Database
            Dim docEditor As Editor = autocadDoc.Editor
            Dim usedLayouts As New List(Of Layout)

            Using acTransaction As Transaction = docDatabase.TransactionManager.StartTransaction()

                'Get the collection of layouts
                Dim layouts As DBDictionary = acTransaction.GetObject(docDatabase.LayoutDictionaryId, OpenMode.ForRead)
                For Each entry In layouts
                    Dim layout As Layout = acTransaction.GetObject(entry.Value, OpenMode.ForRead)

                    'Exclude the modelspace layout
                    If layout.LayoutName &amp;lt;&amp;gt; "Model" Then
                        Dim blockTblRec As BlockTableRecord = acTransaction.GetObject(layout.BlockTableRecordId, OpenMode.ForRead)

                        'Check if the layout contains more than one object.  If so, add it to the list.
                        '(All initialized layouts contain a default paperspace viewport, which counts as one object)
                        If blockTblRec.Cast(Of ObjectId)().Count &amp;gt; 1 Then usedLayouts.Add(layout)
                    End If
                Next
                acTransaction.Commit()
            End Using

            Return usedLayouts
        End Function

        Public Sub CreateViewport(targetLayout As Layout)
            Dim autocadDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim docDatabase As Database = autocadDoc.Database
            Dim viewName As String = "AutoMergeNewView"
            Using acTransaction As Transaction = docDatabase.TransactionManager.StartTransaction()
                'Open the block table for read
                Dim blockTbl As BlockTable = acTransaction.GetObject(docDatabase.BlockTableId, OpenMode.ForRead)

                'Open the block table record paper space for write
                Dim blockTblRec As BlockTableRecord = acTransaction.GetObject(blockTbl(BlockTableRecord.PaperSpace), OpenMode.ForWrite)

                'Switch to the target layout
                Application.SetSystemVariable("TILEMODE", 0)
                autocadDoc.Editor.SwitchToPaperSpace()
                LayoutManager.Current.CurrentLayout = targetLayout.LayoutName

                'Create the viewport
                Dim acVport As Viewport = New Viewport()
                'Add the new object to the block table record and the transaction
                blockTblRec.AppendEntity(acVport)
                acTransaction.AddNewlyCreatedDBObject(acVport, True)

                Dim landscape As Boolean = targetLayout.PlotRotation = PlotRotation.Degrees000 Or targetLayout.PlotRotation = PlotRotation.Degrees180
                Dim paperUnits As PlotPaperUnit = targetLayout.PlotPaperUnits
                Dim paperUnitFactor As Double = If(paperUnits = PlotPaperUnit.Inches, 25.4, 1)

                Dim width As Double = If(landscape, targetLayout.PlotPaperSize.X / paperUnitFactor, targetLayout.PlotPaperSize.Y / paperUnitFactor)
                Dim height As Double = If(landscape, targetLayout.PlotPaperSize.Y / paperUnitFactor, targetLayout.PlotPaperSize.X / paperUnitFactor)
                acVport.CenterPoint = New Point3d(width / 2, height / 2, 0)
                acVport.Width = width
                acVport.Height = height
                acVport.SetDatabaseDefaults()
                acVport.Layer = "0"
                acVport.ViewDirection = Vector3d.ZAxis
                acVport.ViewTarget = New Point3d(0, 0, 0)
                acVport.ViewCenter = Point2d.Origin

                'Enable the viewport
                acVport.Visible = True
                acVport.On = True
                acVport.UpdateDisplay()

                'Activate model space in the viewport
                autocadDoc.Editor.SwitchToModelSpace()

                'Set the viewport current
                Application.SetSystemVariable("CVPORT", acVport.Number)

                acTransaction.Commit()

                Application.SetSystemVariable("TILEMODE", 0)

            End Using

            'Switch back to paper space
            autocadDoc.Editor.SwitchToPaperSpace()
            autocadDoc.Editor.WriteMessage(vbLf &amp;amp; "Viewports found: {0}", CheckForViewport(targetLayout).ToString)

        End Sub	

    End Module
End Namespace
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cameron Whetten&lt;BR /&gt;AutoCAD Mechanical 2017&lt;BR /&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/454603i0DEDCFC9A00E24F3/image-size/large/is-moderation-mode/true?v=1.0&amp;amp;px=705" border="0" width="150" title="EE forum signature logo.png" alt="EE forum signature logo.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 18:14:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9193996#M20628</guid>
      <dc:creator>cwhetten</dc:creator>
      <dc:date>2019-12-10T18:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: Command creates viewport, but VP doesn't exist until command runs second tim</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9194041#M20629</link>
      <description>&lt;P&gt;Here is a screencast demonstrating the behavior that I see:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="iframe-container" style="position: relative; height: 0; margin: 0; padding-bottom: 96.875%;"&gt;&lt;IFRAME width="640" height="620" style="position: absolute; left: 0; top: 0; width: 100%; height: 100%;" src="https://screencast.autodesk.com/Embed/Timeline/eedb48e0-e56b-47d6-9cb0-5fdcc1a422a6" frameborder="0" allowfullscreen="true" webkitallowfullscreen="true" scrolling="no"&gt;&lt;/IFRAME&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 10 Dec 2019 18:27:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9194041#M20629</guid>
      <dc:creator>cwhetten</dc:creator>
      <dc:date>2019-12-10T18:27:57Z</dc:date>
    </item>
    <item>
      <title>Re: Command creates viewport, but VP doesn't exist until command runs second tim</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9194331#M20630</link>
      <description>&lt;P&gt;One code error is in CreateViewPort() method:&lt;/P&gt;
&lt;PRE&gt;            Using acTransaction As Transaction = docDatabase.TransactionManager.StartTransaction()
                'Open the block table for read
                Dim blockTbl As BlockTable = _&lt;BR /&gt;                   acTransaction.GetObject(docDatabase.BlockTableId, OpenMode.ForRead)

                'Open the block table record paper space for write
                Dim blockTblRec As BlockTableRecord = _&lt;BR /&gt;                    acTransaction.GetObject(blockTbl(&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;BlockTableRecord.PaperSpace&lt;/STRONG&gt;&lt;/FONT&gt;), OpenMode.ForWrite)

                'Switch to the target layout
                Application.SetSystemVariable("TILEMODE", 0)
                autocadDoc.Editor.SwitchToPaperSpace()
                &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;LayoutManager.Current.CurrentLayout = targetLayout.LayoutName&lt;/STRONG&gt;&lt;/FONT&gt;

                'Create the viewport
                Dim acVport As Viewport = New Viewport()
                'Add the new object to the block table record and the transaction
                &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;blockTblRec.AppendEntity(acVport)&lt;/FONT&gt;&lt;/STRONG&gt;
                acTransaction.AddNewlyCreatedDBObject(acVport, True)&lt;BR /&gt;                ...&lt;BR /&gt;                ...           &lt;BR /&gt;             End Using&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As the code highlighted in red, you add newly created Viewport to the PaperSpace block, but because you set up the "targetLayout" as current/active layout after you obtain the current PaperSapce block, the PaperSpace may not be the block of the "targetLayout" (i.e. it could be another layout's block). Thus, you probably did not notice that the viewport is actually added to another layout.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, you should set the layout to current before you try to obtain the layout's block by using BlockTableRecord.PaperSpace static property. However, if you have already had Layout object, then you can open its BlockTablerecord by using its BlockId property (so, you do not need to a. open BlockTable; 2, set layout current; and 3 use BlockTableRecord.PaperSpace to get the Layout's BlockId).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, in your code, it is problematic that you collect a list of Layout objects and then use Layout objects outside Transaction. It would be better for the FindUsedLayouts() to return a collection of custom class that has member fields like LayoutName as string, LayoutBlockId As ObjectId (or instead of define a string, you can use Tuple(of String, ObjectId) to hold Layout information for later use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 20:08:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9194331#M20630</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2019-12-10T20:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: Command creates viewport, but VP doesn't exist until command runs second tim</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9194473#M20631</link>
      <description>&lt;P&gt;Thank you for your reply, Norman.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will adjust my code according to what you have suggested and test it.&amp;nbsp; I'll post back with my results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cameron Whetten&lt;BR /&gt;AutoCAD Mechanical 2017&lt;BR /&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/454603i0DEDCFC9A00E24F3/image-size/large/is-moderation-mode/true?v=1.0&amp;amp;px=705" border="0" width="150" title="EE forum signature logo.png" alt="EE forum signature logo.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 21:17:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9194473#M20631</guid>
      <dc:creator>cwhetten</dc:creator>
      <dc:date>2019-12-10T21:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: Command creates viewport, but VP doesn't exist until command runs second tim</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9197416#M20632</link>
      <description>&lt;P&gt;Hi Norman.&amp;nbsp; I took your advice and adjusted my code to collect a list of a custom class instead of layout objects.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That alone didn't solve the problem, but I applied the same approach to the list of viewport objects I was collecting, and this helped me to see a different way to approach the problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was using the Layout.GetViewports method to get all of the viewports, but for whatever reason, the newly created viewport was not in the list returned.&amp;nbsp; I still don't know why, but I realized that I can store the viewport's ObjectID after it's created and later get the viewport directly from the database using that ObjectID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This worked, so thank you for helping me see the problem in a different light.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cameron Whetten&lt;BR /&gt;AutoCAD Mechanical 2017&lt;BR /&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/454603i0DEDCFC9A00E24F3/image-size/large/is-moderation-mode/true?v=1.0&amp;amp;px=705" border="0" width="150" title="EE forum signature logo.png" alt="EE forum signature logo.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2019 23:46:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-creates-viewport-but-vp-doesn-t-exist-until-command-runs/m-p/9197416#M20632</guid>
      <dc:creator>cwhetten</dc:creator>
      <dc:date>2019-12-11T23:46:32Z</dc:date>
    </item>
  </channel>
</rss>

