Rename Layout in an external dwg file using autocad .net c#

Rename Layout in an external dwg file using autocad .net c#

Anonymous
Not applicable
1,318 Views
2 Replies
Message 1 of 3

Rename Layout in an external dwg file using autocad .net c#

Anonymous
Not applicable

Hello, 

 

I can rename a layout of an open file using the following code :

 

 static public void renamelayoutName(string last_name, string new_name)
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (doc.LockDocument())
            {
                //get the Layout name

                bool bUpdate = false;

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {

                    DBDictionary dLayouts = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;

                    if (dLayouts.Contains(last_name))
                    {

                        bUpdate = true;
                    }

                    tr.Commit();

                }

                if (bUpdate)
                {

                    LayoutManager acLayoutMgr = LayoutManager.Current;

                    acLayoutMgr.RenameLayout(last_name, new_name);

                    doc.Editor.Regen();

                }

                else
                {
                    ed.WriteMessage("No Layout with name " + last_name);

                }

            }
        }

I want to rename a layout of an external dwg file, how can I do it ? Thank you

0 Likes
Accepted solutions (1)
1,319 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

You have to set the side database as WorkingDatabase before calling LayoutManager.Current. Do not forget to reset the WorkingDatabase to the active document one.

 

Here's an extension method example.

    public static class Extension
    {
        public static void RenameLayout(this Database targetDb, string oldName, string newName)
        {
            var workingDb = HostApplicationServices.WorkingDatabase;
            try
            {
                HostApplicationServices.WorkingDatabase = targetDb;
                var layoutMgr = LayoutManager.Current;
                layoutMgr.RenameLayout(oldName, newName);
            }
            catch (System.Exception ex)
            {
                Application.ShowAlertDialog(ex.Message);
            }
            finally
            {
                HostApplicationServices.WorkingDatabase = workingDb;
            }
        }
    }

Calling example:

            using (var sideDb = new Database(false, true))
            {
                sideDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, true, null);
                sideDb.RenameLayout(oldName, newName);
                sideDb.SaveAs(filename, DwgVersion.Current);
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable

This worked great, thank you so much!

0 Likes