get family symbol revit api

/*
Pass Document and name string as Variables
This snippet is a utility function to make working with family symbols easier
also is an example on using FilteredElementCollector filters

TESTED REVIT API: 2019
The snippet can be used as is in a Revit Application Macro for test purposes

Author: Robert Curry | https://github.com/RobertCurry0216

This file is shared on www.revitapidocs.com
For more information visit http://github.com/gtalarico/revitapidocs
License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md
*/

public static FamilySymbol GetFamilySymbolByName(Document doc, string name)
{
    var paramId = new ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
    var paramValueProvider = new ParameterValueProvider(paramId);
    var equalsRule = new FilterStringEquals();
    var filterRule = new FilterStringRule(paramValueProvider, equalsRule, name, false);
    var filter = new ElementParameterFilter(filterRule);

    var fec = new FilteredElementCollector(doc);
    fec.OfClass(typeof(FamilySymbol)).WhereElementIsElementType().WherePasses(filter);

    if (fec.GetElementCount() == 1)
    {
        var symbol = fec.FirstElement() as FamilySymbol;
        if (!symbol.IsActive)
        {
            symbol.Activate();
            doc.Regenerate();
        }
        return symbol;
    }
    return null;
}

3.5
4
Sumi 80 points

                                    public Result Execute( 
  ExternalCommandData commandData,
  ref string message,
  ElementSet elements )
{
  UIApplication uiapp = commandData.Application;
  Document doc = uiapp.ActiveUIDocument.Document;
 
  Stopwatch sw = Stopwatch.StartNew();
 
  XElement xmlFamilyInstances = new XElement( 
    "Family_Inventory" );
 
  // retrieve all families. 
  // use the ElementClassFilter shortcut 
  // and filter all "Family" elements.
 
  FilteredElementCollector families 
    = new FilteredElementCollector( doc );
 
  families.OfClass( typeof( Family ) );
 
  int nFamily = 0;
  int nSymbol = 0;
  int nInstance= 0;
 
  foreach( Family family in families )
  {
    ++nFamily;
 
    // XML: Start by adding the Family element
 
    XElement temp = new XElement( 
      "FamilyName", family.Name );
 
    // use the FamilySymbolFilter for each Family
 
    FamilySymbolFilter filterFamSym 
      = new FamilySymbolFilter( family.Id );
 
    FilteredElementCollector famSymbols 
      = new FilteredElementCollector( doc );
 
    famSymbols.WherePasses( filterFamSym );
 
    foreach( FamilySymbol famSymbol in famSymbols )
    {
      ++nSymbol;
 
      FamilyInstanceFilter filterFamilyInst 
        = new FamilyInstanceFilter( 
          doc, famSymbol.Id );
 
      FilteredElementCollector collectorFamInstances 
        = new FilteredElementCollector( 
          doc, doc.ActiveView.Id );
 
      IEnumerable<FamilyInstance> famInstances 
        = collectorFamInstances
          .WherePasses( filterFamilyInst )
          .OfType<FamilyInstance>();
 
      int nInstanceCount 
        = famInstances.Count<FamilyInstance>();
 
      nInstance += nInstanceCount;
 
      temp.Add( new XElement( 
        "SymbolName", 
        famSymbol.Name,
        from fi in famInstances
          select new XElement( 
            "Instance", 
            fi.Id.ToString(),
            new XElement( "Type", 
              fi.GetType().ToString() ),
            new XElement( "Position", 
              LocationString( fi ) ) ) ) );
    }
    xmlFamilyInstances.Add( temp );
  }
 
  // Create the XML report document
 
  XDocument xmldoc =
    new XDocument(
        new XDeclaration( "1.0", "utf-8", "yes" ),
        new XComment( 
          "Current Family Inventory of Revit project: "
          + doc.PathName ),
        xmlFamilyInstances );
 
  string fileName = "C:/FamilyInventory.xml";
  xmldoc.Save( fileName );
 
  Util.ShowElapsedTime( sw,
    "Linq Example 3 XML Report",
    string.Format( "{0} families with {1} symbols and {2} instances",
      nFamily, nSymbol, nInstance ),
    string.Empty );
 
  // We can use Internet Explorer or whatever 
  // your favorite XML viewer is...
  Process.Start(
    "C:/Program Files/Internet Explorer/iexplore.exe", 
    fileName);
 
  // Here is one that is free and is a little more 
  // robust than Internet Explorer. If interested, 
  // download from here: 
  // http://download.cnet.com/XML-Marker/3000-7241_4-10202365.html
  //Process.Start( @"C:/Program Files (x86)/XML Marker/xmlmarker.exe", fileName );
 
  return Result.Succeeded;
}

3.5 (4 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source