I have written this code to check the name of the layouts and determine if the are to our company standard or not and if they are close enough to determine what they should be rename them. I create a drawing with severl differant layout names to test the code but I am getting inconsistent results. The drawing I created has 9 layouts named M2_03, M2_04, m3_01, m3_02, tm01_25, tm2.05, tm2.06a, TM2_01, and TM2_02A. It seems to be crashing on the tm2.06a but I can not figure out why.
Public Class StandFix <Autodesk.AutoCAD.Runtime.CommandMethod("StandFix")> _ Public Sub StandardFixer() Dim ErrString As String = "" ErrString = ErrString & StandardizeLayoutNames() MsgBox(ErrString, MsgBoxStyle.OkOnly, "Non-Standard Entities Found") End Sub Private Function StandardizeLayoutNames() Dim LayoutErrs As String = "" Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = acDoc.Database ' Get the layout dictionary of the current database Using acTrans As Transaction = db.TransactionManager.StartTransaction() Dim item As DBDictionaryEntry = Nothing Dim lays As DBDictionary = acTrans.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) For Each item In lays Dim acLayoutMgr As LayoutManager = LayoutManager.Current Dim objID As ObjectId = item.Value Dim acLayout As Layout = acTrans.GetObject(objID, OpenMode.ForWrite) Dim layoutname As String = acLayout.LayoutName If layoutname Like "T[MPF]#_##[A-Z]" Or layoutname Like "T[MPF]#_##" Or layoutname Like "[MPF]#_##[A-Z]" Or layoutname Like "[MPF]#_##" Or layoutname = "Model" Then GoTo SkipLayout Else ' MsgBox(layoutname) Dim LayoutNameOld As String = layoutname layoutname = layoutname.ToUpper() If layoutname Like "T[MPF]#_##[A-Z]" Or layoutname Like "T[MPF]#_##" Or layoutname Like "[MPF]#_##[A-Z]" Or layoutname Like "[MPF]#_##" Then LayoutErrs = LayoutErrs & "Layout: " & LayoutNameOld & " was renamed to " & layoutname & " to meet company standards" & vbCr Else If layoutname Like "T[MPF]#.##[A-Z]" Or layoutname Like "T[MPF]#.##" Or layoutname Like "[MPF]#.##[A-Z]" Or layoutname Like "[MPF]#.##" Then layoutname = layoutname.Replace(".", "_") LayoutErrs = LayoutErrs & "Layout: " & LayoutNameOld & " was renamed to " & layoutname & " to meet company standards" & vbCr Else LayoutErrs = LayoutErrs & "Layout: " & LayoutNameOld & " is not a standard layout name" & vbCr End If End If Try acLayoutMgr.RenameLayout(LayoutNameOld, layoutname) Catch ex As Exception MsgBox(ex.Message) End Try End If SkipLayout: Next acTrans.Commit() acTrans.Dispose() acDoc.Editor.Regen() End Using Return LayoutErrs End Function End Class
Can anyone tell me why sometimes it work and checks all the layouts, but most of the time it only does some of the layouts.
Thanks
Jeff
Solved! Go to Solution.
Solved by Virupaksha_aithal. Go to Solution.
@jeff.hackney wrote:The drawing I created has 9 layouts named M2_03, M2_04, m3_01, m3_02, tm01_25, tm2.05, tm2.06a, TM2_01, and TM2_02A. It seems to be crashing on the tm2.06a but I can not figure out why.
I tried your code by creating 9 layouts with these names.
It didnt crash on tm2.06a.
Thanks, I just create a new drawing with the same names hoping that maybe the old drawing was corrupt in some way, but I am still getting the same result. The first time I ran it is worked great and all the layouts renamed correctly. I then closed the drawing without saving and reopened it and ran the code again, this time it renamed all except for the tm2.03a. I have tried it several more time and 90% of the time is does not rename all the layouts. I am completely lost as to why it works sometimes and not other.
Hi,
Try below code. Code is same as yours, but puts the calling of RenameLayout API out of transaction.
<Autodesk.AutoCAD.Runtime.CommandMethod("StandFix")> _ Public Sub StandardFixer() Dim ErrString As String = "" ErrString = ErrString & StandardizeLayoutNames() MsgBox(ErrString, MsgBoxStyle.OkOnly, "Non-Standard Entities Found") End Sub Private Function StandardizeLayoutNames() Dim LayoutErrs As String = "" Dim list As New System.Collections.Generic.Dictionary(Of String, String) Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = acDoc.Database ' Get the layout dictionary of the current database Using acTrans As Transaction = db.TransactionManager.StartTransaction() Dim item As DBDictionaryEntry = Nothing 'Dim acLayoutMgr As LayoutManager = LayoutManager.Current Dim lays As DBDictionary = acTrans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) For Each item In lays Dim objID As ObjectId = item.Value Dim acLayout As Layout = acTrans.GetObject(objID, OpenMode.ForRead) Dim layoutname As String = acLayout.LayoutName If layoutname Like "T[MPF]#_##[A-Z]" Or layoutname Like "T[MPF]#_##" Or layoutname Like "[MPF]#_##[A-Z]" Or layoutname Like "[MPF]#_##" Or layoutname = "Model" Then 'do anything.. Else ' MsgBox(layoutname) Dim LayoutNameOld As String = layoutname layoutname = layoutname.ToUpper() If layoutname Like "T[MPF]#_##[A-Z]" Or layoutname Like "T[MPF]#_##" Or layoutname Like "[MPF]#_##[A-Z]" Or layoutname Like "[MPF]#_##" Then LayoutErrs = LayoutErrs & "Layout: " & LayoutNameOld & " was renamed to " & layoutname & " to meet company standards" & vbCr Else If layoutname Like "T[MPF]#.##[A-Z]" Or layoutname Like "T[MPF]#.##" Or layoutname Like "[MPF]#.##[A-Z]" Or layoutname Like "[MPF]#.##" Then layoutname = layoutname.Replace(".", "_") LayoutErrs = LayoutErrs & "Layout: " & LayoutNameOld & " was renamed to " & layoutname & " to meet company standards" & vbCr Else LayoutErrs = LayoutErrs & "Layout: " & LayoutNameOld & " is not a standard layout name" & vbCr End If End If Try list.Add(LayoutNameOld, layoutname) ' acLayoutMgr.RenameLayout(LayoutNameOld, layoutname) Catch ex As System.Exception MsgBox(ex.Message) End Try End If Next acTrans.Commit() acTrans.Dispose() End Using For Each pair In list Dim acLayoutMgr As LayoutManager = LayoutManager.Current acLayoutMgr.RenameLayout(pair.Key, pair.Value) Next acDoc.Editor.Regen() Return LayoutErrs End Function