.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SystemVariableChanged Event Not Firing When Layer Changed via Ribbon Pull-Down Menu in AutoCAD

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
stephen_mitchell
100 Views, 2 Replies

SystemVariableChanged Event Not Firing When Layer Changed via Ribbon Pull-Down Menu in AutoCAD

Hi,

 

I'm trying to detect changes to the current layer, even when a user changes the layer using the Layer pull-down menu on the AutoCAD ribbon. I'm using the Application.SystemVariableChanged event to monitor changes to the CLAYER system variable. 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;

[assembly: CommandClass(typeof(MLS.Acad.Samples.Events))]

namespace MLS.Acad.Samples
{
    public class Events
    {
        [CommandMethod("TestApplicationLevelEvents")]
        public void TestApplicationLevelEvents()
        {
            // Subscribe to the SystemVariableChanged event
            Application.SystemVariableChanged += OnAppSystemVariableChanged;
        }
        
        private void OnAppSystemVariableChanged(object senderObj, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
        {
            // Check if the changed variable is CLAYER
            if (e.Name.Equals("CLAYER", StringComparison.OrdinalIgnoreCase))
            {
                string currentLayer = Application.GetSystemVariable("CLAYER").ToString();
                Document activeDoc = Application.DocumentManager.MdiActiveDocument;
                activeDoc?.Editor.WriteMessage($"\nLayer changed to {currentLayer}.");
            }
        }
    }
}

 

The event handler works correctly when the layer is changed via the command line or using the Layer Properties Manager, but it does not trigger when the layer is changed using the "Layer" pull-down menu on the ribbon.

 

Does anyone know why this event is not firing in response to changes made through the ribbon pull-down menu? Is there another event or method I should use to detect the layer change, regardless of where the change is made?

 

Thanks in advance for your assistance!

 

Some screen shots below. 

 

Layer_2.png

Layer_1.png

 

2 REPLIES 2
Message 2 of 3

I can't tell you why the event isn't being fired when changing the current layer using the Ribbon, but the following should fire in all cases:

 

 

Application.UIBindings.SystemVariables["CLAYER"].PropertyChanged += yourPropertyChangedHandler;

 

The event will fire twice, so you should check the PropertyName property of the event arguments and act only when it is "Value".

 

Message 3 of 3

Thanks for that. Much appreciated.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report