NewSubcategory Method


Add a new subcategory into the Autodesk Revit document.

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

Syntax

C#
public Category NewSubcategory(
	Category parentCategory,
	string name
)
Visual Basic
Public Function NewSubcategory ( _
	parentCategory As Category, _
	name As String _
) As Category
Visual C++
public:
Category^ NewSubcategory(
	Category^ parentCategory, 
	String^ name
)

Parameters

parentCategory
Type: Autodesk.Revit.DBCategory
The parent category.
name
Type: SystemString
The new category name.

Return Value

If successful, the newly created subcategory.

Examples

CopyC#
public void AssignSubCategory(Document document, GenericForm extrusion)
{
    // create a new subcategory 
    Category cat = document.OwnerFamily.FamilyCategory;
    Category subCat = document.Settings.Categories.NewSubcategory(cat, "NewSubCat");

    // create a new material and assign it to the subcategory
    ElementId materialId = Material.Create(document, "Wood Material");
    subCat.Material = document.GetElement(materialId) as Material;

    // assign the subcategory to the element
    extrusion.Subcategory = subCat;
}
CopyVB.NET
Public Sub AssignSubCategory(document As Document, extrusion As GenericForm)
    ' create a new subcategory 
    Dim cat As Category = document.OwnerFamily.FamilyCategory
    Dim subCat As Category = document.Settings.Categories.NewSubcategory(cat, "NewSubCat")

    ' create a new material and assign it to the subcategory
    Dim materialId As ElementId = Material.Create(document, "Wood Material")
    subCat.Material = TryCast(document.GetElement(materialId), Material)

    ' assign the subcategory to the element
    extrusion.Subcategory = subCat
End Sub

Exceptions

ExceptionCondition
Autodesk.Revit.ExceptionsArgumentNullException Thrown when the input argument-"parentCategory" or "name"-is a null reference (Nothing in Visual Basic).
Autodesk.Revit.ExceptionsArgumentException Thrown when the input argument-"parentCategory"-cannot add subcategory. Thrown when the input argument-"name"-is an illegal name.
Autodesk.Revit.ExceptionsInvalidOperationException Thrown when subcategory creation failed.

See Also

ArchiLabs

Stop fighting Revit automation.

Build repeatable BIM design and documentation workflows with scripts, data, and AI in one place, without wrestling brittle one-off automations.

Try ArchiLabs →