Walkthrough: Get Selected Element Parameters

The Element Parameters are retrieved by iterating through the Element ParameterSet. The following code sample illustrates how to retrieve the Parameter from a selected element.

Note: This example uses some Parameter members, such as AsValueString and StorageType, which are covered in subsequent topics.

Code Region 8-1: Getting selected element parameters

void GetElementParameterInformation(Document document, Element element)
{
    // Format the prompt information string
    String prompt = "Show parameters in selected Element: \n\r";

    StringBuilder st = new StringBuilder();
    // iterate element's parameters
    foreach (Parameter para in element.Parameters)
    {
        st.AppendLine(GetParameterInformation(para, document));
    }

    // Give the user some information
    TaskDialog.Show("Revit", prompt + st.ToString());
}

String GetParameterInformation(Parameter para, Document document)
{
    string defName = para.Definition.Name + "\t : ";
    string defValue = string.Empty;
    // Use different method to get parameter data according to the storage type
    switch (para.StorageType)
    {
        case StorageType.Double:
            //covert the number into Metric
            defValue = para.AsValueString();
            break;
        case StorageType.ElementId:
            //find out the name of the element
            Autodesk.Revit.DB.ElementId id = para.AsElementId();
            if (id.IntegerValue >= 0)
            {
                defValue = document.GetElement(id).Name;
            }
            else
            {
                defValue = id.IntegerValue.ToString();
            }
            break;
        case StorageType.Integer:
            if (ParameterType.YesNo == para.Definition.ParameterType)
            {
                if (para.AsInteger() == 0)
                {
                    defValue = "False";
                }
                else
                {
                    defValue = "True";
                }
            }
            else
            {
                defValue = para.AsInteger().ToString();
            }
            break;
        case StorageType.String:
            defValue = para.AsString();
            break;
        default:
            defValue = "Unexposed parameter.";
            break;
    }

    return defName + defValue;
}

Figure 26: Get wall parameters result

Note: In Revit, some parameters have values in the drop-down list in the Element Properties dialog box. You can get the numeric values corresponding to the enumerated type for the Parameter using the Revit Platform API, but you cannot get the string representation for the values using the Parameter.AsValueString() method.