.NET

Reply
Mentor
wang890
Posts: 816
Registered: ‎06-08-2007
Message 1 of 4 (276 Views)

bug in layout.GetViewports

276 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
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form)
End Sub
Private Sub Zoom_Cross_Sections_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

lvwLayout.Clear()
Add_LVW_Columns(lvwLayout)
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
Next

trans.Commit()
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 2012
Work: Xeon W3503, 12GB, Quadro 2000, Yamakasi Sparta 301 + Dell PH2211
Home: 3930k, 12GB, GTX 590, U3011, QX2710
New Member
specterer
Posts: 1
Registered: ‎06-26-2012
Message 2 of 4 (164 Views)

Re: bug in layout.GetViewports

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

I'm confused by the same bug!

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 4 (155 Views)

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()
        {
            InitializeComponent();
            this.listView1.Clear();
            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)
        {
 
            this.Hide();

            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());

                            lvi.SubItems.Add(svi);

                            this.listView1.Items.Add(lvi);
                        }
                }
            }
            this.Show();
        }


        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)
        {
            this.Close();
        }
    }
}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Mentor
jeff
Posts: 327
Registered: ‎05-12-2009
Message 4 of 4 (146 Views)

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
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.