AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of Event Handler Wizards to help automatically and intelligently generate start code of even handlers and register them in an chosen application extension or command method or both.
AutoCAD .NET Document Events Wizard is one of them. After a source file name is given and the Add button pressed, the event wizard Welcome page, Event Chosen page, Naming Convention and Application-Extension/Command-Method Setting page, and Summary page will show up one after another.
If all events are chosen and other settings are like above, the even handler class will look like this:
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 AcadNetAddinByVS2010
{
public class DocEvents1
{
public static void DocEvent_BeginDocumentClose_Handler(object sender, DocumentBeginCloseEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("BeginDocumentClose");
}
public static void DocEvent_CloseAborted_Handler(object sender, EventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("CloseAborted");
}
public static void DocEvent_CloseWillStart_Handler(object sender, EventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("CloseWillStart");
}
public static void DocEvent_CommandCancelled_Handler(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("CommandCancelled");
}
public static void DocEvent_CommandEnded_Handler(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("CommandEnded");
}
public static void DocEvent_CommandFailed_Handler(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("CommandFailed");
}
public static void DocEvent_CommandWillStart_Handler(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("CommandWillStart");
}
public static void DocEvent_ImpliedSelectionChanged_Handler(object sender, EventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("ImpliedSelectionChanged");
}
public static void DocEvent_LispCancelled_Handler(object sender, EventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("LispCancelled");
}
public static void DocEvent_LispEnded_Handler(object sender, EventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("LispEnded");
}
public static void DocEvent_LispWillStart_Handler(object sender, LispWillStartEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("LispWillStart");
}
public static void DocEvent_UnknownCommand_Handler(object sender, UnknownCommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage("UnknownCommand");
}
public DocEvents1()
{
SubscribeForApplication();
AcadApplication.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(DocumentManager_DocumentCreated);
}
public static void SubscribeForApplication()
{
IEnumerator enumerator = AcadApplication.DocumentManager.GetEnumerator();
while (enumerator.MoveNext())
{
SubscribeToDoc(enumerator.Current as AcadDocument);
}
}
public static void UnsubscribeForApplication()
{
IEnumerator enumerator = AcadApplication.DocumentManager.GetEnumerator();
while (enumerator.MoveNext())
{
UnsubscribeFromDoc(enumerator.Current as AcadDocument);
}
}
public static void SubscribeToDoc(AcadDocument doc)
{
doc.BeginDocumentClose += DocEvent_BeginDocumentClose_Handler;
doc.CloseAborted += DocEvent_CloseAborted_Handler;
doc.CloseWillStart += DocEvent_CloseWillStart_Handler;
doc.CommandCancelled += DocEvent_CommandCancelled_Handler;
doc.CommandEnded += DocEvent_CommandEnded_Handler;
doc.CommandFailed += DocEvent_CommandFailed_Handler;
doc.CommandWillStart += DocEvent_CommandWillStart_Handler;
doc.ImpliedSelectionChanged += DocEvent_ImpliedSelectionChanged_Handler;
doc.LispCancelled += DocEvent_LispCancelled_Handler;
doc.LispEnded += DocEvent_LispEnded_Handler;
doc.LispWillStart += DocEvent_LispWillStart_Handler;
doc.UnknownCommand += DocEvent_UnknownCommand_Handler;
}
public static void UnsubscribeFromDoc(AcadDocument doc)
{
doc.BeginDocumentClose -= DocEvent_BeginDocumentClose_Handler;
doc.CloseAborted -= DocEvent_CloseAborted_Handler;
doc.CloseWillStart -= DocEvent_CloseWillStart_Handler;
doc.CommandCancelled -= DocEvent_CommandCancelled_Handler;
doc.CommandEnded -= DocEvent_CommandEnded_Handler;
doc.CommandFailed -= DocEvent_CommandFailed_Handler;
doc.CommandWillStart -= DocEvent_CommandWillStart_Handler;
doc.ImpliedSelectionChanged -= DocEvent_ImpliedSelectionChanged_Handler;
doc.LispCancelled -= DocEvent_LispCancelled_Handler;
doc.LispEnded -= DocEvent_LispEnded_Handler;
doc.LispWillStart -= DocEvent_LispWillStart_Handler;
doc.UnknownCommand -= DocEvent_UnknownCommand_Handler;
}
static void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
{
SubscribeToDoc(e.Document);
}
}
}
Here is the event registration code in the chosen IExtensionApplication implementation by the way:
#region Namespaces
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.Drawing;
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;
#endregion
namespace AcadNetAddinByVS2010
{
public class ExtApp : IExtensionApplication
{
#region IExtensionApplication Members
public void Initialize()
{
//TODO: add code to run when the ExtApp initializes. Here are a few examples:
// Checking some host information like build #, a patch or a particular Arx/Dbx/Dll;
// Creating/Opening some files to use in the whole life of the assembly, e.g. logs;
// Adding some ribbon tabs, panels, and/or buttons, when necessary;
// Loading some dependents explicitly which are not taken care of automatically;
// Subscribing to some events which are important for the whole session;
// Etc.
mAppEvents1.Register();
DbEvents1 mDbEvents1 = new DbEvents1(); //Subscribe to all databases in the whole AutoCAD session.
DocEvents1 mDocEvents1 = new DocEvents1(); //Subscribe to all documents in the whole AutoCAD session.
}
public void Terminate()
{
//TODO: add code to clean up things when the ExtApp terminates. For example:
// Closing the log files;
// Deleting the custom ribbon tabs/panels/buttons;
// Unloading those dependents;
// Un-subscribing to those events;
// Etc.
mAppEvents1.UnRegister();
}
#endregion
AppEvents1 mAppEvents1 = new AppEvents1();
}
}
#region Namespaces
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.Diagnostics;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
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;
#endregion
namespace AcadNetAddinByVS2010
{
public class CmdGroup1
{
[CommandMethod("CmdGroup1", "Command1", null, CommandFlags.Modal, null, "AcadNetAddinByVS2010", "Command1")]
public void Command1_Method()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = AcadApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//TODO: add your code below.
Debug.WriteLine("Command1 ran.");
ed.WriteMessage("Command1 ran.\n");
tr.Commit();
}
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.ToString());
ed.WriteMessage(ex.ToString());
}
DbEvents1.SubscribeToDb(Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase); //Subscribe to the working database.
DocEvents1.SubscribeToDoc(AcadApplication.DocumentManager.MdiActiveDocument); //Subscribe to the active document.
}
}
}
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) has provided many nice Event Wizards to auto-generate start code of even handlers and registers flexibly in no time. People can expect more and cooler wizards, coders, and widgets in the near future.
Recent Comments