-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApplication.cs
245 lines (202 loc) · 8.44 KB
/
Application.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
#region Imported Namespaces
//.NET common used namespaces
using System;
using System.Linq;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
//Revit.NET common used namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI.Selection;
using RevitExplorer;
using View = Autodesk.Revit.DB.View;
using Application = Autodesk.Revit.ApplicationServices.Application;
#endregion
namespace RevitExplorer
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
class Application : IExternalApplication
{
UIControlledApplication uiCtrlApp;
ControlledApplication app;
Document activeDoc;
View activeView;
IEnumerable<Element> allElements;
IEnumerable<Element> structuralElements;
Dialog dialog;
bool documentLoaded;
Process process;
IntPtr handle;
RevitWindowHandle revitWindowHandle;
public Result OnStartup(UIControlledApplication application)
{
documentLoaded = false;
try
{
process = Process.GetCurrentProcess();
handle = process.MainWindowHandle;
revitWindowHandle = new RevitWindowHandle(handle);
uiCtrlApp = application;
app = uiCtrlApp.ControlledApplication;
app.DocumentOpened += ((object o, DocumentOpenedEventArgs e) =>
{
UpdateDocument(e.Document);
documentLoaded = true;
ShowDialog();
});
app.DocumentClosed += ((object o, DocumentClosedEventArgs e) =>
{
documentLoaded = false;
});
uiCtrlApp.ViewActivated += ((object o, ViewActivatedEventArgs e) =>
{
if (documentLoaded)
{
UpdateActiveView(e);
ShowDialog();
}
});
}
catch (Exception)
{
return Result.Failed;
}
return Result.Succeeded;
}
private void SetupDialog()
{
dialog = new Dialog();
dialog.StartPosition = FormStartPosition.CenterScreen;
dialog.revertButton.Enabled = false;
dialog.applyButton.Enabled = false;
dialog.closeButton.Click += ((object o, EventArgs e) => dialog.Close());
dialog.applyButton.Click += ((object o, EventArgs e) =>
{
applyChanges(dialog.elementGridView);
dialog.revertButton.Enabled = false;
dialog.applyButton.Enabled = false;
});
dialog.revertButton.Click += ((object o, EventArgs e) =>
{
setupElementGridView(dialog.elementGridView, structuralElements);
dialog.revertButton.Enabled = false;
dialog.applyButton.Enabled = false;
});
//Enabling Apply and Revert buttons when cell value changes
//It should actually track the value and go back to disabled if there is no change
dialog.elementGridView.CellContentClick += ((object o, DataGridViewCellEventArgs e) =>
{
if (e.ColumnIndex != 2)
return;
dialog.applyButton.Enabled = true;
dialog.revertButton.Enabled = true;
});
}
private void ShowDialog()
{
if (dialog == null || dialog.IsDisposed)
SetupDialog();
dialog.activeViewLabel.Text = activeView.ViewName;
setupElementGridView(dialog.elementGridView, structuralElements);
//I only want to enable the dialog if the active view is a plan
//a section or a 3D view
if (activeView is ViewPlan ||
activeView is ViewSection ||
activeView is View3D)
EnableDialog();
else
DisableDialog();
if (dialog.Visible)
return;
dialog.Show(revitWindowHandle);
}
//Disabling all the children of the dialog but not the dialog itself
//I want to disable all elements of the dialog to make it unusable
//but keep the dialog itself movable
private void DisableDialog()
{
foreach (System.Windows.Forms.Control control in dialog.Controls)
control.Enabled = false;
}
//Opposite to the one above
private void EnableDialog()
{
foreach (System.Windows.Forms.Control control in dialog.Controls)
control.Enabled = true;
}
private void UpdateActiveView(ViewActivatedEventArgs e)
{
if (e.Status == EventStatus.Succeeded)
activeView = e.CurrentActiveView;
}
private void UpdateDocument(Document document)
{
activeDoc = document;
activeView = document.ActiveView;
allElements = new FilteredElementCollector(activeDoc)
.WhereElementIsNotElementType()
.WhereElementIsViewIndependent();
structuralElements = from elements in allElements
where elements.Category != null && elements.Category.HasMaterialQuantities
select elements;
}
private void setupElementGridView(DataGridView gridView, IEnumerable<Element> elements)
{
if (gridView.ColumnCount > 0)
gridView.Columns.Clear();
var textCell = new DataGridViewTextBoxCell();
var checkCell = new DataGridViewCheckBoxCell();
var checkCellStyle = new DataGridViewCellStyle();
checkCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
var elementIdColumn = new DataGridViewColumn(textCell);
var elementNameColumn = new DataGridViewColumn(textCell);
elementNameColumn.HeaderText = "Element Name";
elementNameColumn.ReadOnly = true;
var elementVisibilityColumn = new DataGridViewColumn(checkCell);
elementVisibilityColumn.HeaderText = "Visible";
gridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
gridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
gridView.RowHeadersVisible = false;
gridView.Columns.Add(elementIdColumn);
gridView.Columns.Add(elementNameColumn);
gridView.Columns.Add(elementVisibilityColumn);
gridView.Columns[0].Visible = false;
if (gridView.Rows.Count > 0)
gridView.Rows.Clear();
foreach (Element element in elements)
{
ElementType elementType = activeDoc.get_Element(element.GetTypeId()) as ElementType;
object[] newRow = { element.Id, element.Name, !element.IsHidden(activeView) };
gridView.Rows.Add(newRow);
}
}
void applyChanges(DataGridView gridView)
{
foreach (DataGridViewRow row in gridView.Rows)
{
var element = activeDoc.get_Element(row.Cells[0].Value as ElementId);
ElementSet elementSet = new ElementSet();
elementSet.Insert(element);
Transaction transaction = new Transaction(activeDoc);
transaction.Start(string.Format("Applying visibility of element {0}", element.Id.ToString()));
if ((bool)row.Cells[2].Value == true)
activeView.Unhide(elementSet);
else
if (element.CanBeHidden(activeView))
activeView.Hide(elementSet);
transaction.Commit();
}
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
}