Revit Intro Lab2 - DB Element
Revit Intro Lab2 - DB Element
Lab2 – DB Element
Created by M. Harada, July 2010
Updated by DevTech AEC WG
Last modified: 8/18/2023
<VB.NET>VB.NET Version</VB.NET>
Objective: In this lab, we will learn how an element is represented in Revit and how to retrieve
information about an element. We’ll learn how to:
Identify an element
Retrieve a set of properties of an element
Retrieve a specific property of an element
Retrieve location information
Retrieve geometry information
Tasks: We’ll write a command that prompts the user to pick an element, identify the kind of element,
and displays its information about properties, location and geometry:
1. Pick an element.
2. Show basic information about the element and its family type (i.e., class name, category and
element Id).
3. Identify the element that you have picked. (e.g., is it a wall, window or door?)
4. Show the set of properties of the element and its family type.
5. Show specific properties of the element and its family type.
6. Show the location information of the element
7. Show the geometry information of the element (Optional)
Figure 1 and 2 shows the sample images of output after running the command that you will be defining
in this lab:
Figure 1. Dialogs showing basic information and identity of an element.
Figure 2. Dialogs showing properties (or parameters) of an element and of its type
The following is the breakdown of step by step instructions in this lab:
1.1 Add a new file and define another external command to your project. Let’s name them as follows:
File name: 2_DbElement.vb (or .cs)
Command class name: DBElement
(Once again, you may choose to use any names you want here. When you do so, just remember what
you are calling your own project, and substitute these names as needed while following the instruction
in this document.)
Required Namespaces:
Namespaces needed for this lab are:
(System.Linq) (needed for Lab3)
Autodesk.Revit.DB
Autodesk.Revit.UI
Autodesk.Revit.ApplicationServices
Autodesk.Revit.Attributes
Autodesk.Revit.UI.Selection (this is for selection)
Note (VB.NET only): if you are writing in VB.NET and you import namespaces at the project level, (i.e., in
the project properties, there is no need to explicitly import in each file.
1.2 To make it easier to access the top level objects in our labs, we will define member variables to the
keep the top level object accessible throughout this document or the class. Revit has a concept of
separation between DB and UI objects. This applies to the Revit application and document objects.
These top level objects are accessible as follows:
UIApplication commandData.Application
Application UIApplication.Application
UIDocument UIApplication.ActiveUIDocument
Document UIDocument.Document
Define member variables, e.g., m_rvtApp and m_rvtDoc, to keep DB level application and document
respectively. The following is an example:
<VB.NET>
'' DB Element – learn about Revit element
<Transaction(TransactionMode.Manual)> _
Public Class DBElement
Implements IExternalCommand
'' ...
Return Result.Succeeded
End Function
End Class
</VB.NET>
For C# developer: To make the code easier to read in this instruction, we are omitting the namespace
from now on. Please insert the code between the namespace as need.
<C#>
namespace IntroCs
{
}
</C#>
Note: Using the member variable this way is just to make our life slightly easier during our exercises. You
may find other approaches more suitable depending on the context of your program.
2. Pick an Element
Autodesk.Revit.DB.Element is a base class for objects in the Revit project database. We are going to
pick an arbitrary element on the UI screen and examine it to learn more about elements in Revit.
We can use one of overloaded PickObject() method to pick an object on the screen:
UIDocument.Selection.PickObject(ObjectType.Element, promptString)
(We’ll come to the topic of UI and selection when we get into the UI portion of the training. For now,
this should be enough for the purpose of this lab.)
<VB.NET>
'' (1) pick an object on a screen.
</VB.NET>
PickObject() returns Reference object. You can retrieve the Element from the reference object
returned.
3.1 Write a function which takes Element as an argument, and display the following properties of the
given element:
Class name (or Type in .NET)
Category name
Id (Element Id)
Then do the same with the family type of the given element. To get to the family type of the given
element, you can use Element.GetTypeId() to obtain its Id first, then use
Document.GetElement(elementId).
Let’s name this function, for example, ShowBasicElementInfo(). The following shows a sample code to
do this:
<VB.NET>
Public Sub ShowBasicElementInfo(ByVal elem As Element)
End Sub
</VB.NET>
Then, call this function from your main Execute() method right after you have picked an element:
<VB.NET>
'' we have picked something.
Dim elem As Element = ref.Element
<AddIn Type="Command">
<Text>DB Element</Text>
<FullClassName>IntroVb.DBElement</FullClassName>
<Assembly>C:\...\IntroVB.dll</Assembly>
<AddInId>827AC040-6F44-4c03-82FE-292705580800</AddInId>
<VendorId>ADNP</VendorId>
<VendorDescription>Autodesk, Inc. www.autodesk.com</VendorDescription>
</AddIn>
Run the command, “DB Element”. Pick an element. You will see a dialog like following showing the
class, category and id of the element that you just picked (Figure 3). Try picking a few other element and
observe the output.
Discussion:
Compare the class names and categories among different elements, such as walls, doors and
windows. What do you observe?
Could you identify the element from the class name?
Could you identify the element from the category?
4. Identify Element
As you have found out by now, a class name is not enough to identify an element in Revit. Depending on
an element you have, you will need to check the following:
Class name
Category
If an element is Element Type (Symbol) or not
Table 1 shows the examples of a few elements with their class names and categories used to identify the
element. They are divided into four areas: System Family vs. Component Family, and Family Type vs.
Instance. This probably gives you slightly clear view of why you can identity some element by the class
name and some requires category.
A system family are a built-in object in Revit. There is a designated class for it. You can use it to
identify the element.
A component family has a generic form as FamilyInstance/FamilySymbol. Category is the way to
further identify the kind of object it is representing in Revit.
System Family Component Family
FamilySymbol
WallType &
Family Type
FloorType Category - Doors,
Windows
FamilyInstance
Wall &
Instance
Floor Category - Doors,
Windows
Table 1. class names and categories that you can use to identify
an element for walls, floors, doors and windows.
Now that we have understood how an element is represented in Revit database, let’s add a code to
identify an element. Here is an example:
<VB.NET>
'' identify the type of the element known to the UI.
Public Sub IdentifyElement(ByVal elem As Element)
End Sub
</VB.NET>
Call this function from your main Execute() method right after ShowBasicElementInfo():
<VB.NET>
'' (2) let's see what kind of element we got.
ShowBasicElementInfo(elem)
Build and run the command “DB Element” once again to see if you can identify an element you have
picked. Figure 4 shows a sample image of running the command.
Element.Parameter– takes an argument that can identify the kind of parameter and returns the
single parameter.
Element.LookupParameter – returns a single parameter given the name.
Element. GetOrderedParameters – returns a set of parameters applicable to the given element.
Element.GetParameters – returns all parameters with the given name.
Let’s first look at the Parameters(). The code below demonstrates the usage. Parameters() return a set
of parameters. You can simply loop through it to access each parameter. The main part that you will
need to pay attention is that that you will need to parse each parameter by the StorageType; a
parameter can be Integer, Double, String and ElementId. Depending on the StorageType, you will need
to choose the method to get the actual value.
<VB.NET>
'' show all the parameter values of the element
Public Sub ShowParameters(ByVal elem As Element, ByVal header As String)
TaskDialog.Show(header, s)
End Sub
''
'' Helper function: return a string from of a given parameter.
''
Public Shared Function ParameterToString(ByVal param As Parameter) _
As String
Case StorageType.Integer
Dim iVal As Integer = param.AsInteger
val = iVal.ToString()
Case StorageType.String
Dim sVal As String = param.AsString
val = sVal
Case StorageType.ElementId
Dim idVal As ElementId = param.AsElementId
val = idVal.IntegerValue.ToString
Case StorageType.None
Case Else
End Select
Return val
End Function
</VB.NET>
Call this function from your main Execute() method after IdentifyElement(). You may also use the same
function to display its family type information.
<VB.NET>
'' (3) identify each major types of element.
IdentifyElement(elem)
Build and run the command “DB Element” once again. You should be a list of parameters displayed in
dialogs. Figure 2 (on page 2) shows a sample image of running the command.
Here we will take a look at the first and second. Calling LookupParameter(Xxx) method itself using the
name is straightforward. However, using name has a disadvantage of depending on a language version
of Revit you are running. Therefore, using BuiltInParameter is more ideal. The trick here is to find out
which parameter Id or BuiltInParameter to use. If you look at the RevitAPI.chm documentation, under
BuiltInParameter Enum section, you will see hundreds of BuiltinParameters defined there. Among those
BuiltInParameters, only fraction of enum are applicable to a given element. How can we find out which
BuiltInParameter to use to retrieve a specific parameter?
RevitLookup tool comes handy to explore and find out which BuiltInParameter corresponds to which
parameter name. When you want to find out a parameter for a specific type of element, simple click on
the same type of object in the project >> [Snoop Current Selection …] >> [Parameters]. When you click
on each parameter name, you can check its [Definition] to find out the corresponding BuiltInParameter
(Figure 5).
Another thing that may worth mentioning is that if you look at the list of BuiltInParameters, using [Built-
in Enums Snoop…] or [Built-in Enumes Map…] button, you will see more parameters than you see in
parameters list. Occasionally, you may find some useful properties there. Such a sample will be:
You can use these to retrieve family and type names of the given element. Figure 6 shows a sample
enum mapping.
Figure 6. you can use RevitLookup to explore which BuiltInParameter to use.
Below is a sampler that shows how to retrieve some of commonly used properties. Explore the
BuiltInParameters in RevitLookup and try writing a function to retrieve a few parameters of your
interests using the code below as an example.
<VB.NET>
'' examples of retrieving a specific parameter indivisually.
'' (hard coding for simplicity. This function works best
'' with walls and doors.)
''
Public Sub RetrieveParameter(ByVal elem As Element, ByVal header As
String)
Dim s As String = String.Empty
param = elem.Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS)
If param IsNot Nothing Then
s += "Type Comments (by BuiltInParameter) = " _
+ ParameterToString(param) + vbCr
End If
'' using the BuiltInParameter, you can sometimes access one that is
'' not in the parameters set.
'' Note: this works only for element type.
param = elem.Parameter( _
BuiltInParameter.SYMBOL_FAMILY_AND_TYPE_NAMES_PARAM)
If param IsNot Nothing Then
s += "SYMBOL_FAMILY_AND_TYPE_NAMES_PARAM (only by
BuiltInParameter) = " + _
ParameterToString(param) + vbCr
End If
param = elem.Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM)
If param IsNot Nothing Then
s += "SYMBOL_FAMILY_NAME_PARAM (only by BuiltInParameter) = " + _
ParameterToString(param) + vbCr
End If
End Sub
</VB.NET>
Call your function from your main Execute() method at the end. You may also use the same function
to display its family type information.
<VB.NET>
'' (4) first parameters.
ShowParameters(elem, "Element Parameters: ")
</VB.NET>
Build and run the command “DB Element” once again. You should see a list of parameters of your
choice displayed in dialogs (Figure7).
<VB.NET>
'' show the location information of the given element.
'' location can be LocationPoint (e.g., furniture), and LocationCurve
'' (e.g., wall).
Public Sub ShowLocation(ByVal elem As Element)
s += "LocationPoint" + vbCr
s += "Point = " + PointToString(pt) + vbCr
s += "Rotation = " + r.ToString + vbCr
s += "LocationCurve" + vbCr
s += "EndPoint(0)/Start Point = " + _
PointToString(crv.GetEndPoint(0)) + vbCr
s += "EndPoint(1)/End point = " + _
PointToString(crv.GetEndPoint(1)) + vbCr
s += "Length = " + crv.Length.ToString + vbCr
End If
'' show it
TaskDialog.Show("Show Location", s)
End Sub
'' Helper Function: returns XYZ in a string form.
''
Public Shared Function PointToString(ByVal pt As XYZ) As String
If pt Is Nothing Then
Return ""
End If
End Function
</VB.NET>
Call your function from your main Execute() method at the end.
<VB.NET>
'' (4) first parameters.
...
'' the same logic applies to the type parameter.
RetrieveParameter(elemType, "Type Parameter (by Name and
BuiltInParameter): ")
</VB.NET>
Build and run the command “DB Element” once again. You should see location information displayed in
dialogs. Figure 8 shows an example of location information when you pick an element based on
Location Point, such as a door. When you pick a wall, it will be Location Line. Note, not all elements
allow accessing location information this way.
ElementViewer
RoomViewer
AnalyticalViewer
<VB.NET>
'' show the geometry information of the given element.
Public Sub ShowGeometry(ByVal elem As Element)
End Sub
Else
str += " *** unkown geometry type" & _
geomObj.GetType.ToString
End If
Next
Return str
End Function
</VB.NET>
Call your function from the end of your main Execute() method.
<VB.NET>
'' (5) location
ShowLocation(elem)
</VB.NET>
Build and run the command “DB Element” once again. You should be seeing the geometry information
displayed in dialogs. (Figure 9).
Figure 9. an example of high level geometry information
8. Summary
In this lab, we have learned how an element is represented in Revit and how to retrieve information
about an element. We have learned how to:
In the next lab, we will take a look at a group of elements in the Revit database and learn how to
selectively retrieve elements of our interests, which is called element filtering.