Code Red => .NET => Topic started by: nobody on August 31, 2016, 11:50:52 PM

Title: Erase all Layouts
Post by: nobody on August 31, 2016, 11:50:52 PM
Found this code below here:

It looks pretty straight forward. I can't see anything really majorly wrong with it but would like others input.  If you have experience with deleting all layouts in a drawing this way would you mind offering input?

Code - C#: [Select]
  1. [CommandMethod("EraseAllLayouts")]
  2. public static void EraseAllLayouts() {
  4.    Document doc = Application.DocumentManager.MdiActiveDocument;
  5.    Database db = doc.Database;
  6.    Editor ed = doc.Editor;
  8.    using (Transaction tr = db.TransactionManager.StartTransaction()) {
  10.       // ACAD_LAYOUT dictionary.
  11.       DBDictionary layoutDict = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
  13.       // Iterate dictionary entries.
  14.       foreach (DBDictionaryEntry de in layoutDict) {
  15.          string layoutName = de.Key;
  16.          if (layoutName != "Model") {
  17.             LayoutManager.Current.DeleteLayout(layoutName); // Delete layout.
  18.          }
  19.       }
  20.    }
  22.    ed.Regen();   // Updates AutoCAD GUI to relect changes.
  23. }
Title: Re: Erase all Layouts
Post by: MickD on September 01, 2016, 05:40:30 AM
Looks ok except you can't delete all layouts, you must keep at least one paper space layout.
What I've done is have one layout with "-template" appended and use it as the template layout and clone it for new layouts, I then use a condition in the if to filter it out of the loop and not delete it.

something like this:
Code - C#: [Select]
  1.                 // Iterate dictionary entries.
  2.                 foreach (DBDictionaryEntry de in layoutDict)
  3.                 {
  4.                     string layoutName = de.Key;
  5.                     if (layoutName != "Model" && !layoutName.Contains("-template"))
  6.                     {
  7.                         LayoutManager.Current.DeleteLayout(layoutName); // Delete layout.
  8.                     }
  9.                 }
Title: Re: Erase all Layouts
Post by: Keith Brown on September 01, 2016, 08:23:25 AM
Don't forget to commit the transaction.  :whistling: :whistling:
Title: Re: Erase all Layouts
Post by: rkmcswain on September 01, 2016, 05:07:34 PM
Quote from: MickD
Looks ok except you can't delete all layouts, you must keep at least one paper space layout.

I know the context here is .NET, but if you do something like this, it will delete all the layouts and then make a new "Layout1" as a default layout. Even if you are in a layout when you run it.

Code: [Select]
(foreach item (layoutlist) (vl-cmdf "._layout" "_delete" item))
Title: Re: Erase all Layouts
Post by: jon on June 13, 2019, 07:42:32 AM
When doing LayoutManager operations on external DWGs through the ReadDwgFile() method, I got some pretty strange behavior. The solution that worked for me was to set the active layout to be modelspace before and after doing operations. Hopefully this will save someone some time. From what I've gathered this must be a bug in the LayoutManager itself, probably related to cached viewports or a regen, as the code worked perfectly as long as I used it on DWGs I had opened.

Code: [Select]
//My code is looping through all the layouts in a DWG looking for one specific layout that I know the name of.
//All the other layouts are to be deleted.
DBDictionary layouts;

Database externalDatabase = new Database(false, true);
externalDatabase.ReadDwgFile(@"C:\Users\MyUser\Drawing.dwg", FileOpenMode.OpenForReadAndAllShare, true, "");
HostApplicationServices.WorkingDatabase = externalDatabase;
ObjectId modelSpaceId = LayoutManager.Current.GetLayoutId("Model");

using (var transaction = externalDatabase.TransactionManager.StartTransaction())
    //In the method I use I have to set OpenMode.ForWrite
    //but in most cases I guess OpenMode.ForRead would be enough
    layouts = (DBDictionary)transaction.GetObject(externalDatabase.LayoutDictionaryId, OpenMode.ForWrite);

foreach (DBDictionaryEntry layout in layouts)
    if (String.Equals(layout.Key, "Model"))
    else if (String.Equals(layout.Key, "Name Of Layout To Keep"))
//Don't forget to switch HostApplicationServices.WorkingDatabase back to the original database

I found this thread on Google when I looked for people having the same issue as me, so in case anyone else finds this thread I thought it was cleaner to post the solution here rather than creating my own thread.