/
Element.cs
538 lines (492 loc) · 25.7 KB
/
Element.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
using Autodesk.DesignScript.Runtime;
using Autodesk.Revit.DB;
using Revit.Elements;
using RevitServices.Persistence;
using RevitServices.Transactions;
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.DesignScript.Geometry;
using Dynamo.Graph.Nodes;
using Rhythm.Utilities;
using Revit.GeometryConversion;
using Solid = Autodesk.Revit.DB.Solid;
namespace Rhythm.Revit.Elements
{
/// <summary>
/// Wrapper class for Element.
/// </summary>
public class Elements
{
private Elements()
{
}
/// <summary>
/// Get Null
/// </summary>
/// <returns></returns>
[IsVisibleInDynamoLibrary(false)]
public static object GetNull()
{
return null;
}
/// <summary>
/// This node will convert the given elements to parts.
/// </summary>
/// <param name="element">The element to convert to parts.</param>
/// <returns name="Parts">The created parts from the given element.</returns>
/// <search>
/// Element.CreateParts
/// </search>
[NodeCategory("Actions")]
public static List<global::Revit.Elements.Element> CreateParts(global::Revit.Elements.Element element)
{
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//create a list to hold the element ids and add them to it
ICollection<Autodesk.Revit.DB.ElementId> elementIds = new List<ElementId>();
elementIds.Add(element.InternalElement.Id);
//start a transaction and create the parts
TransactionManager.Instance.EnsureInTransaction(doc);
PartUtils.CreateParts(doc, elementIds);
TransactionManager.Instance.TransactionTaskDone();
doc.ActiveView.PartsVisibility = PartsVisibility.ShowPartsOnly;
//regeneration allows the parts to be selectable
doc.Regenerate();
//collect the parts that have been created
ICollection<ElementId> partIds = PartUtils.GetAssociatedParts(doc, element.InternalElement.Id, false, false);
List<global::Revit.Elements.Element> partList = new List<global::Revit.Elements.Element>();
//collect the newly created parts
foreach (Autodesk.Revit.DB.ElementId id in partIds)
{
partList.Add(doc.GetElement(id).ToDSType(true));
}
return partList;
}
/// <summary>
/// This node will get the parameter as instance or type.
/// </summary>
/// <param name="element">The element to get parameter from.</param>
/// <param name="parameterName">The parameter name.</param>
/// <returns name="value">The parameter value.</returns>
/// <search>
/// TypeOrInstance
/// </search>
[NodeCategory("Actions")]
public static object GetParameterValueByNameTypeOrInstance(global::Revit.Elements.Element element, string parameterName)
{
//create a list to hold the element ids and add them to it
Autodesk.Revit.DB.Element internalElement = element.InternalElement;
Autodesk.Revit.DB.Document doc = internalElement.Document;
//declare variable to assign
object paramValue;
//looks up the parameter to see if it exists
var result = internalElement.LookupParameter(parameterName);
//if parameter exists as instance obtain it, otherwise try for type
if (result != null)
{
paramValue = element.GetParameterValueByName(parameterName);
}
else
{
paramValue =
doc.GetElement(internalElement.GetTypeId()).ToDSType(true).GetParameterValueByName(parameterName);
}
return paramValue;
}
/// <summary>
/// Set one of the element's parameters. Instance if it is instance or type if type.
/// </summary>
/// <param name="element">The element to set parameter to.</param>
/// <param name="parameterName">The parameter name.</param>
/// <param name="value">The value to set.</param>
/// <returns name="element">The element.</returns>
/// <search>
/// TypeOrInstance
/// </search>
[NodeCategory("Actions")]
public static global::Revit.Elements.Element SetParameterByNameTypeOrInstance(global::Revit.Elements.Element element,
string parameterName, object value)
{
//create a list to hold the element ids and add them to it
Autodesk.Revit.DB.Element internalElement = element.InternalElement;
Autodesk.Revit.DB.Document doc = internalElement.Document;
//declare variable to assign
object paramValue;
//looks up the parameter to see if it exists
var result = internalElement.LookupParameter(parameterName);
//if parameter exists as instance obtain it, otherwise try for type
if (result != null)
{
paramValue = element.SetParameterByName(parameterName, value);
}
else
{
paramValue = doc.GetElement(internalElement.GetTypeId())
.ToDSType(true)
.SetParameterByName(parameterName, value);
}
return element;
}
/// <summary>
/// This will take a given element and category and grab the intersecting elements of that category.
/// </summary>
/// <param name="element">The element to run intersections against.</param>
/// <param name="category">The category to check.</param>
/// <returns name="elements">The intersecting elements.</returns>
/// <search>
/// IntersectingElements
/// </search>
[NodeCategory("Query")]
public static List<global::Revit.Elements.Element> GetIntersectingElementsOfCategory(global::Revit.Elements.Element element,
global::Revit.Elements.Category category)
{
//the current document
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//get built in category from user viewable category
BuiltInCategory myCatEnum = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), category.Id.ToString());
//the intersection filter
ElementIntersectsElementFilter filter = new ElementIntersectsElementFilter(element.InternalElement);
//build a collector
FilteredElementCollector collector = new FilteredElementCollector(doc);
//collect the elements that fall in that category
IList<Autodesk.Revit.DB.Element> intersectingElementsInternaList =
collector.OfCategory(myCatEnum).WhereElementIsNotElementType().WherePasses(filter).ToElements();
//build a new list for the intersecting elements to be added to
List<global::Revit.Elements.Element> intersectingElements = new List<global::Revit.Elements.Element>();
//add each user recognizable element to the list
foreach (Autodesk.Revit.DB.Element internalElement in intersectingElementsInternaList)
{
intersectingElements.Add(internalElement.ToDSType(true));
}
return intersectingElements;
}
/// <summary>
/// This node will get a parameter value by search string, regardless of case of the search string. Also accounts for misspellings.
/// Note: If the parameter name appears multiple times it will retrieve the first one that it finds.
/// </summary>
/// <param name="element">The element to get parameter from.</param>
/// <param name="parameterName">The parameter name.</param>
/// <returns name="value">The parameter value.</returns>
/// <search>
/// CaseInsensitive
/// </search>
[NodeCategory("Actions")]
public static object GetParameterValueByNameCaSeiNSeNSiTiVe(global::Revit.Elements.Element element,
string parameterName)
{
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//create a list to hold the element ids and add them to it
Autodesk.Revit.DB.Element internalElement = element.InternalElement;
global::Revit.Elements.Parameter[] elementParams = element.Parameters;
string paramToSet = null;
//score of each match list
List<int> values = new List<int>();
//score the match in the parameter list
foreach (var param in elementParams)
{
values.Add(StringComparisonUtilities.Compute(parameterName, param.Name));
}
//get the closest matching parameter name
int minIndex = values.IndexOf(values.Min());
paramToSet = elementParams[minIndex].Name;
//lookup and get the parameter value
var result = internalElement.LookupParameter(paramToSet);
var parameterValue = global::Revit.Elements.InternalUtilities.ElementUtils.GetParameterValue(result);
return parameterValue;
}
/// <summary>
/// This node will set a parameter value by search string, regardless of case of the search string. Also accounts for misspellings.
/// Note: If the parameter name appears multiple times it will retrieve the first one that it finds.
/// </summary>
/// <param name="element">The element to get parameter from.</param>
/// <param name="parameterName">The parameter name.</param>
/// <param name="value">The parameter value.</param>
/// <returns name="element">The element that had parameters set.</returns>
/// <search>
/// CaseInsensitive
/// </search>
[NodeCategory("Actions")]
public static global::Revit.Elements.Element SetParameterValueByNameCaSeiNSeNSiTiVe(global::Revit.Elements.Element element,
string parameterName, object value)
{
//check Rhythm version
//Rhythm.Utilities.VersionChecker();
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//create a list to hold the element ids and add them to it
global::Revit.Elements.Parameter[] elementParams = element.Parameters;
string paramToSet = null;
//score of each match list
List<int> values = new List<int>();
//score the match in the parameter list
foreach (var param in elementParams)
{
values.Add(StringComparisonUtilities.Compute(parameterName, param.Name));
}
//get the closest matching parameter name
int minIndex = values.IndexOf(values.Min());
paramToSet = elementParams[minIndex].Name;
//Convert to a usable value
var dynval = value as dynamic;
TransactionManager.Instance.EnsureInTransaction(doc);
//set that thing!
var parameterValue = element.SetParameterByName(paramToSet, dynval);
TransactionManager.Instance.TransactionTaskDone();
return element;
}
/// <summary>
/// This will take a given element and category and grab the intersecting elements of that category.
/// </summary>
/// <param name="element">The element to run intersections against.</param>
/// <param name="sourceInstance">Use this input if the elements are from a link! If they are not, leave it blank.</param>
/// <param name="category">The category to check.</param>
/// <returns name="elements">The intersecting elements.</returns>
/// <search>
/// IntersectingElements
/// </search>
[NodeCategory("Query")]
public static List<global::Revit.Elements.Element> GetIntersectingElementsOfCategoryLinkOption(global::Revit.Elements.Element element,
global::Revit.Elements.Category category, [DefaultArgument("Rhythm.Revit.Elements.Element.GetNull()")]global::Revit.Elements.Element sourceInstance)
{
//the current document
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//get built in category from user viewable category
BuiltInCategory myCatEnum = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), category.Id.ToString());
//build a new list for the intersecting elements to be added to
List<global::Revit.Elements.Element> intersectingElements = new List<global::Revit.Elements.Element>();
if (sourceInstance != null)
{
Autodesk.Revit.DB.RevitLinkInstance internalInstance =
(Autodesk.Revit.DB.RevitLinkInstance)sourceInstance.InternalElement;
//get the element's geometry.
GeometryElement geomElement = element.InternalElement.get_Geometry(new Options());
//transform the solid to where the eff the link is.
GeometryElement transformedElement = geomElement.GetTransformed(internalInstance.GetTransform());
//make a solid filter.
Solid solid = null;
foreach (GeometryObject geomObj in transformedElement)
{
solid = geomObj as Solid;
if (solid != null) break;
}
//the intersection filter
ElementIntersectsSolidFilter filter = new ElementIntersectsSolidFilter(solid);
//build a collector
FilteredElementCollector collector = new FilteredElementCollector(doc);
//collect the elements that fall in that category
IList<Autodesk.Revit.DB.Element> intersectingElementsInternaList =
collector.OfCategory(myCatEnum).WhereElementIsNotElementType().WherePasses(filter).ToElements();
//add each user recognizable element to the list
foreach (Autodesk.Revit.DB.Element internalElement in intersectingElementsInternaList)
{
intersectingElements.Add(internalElement.ToDSType(true));
}
}
else
{
//the intersection filter
ElementIntersectsElementFilter filter = new ElementIntersectsElementFilter(element.InternalElement);
//build a collector
FilteredElementCollector collector = new FilteredElementCollector(doc);
//collect the elements that fall in that category
IList<Autodesk.Revit.DB.Element> intersectingElementsInternaList =
collector.OfCategory(myCatEnum).WhereElementIsNotElementType().WherePasses(filter).ToElements();
//add each user recognizable element to the list
foreach (Autodesk.Revit.DB.Element internalElement in intersectingElementsInternaList)
{
intersectingElements.Add(internalElement.ToDSType(true));
}
}
return intersectingElements;
}
/// <summary>
/// This node will report whether or not the given element is hidden in given views.
/// </summary>
/// <param name="element">The element to check.</param>
/// <param name="view">The views to check in.</param>
/// <returns name="bool">Is it hidden?</returns>
/// <search>
/// IsVisible
/// </search>
[NodeCategory("Query")]
public static List<bool> IsHiddenInView(List<global::Revit.Elements.Element> element,
global::Revit.Elements.Views.View view)
{
List<bool> boolList = new List<bool>();
foreach (global::Revit.Elements.Element e in element)
{
Autodesk.Revit.DB.Element internalElement = (Autodesk.Revit.DB.Element)e.InternalElement;
Autodesk.Revit.DB.View internalView = (Autodesk.Revit.DB.View)view.InternalElement;
boolList.Add(internalElement.IsHidden(internalView));
}
return boolList;
}
/// <summary>
/// This node will report what elements depend on the input element. Useful for determining safe deletion.(Available Revit 2018.1+).
/// </summary>
/// <param name="element">The element to check.</param>
/// <returns name="dependentElements">The dependent elements.</returns>
/// <search>
/// DependentElements
/// </search>
[NodeCategory("Query")]
public static List<global::Revit.Elements.Element> DependentElements(global::Revit.Elements.Element element)
{
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//the element filter, this just filters out type elements.
Autodesk.Revit.DB.ElementFilter elemFilter = new ElementIsElementTypeFilter(true);
//the dependent element ids
IList<ElementId> elemIds = element.InternalElement.GetDependentElements(elemFilter);
//get the elements
List<global::Revit.Elements.Element> elems = new List<global::Revit.Elements.Element>(elemIds.Select(e => doc.GetElement(new ElementId(e.IntegerValue)).ToDSType(true)));
return elems;
}
/// <summary>
/// This node will report what elements depend on the input element. Useful for determining safe deletion.(Available Revit 2018.1+).
/// </summary>
/// <param name="element">The element to check.</param>
/// <param name="category">The category to use to filter the elements.</param>
/// <returns name="dependentElements">The dependent elements.</returns>
/// <search>
/// DependentElements
/// </search>
[NodeCategory("Query")]
public static List<global::Revit.Elements.Element> DependentElementsOfCategory(global::Revit.Elements.Element element, global::Revit.Elements.Category category)
{
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//obtain the builtin category from the id
BuiltInCategory myCatEnum = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), category.Id.ToString());
//the filter to use
Autodesk.Revit.DB.ElementFilter elemFilter = new ElementCategoryFilter(myCatEnum);
//dependent element ids
IList<ElementId> elemIds = element.InternalElement.GetDependentElements(elemFilter);
//get the elements
List<global::Revit.Elements.Element> elems = new List<global::Revit.Elements.Element>(elemIds.Select(e => doc.GetElement(new ElementId(e.IntegerValue)).ToDSType(true)));
return elems;
}
/// <summary>
/// This node will report what elements are joined to the input element.
/// </summary>
/// <param name="element">The element to check.</param>
/// <returns name="joinedElements">The joined elements.</returns>
/// <search>
/// Element.GetJoinedElements,JoinedElements
/// </search>
[NodeCategory("Query")]
public static List<global::Revit.Elements.Element> JoinedElements(global::Revit.Elements.Element element)
{
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//the element filter, this just filters out type elements.
Autodesk.Revit.DB.ElementFilter elemFilter = new ElementIsElementTypeFilter(true);
//the joined element ids
ICollection<ElementId> elemIds = JoinGeometryUtils.GetJoinedElements(doc, element.InternalElement);
//get the elements
List<global::Revit.Elements.Element> elems = new List<global::Revit.Elements.Element>(elemIds.Select(e => doc.GetElement(new ElementId(e.IntegerValue)).ToDSType(true)));
return elems;
}
/// <summary>
/// This node will change the pinned status of an element.
/// </summary>
/// <param name="element">The element to change the status of.</param>
/// <param name="status">The pinned status. True = pinned, False = not pinned.</param>
/// <returns name="element">The elements.</returns>
/// <search>
/// Element.SetPinnedStatus
/// </search>
/// works with background docs
[NodeCategory("Actions")]
public static global::Revit.Elements.Element SetPinnedStatus(global::Revit.Elements.Element element, bool status)
{
Autodesk.Revit.DB.Document doc = element.InternalElement.Document;
TransactionManager.Instance.EnsureInTransaction(doc);
element.InternalElement.Pinned = status;
TransactionManager.Instance.TransactionTaskDone();
return element;
}
/// <summary>
/// *BETA* - This node will retrieve the closest area that an element resides in.
/// This uses bounding boxes which encompass the whole geometry, so we take the closest one.
/// This means that there is potential that we grab the wrong one..
/// </summary>
/// <param name="element">The element to find the closest area location for.</param>
/// <returns name="area">The closest area.</returns>
/// <search>
/// Element.AreaLocation
/// </search>
[NodeCategory("Actions")]
public static global::Revit.Elements.Element AreaLocation(global::Revit.Elements.Element element)
{
//the current document
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
//the internal element
Autodesk.Revit.DB.Element internalElement = element.InternalElement;
//list to append area locations to
List<global::Revit.Elements.Element> areaLocations = new List<global::Revit.Elements.Element>();
//ptlocation to append to
Autodesk.DesignScript.Geometry.Point ptLocation = null;
//figure out the location of the given element
if (internalElement.Location is LocationPoint pt)
{
ptLocation = pt.Point.ToPoint(true);
}
else if (internalElement.Location is Autodesk.Revit.DB.LocationCurve)
{
Autodesk.Revit.DB.LocationCurve curve = internalElement.Location as Autodesk.Revit.DB.LocationCurve;
ptLocation = curve.Curve.Evaluate(0.5, true).ToPoint(true);
}
else
{
throw new Exception(@"This element's location is not supported for this operation. ¯\_(ツ)_/¯ ");
}
var allViews = new Autodesk.Revit.DB.FilteredElementCollector(doc)
.OfClass(typeof(Autodesk.Revit.DB.View))
.Cast<Autodesk.Revit.DB.View>()
.Where(x => x.ViewType == ViewType.AreaPlan && !x.IsTemplate)
.ToList();
//collect the areas to do some cool stuff
FilteredElementCollector areaColl = new FilteredElementCollector(doc);
IList<Autodesk.Revit.DB.Element> areas = areaColl.OfCategory(BuiltInCategory.OST_Areas).ToElements();
foreach (Autodesk.Revit.DB.Element area in areas)
{
View view = allViews.First(v => v.GenLevel.Id.IntegerValue == area.LevelId.IntegerValue);
BoundingBox bBox = area.get_BoundingBox(view).ToProtoType();
if (bBox.Contains(ptLocation) && area.LevelId.IntegerValue == internalElement.LevelId.IntegerValue)
{
areaLocations.Add(area.ToDSType(true));
}
}
global::Revit.Elements.Element elem = areaLocations.First(a => a.GetLocation().DistanceTo(ptLocation) == areaLocations.Min(e => e.GetLocation().DistanceTo(ptLocation)));
return elem;
}
/// <summary>
/// This finds all the views an element appears in. Note: "Appears in" means that if it appears when you do a category collector, that counts.
/// </summary>
/// <param name="element">The element to analyze.</param>
/// <returns name="views">The views this element appears in.</returns>
[NodeCategory("Actions")]
public static List<global::Revit.Elements.Element> ViewFinder(global::Revit.Elements.Element element)
{
List<global::Revit.Elements.Element> views = FindAllViewsWhereElementIsVisible(element.InternalElement).Select(v => v.ToDSType(true)).ToList();
return views;
}
private static IEnumerable<Autodesk.Revit.DB.View> FindAllViewsThatCanDisplayElements(Autodesk.Revit.DB.Document doc)
{
ElementMulticlassFilter filter= new ElementMulticlassFilter(new List<Type> {typeof( View3D ),typeof( ViewPlan ),typeof( ViewSection ) });
return new FilteredElementCollector(doc).WherePasses(filter).Cast<View>().Where(v => !v.IsTemplate);
}
private static IEnumerable<Autodesk.Revit.DB.View> FindAllViewsWhereElementIsVisible(
Autodesk.Revit.DB.Element element)
{
Document doc = element.Document;
IEnumerable<View> relevantViewList = FindAllViewsThatCanDisplayElements(doc);
ElementId idToCheck = element.Id;
return (
from v in relevantViewList
let idList
= new FilteredElementCollector(doc, v.Id)
.WhereElementIsNotElementType()
.ToElementIds()
where idList.Contains(idToCheck)
select v);
}
}
}