-
Notifications
You must be signed in to change notification settings - Fork 1
/
Command.cs
130 lines (116 loc) · 4.52 KB
/
Command.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
#region Namespaces
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion
namespace PrintIndex
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public static Nullable<bool> dialogResult = false;
public static IList<string> nameSchedule = new List<string>();
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
Document doc = uidoc.Document;
ElementId eId = new ElementId(-2003100);
//Collect Schedules from active document
foreach (ViewSchedule vSched in new FilteredElementCollector(doc)
.OfClass(typeof(ViewSchedule))
.Cast<ViewSchedule>()
.Where(q => !q.IsTitleblockRevisionSchedule && !q.IsInternalKeynoteSchedule))
{
if (vSched.Definition.CategoryId == eId)
{
nameSchedule.Add(vSched.Name);
}
}
//Instantiate Form
MainWindow mWindow = new MainWindow();
mWindow.ShowDialog();
//Error handling
if(dialogResult == false)
{
GarbageCollect();
return Result.Failed;
}
if(MainWindow.selectedName == null)
{
TaskDialog.Show("Error", "No schedule was selected!");
GarbageCollect();
return Result.Failed;
}
//get ElementId of selected schedule
var viewId = doc.ActiveView.Id;
foreach (ViewSchedule vSched2 in new FilteredElementCollector(doc)
.OfClass(typeof(ViewSchedule))
.Cast<ViewSchedule>()
.Where(q => !q.IsTitleblockRevisionSchedule && !q.IsInternalKeynoteSchedule))
{
if (vSched2.Name == MainWindow.selectedName)
{
viewId = vSched2.Id;
}
}
//Get sheets from selected schedule, catch error for invalid schedules
ViewSet vs = new ViewSet();
try
{
foreach (ViewSheet vSh in new FilteredElementCollector(doc, viewId))
{
//Ignore dummy sheets
if(vSh.CanBePrinted == true)
{
vs.Insert(vSh);
}
}
}
catch (System.InvalidCastException)
{
TaskDialog.Show("Error","Cannot use '" + MainWindow.selectedName + "'" + Environment.NewLine + "'" + MainWindow.selectedName + "' is not a sheet index.");
GarbageCollect();
return Result.Failed;
}
// Save ViewSet with sheets from selected schedule using provided name
using (Transaction tx = new Transaction(doc))
{
tx.Start("SaveSet");
PrintManager printManager = doc.PrintManager;
printManager.PrintRange = PrintRange.Select;
ViewSheetSetting viewSheetSetting = printManager.ViewSheetSetting;
viewSheetSetting.CurrentViewSheetSet.Views = vs;
try
{
viewSheetSetting.SaveAs(MainWindow.setName);
}
catch (Autodesk.Revit.Exceptions.InvalidOperationException)
{
TaskDialog.Show("Error", "The name '" + MainWindow.setName + "' is already in use!" + Environment.NewLine + "Pick a different name.");
GarbageCollect();
return Result.Failed;
}
tx.Commit();
}
TaskDialog.Show("View Set", vs.Size + " sheets added to '" + MainWindow.setName + "'");
GarbageCollect();
return Result.Succeeded;
}
//Reset public variables for next program run
static void GarbageCollect()
{
dialogResult = false;
nameSchedule.Clear();
MainWindow.selectedName = null;
MainWindow.setName = null;
}
}
}