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

0 Members and 1 Guest are viewing this topic.

Delegate

  • Newt
  • Posts: 34
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #15 on: July 17, 2012, 05:46:48 am »
Thats great. I was playing around with the concepts for my own learning and came across a few problems of my own!

When adding viewports the layout needs to be current to use Layout.On otherwise you just get a rectangle that does not function in the layout.  Is there a way to add viewports to multiple layouts without making the layout current?? just to prevent the user see the layouts flicker across the screen  :lmao:

Also I found Layout.GetViewports did not work for viewports added by programme  to existing layouts but only viewport added to a new layout added by programme!
Database.GetViewports picked them all up though!

I tried deep cloning the viewport to multiple layouts , which worked but is buggy and unstable build. So don't use the addviewport extension shown below!


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.    public class MyCommands
  14.    {
  15.        [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
  16.        public void MyCommand() // This method can have any name
  17.        {
  18.            //layout to be skipped variables
  19.            int pageNumber = 1;
  20.            string layouttobeskipped = "";
  21.  
  22.            Document acDoc = Application.DocumentManager.MdiActiveDocument;
  23.            Database acCurDb = acDoc.Database;
  24.            ObjectIdCollection ids = new ObjectIdCollection();
  25.            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  26.            {
  27.                //Get ID of all layers for later freezing
  28.                LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
  29.                foreach (ObjectId item in acLyrTbl)
  30.                {
  31.                    ids.Add(item);
  32.                }
  33.  
  34.                //Get layout dictionary
  35.                DBDictionary dLayouts = acTrans.GetObject(acCurDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
  36.  
  37.                //Add New layouts for example testing
  38.                LayoutManager LM = LayoutManager.Current;
  39.                if (!dLayouts.Contains("New Layout"))
  40.                {
  41.                    LM.CreateLayout("New Layout");
  42.                    LM.CreateLayout("New Layout2");
  43.                }
  44.                else
  45.                {
  46.                    if (dLayouts.Contains("New Layout2"))
  47.                    {
  48.                        LM.CreateLayout("New Layout3");
  49.                        LM.CreateLayout("New Layout4");
  50.                    }
  51.                }
  52.  
  53.                //Add a viewport to each layout
  54.                foreach (DBDictionaryEntry item in dLayouts)
  55.                {
  56.                    Layout la = acTrans.GetObject(item.Value, OpenMode.ForRead) as Layout;
  57.                    //Gets name of layout to be skipped for later layer freezing
  58.                    if (la.TabOrder == pageNumber)
  59.                    {
  60.                        layouttobeskipped = la.LayoutName;
  61.                    }
  62.                    if (la.LayoutName != "Model")
  63.                    {
  64.                        //Add New Viewport
  65.                        acTrans.addviewport(la, acCurDb);
  66.                        la.Dispose();
  67.                    }
  68.                }
  69.  
  70.                //Sets layout to be skipped
  71.                Layout laSKIP = acTrans.GetObject(LM.GetLayoutId(layouttobeskipped), OpenMode.ForRead) as Layout;
  72.                ObjectId BTRIDLayoutskipped = laSKIP.BlockTableRecordId;
  73.  
  74.                //Cycle through database.viewport collection
  75.                ObjectIdCollection IDsVP = acCurDb.GetViewports(false);
  76.                foreach (ObjectId id in IDsVP)
  77.                {
  78.                    Viewport vp = acTrans.GetObject(id, OpenMode.ForWrite) as Viewport;
  79.                    //Ignore layoutskipped
  80.                    if (vp.BlockId != BTRIDLayoutskipped)
  81.                    {
  82.                        vp.FreezeLayersInViewport(ids.GetEnumerator());
  83.                        vp.UpdateDisplay();
  84.                    }
  85.                    vp.Dispose();
  86.                }
  87.                acDoc.Editor.Regen();
  88.                LM.Dispose();
  89.                acTrans.Commit();
  90.            }
  91.        }
  92.    }
  93.  
  94.    public static class myExtensions
  95.    {
  96.        public static void addviewport(this Transaction acTrans, Layout la, Database acCurDb)
  97.        {
  98.            using (LayoutManager LM = LayoutManager.Current)
  99.            {
  100.                //Get database.viewports
  101.                ObjectIdCollection DBVPids = acCurDb.GetViewports(true);
  102.  
  103.                if (DBVPids.Count == 0)
  104.                {
  105.                    //No initialised layouts - Make first layout current
  106.                    //viewport will be created - though need to check variable setting
  107.                    LM.CurrentLayout = la.LayoutName;
  108.                }
  109.  
  110.                //Check layout for viewports and clone if exists
  111.                ObjectIdCollection IDsVP = la.GetViewports();
  112.                Viewport VpNew = new Viewport();
  113.                if (IDsVP.Count != 0 && IDsVP.Count != 1)
  114.                {
  115.                    Viewport vp = acTrans.GetObject(IDsVP[1], OpenMode.ForRead) as Viewport;
  116.                    ObjectIdCollection ids = new ObjectIdCollection();
  117.                    ids.Add(vp.ObjectId);
  118.                    IdMapping IDMAP = new IdMapping();
  119.                    acCurDb.DeepCloneObjects(ids, la.BlockTableRecordId, IDMAP, false);
  120.                    ObjectIdCollection IDsVP2 = la.GetViewports();
  121.                    VpNew = acTrans.GetObject(IDsVP2[IDsVP.Count], OpenMode.ForWrite) as Viewport;
  122.  
  123.                }
  124.  
  125.                if (IDsVP.Count <= 1)
  126.                {
  127.                    if (IDsVP.Count == 0)
  128.                    {
  129.                        //Layout not initialized
  130.                        la.UpgradeOpen();
  131.                        la.Initialize();
  132.                        la.DowngradeOpen();
  133.                    }
  134.                    //No viewports but layout initialized
  135.                    //Find a viewport and clone it in
  136.                    DBVPids = acCurDb.GetViewports(false);
  137.                    ObjectIdCollection ids = new ObjectIdCollection();
  138.                    ids.Add(DBVPids[1]);
  139.                    IdMapping IDMAP = new IdMapping();
  140.                    acCurDb.DeepCloneObjects(ids, la.BlockTableRecordId, IDMAP, false);
  141.                    IDsVP = la.GetViewports();
  142.                    VpNew = acTrans.GetObject(IDsVP[1], OpenMode.ForWrite) as Viewport;
  143.                }
  144.                //Example Viewport Settings
  145.                VpNew.CenterPoint = new Point3d(100, 100, 0);
  146.                VpNew.Width = 500;
  147.                VpNew.Height = 100;
  148.                VpNew.UpdateDisplay();
  149.                VpNew.Dispose();
  150.            }
  151.        }
  152.    }
  153.  
  154. }

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #16 on: July 17, 2012, 11:13:11 am »
I've never tried to create a viewport on a layout, much less multiple layouts, but it sounds like something I'll end up looking into.  If I find anything, I'll let you know.
The funny thing about my code now is that after it's done running the viewports all look great, in that I'm getting what I wanted visually, but the layer manager is still being goofy.  I can double pick in the viewport that shows layer X and layer Y visible, then open the layer manager and see a number of layers including layer X and layer Y to be frozen.  It's like something just didn't quite finish.  I can save the drawing, close it and re-open it and the layer manager and the viewports are all just fine.  Weird.
Well thanks again for the info you posted.  I'm going to check out your code:
Code - C#: [Select]
  1. //Sets layout to be skipped
  2. Layout laSKIP = acTrans.GetObject(LM.GetLayoutId(layouttobeskipped), OpenMode.ForRead) as Layout;
  3. ObjectId BTRIDLayoutskipped = laSKIP.BlockTableRecordId;
  4. //Cycle through database.viewport collection  
  5. ObjectIdCollection IDsVP = acCurDb.GetViewports(false);
  6. foreach (ObjectId id in IDsVP)
  7. {
  8. Viewport vp = acTrans.GetObject(id, OpenMode.ForWrite) as Viewport;
  9. //Ignore layoutskipped
  10. if (vp.BlockId != BTRIDLayoutskipped)
  11. {
  12. vp.FreezeLayersInViewport(ids.GetEnumerator());
  13. vp.UpdateDisplay();
  14. }
  15. vp.Dispose();
  16. }
  17. acDoc.Editor.Regen();
  18. LM.Dispose();
  19. acTrans.Commit();

Maybe that will help with the layer freeze and layer manager.

Jeff H

  • Needs a day job
  • Posts: 4602
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #17 on: July 17, 2012, 07:51:30 pm »
I know or was told that it was a confirmed bug when a LayerTableRecord IsHidden was true and you set it to false it would not show back in the UI.
 
There might be someway to refresh the manager, but to see if it is a similar problem, after running your code add a new layer then see if the layer manager updates.
 
 

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #18 on: July 18, 2012, 11:03:57 am »
Hi Jeff,
I was able to add a layer, but the layers manager didn't change the fozen/thawed layers.
I tried a new test morning.  My code launches AutoCad 2010 (I'm using Visual Studio 2010 and the 2010 objectARX) and after running the program the layers are as I described.  So I launched the same drawing in AutoCad 2008 and ran the same program.  There was no issue at all with the layer manager and frozen/thawed layers.  In AutoCad 2008, the frozen layers were frozen and the thawed layers were thawed.

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #19 on: July 18, 2012, 11:11:17 am »
I just tested it in AutoCad 2012 and it worked great.  The layer manager showed the correct forzen and thawed layers for all the viewports.  Have you heard of this kind of bug in AutoCad 2010?

MGorecki

  • Newt
  • Posts: 25
Re: Viewport Layers Set To "Frozen" Are Still Visible
« Reply #20 on: July 20, 2012, 05:51:46 pm »
Ok, just installed the Service Pack 2 for AutoCad 2010.  That fixed the Layer Manager freeze/thaw issue.