Unload Family Type

We discussed the

loading of a single family type

just two days ago.
Here is a related question that came up and was handled by Saikat Bhattacharya:

Question:
How can I use the API to remove unused family types from a project?
I know there is a command within Revit to do this, but we would like to automate the process.

Answer:
You can unload a specific loaded family symbol from your project using the Document.Delete() method.
I tested this as follows:

  • Load a specific column symbol “457 x 610mm”.
  • Run an external command with the code below to filter out and iterate over all column elements, including symbols.
  • Apply the Delete method to remove and thus unload the specific symbol.

After running the code, the list of loaded family symbols displayed in the user interface confirmed that the specific family symbol was no longer present in the project.
Here is the code for the external command Execute method:


public IExternalCommand.Result Execute(
  ExternalCommandData commandData,
  ref string message,
  ElementSet elements )
{
  Application app = commandData.Application;
  Document doc = app.ActiveDocument;
 
  Filter filter = app.Create.Filter.NewCategoryFilter(
    BuiltInCategory.OST_Columns );
 
  ElementIterator it = doc.get_Elements( filter );
 
  while ( it.MoveNext() )
  {
    Element e = it.Current as Element;
    if ( e.Name.Equals( "457 x 610mm" ) )
    {
      doc.Delete( e );
    }
  }
  return IExternalCommand.Result.Succeeded;
}

Thank you Saikat for this solution!


Comments

2 responses to “Unload Family Type”

  1. And if I want to Delete Family along with all Family Types in it. How can I do that?
    How can I List all families in One Collection Including All Ducts and Pipes also?
    Many thanks.
    Yasir

  2. Dear Yasir,
    1. You can use the Document.Delete method. Its use in this context is described in
    http://thebuildingcoder.typepad.com/blog/2009/06/unload-family-type.html
    2. That sounds like a pretty weird request to me, but it is still doable:
    List.lt.ElementFilter.gt. a
    = new List.lt.ElementFilter.gt.( 3 );
    a.Add( new ElementClassFilter(
    typeof( Family ) ) );
    a.Add( new ElementClassFilter(
    typeof( Duct ) ) );
    a.Add( new ElementClassFilter(
    typeof( Pipe ) ) );
    FilteredElementCollector collector
    = new FilteredElementCollector( doc )
    .WherePasses( new LogicalOrFilter( a ) );
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading