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'~
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.
I am also facing same issue. ObjectIdCollection viewportColl = layout.GetViewports(); returns 0 viewports. If I manually click on the 'PaperSpace' and then switch back to ModelSpace and then run above code then collection gets more than 0 viewports.
Any idea why it happens? Is there any method to activate PaperSpace through code so it can be the workaround.
Please help.
Thanks in advance.
@autoDeveloper wrote:I am also facing same issue. ObjectIdCollection viewportColl = layout.GetViewports(); returns 0 viewports. If I manually click on the 'PaperSpace' and then switch back to ModelSpace and then run above code then collection gets more than 0 viewports.
Any idea why it happens? Is there any method to activate PaperSpace through code so it can be the workaround.
Please help.
Thanks in advance.
Layout.Initialize() needs to be called to create the paper space viewport on the layout, if it was never activated since having been created.
From the docs:
If the list returned is empty, then this Layout has never been switched to (activated). The list is updated each time a layout is activated, and kept up-to-date while that layout is active (whenever viewports are added or deleted).
If you want to find how many viewports are on a particular layout, you need to use Database.GetViewport(bGetPaperspaceVports: true) to get all the viewports in the drawing as advised by @Anonymous . After that, you can find the layout of each viewport: get the owner BlockTableRecord with viewport.BlockId, then on the BlockTableRecord you have a property LayoutId which allow you to match the viewport to its layout.
Can't find what you're looking for? Ask the community or share your knowledge.