Create Method


Creates a new Truss.

Namespace: Autodesk.Revit.DB.Structure
Assembly: RevitAPI (in RevitAPI.dll) Version: 17.0.0.0 (17.0.484.0)
Since: 2014

Syntax

C#
public static Truss Create(
	Document document,
	ElementId trussTypeId,
	ElementId sketchPlaneId,
	Curve curve
)
Visual Basic
Public Shared Function Create ( _
	document As Document, _
	trussTypeId As ElementId, _
	sketchPlaneId As ElementId, _
	curve As Curve _
) As Truss
Visual C++
public:
static Truss^ Create(
	Document^ document, 
	ElementId^ trussTypeId, 
	ElementId^ sketchPlaneId, 
	Curve^ curve
)

Parameters

document
Type: Autodesk.Revit.DB Document
The document in which the new Truss is created.
trussTypeId
Type: Autodesk.Revit.DB ElementId
Element id of the truss type.
sketchPlaneId
Type: Autodesk.Revit.DB ElementId
Element id of a SketchPlane.
curve
Type: Autodesk.Revit.DB Curve
The curve of the truss element. It must be a line, must not be a vertical line, and must be within the sketch plane.

Examples

Copy C#
Truss CreateTruss(Autodesk.Revit.DB.Document document, 
    FamilyInstance column1, FamilyInstance column2)
{
    Truss truss = null;
    using (Transaction transaction = new Transaction(document, "Add Truss"))
    {
       if (transaction.Start() == TransactionStatus.Started)
       {
           //sketchPlane
           XYZ origin = new XYZ(0, 0, 0);
           XYZ xDirection = new XYZ(1, 0, 0);
           XYZ yDirection = new XYZ(0, 1, 0);
           XYZ zDirection = new XYZ(0, 0, 1);
           Plane plane = Plane.Create(new Frame(origin, xDirection, yDirection, zDirection));
           SketchPlane sketchPlane = SketchPlane.Create (document, plane);

           //new base Line - use line that spans two selected columns
           AnalyticalModel frame1 = column1.GetAnalyticalModel() as AnalyticalModel;
           XYZ centerPoint1 = (frame1.GetCurve() as Line).GetEndPoint(0);

           AnalyticalModel frame2 = column2.GetAnalyticalModel() as AnalyticalModel;
           XYZ centerPoint2 = (frame2.GetCurve() as Line).GetEndPoint(0);

           XYZ startPoint = new XYZ(centerPoint1.X, centerPoint1.Y, 0);
           XYZ endPoint = new XYZ(centerPoint2.X, centerPoint2.Y, 0);
           Autodesk.Revit.DB.Line baseLine = null;

           try
           {
               baseLine = Line.CreateBound(startPoint, endPoint);
           }
           catch (System.ArgumentException)
           {
               throw new Exception("Selected columns are too close to create truss.");
           }

           // use the active view for where the truss's tag will be placed; View used in
           // NewTruss should be plan or elevation view parallel to the truss's base line 
           Autodesk.Revit.DB.View view = document.ActiveView;

           // Get a truss type for the truss
           FilteredElementCollector collector = new FilteredElementCollector(document);
           collector.OfClass(typeof(FamilySymbol));
           collector.OfCategory(BuiltInCategory.OST_Truss);

           TrussType trussType = collector.FirstElement() as TrussType;

           if (null != trussType)
           {
               truss = Truss.Create(document, trussType.Id, sketchPlane.Id, baseLine);
               transaction.Commit();
           }
           else
           {
               transaction.RollBack();
               throw new Exception("No truss types found in document.");
           }
       }
    }

    return truss;
}
Copy VB.NET
Private Function CreateTruss(document As Autodesk.Revit.DB.Document, column1 As FamilyInstance, column2 As FamilyInstance) As Truss
    Dim truss__1 As Truss = Nothing
    Using transaction As New Transaction(document, "Add Truss")
        If transaction.Start() = TransactionStatus.Started Then
            'sketchPlane
            Dim origin As New XYZ(0, 0, 0)
            Dim xDirection As New XYZ(1, 0, 0)
       Dim yDirection As New XYZ(0, 1, 0)
       Dim zDirection As New XYZ(0, 0, 1)
       Dim planeFrame As New Autodesk.Revit.DB.Frame(origin, xDirection, yDirection, zDirection)
       Dim plane As Plane = Autodesk.Revit.DB.Plane.Create(planeFrame)
       Dim sketchPlane__2 As SketchPlane = SketchPlane.Create(document, plane)

            'new base Line - use line that spans two selected columns
            Dim frame1 As AnalyticalModel = TryCast(column1.GetAnalyticalModel(), AnalyticalModel)
            Dim centerPoint1 As XYZ = TryCast(frame1.GetCurve(), Line).GetEndPoint(0)

            Dim frame2 As AnalyticalModel = TryCast(column2.GetAnalyticalModel(), AnalyticalModel)
            Dim centerPoint2 As XYZ = TryCast(frame2.GetCurve(), Line).GetEndPoint(0)

            Dim startPoint As New XYZ(centerPoint1.X, centerPoint1.Y, 0)
            Dim endPoint As New XYZ(centerPoint2.X, centerPoint2.Y, 0)
            Dim baseLine As Autodesk.Revit.DB.Line = Nothing

            Try
                baseLine = Line.CreateBound(startPoint, endPoint)
            Catch generatedExceptionName As System.ArgumentException
                Throw New Exception("Selected columns are too close to create truss.")
            End Try

            ' use the active view for where the truss's tag will be placed; View used in
            ' NewTruss should be plan or elevation view parallel to the truss's base line 
            Dim view As Autodesk.Revit.DB.View = document.ActiveView

            ' Get a truss type for the truss
            Dim collector As New FilteredElementCollector(document)
            collector.OfClass(GetType(FamilySymbol))
            collector.OfCategory(BuiltInCategory.OST_Truss)

            Dim trussType As TrussType = TryCast(collector.FirstElement(), TrussType)

            If trussType IsNot Nothing Then
                truss__1 = Truss.Create(document, trussType.Id, sketchPlane__2.Id, baseLine)
                transaction.Commit()
            Else
                transaction.RollBack()
                Throw New Exception("No truss types found in document.")
            End If
        End If
    End Using

    Return truss__1
End Function

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentException The input curve points to a helical curve and is not supported for this operation. -or- The element id should refer to a valid TrussType. -or- The element id should refer to a valid SketchPlane. -or- The curve is invalid to be the base curve of a truss.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was NULL
Autodesk.Revit.Exceptions InvalidOperationException This function is only enabled in Revit Structure and Revit Architecture. -or- Failed to create Truss element.

See Also