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 UserConfigurationManager 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 UserConfigManagerEvents
{
[CommandMethod("TestUserConfigManagerEvents")]
public void TestUserConfigManagerEvents_Method()
{
UserConfigManagerEvents eventManager = new UserConfigManagerEvents();
eventManager.Register();
}
public void Register()
{
AcadApplication.UserConfigurationManager.CurrentProfileChanged +=new ProfileEventHandler(UserConfigurationManager_CurrentProfileChanged);
AcadApplication.UserConfigurationManager.CurrentProfileChanging += new ProfileEventHandler(UserConfigurationManager_CurrentProfileChanging);
}
void UserConfigurationManager_CurrentProfileChanging(object sender, ProfileEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\n{0}: {1}\n", "CurrentProfileChanging", e.ProfileName));
}
void UserConfigurationManager_CurrentProfileChanged(object sender, ProfileEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\n{0}: {1}\n", "CurrentProfileChanged", e.ProfileName));
}
}
}
After the command TestUserConfigManagerEvents runs, the two current profile related events will be monitored, CurrentProfileChanging and CurrentProfileChanged.
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: TestUserConfigManagerEvents
Command: options
CurrentProfileChanging: NewProfile
CurrentProfileChanged: NewProfile
Customization file loaded successfully. Customization Group: ACAD
Customization file loaded successfully. Customization Group: CUSTOM
Customization file loaded successfully. Customization Group: AUTOCADWS
Customization file loaded successfully. Customization Group: MODELDOC
Customization file loaded successfully. Customization Group: CONTENTEXPLORER
Customization file loaded successfully. Customization Group: ACFUSION
Customization file loaded successfully. Customization Group: EXPRESS
Customization file loaded successfully. Customization Group: ACADNETADDIN
CurrentProfileChanging: NewProfile
CurrentProfileChanged: NewProfile
Customization file loaded successfully. Customization Group: ACAD
Customization file loaded successfully. Customization Group: CUSTOM
Customization file loaded successfully. Customization Group: AUTOCADWS
Customization file loaded successfully. Customization Group: MODELDOC
Customization file loaded successfully. Customization Group: CONTENTEXPLORER
Customization file loaded successfully. Customization Group: ACFUSION
Customization file loaded successfully. Customization Group: EXPRESS
Customization file loaded successfully. Customization Group: ACADNETADDIN
CurrentProfileChanging: <<Unnamed Profile>>
CurrentProfileChanged: <<Unnamed Profile>>
Customization file loaded successfully. Customization Group: ACAD
Customization file loaded successfully. Customization Group: CUSTOM
Customization file loaded successfully. Customization Group: AUTOCADWS
Customization file loaded successfully. Customization Group: MODELDOC
Customization file loaded successfully. Customization Group: CONTENTEXPLORER
Customization file loaded successfully. Customization Group: ACFUSION
Customization file loaded successfully. Customization Group: EXPRESS
Customization file loaded successfully. Customization Group: ACADNETADDIN
Command:.
As can be noticed, the events CurrentProfileChanging and CurrentProfileChanged showed up at right time, not too early, not too late, not less and not more, only once per time.
By the way, 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 UserConfigurationManager Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Recent Comments