Wednesday, September 19, 2012

Getting all File Based Family Elements by Category

I haven't shared any code on here in a while and thought this might be a good time to get back in the general sharing mode. I get asked a bunch about how the best way is to collect elements in the model especially file based component families (FamilySymbol).

This sample should work in Revit 2012 as well as Revit 2013.

Here's a quick and easy way to get all elements in your model of FamilySymbol organized by Category. We'll use a basic Dictionary class to hold the elements since Dictionaries are super fast to access values from without any iteration required.

First you will need to declare your dictionary somewhere in your code as either a private or public property (you decide), I'm using a property in this example.


  ''' <summary>
  ''' All Family Symbol Elements in the Model by Category
  ''' </summary>
  ''' <value></value>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Property FamilySymbolElements As Dictionary(Of Category, List(Of FamilySymbol))


The next thing is to provide a subroutine where you can perform the gathering of the elements. After we instantiate a fresh dictionary to hold the data, we build a filtered element collector that collects elements by their class type. The class type we're interested in is FamilySymbol.

We then iterate over the elements returned by the collector and check if the category that the element belongs to is in our dictionary. Where the category does not exists as a key in our dictionary, we add a fresh list containing the single element. Where it already exists, we get the list containing the elements and add the new element to the list and apply the updated list back as the value to the dictionary for that category.


  ''' <summary>
  ''' Get all Family Symbol Elements
  ''' </summary>
  ''' <remarks></remarks>
  Private Sub GetSymbols()
 
    ' Fresh Dictionary
    FamilySymbolElements = New Dictionary(Of Category, List(Of FamilySymbol))
 
    ' Collector
    Using col As New FilteredElementCollector(Doc)
      col.OfClass(GetType(FamilySymbol))
      For Each x In col.ToElements
 
        Try
 
          ' Cast and Collect
          Dim m_fs As FamilySymbol = TryCast(x, FamilySymbol)
 
          ' Does the Category already exist in the system
          Dim m_cat As Category = x.Category
          If Not FamilySymbolElements.ContainsKey(m_cat) Then
 
            ' New List
            Dim m_l As New List(Of FamilySymbol)
            m_l.Add(m_fs)
            FamilySymbolElements(m_cat) = m_l
 
          Else
 
            Try
 
              ' Updated List
              Dim m_l As New List(Of FamilySymbol)
              FamilySymbolElements.TryGetValue(m_cat, m_l)
              m_l.Add(m_fs)
 
              ' Update the Key
              FamilySymbolElements(m_cat) = m_l
 
            Catch ex As Exception
 
            End Try
 
          End If
 
        Catch ex As Exception
 
        End Try
 
      Next
    End Using
 
  End Sub


Now we have a complete list of FamilySymbol elements in the model organized by category.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.