-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cs
300 lines (264 loc) · 10.3 KB
/
App.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using AutoSync.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
namespace AutoSync
{
public class App : IExternalApplication
{
//time always need to be multiples of checkinterval
public static readonly int CheckInterval = 5 * 60;//5 min
public static readonly int DismissTime = 30 * 60;//30 min
public static readonly int RetrySync = 30 * 60;//30 min
public static readonly int RelinquishCheck = 15 * 60;//15 min
public static readonly int SyncCheck = 120 * 60;//120 min
public static int CountDown = 0;
public static bool IsRunning = false;
public static bool IsRelinquishing = false;
public static bool IsSyncing = false;
public static readonly HashSet<Document> Documents = new HashSet<Document>();
private static readonly Timer IdleTimer = new Timer(Tick, null, CheckInterval * 1000, CheckInterval * 1000);
private static readonly IdleTimerEvent TimerEvent = new IdleTimerEvent();
private static ExternalEvent ExternalEvent = null;
private void AddRibbonButton(UIControlledApplication uiapp)
{
RibbonPanel ribbonPanel = uiapp.CreateRibbonPanel("AutoSync");
string assembly = Assembly.GetExecutingAssembly().Location;
PushButtonData data = new PushButtonData("AutoSync", "AutoSync", assembly, "AutoSync.ShowActive")
{
AvailabilityClassName = "AutoSync.Availability"
};
PushButton button = ribbonPanel.AddItem(data) as PushButton;
button.ToolTip = "Automatically Sync Inactive Revit Documents.";
Uri uri = new Uri("pack://application:,,,/AutoSync;component/AutoSync.png");
BitmapImage image = new BitmapImage(uri);
button.LargeImage = image;
}
public Result OnStartup(UIControlledApplication uiapp)
{
AddRibbonButton(uiapp);
ExternalEvent = ExternalEvent.Create(TimerEvent);
uiapp.ControlledApplication.DocumentOpening += new EventHandler<DocumentOpeningEventArgs>(OnOpening);
uiapp.ControlledApplication.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(OnOpened);
uiapp.ControlledApplication.DocumentCreated += new EventHandler<DocumentCreatedEventArgs>(OnCreated);
uiapp.ControlledApplication.DocumentClosing += new EventHandler<DocumentClosingEventArgs>(OnClosing);
uiapp.ControlledApplication.DocumentChanged += new EventHandler<DocumentChangedEventArgs>(OnChanged);
uiapp.ViewActivated += new EventHandler<ViewActivatedEventArgs>(OnViewActivated);
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication uiapp)
{
IdleTimer.Dispose();
return Result.Succeeded;
}
internal void OnOpening(object sender, DocumentOpeningEventArgs e)
{
IsRunning = true;
}
internal void OnOpened(object sender, DocumentOpenedEventArgs e)
{
Reset();
NewDocument(e.Document);
IsRunning = false;
}
internal void OnClosing(object sender, DocumentClosingEventArgs e)
{
Reset();
RemoveDocument(e.Document);
}
internal void OnCreated(object sender, DocumentCreatedEventArgs e)
{
Reset();
NewDocument(e.Document);
}
public void OnChanged(object sender, DocumentChangedEventArgs e)
{
Reset();
}
internal void OnViewActivated(object sender, ViewActivatedEventArgs e)
{
Reset();
}
private void NewDocument(Document doc)
{
if (doc.IsFamilyDocument | doc.IsDetached | !doc.IsWorkshared)
{
return;
}
Documents.Add(doc);
}
private static void RemoveDocument(Document doc)
{
if (Documents.Contains(doc))
{
Documents.Remove(doc);
}
}
private static void Reset()
{
if (IsRunning) { return; }
CountDown = 0;
IsRelinquishing = false;
IsSyncing = false;
}
private static void Tick(object state)
{
if (ExternalEvent == null | Documents.Count == 0)
{
Reset();
return;
}
if (IsRunning | IsSyncing | IsRelinquishing)
{
return;
}
CountDown += CheckInterval;
if (CountDown > SyncCheck)
{
IsSyncing = true;
ExternalEvent.Raise();
}
else
{
if (CountDown % RelinquishCheck == 0)
{
IsRelinquishing = true;
ExternalEvent.Raise();
}
}
}
private static bool Dismiss()
{
var dismissdialog = new DismissDialog(DismissTime / 60);
var dialog = dismissdialog.ShowDialog();
if (dialog == true)
{
CountDown -= DismissTime;
return true;
}
else
{
return false;
}
}
public class IdleTimerEvent : IExternalEventHandler
{
public void Execute(UIApplication app)
{
if (Documents.Count == 0)
{
return;
}
TransactWithCentralOptions transact = new TransactWithCentralOptions();
SynchLockCallback transCallBack = new SynchLockCallback();
transact.SetLockCallback(transCallBack);
SynchronizeWithCentralOptions syncset = new SynchronizeWithCentralOptions();
RelinquishOptions relinquishoptions = new RelinquishOptions(true)
{
CheckedOutElements = true
};
syncset.SetRelinquishOptions(relinquishoptions);
try
{
IsRunning = true;
if (IsRelinquishing)
{
foreach (Document doc in Documents)
{
try
{
if (doc != null && doc.IsValidObject)
{
WorksharingUtils.RelinquishOwnership(doc, relinquishoptions, transact);
}
}
catch { }
}
IsRelinquishing = false;
Dismiss();
}
if (IsSyncing)
{
if (Dismiss())
{
IsSyncing = false;
return;
}
app.Application.FailuresProcessing += FailureProcessor;
foreach (Document doc in Documents)
{
try
{
if (doc != null && doc.IsValidObject)
{
WorksharingUtils.RelinquishOwnership(doc, relinquishoptions, transact);
doc.SynchronizeWithCentral(transact, syncset);
CountDown = 0;
app.Application.WriteJournalComment("AutoSync", true);
}
}
catch
{
CountDown -= RetrySync;
}
}
app.Application.FailuresProcessing -= FailureProcessor;
IsSyncing = false;
}
}
finally
{
IsRunning = false;
transact.Dispose();
syncset.Dispose();
relinquishoptions.Dispose();
}
}
public string GetName()
{
return "AutoSync";
}
private void FailureProcessor(object sender, FailuresProcessingEventArgs e)
{
FailuresAccessor fas = e.GetFailuresAccessor();
List<FailureMessageAccessor> fma = fas.GetFailureMessages().ToList();
foreach (FailureMessageAccessor fa in fma)
{
fas.DeleteWarning(fa);
}
}
}
class SynchLockCallback : ICentralLockedCallback
{
public bool ShouldWaitForLockAvailability()
{
return false;
}
}
}
public class Availability : IExternalCommandAvailability
{
public bool IsCommandAvailable(UIApplication a, CategorySet b)
{
return true;
}
}
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
class ShowActive : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
TaskDialog.Show("Status", $"AutoSync is active.\n\nRelinquish every {App.RelinquishCheck} minutes.\nSync after {App.SyncCheck} minutes.");
return Result.Succeeded;
}
}
}