In my previous post about adjusting cutbacks using ExtensionUtility, I used two helper functions FindFamilyType() and FindElement() to retrieve a specific structural framing family type and a level. Both are a part of our training labs code as I have already pointed out. They are so fundamental, yet written in a generic form. So we often use them in our other sample code to make it easy to read the code. I thought it may not be a bad idea to post the code here so that people can see the code on the blog instead of downloading the whole zip. Also for our blogging purposes, we can simply point to this blog in future if we have it here.
Using these helper functions, for example, you can access to a specific element like following:
FamilySymbol doorType =
(FamilySymbol)Utils.FindFamilyType(
m_rvtDoc, GetType(FamilySymbol),
"M_Single-Flush", "0915 x 2134mm",
BuiltInCategory.OST_Doors);
WallType wallType =
(WallType)Utils.FindFamilyType(
m_rvtDoc, GetType(WallType),
"Basic Wall", "Generic - 200mm");
Level level1 =
(Level)Utils.FindElement(
m_rvtDoc, typeof(Level), "Level 1");
So here it is the full code for the functions:
public class Utils
{
// Helper function
// Find an element of the given type, name,
// and category(optional)
// You can use this, for example, to find a specific
// wall and window family with the given name.
// e.g.,
// FindFamilyType(
// m_rvtDoc,
// GetType(WallType),
// "Basic Wall",
// "Generic - 200mm",
// null)
//
// FindFamilyType(
// m_rvtDoc,
// GetType(FamilySymbol),
// "M_Single-Flush",
// "0915 x 2134mm",
// BuiltInCategory.OST_Doors)
public static Element FindFamilyType(
Document rvtDoc,
Type targetType,
string targetFamilyName,
string targetTypeName,
BuiltInCategory targetCategory)
{
// First narrow down to a collection of the given
// class and category
FilteredElementCollector collector =
new FilteredElementCollector(rvtDoc).
OfClass(targetType);
if (!targetCategory.Equals(BuiltInCategory.INVALID))
{
collector.OfCategory(targetCategory);
}
// Filter for the given family and type name,
// using LINQ query
IEnumerable<Element> targetElems =
from Element element in collector
where element.Name.Equals(targetTypeName) &&
element.get_Parameter(
BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM).
AsString().Equals(targetFamilyName)
select element;
IList<Element> elems = targetElems.ToList();
// We should have only one element or nothing
if (elems.Count > 0)
{
return elems[0];
}
// Cannot find it.
return null;
}
// Overroad to omitt targetTypeName as
// it will be redundant for system type.
public static Element FindFamilyType(
Document rvtDoc,
Type targetType,
string targetFamilyName,
string targetTypeName)
{
return FindFamilyType(
rvtDoc,
targetType,
targetFamilyName,
targetTypeName,
BuiltInCategory.INVALID);
}
// helper function: find an element of the given type and
// the name.
// You can use this, for example, to find Reference or Level
// with the given name.
public static Element FindElement(
Document rvtDoc,
Type targetType,
string targetName)
{
// Get the elements of the given class
FilteredElementCollector collector =
new FilteredElementCollector(rvtDoc);
collector.WherePasses(new ElementClassFilter(targetType));
// Parse the collection for the given name
// using LINQ query here.
IEnumerable<Element> targetElems =
from element in collector
where element.Name.Equals(targetName)
select element;
IList<Element> elems = targetElems.ToList();
if (elems.Count > 0)
{
// We should have only one with the given name.
return elems[0];
}
// Cannot find it.
return null;
}
} // The end of class Util