Dear Stephen,
Thank you for your query, and many thanks to Recep for his valuable help.
The method you mention, GetFamilyTypeIdByName, was probably implemented very similarly to Recep's suggestion.
FamilyType is really more suited for the family editor context.
In the project context, you have a very general base class, ElementType.
The TextNoteType class is derived from that, as is the FamilySymbol class.
Just from the name, I cannot infer whether GetFamilyTypeIdByName retrieved ElementType objects, FamilySymbol ones, or something else.
Anyway, for safety's sake, I implemented and added three new methods to The Building Coder samples for you:
- GetElementTypeByName
- GetFamilySymbolByName
- GetTextNoteTypeByName
Their implementations are almost identical, except that they retrieve the first named object of the specific class, respectively.
Since there are more ElementType objects than TextNoteType ones in the project, the latter method is faster.
Also, all three methods could be be speeded up by using a (quick) parameter filter instead of the (slower than slow) LINQ post-processing accessed by the `First` method, as described in the recent discussion by The Building Coder on Slow, Slower Still and Faster Filtering:
https://thebuildingcoder.typepad.com/blog/2019/04/slow-slower-still-and-faster-filtering.html#2
You can see the methods I added in this diff to the previous version:
https://github.com/jeremytammik/the_building_coder_samples/compare/2020.0.145.1...2020.0.145.2
For the sake of completeness, here is their code as well:
/// <summary>
/// Return the first element type matching the given name.
/// This filter could be speeded up by using a (quick)
/// parameter filter instead of the (slower than slow)
/// LINQ post-processing.
/// </summary>
public static ElementType GetElementTypeByName(
Document doc,
string name )
{
return new FilteredElementCollector( doc )
.OfClass( typeof( ElementType ) )
.First( q => q.Name.Equals( name ) )
as ElementType;
}
/// <summary>
/// Return the first family symbol matching the given name.
/// Note that FamilySymbol is a subclass of ElementType,
/// so this method is more restrictive above all faster
/// than the previous one.
/// </summary>
public static ElementType GetFamilySymbolByName(
Document doc,
string name )
{
return new FilteredElementCollector( doc )
.OfClass( typeof( FamilySymbol ) )
.First( q => q.Name.Equals( name ) )
as FamilySymbol;
}
/// <summary>
/// Return the first text note type matching the given name.
/// Note that TextNoteType is a subclass of ElementType,
/// so this method is more restrictive above all faster
/// than Util.GetElementTypeByName.
/// </summary>
TextNoteType GetTextNoteTypeByName(
Document doc,
string name )
{
return new FilteredElementCollector( doc )
.OfClass( typeof( TextNoteType ) )
.First( q => q.Name.Equals( name ) )
as TextNoteType;
}
I hope this helps.
Best regards,
Jeremy