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 DocumentCollection 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 DocumentCollectionEvents1
{
public void Register()
{
AcadApplication.DocumentManager.DocumentToBeDestroyed += DocCollEvent_DocumentToBeDestroyed_Handler;
AcadApplication.DocumentManager.DocumentToBeDeactivated += DocCollEvent_DocumentToBeDeactivated_Handler;
AcadApplication.DocumentManager.DocumentToBeActivated += DocCollEvent_DocumentToBeActivated_Handler;
AcadApplication.DocumentManager.DocumentLockModeWillChange += DocCollEvent_DocumentLockModeWillChange_Handler;
AcadApplication.DocumentManager.DocumentLockModeChanged += DocCollEvent_DocumentLockModeChanged_Handler;
AcadApplication.DocumentManager.DocumentLockModeChangeVetoed += DocCollEvent_DocumentLockModeChangeVetoed_Handler;
AcadApplication.DocumentManager.DocumentDestroyed += DocCollEvent_DocumentDestroyed_Handler;
AcadApplication.DocumentManager.DocumentCreationCanceled += DocCollEvent_DocumentCreationCanceled_Handler;
AcadApplication.DocumentManager.DocumentCreated += DocCollEvent_DocumentCreated_Handler;
AcadApplication.DocumentManager.DocumentCreateStarted += DocCollEvent_DocumentCreateStarted_Handler;
AcadApplication.DocumentManager.DocumentBecameCurrent += DocCollEvent_DocumentBecameCurrent_Handler;
AcadApplication.DocumentManager.DocumentActivationChanged += DocCollEvent_DocumentActivationChanged_Handler;
AcadApplication.DocumentManager.DocumentActivated += DocCollEvent_DocumentActivated_Handler;
}
public void UnRegister()
{
AcadApplication.DocumentManager.DocumentToBeDestroyed -= DocCollEvent_DocumentToBeDestroyed_Handler;
AcadApplication.DocumentManager.DocumentToBeDeactivated -= DocCollEvent_DocumentToBeDeactivated_Handler;
AcadApplication.DocumentManager.DocumentToBeActivated -= DocCollEvent_DocumentToBeActivated_Handler;
AcadApplication.DocumentManager.DocumentLockModeWillChange -= DocCollEvent_DocumentLockModeWillChange_Handler;
AcadApplication.DocumentManager.DocumentLockModeChanged -= DocCollEvent_DocumentLockModeChanged_Handler;
AcadApplication.DocumentManager.DocumentLockModeChangeVetoed -= DocCollEvent_DocumentLockModeChangeVetoed_Handler;
AcadApplication.DocumentManager.DocumentDestroyed -= DocCollEvent_DocumentDestroyed_Handler;
AcadApplication.DocumentManager.DocumentCreationCanceled -= DocCollEvent_DocumentCreationCanceled_Handler;
AcadApplication.DocumentManager.DocumentCreated -= DocCollEvent_DocumentCreated_Handler;
AcadApplication.DocumentManager.DocumentCreateStarted -= DocCollEvent_DocumentCreateStarted_Handler;
AcadApplication.DocumentManager.DocumentBecameCurrent -= DocCollEvent_DocumentBecameCurrent_Handler;
AcadApplication.DocumentManager.DocumentActivationChanged -= DocCollEvent_DocumentActivationChanged_Handler;
AcadApplication.DocumentManager.DocumentActivated -= DocCollEvent_DocumentActivated_Handler;
}
public void DocCollEvent_DocumentActivated_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentActivated ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentActivationChanged_Handler(object sender, DocumentActivationChangedEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentActivationChanged ({0})\n", e.NewValue));
}
public void DocCollEvent_DocumentBecameCurrent_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentBecameCurrent ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentCreateStarted_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentCreateStarted ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentCreated_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentCreated ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentCreationCanceled_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentCreationCanceled ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentDestroyed_Handler(object sender, DocumentDestroyedEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentDestroyed ({0})\n", e.FileName));
}
public void DocCollEvent_DocumentLockModeChangeVetoed_Handler(object sender, DocumentLockModeChangeVetoedEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentLockModeChangeVetoed ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentLockModeChanged_Handler(object sender, DocumentLockModeChangedEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentLockModeChanged ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentLockModeWillChange_Handler(object sender, DocumentLockModeWillChangeEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentLockModeWillChange ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentToBeActivated_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentToBeActivated ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentToBeDeactivated_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentToBeDeactivated ({0})\n", e.Document.Name));
}
public void DocCollEvent_DocumentToBeDestroyed_Handler(object sender, DocumentCollectionEventArgs e)
{
(sender as DocumentCollection).MdiActiveDocument.Editor.WriteMessage(String.Format("\nDocumentToBeDestroyed ({0})\n", e.Document.Name));
}
}
}
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.
mDocumentCollectionEvents1.Register();
}
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();
mDocumentCollectionEvents1.UnRegister();
}
#endregion
AppEvents1 mAppEvents1 = new AppEvents1();
DocumentCollectionEvents1 mDocumentCollectionEvents1 = new DocumentCollectionEvents1();
}
}
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.
Where can I download the wizards? The AcadNetAddinWizard has only 1 wizards, the ".NET Addint Wizard, I cannot find the other wizards lke the event wizards etc...
Also, I see on your screenshots that you have an "AutoCAD Addin" item under your C# item, when I use the AcadNetAddinWizard, I don't get that, I only get 1 item on the right. These wizards looks awesome!
I would really like to use them.
Thanx.
Chris
Posted by: Christian Vorster | 01/26/2012 at 10:02 AM
Glad to hear you like it.
I should have made it clear that there are two kinds of wizards that the AcadNetAddinWizard offers. The first kind is the Project Wizard that you have found, which can be found from C#, VB.NET and C++ project types. The second kind is the project Item Wizards, which have many as you wanted, and support both C# and VB.NET projects.
They can be found from along this path:
[Project] menu
-> [Add New Item ...] menu
-> [Add New Item] dialog
-> [Categories] pane on the left
-> [Common Items] root node
-> [AutoCAD Addin] sub node
-> [Templates] pane on the right
-> the various Item Wizards are all there
The [Add New Item] dialog can also be launched through right clicking the project node (in which you'd like to add the project item) in the [Solution Explorer] window, choosing the [Add] item from the context menu, and then picking the [New Item ...] menu item.
You will not miss it this time!
Posted by: Spiderinnet1 | 01/26/2012 at 11:37 PM