.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

bug in layout.GetViewports

6 REPLIES 6
Reply
Message 1 of 7
wang890
1215 Views, 6 Replies

bug in layout.GetViewports

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
Stantec
Dell Precision 5530, Prism M320PU, C3D 14/17/19
6 REPLIES 6
Message 2 of 7
specterer
in reply to: wang890

I'm confused by the same bug!

Message 3 of 7
Hallex
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
Message 4 of 7
jeff
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
Message 5 of 7
autoDeveloper
in reply to: wang890

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.

Message 6 of 7


@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.

Message 7 of 7
FRFR1426
in reply to: autoDeveloper

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 @jeff . 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.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost