2020 Method
Changes 4
M

Document.NewFamilyInstance

Description:
Inserts a new instance of a family into the document, using a curve, type/symbol and reference level.
Remarks:
This method is used to insert one family instance into another element along the geometry of a curve. If the instance fails to be created an exception may be thrown.

The type/symbol that is used must be loaded into the document before this method is called. Families and their symbols can be loaded using the Document.LoadFamily or Document.LoadFamilySymbol methods.

The host object must be one that supports insertion of instances otherwise this method will fail. All levels within the document can be found by iterating over the entire document and searching for objects of type Autodesk.Revit.Elements.Level.

Note: if the created family instance includes nested instances, the API framework will automatically regenerate the document during this method call.

Overloads (3):
NewFamilyInstance(Curve,FamilySymbol,Level,StructuralType)
public FamilyInstance NewFamilyInstance(
	Curve curve,
	FamilySymbol symbol,
	Level level,
	StructuralType structuralType
)
Return Value nullNothingnullptr If creation was successful then an instance to the new object is returned, otherwise null Nothing nullptr a null reference ( Nothing in Visual Basic) .
FamilyInstance CreateBeam(Autodesk.Revit.DB.Document document, View view)
{

   // get the given view's level for beam creation
    Level level = document.GetElement(view.LevelId) as Level;

    // get a family symbol
    FilteredElementCollector collector = new FilteredElementCollector(document);
    collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming);

    FamilySymbol gotSymbol = collector.FirstElement() as FamilySymbol;

    // create new beam 10' long starting at origin
    XYZ startPoint = new XYZ(0, 0, 0);
    XYZ endPoint = new Autodesk.Revit.DB.XYZ(10, 0, 0);

    Autodesk.Revit.DB.Curve beamLine = Line.CreateBound(startPoint, endPoint);

    // create a new beam
    FamilyInstance instance = document.Create.NewFamilyInstance(beamLine, gotSymbol,
                                                                level, StructuralType.Beam);

    return instance;
}