Hi all,
i would like to delete all layouts. I tested these two method.
I have problem with LayoutManager becuase delete is very slow.
If DWG is complex and has for example 20 pages it toos lots of time (because function still swap page by page).
This second method is much more fastert but i will getting error if i delete all pages (even if i before created one which i jump over).
i know that layer manager delete all dependencies but is there any way how to delete all old layouts faster?
(in second method my plan was call purge function after that)
private static bool DeleteAllLayouts()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor ed = acDoc.Editor;
using (DocumentLock LockDoc = acDoc.LockDocument())
{
using (Database db = acDoc.Database)
{
// metode 1
using (Transaction tr = db.TransactionManager.StartTransaction())
{
using (DBDictionary layoutDict = (DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForWrite))
{
LayoutManager acLayoutMgr = LayoutManager.Current;
acLayoutMgr.CreateLayout("LayoutXX");
foreach (DBDictionaryEntry entry in layoutDict)
{
string layoutName = entry.Key.ToUpper();
if (layoutName != "MODEL" && layoutName != "LAYOUTXX")
{
acLayoutMgr.DeleteLayout(layoutName);
}
}
acLayoutMgr.RenameLayout("LayoutXX", "Layout1");
acLayoutMgr.CurrentLayout = "Model";
tr.Commit();
acLayoutMgr.Dispose();
}
}
// metode 2
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary layoutsEx = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
BlockTable blkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
using (BlockTableRecord blkBlkRec = new BlockTableRecord())
{
int layoutCount = layoutsEx.Count - 1;
blkBlkRec.Name = "*Paper_Space" + layoutCount.ToString();
blkTbl.Add(blkBlkRec);
tr.AddNewlyCreatedDBObject(blkBlkRec, true);
DBDictionary layouts = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) as DBDictionary;
using (Layout lay = new Layout())
{
lay.LayoutName = "LayoutXX";
lay.AddToLayoutDictionary(db, blkBlkRec.ObjectId);
tr.AddNewlyCreatedDBObject(lay, true);
}
}
tr.Commit();
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
using (DBDictionary layoutDict = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary)
{
foreach (DBDictionaryEntry de in layoutDict)
{
Layout layout = (Layout)tr.GetObject(de.Value, OpenMode.ForWrite);
string layoutName = de.Key;
if (layoutName != "Model" && layoutName != "LayoutXX")
{
layout.Erase();
}
}
tr.Commit();
ed.Regen();
}
}
}
}
return true;
}
The following example has been tested on a drawing with 75 complex layouts. It appears to me that you don't need to create a layout to keep. The layout manager will create a new layout if you delete all of them.
<CommandMethod("LayoutDeleteAll")>
Public Sub CmdLayoutDeleteAll()
GetCurrent() 'CommandBase - Sets Private aDoc, db, and ed
Dim lm As LayoutManager = LayoutManager.Current
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim dict As DBDictionary = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead)
For Each entry As DBDictionaryEntry In dict
Dim name As String = entry.Key.ToUpper
If name <> "MODEL" Then
lm.DeleteLayout(name)
End If
Next
tr.Commit()
End Using
End Sub
You are correct that I don’t have to create a new layout this way…. but your sample is exactly my method 1.
I just tested (your VB code) and I have problem with this command (maybe it depends on complexity of model in model space)
lm.DeleteLayout(name)
every time when it is call this command model space behaves like refresh and there is delay 1-2s… if there is simply model in model space that this function is fast
Like you can see there is take lots of the time and pc is powerful for CAD purpose
I see what you mean. I guess I don't know. My model space wasn't too complex, but the layouts were and there were 75 of them. The command executed very quickly.
Can't find what you're looking for? Ask the community or share your knowledge.