.NET

Reply
New Member
jeff.hackney
Posts: 4
Registered: ‎10-17-2007
Message 1 of 4 (227 Views)
Accepted Solution

Inconsistent Layout Renaming

227 Views, 3 Replies
10-23-2013 07:35 AM

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

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

 

Valued Mentor
Ajilal.Vijayan
Posts: 446
Registered: ‎01-12-2008
Message 2 of 4 (194 Views)

Re: Inconsistent Layout Renaming

10-23-2013 09:41 PM in reply to: jeff.hackney

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.

 

New Member
jeff.hackney
Posts: 4
Registered: ‎10-17-2007
Message 3 of 4 (183 Views)

Re: Inconsistent Layout Renaming

10-24-2013 06:52 AM in reply to: jeff.hackney

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.

ADN Support Specialist
Virupaksha.aithal
Posts: 100
Registered: ‎06-15-2011
Message 4 of 4 (139 Views)

Re: Inconsistent Layout Renaming

10-28-2013 04:17 AM in reply to: jeff.hackney

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

 



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.