In fact, the technique has already been demonstrated in an early post, Set FamilyParameter Values with VB.NET. And this can be an addition to the three approaches of converting Set to List as introduced in another article, Convert ParameterSet to List of Parameter with VB.NET, and I would vote this approach as the best so far.
Here is the generic way to do the conversion:
Public Shared Function RawConvertSetToList(Of T)(ByVal setVar As IEnumerable) As List(Of T)
Dim list As List(Of T) = (From p In setVar Select p)
Return List
End Function
The method works with any enumerable Set or Collection instances regardless where they come fromI. The generic type T represents the data type of the members of the Set or Collection, for example, Parameter for the ParameterSet which can be retrieved from the Element.Parameters property, and FamilyParameter for the FamilyParameterSet from the FamilyManager.Parameters.
The following is a typical usage of the method:
…
Dim list As List(Of Parameter) = RawConvertSetToList(Of Parameter)(element.Parameters)
…
We have to specify the same Parameter type twice, but that is the way the generic method, its return type, and the generic type List<T> work. Anyway it’s more concise, self explanatory and readable. We do not need to worry about the LINQ grammars anymore.
Here are some more usage examples:
…
Dim fp As FamilyParameter = RawConvertSetToList(Of FamilyParameter)(fm.Parameters).FirstOrDefault(Function(e) e.Definition.Name.Equals(parameterName, StringComparison.CurrentCultureIgnoreCase))
Dim famType As FamilyType = RawConvertSetToList(Of FamilyType)(fm.Types).FirstOrDefault(Function(e) e.Name.Equals(familyTypeName, StringComparison.CurrentCultureIgnoreCase))
…
Besides these examples, the technique actually can be applied to all Set or Collection classes. In terms of Revit API ones, a few more are listed below:
ElementSet
CitySet
CategorySet
ConnectorSet
DimensionTypeSet
ViewSet
WallTypeSet
RevitAddinWizard uses this technique in its various Parameter related coders in either VB.NET or C#.
Recent Comments