.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Erasing Layouts

20 REPLIES 20
Reply
Message 1 of 21
s.jabs
6106 Views, 20 Replies

Erasing Layouts

Does anyone have an example of how to properly remove a layout? I'm writing a routine to strip down drawings to an optimized (and standardized) format, but I'm having trouble with this one bit of code:

{code}

Protected Friend Shared Sub RemoveLayouts(ByVal dbAcad As Database)
Dim edCmdLine As Editor = AcApp.Application.DocumentManager.MdiActiveDocument.Editor
Dim alLayouts As ArrayList = New ArrayList()
Dim lmAcad As LayoutManager = LayoutManager.Current
Dim oiTemp As ObjectId

Try
' Create empty layout to satisfy one minimum layout requirement
oiTemp = lmAcad.CreateLayout("Some-Random-Name")

Using trAcad As Transaction = dbAcad.TransactionManager.StartTransaction()
Dim deItem As DictionaryEntry
Dim dicLayouts As DBDictionary = DirectCast(trAcad.GetObject(dbAcad.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
Dim intI As Integer
Dim lItem As Layout
Dim oiBlock As ObjectId
Dim oicViewports As ObjectIdCollection
Dim vItem As Viewport

' Get the list of layouts
For Each deItem In dicLayouts
alLayouts.Add(DirectCast(trAcad.GetObject(CType(deItem.Value, ObjectId), OpenMode.ForRead), Layout).LayoutName)
Next

' Cycle through the layouts, deleting all but model space and one layout
For intI = 0 To alLayouts.Count - 1
If alLayouts(intI) <> "Model" And alLayouts(intI) <> "Some-Random-Name" Then
Try
lItem = DirectCast(trAcad.GetObject(lmAcad.GetLayoutId(alLayouts(intI)), OpenMode.ForWrite), Layout)
oiBlock = lItem.BlockTableRecordId

' Deals with viewports
oicViewports = lItem.GetViewports()
For Each oiViewport In oicViewports
vItem = DirectCast(trAcad.GetObject(oiViewport, OpenMode.ForWrite), Viewport)
vItem.Erase()
Next

lmAcad.DeleteLayout(lItem.LayoutName)
Catch ex As Exception
End Try
End If
Next

trAcad.Commit()
End Using

Using trAcad As Transaction = dbAcad.TransactionManager.StartTransaction()
Dim lItem As Layout

lItem = DirectCast(trAcad.GetObject(oiTemp, OpenMode.ForWrite), Layout)
lItem.Initialize()
lItem.LayoutName = "Layout1"

trAcad.Commit()
End Using
Catch ex As Exception
End Try
End Sub
{code}

If I don't remove the viewports "manually", I get drawing corruption. If the layout to be removed contains a block with attribute definitions, that too causes corruption. If I get the BlockTableRecord from the layout, and erase it instead, I don't get corruption, but the drawing crashes when I switch layout tabs.
20 REPLIES 20
Message 21 of 21
SENL1362
in reply to: AUGIFrance

 

Before (in AutoCAD MAP 2014):

Screenshot_1.png

 

        [CommandMethod("TestDelLayout")]
        public void TestDelLayout()
        {
            Document doc = null;
            Database db = null;
            Editor ed = null;

            try
            {
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                db = doc.Database;
                ed = doc.Editor;
                var layoutManager=LayoutManager.Current;

                //Get the Layouts in the order visible to the user;
                //Delete the default Layers, Layout*
                foreach (string layoutName in GetTabOrderedLayoutNames(db))
                {
                    if (Regex.IsMatch(layoutName,"^LAYOUT[0-9]+", RegexOptions.IgnoreCase))
                        layoutManager.DeleteLayout(layoutName);
                }
                ed.Regen();
            }
            catch (System.Exception ex)
            {
                if (ed != null)
                    ed.WriteMessage("\n Error: " + ex.Message);
                else
                    MessageBox.Show("Error: " + ex.Message, "TestDelLayout", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }




        public static List<string> GetTabOrderedLayoutNames(Database db)
        {
            if (db == null)
                return null;

            var tabOrderedLayouts = new Dictionary<int, string>();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry layoutEntry in layoutDict)
                {
                    Layout layout = (Layout)tr.GetObject(layoutEntry.Value, OpenMode.ForRead);
                    if (!layout.ModelType)
                    tabOrderedLayouts[layout.TabOrder] = layout.LayoutName;
                }
                tr.Commit();
            }
            return tabOrderedLayouts.OrderBy(n => n.Key).Select(n => n.Value).ToList();
        }

After:

Screenshot_2.png

 

 

No Commit in the "Delete" procedure only ed.Regen();

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost