Category Class


Represents the category or subcategory to which an element belongs.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 2015.0.0.0 (2015.0.0.0)

Syntax

C#
public class Category : APIObject
Visual Basic
Public Class Category _
	Inherits APIObject
Visual C++
public ref class Category : public APIObject

Remarks

Categories are an import tool within Revit for identifying the inferred type of an element, such as anything in the Walls category should be considered as a wall. The API exposes access to the built in categories within Revit via the Document.Settings.Categories property.

Examples

Copy C#
Element selectedElement = null;
foreach (ElementId id in uidoc.Selection.GetElementIds())
{
    selectedElement = document.GetElement(id);
    break;  // just get one selected element
}

// Get the category instance from the Category property
Category category = selectedElement.Category;

BuiltInCategory enumCategory = (BuiltInCategory)category.Id.IntegerValue;

// Format the prompt string, which contains the category information
String prompt = "The category information of the selected element is: ";
prompt += "\n\tName:\t" + category.Name;   // Name information

prompt += "\n\tId:\t" + enumCategory.ToString();    // Id information
prompt += "\n\tParent:\t";
if (null == category.Parent)
{
    prompt += "No Parent Category";   // Parent information, it may be null
}
else
{
    prompt += category.Parent.Name;
}

prompt += "\n\tSubCategories:"; // SubCategories information, 
CategoryNameMap subCategories = category.SubCategories;
if (null == subCategories || 0 == subCategories.Size) // It may be null or has no item in it
{
    prompt += "No SubCategories;";
}
else
{
    foreach (Category ii in subCategories)
    {
        prompt += "\n\t\t" + ii.Name;
    }
}

// Give the user some information
TaskDialog.Show("Revit",prompt);
Copy VB.NET
Dim selectedElement As Element = Nothing
For Each id As ElementId In uidoc.Selection.GetElementIds()
    selectedElement = document.GetElement(id)
    ' just get one selected element
    Exit For
Next

' Get the category instance from the Category property
Dim category As Category = selectedElement.Category

Dim enumCategory As BuiltInCategory = DirectCast(category.Id.IntegerValue, BuiltInCategory)

' Format the prompt string, which contains the category information
Dim prompt As [String] = "The category information of the selected element is: "
prompt += vbLf & vbTab & "Name:" & vbTab + category.Name
' Name information
prompt += vbLf & vbTab & "Id:" & vbTab & enumCategory.ToString()
' Id information
prompt += vbLf & vbTab & "Parent:" & vbTab
If category.Parent Is Nothing Then
    ' Parent information, it may be null
    prompt += "No Parent Category"
Else
    prompt += category.Parent.Name
End If

prompt += vbLf & vbTab & "SubCategories:"
' SubCategories information, 
Dim subCategories As CategoryNameMap = category.SubCategories
If subCategories Is Nothing OrElse 0 = subCategories.Size Then
    ' It may be null or has no item in it
    prompt += "No SubCategories;"
Else
    For Each ii As Category In subCategories
        prompt += vbLf & vbTab & vbTab + ii.Name
    Next
End If

' Give the user some information
TaskDialog.Show("Revit", prompt)

Inheritance Hierarchy

See Also