-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErasedObjectObserver.cs
179 lines (157 loc) · 5.35 KB
/
ErasedObjectObserver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
namespace CommandObserverExamplePattern
{
/// <summary>
/// A class that collects and operates on objects
/// that are erased by the ERASE command.
///
/// This class serves as an example of a more generic
/// pattern that involves observing specific events
/// only while a specified command or set of commands
/// are running, with minimal event-handling overhead.
///
/// The only event handlers that continuously listen to
/// events are the CommandWillStart event handler (one
/// for each open Document), and the DocumentAdded event
/// handler.
///
/// All other event handlers are added to their respective
/// events only when they are needed, and are removed as
/// soon as they are no longer needed.
///
/// The generic argument specifies the type of objects
/// that are observed/operated on.
///
/// Note: DO NOT use the [PerDocumentClass] attribute on this class!!!
/// </summary>
/// <typeparam name="T">The type of DBObject to observe</typeparam>
public class ErasedObjectObserver<T> where T: DBObject
{
static readonly DocumentCollection docs = Application.DocumentManager;
protected readonly Document Document;
protected readonly HashSet<ObjectId> erasedObjects = new HashSet<ObjectId>();
/// <summary>
/// Private constructor.
///
/// Instances of this class are not directly-creatable,
/// and are only created by other methods of this class
/// that are driven by handlers of events.
///
/// There is one instance of this class created for each
/// Document, which it is permanently associated with.
/// </summary>
/// <param name="doc">The associated Document</param>
ErasedObjectObserver(Document doc)
{
this.Document = doc;
doc.CommandWillStart += commandWillStart;
}
public static void Initialize()
{
/// Dummy method to force static constructor to run
}
static ErasedObjectObserver()
{
foreach(Document doc in docs)
{
Initialze(doc);
}
docs.DocumentCreated += documentCreated;
}
static void Initialze(Document doc)
{
doc.UserData[typeof(ErasedObjectObserver<T>)] = new ErasedObjectObserver<T>(doc);
}
public static ErasedObjectObserver<T>? Item(Document doc)
{
return doc.UserData[typeof(ErasedObjectObserver<T>)] as ErasedObjectObserver<T>;
}
static void documentCreated(object sender, DocumentCollectionEventArgs e)
{
Initialze(e.Document);
}
void commandWillStart(object sender, CommandEventArgs e)
{
if(e.GlobalCommandName == "ERASE")
{
Document doc = this.Document;
doc.CommandEnded += commandEnded;
doc.CommandCancelled += commandCancelled;
doc.CommandFailed += commandCancelled;
Document.Database.ObjectErased += objectErased;
erasedObjects.Clear();
}
}
private void objectErased(object sender, ObjectErasedEventArgs e)
{
if(e.DBObject is T)
{
if(e.Erased)
erasedObjects.Add(e.DBObject.Id);
else
erasedObjects.Remove(e.DBObject.Id);
}
}
private void OnCommandEnded(bool cancelled = false)
{
Document.CommandEnded -= commandEnded;
Document.CommandCancelled -= commandCancelled;
Document.CommandFailed -= commandCancelled;
Document.Database.ObjectErased -= objectErased;
if(!cancelled)
ProcessErasedObjects();
erasedObjects.Clear();
}
private void commandCancelled(object sender, CommandEventArgs e)
{
OnCommandEnded(true);
}
private void commandEnded(object sender, CommandEventArgs e)
{
OnCommandEnded(false);
}
void ProcessErasedObjects()
{
if(erasedObjects.Count == 0)
return;
using(var tr = new OpenCloseTransaction())
{
foreach(var id in erasedObjects)
{
T obj = (T)tr.GetObject(id, OpenMode.ForRead, true);
ProcessErasedObject(obj);
}
tr.Commit();
}
}
private void ProcessErasedObject(T erasedObject)
{
// TODO: Operate on each erased object here
}
}
/// <summary>
/// Example IExtensionApplication that initializes the
/// ErasedObjectObserver to observe the erasure of all
/// BlockReferences in every document.
/// </summary>
public class MyApplication : IExtensionApplication
{
public void Initialize()
{
Application.Idle += idle;
}
private void idle(object? sender, EventArgs e)
{
if(Application.DocumentManager.MdiActiveDocument != null)
{
Application.Idle -= idle;
ErasedObjectObserver<BlockReference>.Initialize();
}
}
public void Terminate()
{
}
}
}