forked from ASRRtechnologies/lib-asrr-revit-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.cs
292 lines (240 loc) · 9.96 KB
/
Utilities.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using NLog;
namespace ASRR.Revit.Core.Elements
{
public class Utilities
{
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
public static void ZoomToPoint(UIDocument uidoc, XYZ point, float zoom)
{
var doc = uidoc.Document;
var view = doc.ActiveView;
UIView uiView = null;
var uiviews = uidoc.GetOpenUIViews();
foreach (var uv in uiviews)
if (uv.ViewId.Equals(view.Id))
{
uiView = uv;
break;
}
if (uiView == null)
return;
var corners = uiView.GetZoomCorners();
var bottomLeftCorner = corners[0];
var topRightCorner = corners[1];
var diagonal = (topRightCorner - bottomLeftCorner).Normalize();
diagonal *= zoom;
uiView.ZoomAndCenterRectangle(point - diagonal, point + diagonal);
}
public static View3D Get3dView(Document doc)
{
//TODO: zorgen voor altijd een juiste view, bijv voorkeur view met naam '{3D}'
var collector = new FilteredElementCollector(doc).OfClass(typeof(View3D));
foreach (View3D v in collector)
// Skip view template here because view templates are invisible in project browser
if (!v.IsTemplate)
return v;
return null;
}
public static IEnumerable<Room> GetRooms(Document doc)
{
var elements = GetElementsOfCategory(doc, BuiltInCategory.OST_Rooms);
return elements.Select(e => e as Room);
}
public static ICollection<Element> GetElementsOfCategory(Document doc, BuiltInCategory category)
{
var collector = new FilteredElementCollector(doc);
var filter = new ElementCategoryFilter(category);
return collector.WherePasses(filter).ToElements();
}
public static ICollection<Element> GetElementsOfCategories(Document doc,
ICollection<BuiltInCategory> categories)
{
var collector = new FilteredElementCollector(doc);
var filter = new ElementMulticategoryFilter(categories);
return collector.WherePasses(filter).ToElements();
}
public static bool SetParameter(Element element, string parameterName, int value)
{
var parameter = element.LookupParameter(parameterName);
if (parameter != null)
try
{
switch (parameter.StorageType)
{
case StorageType.None:
return false;
case StorageType.Integer:
parameter.Set(value);
return true;
case StorageType.Double:
parameter.Set((double) value);
return true;
case StorageType.String:
return false;
case StorageType.ElementId:
parameter.Set(new ElementId(value));
return true;
}
}
catch
{
}
return false;
}
public static void DrawLine(Document doc, XYZ start, XYZ end)
{
if (GeometryUtils.IsAlmostEqual(start, end))
throw new ArgumentException("Expected two different points.");
var line = Line.CreateBound(start, end);
if (null == line)
throw new Exception(
"Geometry line creation failed.");
doc.Create.NewModelCurve(line, NewSketchPlanePassLine());
SketchPlane NewSketchPlanePassLine()
{
XYZ norm;
if (start.X == end.X)
norm = XYZ.BasisX;
else if (start.Y == end.Y)
norm = XYZ.BasisY;
else
norm = XYZ.BasisZ;
var plane = Plane.CreateByNormalAndOrigin(norm, start);
return SketchPlane.Create(doc, plane);
}
}
public static string CurveToString(Curve curve)
{
var result = "Start: ";
result += XYZToString(curve.GetEndPoint(0));
result += "End: ";
result += XYZToString(curve.GetEndPoint(1));
result += Environment.NewLine;
return result;
}
public static string XYZToString(XYZ point)
{
return "(" + point.X + ", " + point.Y + ", " + point.Z + ")";
}
public static double SquareFootToSquareM(double sqFoot, int decimals = 2)
{
return Math.Round(sqFoot * 0.092903, decimals);
}
public static List<T> GetAllOfType<T>(Document doc) where T : class
{
var collector = new FilteredElementCollector(doc);
return collector.OfClass(typeof(T)).Select(e => e as T).ToList();
}
public static T GetFirstOfType<T>(Document doc) where T : class
{
return GetAllOfType<T>(doc).FirstOrDefault();
}
public static T FindElementByName<T>(Document doc, string elementName) where T : class
{
var allElements = GetAllOfType<T>(doc);
var element = allElements.FirstOrDefault(e => (e as Element).Name == elementName);
return element;
}
public static List<T> FindElementsByName<T>(Document doc, IEnumerable<string> elementNames) where T : class
{
var result = new List<T>();
if (elementNames == null)
return result;
var allElements = GetAllOfType<T>(doc);
foreach (var elementName in elementNames)
{
var element = allElements.FirstOrDefault(e => (e as Element).Name == elementName);
if (element == null)
{
_log.Error($"Could not find element with name '{elementName}'");
continue; //Don't add null when element can't be found
}
result.Add(element);
}
return result;
}
//Saves and closes, as well as removes annoying back-up files
public static void SaveAndCloseDocument(Document doc, string destinationFilePath, bool overwrite = true)
{
var destinationDirectory = Path.GetDirectoryName(destinationFilePath);
Directory.CreateDirectory(destinationDirectory);
var saveAsOptions = new SaveAsOptions {OverwriteExistingFile = overwrite};
doc.SaveAs(destinationFilePath, saveAsOptions);
doc.Close();
RemoveBackUpFilesFromDirectory(new FileInfo(destinationFilePath).Directory.FullName);
}
/// <summary>
/// Opens a Revit file and sets the main 3d view as the active view, with the visual style set to "Shaded"
/// </summary>
public static void OpenDocumentIntoShaded3DView(UIApplication uiApp, string filePath)
{
var uiDoc = uiApp.OpenAndActivateDocument(filePath);
var doc = uiDoc.Document;
var view = GetFirstOfType<View3D>(doc);
if (view == null)
return;
//These UI commands must happen outside of a transaction
uiDoc.ActiveView = view;
//Close the view that opened when this document started if it's not the 3d view
var openViews = uiDoc.GetOpenUIViews();
foreach (var uiView in openViews)
if (uiView.ViewId != view.Id)
uiView.Close();
using (var transaction = new Transaction(doc))
{
transaction.Start("Set view graphics to Shaded");
view.get_Parameter(BuiltInParameter.MODEL_GRAPHICS_STYLE).Set(3); //3 = Shaded
transaction.Commit();
}
}
public static void RemoveBackUpFilesFromDirectory(string directory)
{
//Remove annoying backup files
foreach (var file in Directory.GetFiles(directory))
for (var i = 1; i < 10; i++)
if (file.EndsWith($".000{i}.rvt"))
File.Delete(file);
}
public static string WriteXyz(XYZ vector)
{
return $"({vector.X:0.##}, {vector.Y:0.##}, {vector.Z:0.##})";
}
public static CopyPasteOptions UseDestinationOnDuplicateNameCopyPasteOptions()
{
var copyOptions = new CopyPasteOptions();
copyOptions.SetDuplicateTypeNamesHandler(new UseDestinationHandler());
return copyOptions;
}
/// <summary>
/// Compose a clean link of separate parts like Path.Combine does
/// </summary>
public static string CombineUris(params string[] uris)
{
if (uris == null)
throw new ArgumentNullException(nameof(uris));
var urisList = uris.ToList();
var result = "";
for (var i = 0; i < urisList.Count; i++)
if (urisList[i] != null)
{
var trimmedUri = urisList[i];
trimmedUri = trimmedUri.TrimStart('/', '\\');
trimmedUri = trimmedUri.TrimEnd('/', '\\');
var slash = i == 0 ? "" : "/";
result += $"{slash}{trimmedUri}";
}
return result;
}
public static string CleanUpPath(string path)
{
return path == null ? throw new ArgumentNullException(nameof(path)) : CombineUris(path);
}
}
}