831 Posts
7 Kudos
Registered: ‎06-08-2007
Post 1 of 4

bug in layout.GetViewports

293 Views, 3 Replies
06-26-2009 12:25 PM
i have a (or any) drawing which i just opened with contains several layouts and each has viewports, i run my program to list the layout with viewport will only get the first layout. until i go activate all the layout in autocad and then run the following code then i'll get what i want. basically the getviewport will not get any layout viewport except for the first one.

Public Class frmZoom_Cross_Sections
Public Sub Zoom_XS()
Dim form As New frmZoom_Cross_Sections
End Sub
Private Sub Zoom_Cross_Sections_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase()
Using trans As Transaction = db.TransactionManager.StartTransaction()

Dim ldic As DBDictionary = CType(trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)

For Each entr As DictionaryEntry In ldic
If entr.Key.ToString.ToUpper <> "MODEL" Then
Dim lo As Layout = CType(trans.GetObject(entr.Value, OpenMode.ForWrite), Layout)

Dim oID_Col As ObjectIdCollection = lo.GetViewports
If oID_Col.Count <> 0 Then
Dim id As Object = oID_Col.Item(0)
Dim PSVport As Viewport = CType(trans.GetObject(id, OpenMode.ForWrite, False, False), Viewport)
Dim olvwItem As ListViewItem = lvwLayout.Items.Add(lo.LayoutName)
olvwItem.Tag = id
End If
End If

End Using

End Sub

Private Sub Add_LVW_Columns(ByRef lvw As ListView)
lvwLayout.Columns.Add("Layout Name", 100, HorizontalAlignment.Center)
lvwLayout.Columns.Add("ObjectID", 100, HorizontalAlignment.Center)
End Sub

End Class
Civil 3D 2014
Work: i7 4770, SSD, 16GB, K2000, Dell PH2414H
Home: i7 3930k, SSD, 16GB, GTX 590, U3011
New Member
1 Posts
0 Kudos
Registered: ‎06-26-2012
Post 2 of 4

Re: bug in layout.GetViewports

06-26-2012 09:03 PM in reply to: wang890

I'm confused by the same bug!

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 3 of 4

Re: bug in layout.GetViewports

06-27-2012 02:38 AM in reply to: specterer

Try this code, tested on A2010 only


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System.Collections;
[assembly: CommandClass(typeof(FormViewPorts.frmViewPorts))]

namespace FormViewPorts
    public partial class frmViewPorts : Form
        public frmViewPorts()
            this.listView1.View = View.Details;
            this.listView1.FullRowSelect = false;
            this.listView1.Columns.Add("Layout Name", 300, HorizontalAlignment.Center);
            this.listView1.Columns.Add("ObjectId", 200, HorizontalAlignment.Center);
            this.listView1.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged);


        private void frmViewPorts_Load(object sender, EventArgs e)

            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
                DBDictionary layoutmgr = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;

                foreach (DBDictionaryEntry entry in layoutmgr)

                        Layout layout = tr.GetObject(entry.Value, OpenMode.ForRead) as Layout;

                        ObjectIdCollection vids = layout.GetViewports();
                        if (vids.Count > 0)
                            ObjectId vid = vids[0];
                            Viewport pvport = tr.GetObject(vid, OpenMode.ForWrite, false, false) as Viewport;

                            ListViewItem lvi = new ListViewItem(layout.LayoutName);
                            ListViewItem.ListViewSubItem svi = new ListViewItem.ListViewSubItem(lvi, vid.ToString());



        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
            if (listView1.SelectedIndices.Count > 0)
                MessageBox.Show("Layout Name: " + ((ListView)sender).SelectedItems[0].Text +

                    "\n" + "Objectid: " + ((ListView)sender).SelectedItems[0].SubItems[1].Text);              

        private void button1_Click(object sender, EventArgs e)



Valued Mentor
341 Posts
53 Kudos
Registered: ‎05-12-2009
Post 4 of 4

Re: bug in layout.GetViewports

06-27-2012 07:50 AM in reply to: wang890

I think that is normal and done for performance reason, and do not remeber if there is a system variable, but try using Database.GetViewports() and pass true for argument if you want viewports associated with layout.


From docs 

Input flag indicating whether to return paperspace viewports associated with layouts
This function enumerates the Viewports in a drawing.





You can also find your answers @ TheSwamp
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!