Identify un-initialized layouts

By Virupaksha Aithal

One way to identify the un- initialized layout is check the number of viewports in the layout. If the number of viewports are greater than 0, then layout is initialized. Refer below sample code

[CommandMethod("Testlayout")]
public static void Testlayout()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
 
    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        DBDictionary dic = Tx.GetObject(db.LayoutDictionaryId,
                                   OpenMode.ForRead) as DBDictionary;
 
        Layout layout = null;
        foreach (DBDictionaryEntry entry in dic)
        {
            layout = Tx.GetObject(entry.Value, 
                                         OpenMode.ForRead) as Layout;
 
            if (!layout.ModelType)
            {
                ObjectIdCollection ids = layout.GetViewports();
 
                if (ids.Count == 0)
                {
                    ed.WriteMessage(layout.LayoutName +
                            " is not initializedn");
                }
                else
                {
                    ed.WriteMessage(layout.LayoutName +
                            " is initializedn");
                }
            }            
        }
        Tx.Commit();
    }
}

Comments

2 responses to “Identify un-initialized layouts”

  1. I have a 2015 dwg file where some layouts contains entities + viewports, but the GetVieports function gives me empty collection. Is this behaviour planed?
    Once you switch to a layout, GetVieports should return one viewport permanently. or am I wrong?

  2. You can not use Layout.GetViewports() to find if a layout is initialized because it returns an empty collection if the layout has never been activated. It is explained in the docs.
    You can use Database.GetViewport(bGetPaperspaceVports: true) to get all the viewports in the drawing. After that, you can find the layout of each viewport: get the owner BlockTableRecord with viewport.BlockId, then you have a property LayoutId which allow you to match the viewport to its layout.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading