Author Topic: How to detect layout name changed.  (Read 205 times)

0 Members and 1 Guest are viewing this topic.

kh001058

  • Mosquito
  • Posts: 2
How to detect layout name changed.
« on: March 15, 2017, 04:22:26 am »
Hi Everybody,

Is there anyway to detect layout name <CTAB system variable> changed ?
(in case user rename layout; Not switch to other layout)

Event SystemVariableChangedEventHandler below not fired CTAB sysvar>

Thank you in advance.

Code: [Select]
        [CommandMethod("SysVariableChange")]
        public void SysVarChangedmonitor()
        {
            AcadApp.SystemVariableChanged += new Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventHandler(SysVariableChange);
        }

        void SysVariableChange(object sender, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
        {
            try
            {
                object oVal = AcadApp.GetSystemVariable(e.Name);
                AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n" + e.Name + " was changed." + "\tNew value: " + oVal.ToString());
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nSysVarChanged: " + e.Name + " - " + ex.Message + "\n");
            }
        }
« Last Edit: March 15, 2017, 04:29:48 am by kh001058 »

gile

  • Water Moccasin
  • Posts: 2067
  • Marseille, France
Re: How to detect layout name changed.
« Reply #1 on: March 15, 2017, 06:23:16 am »
Hi,

An equivalent for CTAB sysvar changed would be the Document.LayoutSwitched event but it looks like it's not what your looking for.

To catch layout renaming, you can handle the Database.ObjectModified event.

Code - C#: [Select]
  1.        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  2.        ObjectId layoutId = ObjectId.Null;
  3.        string layoutName;
  4.  
  5.        [CommandMethod("TEST")]
  6.        public void Test()
  7.        {
  8.            Application.DocumentManager.MdiActiveDocument.Database.ObjectOpenedForModify += OnObjectOpenedForModify;
  9.        }
  10.  
  11.        private void OnObjectOpenedForModify(object sender, ObjectEventArgs e)
  12.        {
  13.            if (e.DBObject is Layout)
  14.            {
  15.                var layout = (Layout)e.DBObject;
  16.                layoutId = layout.ObjectId;
  17.                layoutName = layout.LayoutName;
  18.                layout.Database.ObjectModified += OnObjectModified;
  19.            }
  20.        }
  21.  
  22.        void OnObjectModified(object sender, ObjectEventArgs e)
  23.        {
  24.            if (e.DBObject.ObjectId == layoutId)
  25.            {
  26.                e.DBObject.Database.ObjectModified -= OnObjectModified;
  27.                var layout = (Layout)e.DBObject;
  28.                if (layoutName != layout.LayoutName)
  29.                {
  30.                    ed.WriteMessage($"\nLayout '{layoutName}' has been renamed: '{layout.LayoutName}'.");
  31.                }
  32.            }
  33.        }
Speaking English as a French Frog

kh001058

  • Mosquito
  • Posts: 2
Re: How to detect layout name changed.
« Reply #2 on: March 15, 2017, 11:23:54 am »
Many thanks, Gilles,
It works as expected.