Author Topic: Rename layouts custom way  (Read 940 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Rename layouts custom way
« on: May 19, 2013, 02:37:37 PM »
   
Code: [Select]
VB.NET
        'Public Class LayoutTools

        ' declare layout names outside of method, keep them empty:
        Private oldname As String = ""
        Private newname As String = ""
        <CommandMethod("lre")> _
        Public Sub testLayoutsRename()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = doc.Editor
            Dim tr As Transaction = db.TransactionManager.StartTransaction()

            Dim ltnames As New SortedList(Of String, String)()
           ' lst of pairs <old layout name,new layout name>: '
            ltnames.Add("Layout1", "General")
            ltnames.Add("Layout2", "Floor Plan 1")
            ltnames.Add("Layout3", "Floor Plan 2")
            ltnames.Add("Layout4", "Floor Plan 3")
            '   ETC ...
            Dim lm As LayoutManager = LayoutManager.Current
            Dim lyid As ObjectId = ObjectId.Null
            Try
                Using tr
                    For Each kvp As KeyValuePair(Of String, String) In ltnames
                        oldname = kvp.Key
                        newname = kvp.Value
                        lyid = lm.GetLayoutId(oldname)

                        If lm.GetLayoutId(oldname) <> ObjectId.Null Then
                            Dim lt As Layout = TryCast(tr.GetObject(lyid, OpenMode.ForWrite), Layout)

                            AddHandler lm.LayoutRenamed, AddressOf lm_LayoutRenamed
                            AddHandler lm.LayoutToBeRenamed, AddressOf lm_LayoutToBeRenamed
                            lt.LayoutName = newname
                        End If
                    Next
                    tr.Commit()
                    ed.Regen()
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf & "Error Reason: {0}" & vbLf, ex.ErrorStatus)
                Return
            End Try

        End Sub

        Private Sub lm_LayoutRenamed(sender As Object, e As LayoutRenamedEventArgs)
            Dim newmame As String = ""
            Try
                newmame = e.NewName
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf & "Invalid layout name: {0}" & vbLf & "{1}", e.NewName, ex.ErrorStatus)
                Return
            End Try

        End Sub
        Private Sub lm_LayoutToBeRenamed(sender As Object, e As LayoutRenamedEventArgs)
            Try
                SymbolUtilityServices.ValidateSymbolName(oldname, False)
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf & "Invalid layout name: {0}" & vbLf & "{1}", e.NewName, ex.ErrorStatus)
                Return
            End Try

        End Sub

        ' End Class 'end of class

   C#

   
Code: [Select]
        // public class LayoutTools
        // {
        // declare layout names outside of method, keep them empty:
        string oldname = ""; string newname = "";
        [CommandMethod("lre")]
        public void testLayoutsRename()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = doc.Editor;
            Transaction tr = db.TransactionManager.StartTransaction();

            SortedList<string, string> ltnames = new SortedList<string, string>();
           // lst of pairs <old layout name,new layout name>: //
            ltnames.Add("Layout1", "General");
            ltnames.Add("Layout2", "Floor Plan 1");
            ltnames.Add("Layout3", "Floor Plan 2");
            ltnames.Add("Layout4", "Floor Plan 3");//   ETC ...
            LayoutManager lm = LayoutManager.Current;
            ObjectId lyid = ObjectId.Null;
            try
            {
                using (tr)
                {
                    foreach (KeyValuePair<string, string> kvp in ltnames)
                    {
                        oldname = kvp.Key; newname = kvp.Value;
                        lyid = lm.GetLayoutId(oldname);

                        if (lm.GetLayoutId(oldname) != ObjectId.Null)
                        {
                            Layout lt = tr.GetObject(lyid, OpenMode.ForWrite) as Layout;

                            lm.LayoutRenamed += new LayoutRenamedEventHandler(lm_LayoutRenamed);
                            lm.LayoutToBeRenamed += new LayoutRenamedEventHandler(lm_LayoutToBeRenamed);
                            lt.LayoutName = newname;
                        }
                    }
                    tr.Commit();
                    ed.Regen();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                    "\nRerror Reason: {0}\n", ex.ErrorStatus);
                return;
            }

        }

        private void lm_LayoutRenamed(object sender, LayoutRenamedEventArgs e)
        {
            string newmame = "";
            try
            {
                newmame = e.NewName;
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                    "\nInvalid layout name: {0}\n{1}", e.NewName, ex.ErrorStatus);
                return;
            }

        }
        private void lm_LayoutToBeRenamed(object sender, LayoutRenamedEventArgs e)
        {
            try
            {
                SymbolUtilityServices.ValidateSymbolName(oldname, false);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                    "\nInvalid layout name: {0}\n{1}", e.NewName, ex.ErrorStatus);
                return;
            }

        }

    //}// end of class