Remove Layout

Remove Layout

ccalvo12
Enthusiast Enthusiast
1,981 Views
2 Replies
Message 1 of 3

Remove Layout

ccalvo12
Enthusiast
Enthusiast

Dear:
The following code attempts to delete a layout of a closed drawing. In the procedure, lines down, give like data file path and should result in me a copy of this least Layout indicated. In the picture you can see that does not eliminate the Layotu if not rather renames. there is something wrong that I'm doing in my code?

Thank you

 

 

Private Sub datosLyDWg(ByVal FileDWG As String)
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Using doc.LockDocument
            Dim dbFuente As New Database(False, True)
            Using dbFuente
                dbFuente.ReadDwgFile(FileDWG, System.IO.FileShare.ReadWrite, False, Nothing)
                Dim NombreDibujo As String = System.IO.Path.GetFileName(FileDWG)
                Using tr As Transaction = dbFuente.TransactionManager.StartTransaction
                    Dim layoutDicc As DBDictionary = TryCast(tr.GetObject(dbFuente.LayoutDictionaryId, OpenMode.ForRead, False), DBDictionary)
                    For Each entrada As DBDictionaryEntry In layoutDicc
                        Dim layoutId As ObjectId = entrada.Value
                        Dim layout As Layout = TryCast(tr.GetObject(layoutId, OpenMode.ForWrite), Layout)
                        If Not layout.LayoutName.Equals("Model") Then
                            If layout.LayoutName.Equals("1800A") Then
                                layout.Erase()                                
                            End If                            
                        End If
                    Next
                    dbFuente.SaveAs("D:\TEST_SHEEP-SET\Test\Exit\1800A.dwg", DwgVersion.Current)
                End Using
            End Using
        End Using
    End Sub
0 Likes
Accepted solutions (1)
1,982 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Here's an example using the target databse LayoutManager:

 

        private string DeleteLayout(string fileName, string layoutName)
        {
            Database currentDatabase = HostApplicationServices.WorkingDatabase;
            try
            {
                using (Database targetDatabase = new Database(false, true))
                {
                    targetDatabase.ReadDwgFile(fileName, System.IO.FileShare.ReadWrite, false, null);
                    HostApplicationServices.WorkingDatabase = targetDatabase;
                    LayoutManager lm = LayoutManager.Current;
                    lm.DeleteLayout(layoutName);
                    targetDatabase.SaveAs(fileName, DwgVersion.Current);
                }
                return "Delete layout succeeded";
            }
            catch (System.Exception ex)
            {
                return "\nDelete layout failed: " + ex.Message;
            }
            finally
            {
                HostApplicationServices.WorkingDatabase = currentDatabase;
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

ccalvo12
Enthusiast
Enthusiast

Thanks Dear, now able to continue with the application without changing course.

 

0 Likes