next previous home

5. Groups and Geometry

5-1 List all groups and group types in the model

Groups of elements are useful when users need to create entities that represent repeating layouts or are common to many building projects. In this lab, we shall list all the existing groups in a Revit model and display their group Id and group type. This can be done by filtering all the elements of Group type and displaying the property of each. The latter part of the lab focuses on listing all the available GroupTypes and listing the properties of each GroupType element. It will also cover extracting all the 'Model' group types from the Revit model and displaying their names.

Using the new filtering mechanism in Revit 2009, we can select all elements of 'Group' type only. This avoids iterating through all the elements in the model and thus is a huge performance advantage. Once we collect all the group elements, we will simply display some of the properties like Id, Name etc. of each group:

  public class Lab5_1_GroupsAndGroupTypes : IExternalCommand
  {
    public IExternalCommand.Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      Application app = commandData.Application;

      // List all Group Elements
      List<Element> groups = new List<Element>();
      Filter filterType = app.Create.Filter.NewTypeFilter( typeof( Group ) );
      app.ActiveDocument.get_Elements( filterType, groups );

      string sMsg = "All GROUPS in the doc are:\r\n";
      foreach( Group grp in groups )
      {
        sMsg += "\r\n  Id=" + grp.Id.Value.ToString() + "; Type=" + grp.GroupType.Name;
      }
      LabUtils.InfoMsg( sMsg );
      return IExternalCommand.Result.Succeeded;
    }
  }
    Public Class Lab5_1_GroupsAndGroupTypes
        Implements IExternalCommand

        Public Function Execute(ByVal commandData As Autodesk.Revit.ExternalCommandData,
        ByRef message As String, ByVal elements As Autodesk.Revit.ElementSet) As Autodesk.Revit.IExternalCommand.Result
        Implements Autodesk.Revit.IExternalCommand.Execute

            Dim app As Revit.Application = commandData.Application

            ' List all Group Elements using filtering mechanism in 2009
            Dim groups As New List(Of Element)
            Dim filterType As Filter = app.Create.Filter.NewTypeFilter(GetType(Group))
            Dim iRetVal As Integer = app.ActiveDocument.Elements(filterType, groups)

            Dim sMsg As String = "All GROUPS in the doc are:" & vbCrLf
            Dim grp As Group
            For Each grp In groups
                sMsg += vbCrLf + "  Id=" & grp.Id.Value.ToString & "; Type=" & grp.GroupType.Name
            Next
            MsgBox(sMsg)

            Return IExternalCommand.Result.Succeeded
        End Function
    End Class

Groups can be of Model and Detail types and these are referred to as GroupTypes. Model groups contain model elements and Detail groups contain view-specific elements. We shall use filtering mechanism available in Revit 2009 again to get all the elements of 'GroupType' type and list their Built-in parameters (for example, we can use symbol_family_name_param built-in parameter to extract the GroupType family name).

Further, we shall attempt to extract the name of each model group type in the Revit model. For this, you can use the NewLogicAndFilter to filter out elements which are of 'GroupType' AND family name 'Model Group'. Once we have all the model group types listed, we can directly display the Name property from the GroupType element:

    // List all Group Type Elements
    List<Element> groupTypes = new List<Element>();
    Filter filterType = app.Create.Filter.NewTypeFilter( typeof( GroupType ) );
    app.ActiveDocument.get_Elements( filterType, groupTypes );

    sMsg = "All GROUP TYPES in the doc are:\r\n";
    foreach( GroupType grpTyp in groupTypes )
    {
      // determine the GroupType system family
      // (cf. Labs3 for standard symbols):
      Parameter p = grpTyp.get_Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM);
      string famName = null == p ? "?" : p.AsString();
      sMsg += "\r\n  Name=" + grpTyp.Name + "; Id=" + grpTyp.Id.Value.ToString() + "; Family=" + famName;
    }
    LabUtils.InfoMsg( sMsg );

    List<Element> modelGroupTypes = new List<Element>();
    Filter filterType = app.Create.Filter.NewTypeFilter( typeof( GroupType ) );
    Filter filterParam = app.Create.Filter.NewParameterFilter(
      BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM, CriteriaFilterType.Equal, "Model Group" );
    Filter filterAnd = app.Create.Filter.NewLogicAndFilter( filterType, filterParam );
    app.ActiveDocument.get_Elements( filterAnd, modelGroupTypes );

    sMsg = "All *MODEL* GROUP TYPES in the doc are:\r\n";
    foreach( GroupType grpTyp in modelGroupTypes )
    {
      sMsg += "\r\n  Name=" + grpTyp.Name + "; Id=" + grpTyp.Id.Value.ToString();
    }
    LabUtils.InfoMsg( sMsg );
    ' List all Group Type Elements
    Dim groupTypes As New List(Of Element)
    Dim filterType As Filter = app.Create.Filter.NewTypeFilter(GetType(GroupType))
    Dim iRetVal As Integer = app.ActiveDocument.Elements(filterType, groupTypes)

    sMsg = "All GROUP TYPES in the doc are:" & vbCrLf
    Dim grpTyp As GroupType
    For Each grpTyp In groupTypes
        ' To Determine the GroupType system Family,
        ' we need the following (as in Labs3 for standard Symbols):
        Dim famName As String = "?"
        Dim p As Parameter = grpTyp.Parameter(Parameters.BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM)
        If Not p Is Nothing Then
            famName = p.AsString
        End If
        sMsg += vbCrLf + "  Name=" & grpTyp.Name & "; Id=" & grpTyp.Id.Value.ToString & "; Family=" & famName
    Next
    MsgBox(sMsg)

    ' Typically, only "Model" types will be needed,
    Dim modelGroupTypes As New List(Of Element)
    Dim filterType As Filter = app.Create.Filter.NewTypeFilter(GetType(GroupType))
    Dim filterParam = app.Create.Filter.NewParameterFilter(
        BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM, CriteriaFilterType.Equal, "Model Group")
    Dim filter As Filter = app.Create.Filter.NewLogicAndFilter(filterType, filterParam)
    Dim n As Integer = app.ActiveDocument.Elements(filter, modelGroupTypes)

    sMsg = "All *MODEL* GROUP TYPES in the doc are:" & vbCrLf
    For Each grpTyp In modelGroupTypes
        sMsg += vbCrLf + "  Name=" & grpTyp.Name & "; Id=" & grpTyp.Id.Value.ToString
    Next
    MsgBox(sMsg)

Build the project, adjust the ini file and examine the output with various pre-existing model groups and types as displayed in the UI.

next previous home copyright © 2007-2008 jeremy tammik, autodesk inc. all rights reserved.