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 DBObject 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 DBObjEvents1
{
[CommandMethod("TestDBObjEvents")]
public void TestDBObjEvents_Method()
{
DBObjEvents1 mDBObjEvents1 = new DBObjEvents1();
mDBObjEvents1.Register(HostApplicationServices.WorkingDatabase.LayerZero);
}
[CommandMethod("TestDBObjEvents1")]
public void TestDBObjEvents1_Method()
{
DBObjEvents1 mDBObjEvents1 = new DBObjEvents1();
using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
LayerTable layerTbl = (LayerTable)trans.GetObject(HostApplicationServices.WorkingDatabase.LayerTableId, OpenMode.ForNotify);
mDBObjEvents1.Register(layerTbl);
trans.Commit();
}
}
ObjectIdCollection mObjIds = new ObjectIdCollection();
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 Register(ObjectIdCollection 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(SymbolTable ids)
{
foreach (ObjectId id in ids)
{
Register(id);
}
}
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");
}
}
}
The command TestDBObjEvents will monitor various aspects of the layer zero such as Cancelled, Copied, Erased, Goodbye, Modified, ModifiedXData, ModifyUndone, ObjectClosed, OpenedForModify, Reappended, SubObjectModified, and Unappended. The command TestDBObjEvents1 will monitor all existing layers instead.
The code is short and self-explanatory so we will not waste our words here. Let us play around the commands and look at some command outputs:
Command: TestDBObjEvents
Command: -layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: c
New color [Truecolor/COlorbook] : 1
Enter name list of layer(s) for color 1 (red) <0>: "0"
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: lw
Enter lineweight (0.0mm - 2.11mm): 0.2
Enter name list of layers(s) for lineweight 0.20mm <0>: "0"
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: d
Enter layer description: "Layer zero"
Enter name list of layer(s) to apply description or <select objects>: <*>: "0"
OpenedForModify
ModifiedXData
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Another test for all existing layers:
Command: -LAYER
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: M
Enter name for new layer (becomes the current layer) <0>: 1
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: M
Enter name for new layer (becomes the current layer) <1>: 2
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: M
Enter name for new layer (becomes the current layer) <2>: 3
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: TestDBObjEvents1
Command: -LAYER
Current layer: "3"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: ?
Enter layer name(s) to list <*>:
Layer name State Color Linetype Lineweight
------------------ ----------- ------------------ ------------ ------------
"0" on -P 7 (white) "Continuous" Default
"1" on -P 7 (white) "Continuous" Default
"2" on -P 7 (white) "Continuous" Default
"3" on -P 7 (white) "Continuous" Default
Current layer: "3"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: S
Enter layer name to make current or <select object>: 1
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: N
Enter name list for new layer(s): 2
Layer "2" already exists.
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: R
Enter old layer name: 3
Enter new layer name: New3
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: on
Enter name list of layer(s) to turn on: 1
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: off
Enter name list of layer(s) to turn off or <select objects>: 2
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: c
New color [Truecolor/COlorbook] : co
Enter Color Book name: 3
Color book not found.
Enter Color Book name: duntknow
Color book not found.
Enter Color Book name:
Color book not found.
Enter Color Book name: *Cancel*
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: c
New color [Truecolor/COlorbook] : 6
Enter name list of layer(s) for color 6 (magenta) <1>: 2
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: l
Enter loaded linetype name or [?] <Continuous>: ?
Enter linetype name(s) to list <*>:
Loaded linetypes:
Name Description
---------------- ------------------
"Continuous" Solid line
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: lw
Enter lineweight (0.0mm - 2.11mm): 1
Enter name list of layers(s) for lineweight 1.00mm <1>: 2
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: tr
Enter transparency value (0-90): 50
Enter name list of layer(s) for transparency 50% <1>: 0
OpenedForModify
ModifiedXData
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: MAT
Enter material name or [?] <Global>: ?
Enter material name(s) to list <*>:
Material names:
Name Description
---------------- ------------------
"Global"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: MAT
Enter material name or [?] <Global>:
Enter name list of layer(s) for material "Global" <1>: 2
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: p
Enter a plotting preference [Plot/No plot] <Plot>: n
Enter layer name(s) for this plot preference <1>: 3
No matching layer names found.
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: f
Enter name list of layer(s) to freeze or <select objects>: 2
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: lo
Enter name list of layer(s) to lock or <select objects>: 1
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: a
Enter an option [?/Save/Restore/Edit/Name/Delete/Import/EXport]: s
Enter new layer state name: LayerState1
States
On Yes
Frozen Yes
Lock Yes
Plot Yes
Newvpfreeze Yes
Color Yes
lineType Yes
lineWeight Yes
Transparency Yes
plotStyle Yes
Enter states to change
[On/Frozen/Lock/Plot/Newvpfreeze/Color/lineType/lineWeight/TRansparency/plotStyl
e]: w
States
On Yes
Frozen Yes
Lock Yes
Plot Yes
Newvpfreeze Yes
Color Yes
lineType Yes
lineWeight No
Transparency Yes
plotStyle Yes
Enter states to change
[On/Frozen/Lock/Plot/Newvpfreeze/Color/lineType/lineWeight/TRansparency/plotStyl
e]: c
States
On Yes
Frozen Yes
Lock Yes
Plot Yes
Newvpfreeze Yes
Color No
lineType Yes
lineWeight No
Transparency Yes
plotStyle Yes
Enter states to change
[On/Frozen/Lock/Plot/Newvpfreeze/Color/lineType/lineWeight/TRansparency/plotStyl
e]:
Enter an option [?/Save/Restore/Edit/Name/Delete/Import/EXport]: n
Enter name of layer state to rename or [?]: LayerState1
Enter new layer state name: LayerState2
Enter an option [?/Save/Restore/Edit/Name/Delete/Import/EXport]:
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: e
Enter name list of layer(s) to turn reconcile or <select objects> or [?]: 2
OpenedForModify
Modified
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: layer
Command:
OpenedForModify
Erased
Modified
Command:
OpenedForModify
Modified
As can be noticed, the event OpenedForModify were fired off many times necessary or not but the event ObjectClosed was never. It looks like something is missing internally in AutoCAD, either the ObjectClosed notification is forgotten to be sent out when the object is being closed, or these layers are not closed at all!
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of AutoCAD .NET Event Handler Wizards including an AutoCAD DBObject Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Posted by: |