Revit Parameter can store a few different data types including integer, double, string and ElementId just as the StorageType enumeration indicates. Of course, the None one does not really mean anything for data storage.
In this post, let’s see how to read Parameter values and write them with different approaches when applicable. The following code can read all Parameter values from a selected Element such as a Wall:
Element element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element; using (StreamWriter sw = new StreamWriter(@"c:\ParametersValue.txt"))
{
sw.WriteLine(string.Format("{0, -25} {1,-10} {2, -10} {3, -20} {4, -10} {5, -25}",
"Name", "Integer", "Double", "String", "ElementId", "ValueString"));
foreach (Parameter p in element.Parameters)
{
sw.WriteLine(string.Format("{0, -25} {1,-10} {2, -10} {3, -20} {4, -10} {5, -25}",
p.Definition.Name,
(p.StorageType == StorageType.Integer ? p.AsInteger().ToString() : @"N/A"),
(p.StorageType == StorageType.Double ? Math.Round(p.AsDouble(), 2).ToString() : @"N/A"),
(p.StorageType == StorageType.String ? p.AsString() : @"N/A"),
(p.StorageType == StorageType.ElementId ? p.AsElementId().IntegerValue.ToString() : @"N/A"),
p.AsValueString())); if (!p.IsShared && (p.Definition as InternalDefinition).BuiltInParameter == BuiltInParameter.WALL_TOP_OFFSET)
{
p.SetValueString("1\'6\"");
}
}
} The help method SelElement which can be created with the Object Picker in no time has also been appended below to make the example code complete:
public static Reference SelElement(Selection selection)
{
Reference picked = selection.PickObject(ObjectType.Element, "Please select an element");
return picked;
}
The Parameter class provides different methods to convert the internal data to the right storage type. As shown in the code, each such method should be called based on the StorageType of the Parameter; otherwise exception may just be thrown out. The Parameter also provides another method, AsValueString(), to interpret the Double type data with unit information. The good thing is that for other data types it would return an empty string rather than throw out some exceptions and this will surely make the coding path smoother. One more good thing is that a corresponding write method is also provided, SetValueString(). As demonstrated, it accepts both data and unit information in a string argument and will convert it properly to the internal value and unit. The string "1\'6\"" in the example code will be properly interpreted as one foot and six inches by the Revit Parameter API. And the better part is that it supports different units for a single parameter. For example, if a value in centimeter is provided like the following: p.SetValueString("25 cm"); the value 25 will be properly interpreted in centimeter and converted to the right internal unit (feet). Please just keep in mind to use the right unit abbreviations that the SetValueString() accepts and the Revit supports, otherwise something unexpected will happen for sure. The Revit Parameter API also provides methods to set values individually for each supported data type: Set(ElementId)
Set(Double)
Set(Int32)
Set(String) A kind reminder once again: please do not forget to check the StorageType and the IsReadOnly flag before trying to write to any of parameters using the above methods. If a wall is selected, the ParametersValue.txt file containing its parameter value information may look like: Name Integer Double String ElementId ValueString
Length N/A 26.15 N/A N/A 26' - 1 105/128"
Room Bounding 0 N/A N/A N/A
Area N/A 259.94 N/A N/A 259.94 SF
Top is Attached 0 N/A N/A N/A
Related to Mass 0 N/A N/A N/A
Top Extension Distance N/A 0 N/A N/A 0' - 0"
Phase Demolished N/A N/A N/A -1
Mark N/A N/A Mark N/A
Comments N/A N/A Test N/A
Phase Created N/A N/A N/A 21885
Base Offset N/A 0 N/A N/A 0' - 0"
Top Constraint N/A N/A N/A 118280
Base is Attached 0 N/A N/A N/A
Top Offset N/A 0 N/A N/A 0' - 0"
Volume N/A 252.8 N/A N/A 252.80 CF
Location Line 0 N/A N/A N/A
Base Constraint N/A N/A N/A 30
Base Extension Distance N/A 0 N/A N/A 0' - 0"
Structural Usage 0 N/A N/A N/A
Unconnected Height N/A 11.5 N/A N/A 11' - 6" RevitAddinWizardprovides a few coders to manage Revit Parameter in various ways.
|