In this article, let us try to find some other rooms, which have odd room numbers and are in a particular level.
Obviously, a FilterStringRule and a FilterStringEquals can be used to filter the BuiltInParameter.LEVEL_NAME parameter out from room elements which can be found by applying a category filter (or using its shortcut method OfCategory). However, no existing filter rules or evaluators can achieve the first goal, finding rooms with odd numbers, for us.
So this time again, we use our own way to find these rooms:
public static ICollection<ElementId> GetRoomsInLevel1WithOddNumbers(RvtDocument doc)
{
List<ElementId> finalRooms = new List<ElementId>();
foreach (Element room in new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).ToElements())
{
Parameter param1 = room.get_Parameter(BuiltInParameter.ROOM_NUMBER);
Parameter param2 = room.get_Parameter(BuiltInParameter.LEVEL_NAME);
if (param1 != null && param2 != null)
{
int num;
if (param2.AsString() == "Level 1" && int.TryParse(param1.AsString(), out num) && num%2 == 1)
finalRooms.Add(room.Id);
}
}
return finalRooms;
}
Here is the test code:
…
DateTime time = DateTime.Now;
ICollection<ElementId> roomids1 = GetRoomsInLevel1WithOddNumbers(CachedDoc);
TimeSpan timespan = DateTime.Now - time;
string message = string.Format("{0} rooms are found in {1} milliseconds.", roomids1.Count, timespan.TotalMilliseconds);
foreach (ElementId id in roomids1)
message += (CachedDoc.get_Element(id) as Room).Number + ", ";
MessageBox.Show(message, "GetRoomsInLevel1WithOddNumbers");
…
It is simple and short, isn’t it? Its performance is also good.
ElementParameterFilter Creator of RevitAddinCoder can help create code combining various existing filter rules and evaluators to filter element parameters.
Recent Comments