Message 1 of 21
Erasing Layouts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
{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.