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 purpose.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
In this post, let’s see how to read Parameter values and write them with different approaches in VB.NET when applicable. The following code can read all Parameter values from a selected Element such as a Wall:
Dim element As Autodesk.Revit.DB.Element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element
Using sw As 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"))
For Each p As Parameter In element.Parameters
sw.WriteLine(String.Format("{0, -25} {1,-10} {2, -10} {3, -20} {4, -10} {5, -25}", p.Definition.Name, (If(p.StorageType = StorageType.Integer, p.AsInteger().ToString(), "N/A")), (If(p.StorageType = StorageType.Double, Math.Round(p.AsDouble(), 2).ToString(), "N/A")), (If(p.StorageType = StorageType.String, p.AsString(), "N/A")), (If(p.StorageType = StorageType.ElementId, p.AsElementId().IntegerValue.ToString(), "N/A")), _
p.AsValueString()))
If Not p.IsShared AndAlso TryCast(p.Definition, InternalDefinition).BuiltInParameter = BuiltInParameter.WALL_TOP_OFFSET Then
p.SetValueString("1'6""")
End If
Next
End Using
The help method SelElement which can be created with the Object Picker of RevitAddinWizard in no time has also been appended below to make the example code complete:
Public Shared Function SelElement(ByVal selection As Autodesk.Revit.UI.Selection.Selection) As Reference
Dim picked As Reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Please select an element")
Return picked
End Function
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 value 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 OffsetN/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"
RevitAddinWizard provides a few coders to help manage Revit Parameter in various ways in VB.NET or C#.
Recent Comments