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 DBObject 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 DBObjEvents1
{
ObjectIdCollection mObjIds = new ObjectIdCollection();
public void Register(ObjectIdCollection ids)
{
foreach (ObjectId id in ids)
{
Register(id);
}
}
public void Register(SymbolTable ids)
{
foreach (ObjectId id in ids)
{
Register(id);
}
}
public void Register(ObjectId[] ids)
{
foreach (ObjectId id in ids)
{
Register(id);
}
}
public void Register(List<ObjectId> ids)
{
foreach (ObjectId id in ids)
{
Register(id);
}
}
public void Register(ObjectId id)
{
if (id.IsNull || !id.IsValid) return;
using (Transaction tr = AcadApplication.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
DBObject dbObj = tr.GetObject(id, OpenMode.ForNotify);
dbObj.Cancelled += DBObjEvent_Cancelled_Handler;
dbObj.Copied += DBObjEvent_Copied_Handler;
dbObj.Erased += DBObjEvent_Erased_Handler;
dbObj.Goodbye += DBObjEvent_Goodbye_Handler;
dbObj.Modified += DBObjEvent_Modified_Handler;
dbObj.ModifiedXData += DBObjEvent_ModifiedXData_Handler;
dbObj.ModifyUndone += DBObjEvent_ModifyUndone_Handler;
dbObj.ObjectClosed += DBObjEvent_ObjectClosed_Handler;
dbObj.OpenedForModify += DBObjEvent_OpenedForModify_Handler;
dbObj.Reappended += DBObjEvent_Reappended_Handler;
dbObj.SubObjectModified += DBObjEvent_SubObjectModified_Handler;
dbObj.Unappended += DBObjEvent_Unappended_Handler;
tr.Commit();
}
}
public void UnRegister()
{
if (mObjIds == null || mObjIds.Count < 1) return;
using (Transaction tr = AcadApplication.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
foreach (ObjectId id in mObjIds)
{
if (id.IsNull || !id.IsValid) continue;
DBObject dbObj = tr.GetObject(id, OpenMode.ForNotify);
dbObj.Cancelled -= DBObjEvent_Cancelled_Handler;
dbObj.Copied -= DBObjEvent_Copied_Handler;
dbObj.Erased -= DBObjEvent_Erased_Handler;
dbObj.Goodbye -= DBObjEvent_Goodbye_Handler;
dbObj.Modified -= DBObjEvent_Modified_Handler;
dbObj.ModifiedXData -= DBObjEvent_ModifiedXData_Handler;
dbObj.ModifyUndone -= DBObjEvent_ModifyUndone_Handler;
dbObj.ObjectClosed -= DBObjEvent_ObjectClosed_Handler;
dbObj.OpenedForModify -= DBObjEvent_OpenedForModify_Handler;
dbObj.Reappended -= DBObjEvent_Reappended_Handler;
dbObj.SubObjectModified -= DBObjEvent_SubObjectModified_Handler;
dbObj.Unappended -= DBObjEvent_Unappended_Handler;
}
tr.Commit();
}
}
public void DBObjEvent_Cancelled_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nCancelled\n");
}
public void DBObjEvent_Copied_Handler(object sender, ObjectEventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nCopied\n");
}
public void DBObjEvent_Erased_Handler(object sender, ObjectErasedEventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nErased\n");
}
public void DBObjEvent_Goodbye_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nGoodbye\n");
}
public void DBObjEvent_Modified_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nModified\n");
}
public void DBObjEvent_ModifiedXData_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nModifiedXData\n");
}
public void DBObjEvent_ModifyUndone_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nModifyUndone\n");
}
public void DBObjEvent_ObjectClosed_Handler(object sender, ObjectClosedEventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nObjectClosed\n");
}
public void DBObjEvent_OpenedForModify_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nOpenedForModify\n");
}
public void DBObjEvent_Reappended_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nReappended\n");
}
public void DBObjEvent_SubObjectModified_Handler(object sender, ObjectEventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nSubObjectModified\n");
}
public void DBObjEvent_Unappended_Handler(object sender, EventArgs e)
{
AcadApplication.DocumentManager.GetDocument((sender as DBObject).Database).Editor.WriteMessage("\nUnappended\n");
}
}
}
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();
mDynamicLinkerEvents1.Register();
mLayoutManagerEvents1.Register();
mPublisherEvents1.Register();
mUserConfigurationManagerEvents1.Register();
mDBObjEvents1.Register(ObjectId.Null); //Replace with some real id (collection)
}
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();
mDynamicLinkerEvents1.UnRegister();
mLayoutManagerEvents1.UnRegister();
mPublisherEvents1.UnRegister();
mUserConfigurationManagerEvents1.UnRegister();
mDBObjEvents1.UnRegister();
}
#endregion
AppEvents1 mAppEvents1 = new AppEvents1();
DocumentCollectionEvents1 mDocumentCollectionEvents1 = new DocumentCollectionEvents1();
DynamicLinkerEvents1 mDynamicLinkerEvents1 = new DynamicLinkerEvents1();
LayoutManagerEvents1 mLayoutManagerEvents1 = new LayoutManagerEvents1();
PublisherEvents1 mPublisherEvents1 = new PublisherEvents1();
UserConfigurationManagerEvents1 mUserConfigurationManagerEvents1 = new UserConfigurationManagerEvents1();
DBObjEvents1 mDBObjEvents1 = new DBObjEvents1();
}
}
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