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 Application events, specifically some dialog displaying related. 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 AppEvents
{
[CommandMethod("TestAppEvents")]
public void TestAppEvents_Method()
{
AppEvents eventManager = new AppEvents();
eventManager.Register();
}
public void Register()
{
AcadApplication.DisplayingCustomizeDialog += new TabbedDialogEventHandler(AcadApplication_DisplayingCustomizeDialog);
AcadApplication.DisplayingDraftingSettingsDialog += new TabbedDialogEventHandler(AcadApplication_DisplayingDraftingSettingsDialog);
AcadApplication.DisplayingOptionDialog += new TabbedDialogEventHandler(AcadApplication_DisplayingOptionDialog);
}
void AcadApplication_DisplayingOptionDialog(object sender, TabbedDialogEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDisplayingOptionDialog\n");
}
void AcadApplication_DisplayingDraftingSettingsDialog(object sender, TabbedDialogEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDisplayingDraftingSettingsDialog\n");
}
void AcadApplication_DisplayingCustomizeDialog(object sender, TabbedDialogEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDisplayingCustomizeDialog\n");
}
}
}
After the command TestAppEvents is invoked, dialog displaying related events will be monitored such as DisplayingOptionDialog, DisplayingDraftingSettingsDialog, and DisplayingCustomizeDialog.
The code is succinct and self-explanatory so we will not waste our words for them. Let us play around the command and look at some command outputs:
Command: TestAppEvents
Command: OPTIONS
DisplayingOptionDialog
Command:
Command: CUI
Command: CUSTOMIZE
DisplayingCustomizeDialog
Command: DSETTINGS
DisplayingDraftingSettingsDialog
As can be noticed, the three dialogs (Options Dialog, Customization Dialog, and Drafting Settings Dialog) can be launched by three corresponding commands, Options, Customize, and DSettings. And before the dialog really shows up, the proper notification will be sent out. Please note a small thing here, the CUI will not fire off the DisplayingCustomizeDialog event.
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 Application Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Posted by: |