Author Topic: Erase all Layouts  (Read 4654 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Erase all Layouts
« on: August 31, 2016, 11:50:52 PM »
Found this code below here: http://forums.autodesk.com/t5/net/erasing-layouts/td-p/2669032

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() {
  3.  
  4.    Document doc = Application.DocumentManager.MdiActiveDocument;
  5.    Database db = doc.Database;
  6.    Editor ed = doc.Editor;
  7.  
  8.    using (Transaction tr = db.TransactionManager.StartTransaction()) {
  9.  
  10.       // ACAD_LAYOUT dictionary.
  11.       DBDictionary layoutDict = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
  12.  
  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.    }
  21.  
  22.    ed.Regen();   // Updates AutoCAD GUI to relect changes.
  23. }

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Erase all Layouts
« Reply #1 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.                 }
  10.  
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Erase all Layouts
« Reply #2 on: September 01, 2016, 08:23:25 AM »
Don't forget to commit the transaction.  :whistling: :whistling:
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: Erase all Layouts
« Reply #3 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))

jon

  • Guest
Re: Erase all Layouts
« Reply #4 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.

Example:
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);
    transaction.Commit();
    transaction.Dispose();
}

foreach (DBDictionaryEntry layout in layouts)
{
    if (String.Equals(layout.Key, "Model"))
    {
        continue;
    }
    else if (String.Equals(layout.Key, "Name Of Layout To Keep"))
    {
        continue;
    }
    else
    {
        LayoutManager.Current.SetCurrentLayoutId(modelSpaceId);
        LayoutManager.Current.DeleteLayout(layout.Key);
        LayoutManager.Current.SetCurrentLayoutId(modelSpaceId);
    }
}
//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.
« Last Edit: June 13, 2019, 07:47:26 AM by jon »