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.
Solved! Go to Solution.
Solved by ActivistInvestor. Go to Solution.
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".
Can't find what you're looking for? Ask the community or share your knowledge.