NewType Method


Add a new family type with a given name and makes it be the current type.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 2015.0.0.0 (2015.0.0.0)

Syntax

C#
public FamilyType NewType(
	string typeName
)
Visual Basic
Public Function NewType ( _
	typeName As String _
) As FamilyType
Visual C++
public:
FamilyType^ NewType(
	String^ typeName
)

Parameters

typeName
Type: System String
The name of new family type.

Examples

Copy C#
public void EditFamilyTypes(Document document, FamilyInstance familyInstance)
{
    // example works best when familyInstance is a rectangular concrete element

    if ((null == document) || (null == familyInstance.Symbol))
    {
        return;   // invalid arguments
    }

    // Get family associated with this
    Family family = familyInstance.Symbol.Family;
    if (null == family)
    {
        return;    // could not get the family
    }

    // Get Family document for family
    Document familyDoc = document.EditFamily(family);
    if (null == familyDoc)
    {
        return;    // could not open a family for edit
    }

    FamilyManager familyManager = familyDoc.FamilyManager;
    if (null == familyManager)
    {
        return;  // cuould not get a family manager
    }

    // Start transaction for the family document
    using (Transaction newFamilyTypeTransaction = new Transaction(familyDoc, "Add Type to Family"))
    {
        int changesMade = 0;
        newFamilyTypeTransaction.Start();

        // add a new type and edit its parameters
        FamilyType newFamilyType = familyManager.NewType("2X2");

        if (newFamilyType != null)
        {
            // look for 'b' and 'h' parameters and set them to 2 feet
            FamilyParameter familyParam = familyManager.get_Parameter("b");
            if (null != familyParam)
            {
                familyManager.Set(familyParam, 2.0);
                changesMade += 1;
            }

            familyParam = familyManager.get_Parameter("h");
            if (null != familyParam)
            {
                familyManager.Set(familyParam, 2.0);
                changesMade += 1;
            }
        }

        if (2 == changesMade)   // set both paramaters?
        {
            newFamilyTypeTransaction.Commit();
        }
        else   // could not make the change -> should roll back 
        {
            newFamilyTypeTransaction.RollBack();
        }

        // if could not make the change or could not commit it, we return
        if (newFamilyTypeTransaction.GetStatus() != TransactionStatus.Committed)
        {
            return;
        }
    }

    // now update the Revit project with Family which has a new type
    LoadOpts loadOptions = new LoadOpts();

    // This overload is necessary for reloading an edited family
    // back into the source document from which it was extracted
    family = familyDoc.LoadFamily(document, loadOptions);
    if (null != family)
    {
        // find the new type and assign it to FamilyInstance
        ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
        foreach (ElementId id in familySymbolIds)
        {
            FamilySymbol familySymbol = family.Document.GetElement(id) as FamilySymbol;
            if ((null != familySymbol) && familySymbol.Name == "2X2")
            {
                using (Transaction changeSymbol = new Transaction(document, "Change Symbol Assignment"))
                {
                    changeSymbol.Start();
                    familyInstance.Symbol = familySymbol;
                    changeSymbol.Commit();
                }
                break;
            }
        }
    }
}

class LoadOpts : IFamilyLoadOptions
{
    public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
    {
        overwriteParameterValues = true;
        return true;
    }

    public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
    {
        source = FamilySource.Family;
        overwriteParameterValues = true;
        return true;
    }
}
Copy VB.NET
Public Sub EditFamilyTypes(document As Document, familyInstance As FamilyInstance)
    ' example works best when familyInstance is a rectangular concrete element


    If (document Is Nothing) OrElse (familyInstance.Symbol Is Nothing) Then
        ' invalid arguments
        Return
    End If

    ' Get family associated with this
    Dim family As Family = familyInstance.Symbol.Family
    If family Is Nothing Then
        ' could not get the family
        Return
    End If

    ' Get Family document for family
    Dim familyDoc As Document = document.EditFamily(family)
    If familyDoc Is Nothing Then
        ' could not open a family for edit
        Return
    End If

    Dim familyManager As FamilyManager = familyDoc.FamilyManager
    If familyManager Is Nothing Then
        ' cuould not get a family manager
        Return
    End If

    ' Start transaction for the family document
    Using newFamilyTypeTransaction As New Transaction(familyDoc, "Add Type to Family")
        Dim changesMade As Integer = 0
        newFamilyTypeTransaction.Start()

        ' add a new type and edit its parameters
        Dim newFamilyType As FamilyType = familyManager.NewType("2X2")

        If newFamilyType IsNot Nothing Then
            ' look for 'b' and 'h' parameters and set them to 2 feet
            Dim familyParam As FamilyParameter = familyManager.Parameter("b")
            If familyParam IsNot Nothing Then
                familyManager.[Set](familyParam, 2.0)
                changesMade += 1
            End If

            familyParam = familyManager.Parameter("h")
            If familyParam IsNot Nothing Then
                familyManager.[Set](familyParam, 2.0)
                changesMade += 1
            End If
        End If

        If 2 = changesMade Then
            ' set both paramaters?
            newFamilyTypeTransaction.Commit()
        Else
            ' could not make the change -> should roll back 
            newFamilyTypeTransaction.RollBack()
        End If

        ' if could not make the change or could not commit it, we return
        If newFamilyTypeTransaction.GetStatus() <> TransactionStatus.Committed Then
            Return
        End If
    End Using

    ' now update the Revit project with Family which has a new type
    Dim loadOptions As New LoadOpts()

    ' This overload is necessary for reloading an edited family
    ' back into the source document from which it was extracted
    family = familyDoc.LoadFamily(document, loadOptions)
    If family IsNot Nothing Then
        ' find the new type and assign it to FamilyInstance
        Dim familySymbolIds As ISet(Of ElementId) = family.GetFamilySymbolIds()
        For Each id As ElementId In familySymbolIds
            Dim familySymbol As FamilySymbol = TryCast(family.Document.GetElement(id), FamilySymbol)
            If (familySymbol IsNot Nothing) AndAlso familySymbol.Name = "2X2" Then
                Using changeSymbol As New Transaction(document, "Change Symbol Assignment")
                    changeSymbol.Start()
                    familyInstance.Symbol = familySymbol
                    changeSymbol.Commit()
                End Using
                Exit For
            End If
        Next
    End If
End Sub

Private Class LoadOpts
    Implements IFamilyLoadOptions
    Public Function OnFamilyFound(familyInUse As Boolean, ByRef overwriteParameterValues As Boolean) As Boolean Implements IFamilyLoadOptions.OnFamilyFound
        overwriteParameterValues = True
        Return True
    End Function

    Public Function OnSharedFamilyFound(sharedFamily As Family, familyInUse As Boolean, ByRef source As FamilySource, ByRef overwriteParameterValues As Boolean) As Boolean Implements IFamilyLoadOptions.OnSharedFamilyFound
        source = FamilySource.Family
        overwriteParameterValues = True
        Return True
    End Function
End Class

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentNullException Thrown when the input argument-"typeName"-is a null reference ( Nothing in Visual Basic) .
Autodesk.Revit.Exceptions ArgumentException Thrown when the input argument-"typeName"-is already in use.
Autodesk.Revit.Exceptions InvalidOperationException Thrown when the family type creation failed.

See Also