AutoCAD .NET has wrapped almost all ObjectARX events through one way or another using still the event concept or something slightly different. As a whole, the AutoCAD .NET event or notification system is powerful and useful and a professional and relatively big addin cannot totally ignore the important force.
In this article, we will be talking about the AutoCAD .NET LayerStateManager events. It is always good to start with some cool, concise and still full sample code. Here it is:
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcadWindows = Autodesk.AutoCAD.Windows;
namespace AcadNetAddinWizard_Namespace
{
public class LayerStateEvents
{
[CommandMethod("TestLayerStateEvents")]
public void TestLayerStateEvents_Method()
{
LayerStateEvents eventManager = new LayerStateEvents();
eventManager.Register();
}
public void Register()
{
HostApplicationServices.WorkingDatabase.LayerStateManager.LayerStateCreated += new LayerStateEventHandler(LayerStateManager_LayerStateCreated);
HostApplicationServices.WorkingDatabase.LayerStateManager.LayerStateDeleted += new LayerStateDeletedEventHandler(LayerStateManager_LayerStateDeleted);
HostApplicationServices.WorkingDatabase.LayerStateManager.LayerStateRenamed += new LayerStateRenameEventHandler(LayerStateManager_LayerStateRenamed);
HostApplicationServices.WorkingDatabase.LayerStateManager.LayerStateRestored += new LayerStateEventHandler(LayerStateManager_LayerStateRestored);
}
void LayerStateManager_LayerStateRestored(object sender, LayerStateEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("LayerStateRestored\n");
}
void LayerStateManager_LayerStateRenamed(object sender, LayerStateRenameEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("LayerStateRenamed\n");
}
void LayerStateManager_LayerStateDeleted(object sender, LayerStateDeletedEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("LayerStateDeleted\n");
}
void LayerStateManager_LayerStateCreated(object sender, LayerStateEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("LayerStateCreated\n");
}
}
}
The code is short and self-explanatory so we will not waste our words here. Let us play around the command and look at some command outputs:
Command: TestLayerStateEvents
Command: LAYERSTATE
LayerStateCreated
LayerStateCreated
Application does not support just-in-time (JIT)
debugging. See the end of this message for details.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at Autodesk.AutoCAD.DatabaseServices.LayerStateManagerReactorImpl.FireLayerStateToBeRenamed(LayerStateManagerReactorImpl* , LayerStateRenameEventArgs e)
Application does not support just-in-time (JIT)
debugging. See the end of this message for details.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at Autodesk.AutoCAD.DatabaseServices.LayerStateManagerReactorImpl.FireLayerStateToBeDeleted(LayerStateManagerReactorImpl* , LayerStateEventArgs e)
at Autodesk.AutoCAD.DatabaseServices.LayerStateManagerReactorImpl.layerStateToBeDeleted(LayerStateManagerReactorImpl* , Char* layerStateName, AcDbObjectId* layerStateId)
Application does not support just-in-time (JIT)
debugging. See the end of this message for details.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at Autodesk.AutoCAD.DatabaseServices.LayerStateManagerReactorImpl.FireLayerStateToBeRestored(LayerStateManagerReactorImpl* , LayerStateEventArgs e)
at Autodesk.AutoCAD.DatabaseServices.LayerStateManagerReactorImpl.layerStateToBeRestored(LayerStateManagerReactorImpl* , Char* layerStateName, AcDbObjectId* layerStateId)
As can be seen, the LayerStateCreated event was fired properly, but not others such as LayerStateRenamed, LayerStateDeleted, and LayerStateRestored. When their corresponding underlying functions such as FireLayerStateToBeRenamed(), FireLayerStateToBeDeleted() and FireLayerStateToBeRestored() were triggered by our even handlers, exceptions just came up. The exception message ‘Application does not support just-in-time (JIT) debugging’ might indicate that these events were not able to show up in the Visual Studio debugging environment, but still it did not make any sense since the LayerStateCreated event behaved just well.
So there must be something wrong elsewhere internal and we cannot do anything about it external.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of AutoCAD .NET Event Handler Wizards including an AutoCAD LayerStateManager Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Recent Comments