iterating viewports

iterating viewports

Anonymous
Not applicable
1,915 Views
6 Replies
Message 1 of 7

iterating viewports

Anonymous
Not applicable

can someone provide a clear explaination (& maybe an example (or pseudocode)) of iterating all of the viewports (PS & MS) of all layout tabs in a (currently open) drawing & make them current & then lets  display a MSGBOX indicating something about that current VP.

Additionally, lets pretend that in an example DWG, the current VP is one of three MS VPs that exist in a layout tab called "TESTlayout" & we would like to reset that to the current layout & VP when our routine is finished.

0 Likes
1,916 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

 

Here's a little example:

 

        [CommandMethod("Foo")]
        public void foo()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary layouts = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry entry in layouts)
                {
                    if (entry.Key != "Model")
                    {
                        Layout layout = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
                        string name = layout.LayoutName;
                        LayoutManager.Current.CurrentLayout = name;
                        foreach (ObjectId id in layout.GetViewports())
                        {
                            Viewport vp = (Viewport)tr.GetObject(id, OpenMode.ForRead);
                            int vpNum = vp.Number;
                            if (vpNum == 1)
                            {
                                ed.SwitchToPaperSpace();
                            }
                            else
                            {
                                ed.SwitchToModelSpace();
                                AcAp.SetSystemVariable("cvport", vpNum);
                            }
                            AcAp.ShowAlertDialog("Layout: " + name + "\nViewport number: " + vpNum.ToString());
                        }
                    }
                }
                tr.Commit();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

Anonymous
Not applicable

Thanks, that helps a bit.

 

I'm still not sure how to find the current viewport. I've seen stuff where people talk about finding the word "*ACTIVE" in the viewtable (?), but I'n not clear about this & how that relates to the current VP.

 

I'm using VB, but I'm semi-literate in C++

0 Likes
Message 4 of 7

Anonymous
Not applicable

got it, very simple. I had found an example in the autodesk documentation which lead me astray, but I figured it out:

 

Dim strCurrentLayout AsString

Dim iCurrentTilemode AsInteger = Application.GetSystemVariable("TILEMODE")

Dim vpCurrentVP AsViewport

       

Using Tx AsTransaction = ACF.Db.TransactionManager.StartTransaction()

            strCurrentLayout =LayoutManager.Current.CurrentLayout

           

If iCurrentTilemode = 1 Then'      = Model Layout

           

Else                                'you're in a layout, so get current VP

                vpCurrentVP = (Tx.GetObject(ACF.acDoc.Editor.CurrentViewportObjectId,OpenMode.ForRead))          

 

EndIf

0 Likes
Message 5 of 7

Anonymous
Not applicable

OK, I've implemented proper procedures to iterate each layout & each viewport ( PS too) w/in each layout. I'm trying to use VPLAYER via sendcommand (or SendStringToExecute) to freeze a particular layer in the VP. It will not work. I get no errors, but it just soesn't work. Except it will freeze the layer in the VP that was origianlly current (which is also the last VP switched to) (even if it is not supposed to (becasue the user (me) didn't select that layout for inclusion in the list of layouts to have the layer frozen in that layout's VPs)).

 

 

It almost seems as if the SENDCOMMAND calls aren't being executed until the whole routine is finished, therefore only affecting the one VP.

 

I tried  a SendStringToExecute & that didn't work either.

 

I'd attach some code, but the routine is prretty complicated with multiple forms in different classes.

 

0 Likes
Message 6 of 7

Anonymous
Not applicable

PS

in other sections of my code, the same exact VPLAYER string is used in a SENDCOMMAND & it works just fine. This section does NOT involve changing layouts or VPs.

0 Likes
Message 7 of 7

_gile
Consultant
Consultant

Hi

 

Try to avoid using Sendcommand or SendStringToExecute as they do not run synchronously.

 

The Viewport class has the method you need:

FreezeLayersInViewport()

ThawLayersInViewport()

ThawAllLayersInViewport()

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub