About family documents

About family documents

Family

The Family object represents an entire Revit family. A Family Document is a Document that represents a Family rather than a Revit project.

Using the Family Creation functionality of the Revit API, you can create and edit families and their types. This functionality is particularly useful when you have pre-existing data available from an external system that you want to convert to a Revit family library.

API access to system family editing is not available.

Categories

As noted in the previous chapter, the FamilyBase.FamilyCategory property indicates the category of the Family such as Columns, Furniture, Structural Framing, or Windows.

The following code can be used to determine the category of the family in an open Revit Family document.

Code Region 13-1: Category of open Revit Family Document

string categoryName = familyDoc.OwnerFamily.FamilyCategory.Name;
The FamilyCategory can also be set, allowing the category of a family that is being edited to be changed.

Parameters

Family parameters can be accessed from the OwnerFamily property of a Family Document as the following example shows.

Code Region 13-2: Category of open Revit Family Document

// get the owner family of the family document.
Family family = familyDoc.OwnerFamily; 
Parameter param = family.get_Parameter(BuiltInParameter.FAMILY_WORK_PLANE_BASED);
// this param is a Yes/No parameter in UI, but an integer value in API
// 1 for true and 0 for false
int isTrue = param.AsInteger(); 
// param.Set(1); // set value to true.

Creating a Family Document

The ability to modify Revit Family documents and access family types and parameters is available from the Document class if the Document is a Family document, as determined by the IsFamilyDocument property. To edit an existing family while working in a Project document, use the EditFamily() functions available from the Document class, and then use LoadFamily() to reload the family back into the owner document after editing is complete. To create a new family document use Application.NewFamilyDocument():

Code Region 13-3: Creating a new Family document

// create a new family document using Generic Model.rft template
string templateFileName = @"C:\Documents and Settings\All Users\Application Data\Autodesk\RST 2011\Imperial Templates\Generic Model.rft";

Document familyDocument = application.NewFamilyDocument(templateFileName);
if (null == familyDocument)
{
        throw new Exception("Cannot open family document");
}

Nested Family Symbols

You can filter a Family Document for FamilySymbols to get all of the FamilySymbols loaded into the Family. In this code sample, all the nested FamilySymbols in the Family for a given FamilyInstance are listed.

Code Region 13-4: Getting nested Family symbols in a Family

public void GetLoadedSymbols(Autodesk.Revit.DB.Document document, FamilyInstance familyInstance)
{
        if (null != familyInstance.Symbol)
        {
                // Get family associated with this
                Family family = familyInstance.Symbol.Family;

                // Get Family document for family
                Document familyDoc = document.EditFamily(family);
                if (null != familyDoc && familyDoc.IsFamilyDocument == true)
                {
                        String loadedFamilies = "FamilySymbols in " + family.Name + ":\n";
                        FilteredElementCollector collector = new FilteredElementCollector(document);
                        ICollection<Element> collection = 
                                collector.OfClass(typeof(FamilySymbol)).ToElements();
                        foreach (Element e in collection)
                        {
                                FamilySymbol fs = e as FamilySymbol;
                                loadedFamilies += "\t" + fs.Name + "\n";
                        }

                        TaskDialog.Show("Revit",loadedFamilies);
                }
        }
}