## 获取过滤元素或元素ID
一旦将一个或多个过滤器应用于FilteredElementCollector,过滤后的集合
可以通过以下三种方式之一检索元素:
1.获取Elements或ElementIds的集合。
ToElements() - 返回通过所有应用过滤器的所有元素
ToElementIds() - 返回传递所有应用过滤器的所有元素的ElementIds
2.获取与过滤器匹配的第一个Element或ElementId。
FirstElement() - 返回第一个元素以传递所有应用的过滤器
FirstElementId() - 返回第一个元素的id以传递所有应用的过滤器
3.获取ElementId或Element迭代器。
GetElementIdIterator() - 将FilteredElementIdIterator返回给传递的元素ID过滤器
GetElementIterator() - 将FilteredElementIterator返回给传递的元素过滤器
GetEnumerator() - 返回循环收集的IEnumerator <Element>传递元素
## 使用ToElements()获取过滤结果
```
ElementClassFilter filter = new ElementClassFilter(typeof(LoadBase));
FilteredElementCollector collector = new FilteredElementCollector(document);
collector.WherePasses(filter);
ICollection<Element> allLoads = collector.ToElements();
String prompt = "The loads in the current document are:\n";
foreach (Element loadElem in allLoads)
{
LoadBase load = loadElem as LoadBase;
prompt += load.GetType().Name + ": " +
load.Name + "\n";
}
TaskDialog.Show("Revit", prompt);
```
## 获取第一个传递元素
```
StructuralInstanceUsageFilter columnFilter =new StructuralInstanceUsageFilter(StructuralInstanceUsage.Column);
FilteredElementCollector collector = new FilteredElementCollector(document);
collector.WherePasses(columnFilter);
FamilyInstance column = collector.FirstElement() as FamilyInstance;
```
## 使用扩展方法获取第一个传递元素
FilteredElementCollector collector = new FilteredElementCollector(document);
Func<View3D, bool> isNotTemplate = v3 => !(v3.IsTemplate);
collector.OfClass(typeof(View3D));
View3D view3D = collector.Cast<View3D>().First<View3D>(isNotTemplate);
## 使用获取过滤器结果作为元素ID
```
FilteredElementCollector collector = new FilteredElementCollector(document);
collector.OfClass(typeof(View3D));
ElementId viewId = collector.FirstElementId();
if (FilteredElementCollector.IsViewValidForElementIteration(document, viewId))
{
FilteredElementCollector viewCollector = new FilteredElementCollector(document, viewId);
viewCollector.OfClass(typeof(FamilyInstance));
ICollection<ElementId> familyInstanceIds = viewCollector.ToElementIds();
document.Delete(familyInstanceIds);
}
```
## 将结果作为元素迭代器获取
```
FilteredElementCollector collector = new FilteredElementCollector(document);
collector.OfClass(typeof(Autodesk.Revit.DB.Plumbing.Pipe));
FilteredElementIterator elemItr = collector.GetElementIterator();
elemItr.Reset();
while (elemItr.MoveNext())
{
Pipe pipe = elemItr.Current as Pipe;
if (pipe.FlowState == PipeFlowState.LaminarState)
{
TaskDialog.Show("Revit", "Model has at least one pipe with Laminar flow state.");
break;
}
}
```
## 将结果作为元素id迭代器获取
```
RoomFilter filter = new RoomFilter();
FilteredElementCollector collector = new FilteredElementCollector(document);
collector.WherePasses(filter);
FilteredElementIdIterator roomIdItr = collector.GetElementIdIterator();
roomIdItr.Reset();
while (roomIdItr.MoveNext())
{
ElementId roomId = roomIdItr.Current;
Room room = document.get_Element(roomId) as Room;
if (room.Area < 50.0)
{
String prompt = "Room is too small: id = " + roomId.ToString();
TaskDialog.Show("Revit", prompt);
break;
}
}
```