We talked about how to get built-in type parameters from Revit elements before using C#. Now let’s see how to do so with VB.NET.
Each native Parameter has a BuiltInParameter enumerator value as its unique identifier but not every BuiltInParameter value can find a counterpart Parameter instance due to legacy matters or something else. Let’s take the window family instance/symbol for example and look at this matter in more detail.
There is no such a class as Window in the Revit API. Each visible window in Revit models is represented by a FamilyInstance of some FamilySymbol window type. The FamilyInstance class only provides some properties common to all family instances and the FamilySymbol class common to all family symbols, and it is certainly natural.
If we’d like to get something specific to a window, we have to resort to the Parameter API. For example, if we want the head height, sill height, width and height out of a window element, what should we do?
Regarding the head height, we need to retrieve the BuiltInParameter.INSTANCE_HEAD_HEIGHT_PARAM parameter from the window element (FamilyInstance) directly; the sill height, BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM from the same instance; the width, BuiltInParameter.WINDOW_WIDTH from its FamilySymbol instead; the height, BuiltInParameter.WINDOW_HEIGHT from the same symbol.
Here is the code to do so:
Public Shared Function GetParameterWindowHeadHeight(ByVal e As Autodesk.Revit.DB.Element) As Parameter
Return e.Parameter(BuiltInParameter.INSTANCE_HEAD_HEIGHT_PARAM)
End Function
Public Shared Function GetParameterWindowSillHeight(ByVal e As Autodesk.Revit.DB.Element) As Parameter
Return e.Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM)
End Function
Public Shared Function GetParameterWindowWidth(ByVal e As Autodesk.Revit.DB.Element) As Parameter
Return e.Parameter(BuiltInParameter.WINDOW_WIDTH)
End Function
Public Shared Function GetParameterWindowHeight(ByVal e As Autodesk.Revit.DB.Element) As Parameter
Return e.Parameter(BuiltInParameter.WINDOW_HEIGHT)
End Function
The last two are somewhat straightforward as the BuiltInParameter enum values WINDOW_WIDTH and WINDOW_HEIGHT are self explanatary which have both the window keyword and the meansurement one. However, the first two are not and we have to make some guess and do some experiments to sort them out.
Somebody, who have read the similar code in C# as demonstrated in another post, may wonder about the c# Element.get_Parameter() thing there and the VB.NET Element.Parameter() stuff here. If interested, please check on details about this in another article which can be found along this path:
Revit Addin Wizards/Coders and Revit API Widgets (the blog's root)
-> Articles
-> Revit API Widgets
-> Revit API – Item() and VB.NET VS. get_Item() and C#
The following code can be used to exercize the four parameter retriever methods:
Dim element As Autodesk.Revit.DB.Element = SelectElement(cmdData.Application.ActiveUIDocument.Selection).Element
Dim winType As Autodesk.Revit.DB.Element = CachedDoc.Element(element.GetTypeId())
Dim p As Parameter = GetParameterWindowHeadHeight(element)
Dim str As String = String.Format("Head height: {0}" & vbLf, p.AsValueString())
p = GetParameterWindowSillHeight(element)
str += String.Format("Sill height: {0}" & vbLf, p.AsValueString())
p = GetParameterWindowWidth(winType)
str += String.Format("Window width: {0}" & vbLf, p.AsValueString())
p = GetParameterWindowHeight(winType)
str += String.Format("Window height: {0}" & vbLf, p.AsValueString())
MessageBox.Show(str, "Window Parameters")
Something similar to the following will be reported if a particular window is selected in a model:
Things seem fine so far, but wait a moment. Let’s look at another case. As can be seen, staying side by side with the WINDOW_WIDTH and WINDOW_HEIGHT parameters, another similar and looking related one, WINDOW_THICKNESS, just comes in sight. What is it about?
If the following parameter retriever method and test code are tried, an exception will be thrown out:
Public Shared Function GetParameterWindowThickness(ByVal e As Autodesk.Revit.DB.Element) As Parameter
Return e.Parameter(BuiltInParameter.WINDOW_THICKNESS)
End Function
p = GetParameterWindowThickness(winType)
MessageBox.Show(p.AsValueString(), "Window Thickness")
Things would not have changed if a FamilyInstance instead of a FamilySymbol were provided to the method.
That could indicate the WINDOW_THICKNESS parameter needs to be purged out from the BuiltInParameter list or in some very rare cases it might really return something but people may not care about those rare cases at all. As a workaround, the wall thickness can be retrieved and used as the thickness of the hosted window, it seems.
Though it seems no way to get the window thickness directly from a window instance or symbol, many alternative ways exist to retrieve the window height and width besides the obvious WINDOW_HEIGHT and WINDOW_WIDTH parameters. Here are a few more such pairs:
CASEWORK_HEIGHT
CASEWORK_WIDTH
DOOR_HEIGHT
DOOR_WIDTH
FAMILY_HEIGHT_PARAM
FAMILY_WIDTH_PARAM
FURNITURE_HEIGHT
FURNITURE_WIDTH
GENERIC_HEIGHT
GENERIC_WIDTH
So it is not a one to one relationship from BuiltInParameter to an element property, instead, it is a multiple to one as verified. Not sure whether vise visa though.
The selection help method and the ISelectionFilter derivative have also been appended below to make the code complete:
Public Shared Function SelectElement(ByVal selection As Autodesk.Revit.UI.Selection.Selection) As Reference
Dim picked As Reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, New SelectionFilter3(), "Select an element")
Return picked
End Function
Public Class SelectionFilter3
Implements ISelectionFilter
Public Function AllowElement(ByVal elem As Autodesk.Revit.DB.Element) As Boolean Implements ISelectionFilter.AllowElement
If elem.Category.Id.IntegerValue = CInt(BuiltInCategory.OST_Windows) Then
Return True
End If
Return False
End Function
Public Function AllowReference(ByVal refer As Autodesk.Revit.DB.Reference, ByVal pos As Autodesk.Revit.DB.XYZ) As Boolean Implements ISelectionFilter.AllowReference
Return True
End Function
End Class
Parameter Retrieve of RevitAddinWizard can help find a BuiltInParameter enumerator value of interest quickly and create help methods accordingly for various element classes.
Recent Comments