TheSwamp

Code Red => .NET => Topic started by: bchapman on February 08, 2013, 10:50:13 PM

Title: Is there a better way to lock all viewports?
Post by: bchapman 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();
            }

        }
    }
}
Title: Re: Is there a better way to lock all viewports?
Post by: bchapman 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();
            }

        }
    }
}
Title: Re: Is there a better way to lock all viewports?
Post by: Jeff H 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.
Title: Re: Is there a better way to lock all viewports?
Post by: bchapman 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();
                }
               
            }

        }
    }
Title: Re: Is there a better way to lock all viewports?
Post by: bchapman 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.