Trouble with getting families from specific category element

Trouble with getting families from specific category element

Anonymous
Not applicable
487 Views
1 Reply
Message 1 of 2

Trouble with getting families from specific category element

Anonymous
Not applicable

Hi everyone!

 

I've been looking for ways to get the families who are in specific category, but in some categories, my code don't shows me some families.

 

This is my code for looking Families in Categories, I hope you will support me.

 

int iViewID = Convert.ToInt32(lbView.Text);
ElementId id = new ElementId(iViewID);

int iCategory = Convert.ToInt32(cmbCategory.SelectedValue.ToString());
ElementId catID = new ElementId(iCategory);


List<string> lstFamily = new List<string>();
List<string> lstFamilyType = new List<string>();



Dictionary<string, List<FamilySymbol>> FamilyTypes = new FilteredElementCollector(docM)
  .WherePasses(new ElementClassFilter(typeof(FamilySymbol)))
  .OfCategoryId(catID)
  .Cast<FamilySymbol>()
  .GroupBy(e3 => e3.Family.Name)
  .ToDictionary(e3 => e3.Key, e3 => e3.ToList());



            
foreach (KeyValuePair<string, List<FamilySymbol>> entry in FamilyTypes)
{
	lstFamily.Add(entry.Key);
                
	foreach (FamilySymbol item in entry.Value)
	{
		Debug.Print(item.Name);
		lstFamilyType.Add(item.Name);
	}
}

            
           
cmbFamily.DataSource = lstFamily;

 

Cheers!

0 Likes
488 Views
1 Reply
Reply (1)
Message 2 of 2

jeremytammik
Autodesk
Autodesk

Dear Carlos,

 

Sorry, your code is too long for my poor human brain to grok in detail.

 

However, there used to be one problematic aspect when querying a family for its category.

 

The Family.Category property often does not return what you would expect.

 

In that case, you can grab the first type (also called symbol) from it and ask that for its category instead.

 

Oh, and there is also a FamilyCategory property that you can use. That was introduced later.

 

Here are two sample code snippets from the ADN training labs,

 

https://github.com/jeremytammik/AdnRevitApiLabsXtra

 

https://github.com/jeremytammik/AdnRevitApiLabsXtra/blob/master/XtraCs/Labs2.cs:

 

          // The element category is not implemented for all classes,
          // and may return null; for Family elements, one can sometimes
          // use the FamilyCategory property instead.

          s = string.Empty;

          if( null != e.Category )
          {
            s = e.Category.Name;
          }
          if( 0 == s.Length && e is Family && null != ( (Family) e ).FamilyCategory )
          {
            s = ( (Family) e ).FamilyCategory.Name;
          }
          if( 0 == s.Length )
          {
            s = "?";
          }

https://github.com/jeremytammik/AdnRevitApiLabsXtra/blob/master/XtraCs/Labs3.cs:

 

      foreach( Family f in families )
      {
        // Get its category name; notice that the Category property is not
        // implemented for the Family class; use FamilyCategory instead;
        // notice that that is also not always implemented; in that case,
        // use the workaround demonstrated below, looking at the contained
        // family symbols' category:

        a.Add( string.Format( "Name={0}; Category={1}; FamilyCategory={2}",
          f.Name,
          ( ( null == f.Category ) ? "?" : f.Category.Name ),
          ( ( null == f.FamilyCategory ) ? "?" : f.FamilyCategory.Name ) ) );
      }
      #endregion // 3.1.a

      string msg = "{0} standard familie{1} are loaded in this model{2}";
      LabUtils.InfoMsg( msg, a );

      // Loop through the collection of families, and now look at
      // the child symbols (types) as well. These symbols can be
      // used to determine the family category.

      foreach( Family f in families )
      {
        string catName;
        bool first = true;

        // Loop all contained symbols (types):

        //foreach( FamilySymbol s in f.Symbols ) // 2014
        foreach( ElementId id in f.GetFamilySymbolIds() ) // 2015
        {
          FamilySymbol s = doc.GetElement( id ) as FamilySymbol;

          // you can determine the family category from its first symbol.

          if( first )
          {
            first = false;

            #region 3.1.b Retrieve category name of first family symbol:
            catName = s.Category.Name;
            #endregion // 3.1.b

            msg = "Family: Name=" + f.Name
              + "; Id=" + f.Id.IntegerValue.ToString()
              + "; Category=" + catName
              + "\r\nContains Types:";
          }
          msg += "\r\n    " + s.Name + "; Id=" + s.Id.IntegerValue.ToString();
        }

        // Show the symbols for this family and allow user to proceed
        // to the next family (OK) or cancel (Cancel)

        msg += "\r\nContinue?";
        if( !LabUtils.QuestionMsg( msg ) )
        {
          break;
        }
      }

I hope this helps.

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes