/
ScheduleView.cs
435 lines (367 loc) · 14.7 KB
/
ScheduleView.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.DesignScript.Runtime;
using DynamoServices;
using Revit.Schedules;
using RevitServices.Persistence;
using RevitServices.Transactions;
namespace Revit.Elements.Views
{
/// <summary>
/// Base class for Revit Plan views
/// </summary>
[RegisterForTrace]
public class ScheduleView : View
{
#region Internal properties
/// <summary>
/// An internal handle on the Revit element
/// </summary>
internal Autodesk.Revit.DB.ViewSchedule InternalViewSchedule
{
get;
private set;
}
/// <summary>
/// Reference to the Element
/// </summary>
public override Autodesk.Revit.DB.Element InternalElement
{
get { return InternalViewSchedule; }
}
/// <summary>
/// Reference to Schedule Filters
/// </summary>
internal IList<Autodesk.Revit.DB.ScheduleFilter> InternalScheduleFilters
{
get;
private set;
}
#endregion
#region Private constructors
/// <summary>
/// Private constructor
/// </summary>
private ScheduleView(Autodesk.Revit.DB.ViewSchedule view)
{
SafeInit(() => InitScheduleView(view), true);
}
/// <summary>
/// Private constructor for creating Schedules from Category/ScheduleType.
/// </summary>
private ScheduleView(Category category, string name, ScheduleType type)
{
SafeInit(() => InitScheduleView(category, name, type));
}
/// <summary>
/// Private constructor for creating Schedules from AreaScheme.
/// </summary>
private ScheduleView(Element areaScheme, string name)
{
SafeInit(() => InitScheduleView(areaScheme, name));
}
#endregion
#region Helpers for private constructors
/// <summary>
/// Initialize a ScheduleView element
/// </summary>
private void InitScheduleView(Autodesk.Revit.DB.ViewSchedule view)
{
InternalSetScheduleView(view);
}
/// <summary>
/// Initialize a ScheduleView Element by Category, name and ScheduleType.
/// </summary>
/// <param name="category">Category of elements that Schedule will display.</param>
/// <param name="name">Name of the Schedule.</param>
/// <param name="type">ScheduleType ex: Key Schedule.</param>
private void InitScheduleView(Category category, string name, ScheduleType type)
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
// Get existing view if possible
var vs = ElementBinder.GetElementFromTrace<Autodesk.Revit.DB.ViewSchedule>(doc) ??
CreateViewSchedule(category, name, type);
InternalSetScheduleView(vs);
TransactionManager.Instance.TransactionTaskDone();
ElementBinder.CleanupAndSetElementForTrace(doc, InternalElement);
}
/// <summary>
/// Initialize a ScheduleView Element by AreaScheme and name.
/// </summary>
/// <param name="areaScheme">Area Scheme.</param>
/// <param name="name">Name of the Schedule.</param>
private void InitScheduleView(Element areaScheme, string name)
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
// Get existing view if possible
var vs = ElementBinder.GetElementFromTrace<Autodesk.Revit.DB.ViewSchedule>(doc) ??
CreateViewSchedule(areaScheme, name);
InternalSetScheduleView(vs);
TransactionManager.Instance.TransactionTaskDone();
ElementBinder.CleanupAndSetElementForTrace(doc, InternalElement);
}
#endregion
#region Private mutators
/// <summary>
/// Set the InternalViewSchedule property and the associated element id and unique id
/// </summary>
/// <param name="view"></param>
private void InternalSetScheduleView(Autodesk.Revit.DB.ViewSchedule view)
{
InternalViewSchedule = view;
InternalElementId = view.Id;
InternalUniqueId = view.UniqueId;
InternalScheduleFilters = view.Definition.GetFilters();
}
#endregion
#region Private helper methods
private static Autodesk.Revit.DB.ViewSchedule CreateViewSchedule(Category category, string name, ScheduleType type)
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
Autodesk.Revit.DB.ViewSchedule viewSchedule = null;
switch (type)
{
case ScheduleType.KeySchedule:
viewSchedule = Autodesk.Revit.DB.ViewSchedule.CreateKeySchedule(doc, new Autodesk.Revit.DB.ElementId(category.Id));
viewSchedule.Name = name;
break;
case ScheduleType.RegularSchedule:
viewSchedule = Autodesk.Revit.DB.ViewSchedule.CreateSchedule(doc, new Autodesk.Revit.DB.ElementId(category.Id));
viewSchedule.Name = name;
break;
case ScheduleType.MaterialTakeoff:
viewSchedule = Autodesk.Revit.DB.ViewSchedule.CreateMaterialTakeoff(doc, new Autodesk.Revit.DB.ElementId(category.Id));
viewSchedule.Name = name;
break;
}
TransactionManager.Instance.TransactionTaskDone();
return viewSchedule;
}
private static Autodesk.Revit.DB.ViewSchedule CreateViewSchedule(Element areaScheme, string name)
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
var viewSchedule = Autodesk.Revit.DB.ViewSchedule.CreateSchedule(doc,
new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInCategory.OST_Areas),
areaScheme.InternalElement.Id);
viewSchedule.Name = name;
TransactionManager.Instance.TransactionTaskDone();
return viewSchedule;
}
#endregion
#region Public static constructors
/// <summary>
/// Create Schedule by Category, Type and Name.
/// </summary>
/// <param name="category">Category that Schedule will be associated with.</param>
/// <param name="name">Name of the Schedule View.</param>
/// <param name="scheduleType">Type of Schedule View to be created. Ex. Key Schedule.</param>
/// <returns name="scheduleView">Schedule View.</returns>
public static ScheduleView CreateSchedule(Category category, string name, string scheduleType)
{
if (category == null)
{
throw new ArgumentNullException(Properties.Resources.CategoryArgumentException);
}
if (name == null)
{
throw new ArgumentNullException(Properties.Resources.NameArgumentException);
}
try
{
var t = (ScheduleType)Enum.Parse(typeof(ScheduleType), scheduleType);
return new ScheduleView(category, name, t);
}
catch (Exception)
{
throw new ArgumentNullException(Properties.Resources.ViewUnsupportedScheduleType);
}
}
/// <summary>
/// Create Area Schedule by Area Scheme and Name.
/// </summary>
/// <param name="areaScheme">Area Scheme that Schedule will be associated with.</param>
/// <param name="name">Name of the Schedule View.</param>
/// <returns name="scheduleView">Schedule View.</returns>
public static ScheduleView CreateAreaSchedule(Element areaScheme, string name)
{
if (areaScheme == null)
{
throw new ArgumentNullException(Properties.Resources.AreaSchemeArgumentException);
}
if (name == null)
{
throw new ArgumentNullException(Properties.Resources.NameArgumentException);
}
return new ScheduleView(areaScheme, name);
}
/// <summary>
/// Remove Schedule Field from Schedule View.
/// </summary>
/// <param name="fields">Schedule Fields (columns) to be removed.</param>
/// <returns name="scheduleView">Schedule View.</returns>
public ScheduleView RemoveFields(List<ScheduleField> fields)
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
foreach (var field in fields)
{
if (Fields.Any(x => x.Name == field.Name))
{
InternalViewSchedule.Definition.RemoveField(field.InternalScheduleField.FieldId);
}
}
TransactionManager.Instance.TransactionTaskDone();
return this;
}
/// <summary>
/// Add Field (Column) to Schedule View.
/// </summary>
/// <param name="fields">Schedulable Field retrieved from ScheduleView.SchedulableFields.</param>
/// <returns name="scheduleView">Schedule View.</returns>
public ScheduleView AddFields(List<SchedulableField> fields)
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
foreach (var field in fields)
{
if (!Fields.Any(x => x.Name == field.Name))
{
InternalViewSchedule.Definition.AddField(field.InternalSchedulableField);
}
}
TransactionManager.Instance.TransactionTaskDone();
return this;
}
/// <summary>
/// Export View Schedule to CSV, TSV etc.
/// </summary>
/// <param name="path">A valid file path with file extension.</param>
/// <param name="exportOptions">Export Options.</param>
/// <returns name="scheduleView">Schedule View.</returns>
public ScheduleView Export(string path, ScheduleExportOptions exportOptions)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(Properties.Resources.View_ExportAsImage_Path_Invalid, "path");
}
if (exportOptions == null)
{
throw new ArgumentException(Properties.Resources.ExportOptionsArgumentException);
}
// Run export
var folder = System.IO.Path.GetDirectoryName(path);
var name = System.IO.Path.GetFileName(path);
try
{
InternalViewSchedule.Export(folder, name, exportOptions.InternalScheduleExportOptions);
}
catch (Exception ex)
{
throw new Exception(Properties.Resources.ScheduleExportError, ex);
}
return this;
}
/// <summary>
/// Add Schedule Filters to Schedule View.
/// </summary>
/// <param name="scheduleFilters">List of Schedule Filters.</param>
/// <returns name="scheduleView">Schedule View.</returns>
public ScheduleView AddFilters(List<ScheduleFilter> scheduleFilters)
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
var currentFilters = InternalScheduleFilters.Select(x => new ScheduleFilter(x)).ToList();
foreach (var sf in scheduleFilters)
{
if (!currentFilters.Contains(sf))
{
InternalViewSchedule.Definition.AddFilter(sf.InternalScheduleFilter);
currentFilters.Add(sf);
}
}
InternalScheduleFilters = currentFilters.Select(x => x.InternalScheduleFilter).ToList();
TransactionManager.Instance.TransactionTaskDone();
return this;
}
/// <summary>
/// Clear all Schedule Filters from Schedule View.
/// </summary>
/// <returns name="scheduleView">Schedule View.</returns>
public ScheduleView ClearAllFilters()
{
var doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
InternalViewSchedule.Definition.ClearFilters();
InternalScheduleFilters.Clear();
TransactionManager.Instance.TransactionTaskDone();
return this;
}
#endregion
#region Properties
/// <summary>
/// Schedule Filters.
/// </summary>
public List<ScheduleFilter> ScheduleFilters
{
get
{
return InternalScheduleFilters.Select(x => new ScheduleFilter(x)).ToList();
}
}
/// <summary>
/// Schedule Fields.
/// </summary>
public List<ScheduleField> Fields
{
get
{
var fieldIds = InternalViewSchedule.Definition.GetFieldOrder();
return fieldIds.Select(id => new ScheduleField(InternalViewSchedule.Definition.GetField(id))).ToList();
}
}
/// <summary>
/// Schedulable Fields.
/// </summary>
public List<SchedulableField> SchedulableFields
{
get
{
var fields = InternalViewSchedule.Definition.GetSchedulableFields();
return fields.Select(x => new SchedulableField(x)).ToList();
}
}
#endregion
#region Helpers
/// <summary>
/// ScheduleType Enumeration
/// </summary>
[IsVisibleInDynamoLibrary(false)]
public enum ScheduleType
{
KeySchedule,
RegularSchedule,
MaterialTakeoff
}
#endregion
#region Internal static constructors
/// <summary>
/// Create a View from a user selected Element.
/// </summary>
/// <param name="view"></param>
/// <param name="isRevitOwned"></param>
/// <returns></returns>
internal static ScheduleView FromExisting(Autodesk.Revit.DB.ViewSchedule view, bool isRevitOwned)
{
return new ScheduleView(view)
{
IsRevitOwned = isRevitOwned
};
}
#endregion
}
}