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 DynamicLinker 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 DynamicLinkerEvents
{
[CommandMethod("TestDynamicLinkerEvents")]
public void TestDynamicLinkerEvents_Method()
{
DynamicLinkerEvents dynLinkerEvents1 = new DynamicLinkerEvents();
dynLinkerEvents1.Register();
}
public void Register()
{
SystemObjects.DynamicLinker.ModuleLoadAborted += new ModuleLoadAbortedEventHandler(DynamicLinker_ModuleLoadAborted);
SystemObjects.DynamicLinker.ModuleLoaded += new ModuleLoadedEventHandler(DynamicLinker_ModuleLoaded);
SystemObjects.DynamicLinker.ModuleLoading += new ModuleLoadingEventHandler(DynamicLinker_ModuleLoading);
}
void DynamicLinker_ModuleLoading(object sender, DynamicLinkerEventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", "ModuleLoading", e.FileName));
}
void DynamicLinker_ModuleLoaded(object sender, DynamicLinkerEventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", "ModuleLoaded", e.FileName));
}
void DynamicLinker_ModuleLoadAborted(object sender, DynamicLinkerEventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", "ModuleLoadAborted", e.FileName));
}
}
}
After the command TestDynamicLinkerEvents runs, module load related events will be monitored such as ModuleLoadAborted, ModuleLoaded, and ModuleLoading.
The code is short and self-explanatory so we will not waste our words here. Let us do something about module loading and look at some command outputs.
In the command line window:
Command: TestDynamicLinkerEvents
Command: appload
vl.arx already loaded.
textfind.arx successfully loaded.
rectang.arx successfully loaded.
units.arx successfully loaded.
Loading AEC Base...
Loading AEC Base Extended...
Loading AEC Project Base...
Loading AEC Base UI...
AecProjectBase.dbx successfully loaded.
In the Visual Studio output window:
ModuleLoading c:\program files\autodesk\autocad 2012 - english\appload.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\appload.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\textfind.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\textfind.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\rectang.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\rectang.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\units.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\units.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\aecprojectbase.dbx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\aecbaseex.dbx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\aecbase.dbx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\acsceneoe.dbx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\acismobj18.dbx
ModuleLoading acismui.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\aecmodeler60.dbx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\aecloader.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\aecuibase.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\acetransmitui.arx
ModuleLoading acstetransmit.arx
ModuleLoading c:\program files\autodesk\autocad 2012 - english\acblock.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\acsceneoe.dbx
ModuleLoaded acismui.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\acismobj18.dbx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\aecmodeler60.dbx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\aecbase.dbx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\aecbaseex.dbx
ModuleLoaded acstetransmit.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\acetransmitui.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\acblock.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\aecuibase.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\aecloader.arx
ModuleLoaded c:\program files\autodesk\autocad 2012 - english\aecprojectbase.dbx
As can be noticed, sometimes the ModuleLoaded notification just follows the ModuleLoading event for a particular module, but some other times they are loading and loaded in batch. So please do not assume the former is always true for all modules. There seems a mechanism to make the latter happen but that is not the big concern for this article. We may revisit this matter in the future when necessary. By the way, the MODULE here means ObjectARX or DBX files only instead of .NET assemblies that readers may be more interested in.
The sample code does not cover how to unsubscribe these events. I would like to leave it as an exercise to readers who have interest.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of AutoCAD .NET Event Handler Wizards including an AutoCAD DynamicLinker Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Posted by: |