In this post, let’s see how to convert a ParameterSet, which can be retrieved from the Element.Parameters property, to a List<Parameter>.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
There are three widely used approaches to do so as far as I know. The first one, ENUMERATOR way, is cumbersome and old fashioned but still frequently used especially in legacy code:
Element element = SelectElement(cmdData.Application.ActiveUIDocument.Selection).Element;
List<Parameter> paramList1 = new List<Parameter>();
ParameterSet parameterSet = element.Parameters;
IEnumerator enumerator = parameterSet.GetEnumerator();
enumerator.Reset();
while (enumerator.MoveNext())
{
paramList1.Add(enumerator.Current as Parameter);
}
MessageBox.Show(paramList1.Count.ToString(), "Count");
We need to get the interface variable of IEnumerator first through calling the GetEnumerator() method, Reset() the enumerator when necessary, MoveNext() one by one to access the Current item, and finally Add it to the List which has to be built up earlier.
The second approach is nicer, which can be called the FOREACH way:
List<Parameter> paramList2 = new List<Parameter>();
foreach (Parameter p in element.Parameters)
{
paramList2.Add(p);
}
MessageBox.Show(paramList2.Count.ToString(), "Count");
All those GetEnumerator(), Reset(), MoveNext(), and Current stuffs go away and the single FOREACH statement fulfill the same task. So excluding the brackets, only three lines of code is necessary.
The most concise and modern approach is the LINQ way without any doubt:
List<Parameter> paramList3 = (from Parameter p in element.Parameters select p).ToList();
MessageBox.Show(paramList3.Count.ToString(), "Count");
A single line of code can loop through all the items in the Element.Parameters property, find the right ones, and create a list for them. And it does not lose any code readability.
The same technique applies to a variety of collections in the Revit API. A few have been listed out below for reference:
ElementSet
CitySet
CategorySet
ConnectorSet
DimensionTypeSet
FamilyParameterSet
FamilyTypeSet
ViewSet
WallTypeSet
RevitAddinWizard exercises the concise and modern way in its Parameter Filter.
Links to some related articles:
Parameter of Revit API - BuiltInParameter
Parameter Retriever of RevitAddCoder
Parameter of Revit API - Parameter Information
Parameter Infoer of RevitAddCoder
Parameter of Revit API - ParameterType And StorageType
Parameter Typer of RevitAddCoder
Parameter of Revit API - BuiltInParameterGroup
Parameter Grouper of RevitAddCoder
Parameter Filter of RevitAddCoder
Parameter of Revit API - Read/Write Parameter Values
List paramList3 = (from Parameter p in element.Parameters select p).ToList();
Ive been trying to no avail to translate this to VB.Net ... any pointers?
...its just what I need - Ive tried all sorts of variants, but no good ....
Cheers,
Kon.
Posted by: Konrads Samulis | 10/31/2012 at 01:56 AM
Ignore my comment - found the VB.Net reference ......
Posted by: Konrads Samulis | 10/31/2012 at 02:32 AM
Kon, no problem.
Glad to know you found it. Please feel free to raise other questions in case any in the future.
Regards,
Posted by: Spiderinnet | 10/31/2012 at 03:58 AM