-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExternalEventChangeView.cs
283 lines (241 loc) · 11.2 KB
/
ExternalEventChangeView.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
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Bimbot.Bcf;
using Bimbot.Utils;
namespace Bimbot.ExternalEvents
{
/// <summary>
/// Obfuscation Ignore for External Interface
/// </summary>
public class ExtEvntSetViewpoint : IExternalEventHandler
{
public VisualizationInfo v;
/// <summary>
/// External Event Implementation
/// </summary>
/// <param name="app"></param>
public void Execute(UIApplication app)
{
try
{
UIDocument uidoc = app.ActiveUIDocument;
Document doc = uidoc.Document;
// IS ORTHOGONAL
if (v.OrthogonalCamera != null)
{
if (v.OrthogonalCamera.CameraViewPoint == null || v.OrthogonalCamera.CameraUpVector == null || v.OrthogonalCamera.CameraDirection == null)
return;
//type = "OrthogonalCamera";
var zoom = v.OrthogonalCamera.ViewToWorldScale.ToFeet();
var cameraDirection = RevitUtils.GetRevitXYZ(v.OrthogonalCamera.CameraDirection);
var cameraUpVector = RevitUtils.GetRevitXYZ(v.OrthogonalCamera.CameraUpVector);
var cameraViewPoint = RevitUtils.GetRevitXYZ(v.OrthogonalCamera.CameraViewPoint);
var orient3D = RevitUtils.ConvertBasePoint(doc, cameraViewPoint, cameraDirection, cameraUpVector, true);
View3D orthoView = null;
//if active view is 3d ortho use it
if (doc.ActiveView.ViewType == ViewType.ThreeD)
{
var activeView3D = doc.ActiveView as View3D;
if (!activeView3D.IsPerspective)
orthoView = activeView3D;
}
if (orthoView == null)
{
//try to use an existing 3D view
IEnumerable<View3D> viewcollector3D = get3DViews(doc);
if (viewcollector3D.Any(o => o.Name == "{3D}" || o.Name == "BCFortho"))
orthoView = viewcollector3D.First(o => o.Name == "{3D}" || o.Name == "BCFortho");
}
using (var trans = new Transaction(uidoc.Document))
{
if (trans.Start("Open orthogonal view") == TransactionStatus.Started)
{
//create a new 3d ortho view
if (orthoView == null)
{
orthoView = View3D.CreateIsometric(doc, getFamilyViews(doc).First().Id);
orthoView.Name = "BCFortho";
}
else
{
//reusing an existing view, I net to reset the visibility
//placed this here because if set afterwards it doesn't work
orthoView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
}
orthoView.SetOrientation(orient3D);
trans.Commit();
}
}
uidoc.ActiveView = orthoView;
//adjust view rectangle
// **** CUSTOM VALUE FOR TEKLA **** //
// double x = zoom / 2.5;
// **** CUSTOM VALUE FOR TEKLA **** //
double x = zoom;
//if(MySettings.Get("optTekla")=="1")
// x = zoom / 2.5;
//set UI view position and zoom
XYZ m_xyzTl = uidoc.ActiveView.Origin.Add(uidoc.ActiveView.UpDirection.Multiply(x)).Subtract(uidoc.ActiveView.RightDirection.Multiply(x));
XYZ m_xyzBr = uidoc.ActiveView.Origin.Subtract(uidoc.ActiveView.UpDirection.Multiply(x)).Add(uidoc.ActiveView.RightDirection.Multiply(x));
uidoc.GetOpenUIViews().First().ZoomAndCenterRectangle(m_xyzTl, m_xyzBr);
}
//perspective
else if (v.PerspectiveCamera != null)
{
if (v.PerspectiveCamera.CameraViewPoint == null || v.PerspectiveCamera.CameraUpVector == null || v.PerspectiveCamera.CameraDirection == null)
return;
//not used since the fov cannot be changed in Revit
var zoom = v.PerspectiveCamera.FieldOfView;
//FOV - not used
//double z1 = 18 / Math.Tan(zoom / 2 * Math.PI / 180);
//double z = 18 / Math.Tan(25 / 2 * Math.PI / 180);
//double factor = z1 - z;
var cameraDirection = RevitUtils.GetRevitXYZ(v.PerspectiveCamera.CameraDirection);
var cameraUpVector = RevitUtils.GetRevitXYZ(v.PerspectiveCamera.CameraUpVector);
var cameraViewPoint = RevitUtils.GetRevitXYZ(v.PerspectiveCamera.CameraViewPoint);
var orient3D = RevitUtils.ConvertBasePoint(doc, cameraViewPoint, cameraDirection, cameraUpVector, true);
View3D perspView = null;
//try to use an existing 3D view
IEnumerable<View3D> viewcollector3D = get3DViews(doc);
if (viewcollector3D.Any(o => o.Name == "BCFpersp"))
perspView = viewcollector3D.First(o => o.Name == "BCFpersp");
using (var trans = new Transaction(uidoc.Document))
{
if (trans.Start("Open perspective view") == TransactionStatus.Started)
{
if (null == perspView)
{
perspView = View3D.CreatePerspective(doc, getFamilyViews(doc).First().Id);
perspView.Name = "BCFpersp";
}
else
{
//reusing an existing view, I net to reset the visibility
//placed this here because if set afterwards it doesn't work
perspView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
}
perspView.SetOrientation(orient3D);
// turn off the far clip plane
if (perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR).HasValue)
{
Parameter m_farClip = perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR);
m_farClip.Set(0);
}
perspView.CropBoxActive = true;
perspView.CropBoxVisible = true;
trans.Commit();
}
}
uidoc.ActiveView = perspView;
}
//sheet
else if (v.SheetCamera != null)
{
IEnumerable<View> viewcollectorSheet = getSheets(doc, v.SheetCamera.SheetID, v.SheetCamera.SheetName);
if (!viewcollectorSheet.Any())
{
MessageBox.Show("View " + v.SheetCamera.SheetName + " with Id=" + v.SheetCamera.SheetID + " not found.");
return;
}
uidoc.ActiveView = viewcollectorSheet.First();
uidoc.RefreshActiveView();
XYZ m_xyzTl = new XYZ(v.SheetCamera.TopLeft.X, v.SheetCamera.TopLeft.Y, v.SheetCamera.TopLeft.Z);
XYZ m_xyzBr = new XYZ(v.SheetCamera.BottomRight.X, v.SheetCamera.BottomRight.Y, v.SheetCamera.BottomRight.Z);
uidoc.GetOpenUIViews().First().ZoomAndCenterRectangle(m_xyzTl, m_xyzBr);
}
//no view included
else
return;
if (v.Components == null)
return;
var elementsToSelect = new List<ElementId>();
var elementsToHide = new List<ElementId>();
var elementsToShow = new List<ElementId>();
var visibleElems = new FilteredElementCollector(doc, doc.ActiveView.Id)
.WhereElementIsNotElementType()
.WhereElementIsViewIndependent()
.ToElementIds()
.Where(e => doc.GetElement(e).CanBeHidden(doc.ActiveView)); //might affect performance, but it's necessary
bool canSetVisibility = (v.Components.Visibility != null &&
v.Components.Visibility.DefaultVisibilitySpecified &&
v.Components.Visibility.Exceptions.Any())
;
bool canSetSelection = (v.Components.Selection != null && v.Components.Selection.Any());
//loop elements
foreach (var e in visibleElems)
{
var guid = IfcGuid.ToIfcGuid(ExportUtils.GetExportId(doc, e));
if (canSetVisibility)
{
if (v.Components.Visibility.DefaultVisibility)
{
if (v.Components.Visibility.Exceptions.Any(x => x.IfcGuid == guid))
elementsToHide.Add(e);
}
else
{
if (v.Components.Visibility.Exceptions.Any(x => x.IfcGuid == guid))
elementsToShow.Add(e);
}
}
if (canSetSelection)
{
if (v.Components.Selection.Any(x => x.IfcGuid == guid))
elementsToSelect.Add(e);
}
}
using (var trans = new Transaction(uidoc.Document))
{
if (trans.Start("Apply BCF visibility and selection") == TransactionStatus.Started)
{
if (elementsToHide.Any())
doc.ActiveView.HideElementsTemporary(elementsToHide);
//there are no items to hide, therefore hide everything and just show the visible ones
else if (elementsToShow.Any())
doc.ActiveView.IsolateElementsTemporary(elementsToShow);
if (elementsToSelect.Any())
uidoc.Selection.SetElementIds(elementsToSelect);
}
trans.Commit();
}
uidoc.RefreshActiveView();
}
catch (Exception ex)
{
TaskDialog.Show("Error!", "exception: " + ex);
}
}
private IEnumerable<ViewFamilyType> getFamilyViews(Document doc)
{
return from elem in new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
let type = elem as ViewFamilyType
where type.ViewFamily == ViewFamily.ThreeDimensional
select type;
}
private IEnumerable<View3D> get3DViews(Document doc)
{
return from elem in new FilteredElementCollector(doc).OfClass(typeof(View3D))
let view = elem as View3D
select view;
}
private IEnumerable<View> getSheets(Document doc, int id, string stname)
{
ElementId eid = new ElementId(id);
return from elem in new FilteredElementCollector(doc).OfClass(typeof(View))
let view = elem as View
//Get the view with the given Id or given name
where view.Id == eid | view.Name == stname
select view;
}
public string GetName()
{
return "Apply BCF-viewpoint";
}
}
}