Author Topic: Delete Layouts from SideDatabase  (Read 1615 times)

0 Members and 1 Guest are viewing this topic.

djee

  • Newt
  • Posts: 49
Delete Layouts from SideDatabase
« on: May 10, 2017, 12:39:25 PM »
Is there any way to delete layouts from a sidedatabase? Using the Layout named dictionary instead of the layoutManager? I've been using the below code but it's not deleting the layout itself...
Code: [Select]
   Public Shared Sub DeleteTab(ByVal MyFileCollection As MyFiles)
        For Each AutoCadFile In MyFileCollection
            Using ExistDatabase As New Database(False, True)
                ExistDatabase.ReadDwgFile(AutoCadFile.SelectedFilePath, FileOpenMode.OpenForReadAndAllShare, False, Nothing)
                ExistDatabase.CloseInput(True)
                Using AcTrans As Transaction = ExistDatabase.TransactionManager.StartTransaction
                    Dim MyLayouts As DBDictionary = AcTrans.GetObject(ExistDatabase.LayoutDictionaryId, OpenMode.ForRead)
                    ' Step through each named layout and Model
                    For Each item As DBDictionaryEntry In MyLayouts
                        If item.Key = "English_Metric" Then
                            Dim MyItem As DBObject = AcTrans.GetObject(item.Value, OpenMode.ForWrite)
                            MyItem.Erase(True)
                        End If
                    Next
                    ExistDatabase.SaveAs(AutoCadFile.SelectedFilePath, DwgVersion.Current)
                    AcTrans.Commit()
                End Using
            End Using
        Next
    End Sub

djee

  • Newt
  • Posts: 49
Re: Delete Layouts from SideDatabase
« Reply #1 on: May 10, 2017, 01:10:28 PM »
Ok, I've just found this answer from _Gile at this location https://forums.autodesk.com/t5/net/remove-layout/td-p/5824050?nobounce. I did not though about changing the working database to the external drawing...

djee

  • Newt
  • Posts: 49
Re: Delete Layouts from SideDatabase
« Reply #2 on: May 10, 2017, 02:06:19 PM »
For some reason, deleting layouts seems to modify the viewports in my remaining layouts...??
Code: [Select]
   Public Shared Sub DeleteTab(ByVal MyFileCollection As MyFiles, ByVal LayoutName As String)
        Dim currentDatabase As Database = HostApplicationServices.WorkingDatabase
        For Each AutoCadFile In MyFileCollection
            Try
                Using targetDatabase As New Database(False, True)
                    targetDatabase.ReadDwgFile(AutoCadFile.SelectedFilePath, System.IO.FileShare.ReadWrite, False, Nothing)
                    HostApplicationServices.WorkingDatabase = targetDatabase
                    Using tr As Transaction = targetDatabase.TransactionManager.StartTransaction
                        Dim lm As LayoutManager = LayoutManager.Current
                        lm.DeleteLayout(LayoutName)
                        targetDatabase.SaveAs(AutoCadFile.SelectedFilePath, DwgVersion.Current)
                        targetDatabase.CloseInput(True)
                    End Using
                End Using
            Finally
                HostApplicationServices.WorkingDatabase = currentDatabase
            End Try
        Next
    End Sub