Example: Retrieve Geometry Data from a Wall

Example: Retrieve Geometry Data from a Wall

This walkthrough illustrates how to get geometry data from a wall. The following information is covered:

Note Retrieving the geometry data from Element in this example is limited because Instance is not considered. For example, sweeps included in the wall are not available in the sample code. The goal for this walkthrough is to give you a basic idea of how to retrieve geometry data but not cover all conditions. For more information about retrieving geometry data from Element, refer to Example: Retrieving Geometry Data from a Beam.

Create Geometry Options

In order to get the wall's geometry information, you must create a Geometry.Options object which provides detailed customized options. The code is as follows:

Code Region 20-1: Creating Geometry.Options

Autodesk.Revit.DB.Options geomOption = application.Create.NewGeometryOptions();
if (null != geomOption)
{
        geomOption.ComputeReferences = true;
        geomOption.DetailLevel = Autodesk Autodesk.Revit.DB.DetailLevels.Fine;
        
        // Either the DetailLevel or the View can be set, but not both
        //geomOption.View = commandData.Application.ActiveUIDocument.Document.ActiveView;
        
        TaskDialog.Show("Revit", "Geometry Option created successfully.");
}
Note: For more information, refer to Geometry.Options.

Retrieve Faces and Edges

Wall geometry is a solid made up of faces and edges. Complete the following steps to get the faces and edges:

  1. Retrieve a Geometry.Element instance using the Wall class Geometry property. This instance contains all geometry objects in the Object property, such as a solid, a line, and so on.
  2. Iterate the Object property to get a geometry solid instance containing all geometry faces and edges in the Faces and Edges properties.
  3. Iterate the Faces property to get all geometry faces.
  4. Iterate the Edges property to get all geometry edges.

The sample code follows:

Code Region 20-2: Retrieving faces and edges

private void GetFacesAndEdges(Wall wall)
{
        String faceInfo = "";

        Autodesk.Revit.DB.Options opt = new Options();
        Autodesk.Revit.DB.GeometryElement geomElem = wall.get_Geometry(opt);
        foreach (GeometryObject geomObj in geomElem)
        {
                Solid geomSolid = geomObj as Solid;
                if (null != geomSolid)
                {
                        int faces = 0;
                        double totalArea = 0;
                        foreach (Face geomFace in geomSolid.Faces)
                        {
                                faces++;
                                faceInfo += "Face " + faces + " area: " + geomFace.Area.ToString() + "\n";
                                totalArea += geomFace.Area;
                        }
                        faceInfo += "Number of faces: " + faces + "\n";
                        faceInfo += "Total area: " + totalArea.ToString() + "\n";
                        foreach (Edge geomEdge in geomSolid.Edges)
                        {
                                // get wall's geometry edges
                        }
                }
        }
        TaskDialog.Show("Revit", faceInfo);
}