Author Topic: Viewport Layers Set To "Frozen" Are Still Visible  (Read 2410 times)

0 Members and 1 Guest are viewing this topic.

MGorecki

  • Newt
  • Posts: 25
Viewport Layers Set To "Frozen" Are Still Visible
« on: July 11, 2012, 07:00:10 pm »
I have only one layout in the drawing.  When the code runs, it adds a new layout to the drawing from a template.  A dialog pops up and asks which layers they want to see in the new layouts viewport.  It also asks which layers they do not want to see in all the other layouts viewports (because a new layer was added and if there are many more layouts it will freeze the selected layers in all the other viewports).  When the program is done running, the new layout looks great.  It shows the layers I want to see, but, the existing layout still looks lthe same.  The layers are all still visible, even though the layer manager says that the layers I told to freeze are frozen.  It's like it's just not finishing up and ending the last part.

Here's the code that's supposed to freeze all the selected layers in all the other layouts viewports:

Code - vb.net: [Select]
  1.    ' Freezes the selected layers in all other existing viewport layouts
  2.    Public Sub freezeOtherLayouts(ByVal pageNumber As Integer, ByVal layersToFreezeLayerIds As ObjectIdCollection)
  3.        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  4.        Dim db As Database = doc.Database
  5.        Dim ed As Editor = doc.Editor
  6.        Dim vp As Viewport = Nothing
  7.        Dim viewPortFound As Boolean
  8.        Dim freezeVPtrans As Transaction = db.TransactionManager.StartTransaction()
  9.  
  10.        Try
  11.            Dim myBT As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
  12.            For Each btrID As ObjectId In myBT
  13.                Dim myBTR As BlockTableRecord = btrID.GetObject(OpenMode.ForRead)
  14.                ' If the block table record is a layout
  15.                If myBTR.IsLayout Then
  16.                    viewPortFound = False
  17.                    If Not myBTR.Name = "*Model_Space" Then
  18.                        Dim layOut As Layout = myBTR.LayoutId.GetObject(OpenMode.ForRead)
  19.                        ' If the layout is not the new layout
  20.                        If layOut.TabOrder <> pageNumber Then
  21.                            For Each id As ObjectId In myBTR
  22.                                Dim obj As DBObject = id.GetObject(OpenMode.ForWrite)
  23.                                ' If the object is a viewport (there is a model viewport which is found first, we want the second one)
  24.                                If TypeOf obj Is Viewport And viewPortFound = True Then
  25.                                    Dim vpref As Viewport = DirectCast(obj, Viewport)
  26.                                    ' Selected Viewport for write.
  27.                                    vp = freezeVPtrans.GetObject(vpref.ObjectId, OpenMode.ForWrite)
  28.                                    Dim lt As LayerTable = freezeVPtrans.GetObject(db.LayerTableId, OpenMode.ForRead)
  29.                                    ' Freeze the selected layers in the viewports
  30.                                    vp.FreezeLayersInViewport(layersToFreezeLayerIds.GetEnumerator())
  31.                                    ed.Regen()
  32.                                    lt.Dispose()
  33.                                End If
  34.                                If TypeOf obj Is Viewport Then
  35.                                    viewPortFound = True
  36.                                End If
  37.                            Next
  38.                        End If
  39.                    End If
  40.                End If
  41.            Next
  42.        Catch
  43.            ed.WriteMessage("Error!")
  44.        Finally
  45.            freezeVPtrans.Commit()
  46.            freezeVPtrans.Dispose()
  47.        End Try
  48.    End Sub

I just don't understand why the layers I selected to freeze (in layersToFreezeLayerIds) is still visible when the layer manager for the viewport shows them frozen.

Thanks,
« Last Edit: July 12, 2012, 10:35:46 am by MGorecki »

TT

  • Swamp Rat
  • Posts: 826
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #1 on: July 11, 2012, 07:04:49 pm »
Is the existing layout with the newly-frozen layers being regenerated?

trembre

  • Mosquito
  • Posts: 14
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #2 on: July 11, 2012, 09:12:40 pm »
 I wrote a batch "unfreezer" for viewport frozen layers years ago in VBA.  I don't know whether the same rules would apply, but the only way I could find to affect the state of layers frozen/thawed by viewport was by directly modifying the viewports relevant XData.  I'd have to dig out the code to get the specifics, but for the code to work I had to copy the original viewport, delete it from the drawing, delete the appropriate XData records from the copy, then re-create it. 

 Whether you'd have to be so clunky in .NET (or even VBA - all I know is it worked!), I wouldn't know, but the lack of any viewport XData modification stands out to me as a possible issue.

MexicanCustard

  • Bull Frog
  • Posts: 460
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #3 on: July 12, 2012, 07:25:53 am »
instead of ed.Regen() try vp.UpdateDisplay()
AMEP 2014/2015 64bit Win 7

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #4 on: July 12, 2012, 10:30:48 am »
Hi Bullfrog, I have the "ed.Regen()" in the code after it freezes the layers in the viewport.

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #5 on: July 12, 2012, 10:34:37 am »
Hello Mexican Custard, I tried that just now, but it doesn't make a difference.  Thanks though.

Delegate

  • Newt
  • Posts: 34
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #6 on: July 12, 2012, 12:06:22 pm »
I did not have any problem getting the freezelayersinviewport to work and did not need vp.updatedisplay or ed.regen.

Try this code and see if it works on your computer - basically it freezes all layers in all viewports apart from the layout viewport. Then maybe we can see if the problem is elsewhere.

Code - C#: [Select]
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.Geometry;
  6. using Autodesk.AutoCAD.EditorInput;
  7.  
  8. // This line is not mandatory, but improves loading performances
  9. [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in1.MyCommands))]
  10.  
  11. namespace AutoCAD_CSharp_plug_in1
  12. {
  13.  
  14.  
  15.    public class MyCommands
  16.    {
  17.  
  18.        [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
  19.        public void MyCommand() // This method can have any name
  20.        {
  21.            Document acDoc = Application.DocumentManager.MdiActiveDocument;
  22.            Database acCurDb = acDoc.Database;
  23.            ObjectIdCollection ids = new ObjectIdCollection();
  24.            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  25.            {
  26.                LayerTable acLyrTbl;
  27.                acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,OpenMode.ForRead) as LayerTable;
  28.                foreach (ObjectId item in acLyrTbl)
  29.                {
  30.                    ids.Add(item);
  31.                }
  32.  
  33.                BlockTableRecord btr = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
  34.                RXClass VPClass = RXObject.GetClass(typeof(Viewport));
  35.                foreach (ObjectId item in btr)
  36.                {
  37.                    if (item.ObjectClass == VPClass)
  38.                    {
  39.                        Viewport vp = acTrans.GetObject(item, OpenMode.ForWrite) as Viewport;
  40.                        if (vp.Number != 1)
  41.                        {
  42.                            vp.FreezeLayersInViewport(ids.GetEnumerator());
  43.                        }                  
  44.                    }
  45.                }                
  46.                acTrans.Commit();
  47.            }
  48.  
  49.        }        
  50.  
  51.    }
  52.  
  53. }
  54.  
  55.  


MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #7 on: July 12, 2012, 01:43:22 pm »
Hi Delegate,
Thank you for the code to try.  It did freeze all the layers in the new layout, but it did not change the layer visibility in the first layout viewport.
Could something be wrong with the first layout?  Maybe with the viewport itself?  I heard about viewports needing to be activated (though I'm not sure how to do that) before they can be changed.

Thanks,
Mark

Delegate

  • Newt
  • Posts: 34
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #8 on: July 12, 2012, 03:30:11 pm »
Hi

Sorry this maybe as written. I should have said I've got the blocktablerecord from the CurrentSpaceID  so will only work on the active layout unless you've changed it to loop through the layouts.

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #9 on: July 12, 2012, 04:19:59 pm »
I'm new to a lot of the VB.Net stuff, you'll have to explain:
RXClass VPClass = RXObject.GetClass(typeof(Viewport));

What is this and what does it do?  How can I use it?

Thanks,
Mark

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #10 on: July 12, 2012, 04:30:06 pm »
Ok I changed the code so that affects all layouts:
Code - vb.net: [Select]
  1.    Public Sub FreezeAllInVPs()
  2.        ' This method can have any name
  3.        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  4.        Dim acCurDb As Database = acDoc.Database
  5.        Dim ids As New ObjectIdCollection()
  6.        Dim viewPortFound As Boolean, pageNumber As Integer = 2
  7.  
  8.        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  9.            Dim acLyrTbl As LayerTable
  10.            acLyrTbl = TryCast(acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead), LayerTable)
  11.            For Each item As ObjectId In acLyrTbl
  12.                ids.Add(item)
  13.            Next
  14.  
  15.            'Dim btr As BlockTableRecord = TryCast(acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForRead), BlockTableRecord)
  16.            Dim myBT As BlockTable = acCurDb.BlockTableId.GetObject(OpenMode.ForRead)
  17.            For Each btrID As ObjectId In myBT
  18.                Dim myBTR As BlockTableRecord = btrID.GetObject(OpenMode.ForRead)
  19.                ' If the block table record is a layout
  20.                If myBTR.IsLayout Then
  21.                    viewPortFound = False
  22.                    If Not myBTR.Name = "*Model_Space" Then
  23.                        Dim layOut As Layout = myBTR.LayoutId.GetObject(OpenMode.ForRead)
  24.                        ' If the layout is not the new layout
  25.                        If layOut.TabOrder <> pageNumber Then
  26.                            Dim VPClass As RXClass = RXObject.GetClass(GetType(Viewport))
  27.                            For Each item As ObjectId In myBTR
  28.                                If item.ObjectClass = VPClass And viewPortFound = True Then
  29.                                    Dim vp As Viewport = TryCast(acTrans.GetObject(item, OpenMode.ForWrite), Viewport)
  30.                                    If vp.Number <> 1 Then
  31.                                        vp.FreezeLayersInViewport(ids.GetEnumerator())
  32.                                    End If
  33.                                End If
  34.                                If item.ObjectClass = VPClass Then
  35.                                    viewPortFound = True
  36.                                End If
  37.                            Next
  38.                            acTrans.Commit()
  39.                        End If
  40.                    End If
  41.                End If
  42.            Next
  43.        End Using
  44.    End Sub

In the new layout I had selected 2 layers to remain visible.  They were still visible, but when you double pick in the viewport and check the layer manager it shows that all layers have been frozen.
In the original layout, all layers were visible, but when I double picked in the viewport and checked the layer manager, it also showed that all layers were frozen.

Delegate

  • Newt
  • Posts: 34
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #11 on: July 12, 2012, 05:03:00 pm »
I'm new to a lot of the VB.Net stuff, you'll have to explain:
RXClass VPClass = RXObject.GetClass(typeof(Viewport));

What is this and what does it do?  How can I use it?

Thanks,
Mark

Its just another way of checking the type - could be circle, polyline etc.  Your method typeof means you have to cast to object before checking though won't make much difference unless large amount of items involved.

You can delete the viewportfound parts now. The vp.number <> 1 takes care of that. Sure someone will correct that if wrong.

Move the acTrans.commit to be above the End Using statement - your commiting before finishing.

If still not working maybe look at the calling code where you create the new layout - is it fully commited to database before your operations on layers and viewports? Throw in a ed.regen and vp.updatedisplay just to double check that to.

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #12 on: July 12, 2012, 05:12:14 pm »
I removed the viewportFound parts, and now all layers get frozen in paperspace (can't see the title block or anything in ps) but the viewport layers are still visible.

Delegate

  • Newt
  • Posts: 34
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #13 on: July 12, 2012, 05:31:17 pm »
Oh dear read this and ignore what i Sad  :ugly:

http://forums.autodesk.com/t5/NET/Layouts-and-Viewports/td-p/3128748

Also some info about activating layouts .

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #14 on: July 16, 2012, 05:48:24 pm »
Ok, I can't believe it was so simple.  In a previous sub, I forgot to close a transaction (smacking head).  All fixed now, but thank you all for your helpful support.