Trusses

Truss

The Truss class represents all types of trusses in Revit. The TrussType property indicates the type of truss.

Code Region 29-7: Creating a truss over two columns

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;
}