Author Topic: Is there a better way to lock all viewports?  (Read 2195 times)

0 Members and 1 Guest are viewing this topic.

bchapman

  • Guest
Is there a better way to lock all viewports?
« on: February 08, 2013, 10:50:13 PM »
Just finished this. Seems convoluted... is there a better solution?

Code: [Select]
namespace BCLOCKVP
{
    public class BCCLOCKVIEWPORTS
    {
        [CommandMethod("BCC:LOCKVP")]
        static public void BCCLOCKVP()
        {
            Document curDoc = Application.DocumentManager.MdiActiveDocument;
            Database docdb = curDoc.Database;
            ObjectIdCollection BCCVIEWPORTS = docdb.GetViewports(true);
            LayoutManager lm = LayoutManager.Current;
            Editor ed = curDoc.Editor;

            using (Transaction ts = curDoc.TransactionManager.StartTransaction())
            {
                DBDictionary layoutDict = docdb.LayoutDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;

                foreach (DBDictionaryEntry de in layoutDict)
                {
                    Layout layout = de.Value.GetObject(OpenMode.ForRead) as Layout;
                    string layoutName = layout.LayoutName;
                    lm.CurrentLayout = layoutName;
                    ObjectIdCollection vpIdCol = layout.GetViewports();
                    foreach (ObjectId vpId in vpIdCol)
                    {
                        Viewport vp = vpId.GetObject(OpenMode.ForWrite) as Viewport;
                        vp.Locked = true;
                        ed.WriteMessage("VP {0} is Locked", layoutName);

                    }
               
                }
                ts.Commit();
            }

        }
    }
}

bchapman

  • Guest
Re: Is there a better way to lock all viewports?
« Reply #1 on: February 09, 2013, 08:50:39 PM »
None?  Apparently I'm pro after a week :p bahahahahahah
Just finished this. Seems convoluted... is there a better solution?

Code: [Select]
namespace BCLOCKVP
{
    public class BCCLOCKVIEWPORTS
    {
        [CommandMethod("BCC:LOCKVP")]
        static public void BCCLOCKVP()
        {
            Document curDoc = Application.DocumentManager.MdiActiveDocument;
            Database docdb = curDoc.Database;
            ObjectIdCollection BCCVIEWPORTS = docdb.GetViewports(true);
            LayoutManager lm = LayoutManager.Current;
            Editor ed = curDoc.Editor;

            using (Transaction ts = curDoc.TransactionManager.StartTransaction())
            {
                DBDictionary layoutDict = docdb.LayoutDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;

                foreach (DBDictionaryEntry de in layoutDict)
                {
                    Layout layout = de.Value.GetObject(OpenMode.ForRead) as Layout;
                    string layoutName = layout.LayoutName;
                    lm.CurrentLayout = layoutName;
                    ObjectIdCollection vpIdCol = layout.GetViewports();
                    foreach (ObjectId vpId in vpIdCol)
                    {
                        Viewport vp = vpId.GetObject(OpenMode.ForWrite) as Viewport;
                        vp.Locked = true;
                        ed.WriteMessage("VP {0} is Locked", layoutName);

                    }
               
                }
                ts.Commit();
            }

        }
    }
}

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Is there a better way to lock all viewports?
« Reply #2 on: February 09, 2013, 09:12:26 PM »
Any reason for not just enumerating through ObjectIds from

Quote
ObjectIdCollection BCCVIEWPORTS = docdb.GetViewports(true);


Also the argument passed for GetViewports
Quote
Input flag indicating whether to return paperspace viewports associated with layouts
In your situation you would want it to be false, and using this method you might to initialize uninitialized viewports.
« Last Edit: February 09, 2013, 09:15:53 PM by Jeff H »

bchapman

  • Guest
Re: Is there a better way to lock all viewports?
« Reply #3 on: February 09, 2013, 09:23:08 PM »
Thanks! No reason...  I didn't know how to siphon through all the layouts to get the viewports... so I cut that piece from an unrelated program out there... don't remember what it was.

is this what you were recommending?   

Code: [Select]
// locks viewports

namespace BCLOCKVP
{
    public class BCCLOCKVIEWPORTS
    {
        [CommandMethod("BCC:LOCKVP")]
        static public void BCCLOCKVP()
        {
            Document curDoc = Application.DocumentManager.MdiActiveDocument;
            Database docdb = curDoc.Database;
            ObjectIdCollection BCCVIEWPORTS = docdb.GetViewports(true);
           
            using (Transaction ts = curDoc.TransactionManager.StartTransaction())
            {
                foreach (ObjectId de in BCCVIEWPORTS)
                {
                        Viewport vp = de.GetObject(OpenMode.ForWrite) as Viewport;
                        vp.Locked = true;
                       
                    }
               ts.Commit();
                }
               
            }

        }
    }

bchapman

  • Guest
Re: Is there a better way to lock all viewports?
« Reply #4 on: February 09, 2013, 09:41:44 PM »
By the way this forum needs a + system to give kudos to peeps like you.

Any reason for not just enumerating through ObjectIds from

Quote
ObjectIdCollection BCCVIEWPORTS = docdb.GetViewports(true);


Also the argument passed for GetViewports
Quote
Input flag indicating whether to return paperspace viewports associated with layouts
In your situation you would want it to be false, and using this method you might to initialize uninitialized viewports.