forked from DynamoDS/DynamoRevit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElementIDLifecycleManager.cs
212 lines (186 loc) · 6.34 KB
/
ElementIDLifecycleManager.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynamoServices;
namespace RevitServices.Persistence
{
/// <summary>
/// Class to handle the lifetime of elements from their IDs
/// </summary>
public class ElementIDLifecycleManager<T>
{
//Note this is only mutex for a specific type param
private static Object singletonMutex = new object();
private static ElementIDLifecycleManager<T> manager;
private Dictionary<T, List<Object>> wrappers;
private Dictionary<T, bool> revitDeleted;
private ElementIDLifecycleManager()
{
wrappers = new Dictionary<T, List<object>>();
revitDeleted = new Dictionary<T, bool>();
}
public static void DisposeInstance()
{
lock (singletonMutex)
{
if (manager != null)
{
manager = null;
}
}
}
/// <summary>
/// Get the LifecycleManager for the specific type
/// WARNING: This is only a singleton for a given TypeArg
/// </summary>
/// <returns></returns>
public static ElementIDLifecycleManager<T> GetInstance()
{
lock (singletonMutex)
{
if (manager == null)
{
manager = new ElementIDLifecycleManager<T>();
}
return manager;
}
}
/// <summary>
/// Register a new dependency between an element ID and a wrapper
/// </summary>
/// <param name="elementID"></param>
/// <param name="wrapper"></param>
public void RegisterAsssociation(T elementID, Object wrapper)
{
List<Object> existingWrappers;
if (!wrappers.TryGetValue(elementID, out existingWrappers))
{
existingWrappers = new List<object>();
wrappers.Add(elementID, existingWrappers);
}
#if DEBUG
else
{
//ID already existed, check we're not over adding
Validity.Assert(!existingWrappers.Contains(wrapper),
"Lifecycle manager alert: registering the same Revit Element Wrapper twice"
+ " {6528305F}");
//return;
}
#endif
existingWrappers.Add(wrapper);
if (!revitDeleted.ContainsKey(elementID))
{
revitDeleted.Add(elementID, false);
}
}
/// <summary>
/// Remove an association between an element ID and
/// </summary>
/// <param name="elementID"></param>
/// <param name="wrapper"></param>
/// <returns>The number of remaining associations</returns>
public int UnRegisterAssociation(T elementID, Object wrapper)
{
List<Object> existingWrappers;
if (wrappers.TryGetValue(elementID, out existingWrappers))
{
//ID already existed, check we're not over adding
if (existingWrappers.Contains(wrapper))
{
int index = existingWrappers.FindIndex((x) => object.ReferenceEquals(x, wrapper));
existingWrappers.RemoveAt(index);
if (existingWrappers.Count == 0)
{
wrappers.Remove(elementID);
revitDeleted.Remove(elementID);
return 0;
}
else
{
return existingWrappers.Count;
}
}
else
{
throw new InvalidOperationException(
"Attempting to remove a wrapper that wasn't there registered");
}
}
else
{
//The ID didn't exist
throw new InvalidOperationException(
"Attempting to remove a wrapper, but there were no ids registered");
}
}
/// <summary>
/// Get the number of wrappers that are registered
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int GetRegisteredCount(T id)
{
if (!wrappers.ContainsKey(id))
{
return 0;
}
else
{
return wrappers[id].Count;
}
}
/// <summary>
/// Return null if no wrappers are registered with this ID.
/// Otherwise, returns the first wrapper.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public object GetFirstWrapper(T id)
{
List<object> wrappersForGivenId;
if (wrappers.TryGetValue(id, out wrappersForGivenId))
{
if (null != wrappersForGivenId && wrappersForGivenId.Count > 0)
return wrappersForGivenId.First();
}
return null;
}
/// <summary>
/// Checks whether an element has been deleted in Revit
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool IsRevitDeleted(T id)
{
if (!revitDeleted.ContainsKey(id))
{
throw new ArgumentException("Element is not registered");
}
return revitDeleted[id];
}
/// <summary>
/// This method tells the life cycle
/// </summary>
/// <param name="id"The element that needs to be deleted></param>
public void NotifyOfRevitDeletion(T id)
{
revitDeleted[id] = true;
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine("ElementIdLifeCycleManager:");
foreach (var kvp in wrappers)
{
sb.AppendLine(string.Format("\tElement ID {0}:", kvp.Key));
foreach (var item in kvp.Value)
{
sb.AppendLine(string.Format("\t\t{0}:", item));
}
}
return sb.ToString();
}
}
}