In the past, we have received inquiries like how to access the Subcategory information from in-place family instances. This blog-post answers this query with a code snippet.
The Subcategory information from in-place family instances should be available from the GraphicsStyleId which is accessible from the geometry in the project environment. The following code shows how to extract the Subcategory of each face. The code parses through the geometry information of a solid (cube or cylinder) and then access each face of the solid and GraphicsStyleId on the geometry object to extract the Subcategory information.
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
namespace Revit.SDK.Samples.HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Get the application and document from external command data.
UIApplication app = commandData.Application;
Document doc =
commandData.Application.ActiveUIDocument.Document;
Selection s = app.ActiveUIDocument.Selection;
foreach (Element selElement in s.Elements)
{
Options options = new Options();
GeometryElement geomElem = selElement.get_Geometry(options);
ShowGraphicsStyleCategory(doc, geomElem, "Geometry element");
foreach (GeometryObject geomObj in geomElem.Objects)
{
ShowGraphicsStyleCategory(doc, geomObj, "Geometry object");
if (geomObj is GeometryInstance)
{
GeometryInstance instObj = geomObj as GeometryInstance;
GeometryElement instGeomElem = instObj.GetSymbolGeometry();
foreach (GeometryObject instGeomObj in instGeomElem.Objects)
{
ShowGraphicsStyleCategory(doc, instGeomObj, "Instance geometry object");
if (instGeomObj is Solid)
{
Solid solid = instGeomObj as Solid;
foreach (Face face in solid.Faces)
{
ShowGraphicsStyleCategory(doc, face, "Face");
}
}
}
}
if (geomObj is Solid)
{
Solid solid = geomObj as Solid;
foreach (Face face in solid.Faces)
{
ShowGraphicsStyleCategory(doc, face, "Face");
}
}
}
}
return Result.Succeeded;
}
private void ShowGraphicsStyleCategory(Document d, GeometryObject geomObj, String label)
{
ElementId graphicsStyleId = geomObj.GraphicsStyleId;
if (graphicsStyleId == ElementId.InvalidElementId)
return;
Element graphicsStyleElem = d.get_Element(graphicsStyleId);
GraphicsStyle graphicsStyle = graphicsStyleElem as GraphicsStyle;
if (graphicsStyle == null)
return;
Category category = graphicsStyle.GraphicsStyleCategory;
String message = String.Format("Graphics style id {0} Category {1} - {2}",
graphicsStyleId.IntegerValue, category.Name, category.Id.IntegerValue);
TaskDialog.Show(label, message);
}
}
}
The method ShowGraphicsStyleCategory in the code above, extracts this Subcategory information.