Creates a new Truss.
Namespace: Autodesk.Revit.DB.StructureAssembly: RevitAPI (in RevitAPI.dll) Version: 27.0.4.0 (27.0.4.0)
Syntaxpublic static Truss Create(
Document document,
ElementId trussTypeId,
ElementId sketchPlaneId,
Curve curve
)
Public Shared Function Create (
document As Document,
trussTypeId As ElementId,
sketchPlaneId As ElementId,
curve As Curve
) As Truss
public:
static Truss^ Create(
Document^ document,
ElementId^ trussTypeId,
ElementId^ sketchPlaneId,
Curve^ curve
)
static member Create :
document : Document *
trussTypeId : ElementId *
sketchPlaneId : ElementId *
curve : Curve -> Truss Parameters
- document Document
-
The document in which the new Truss is created.
- trussTypeId ElementId
-
Element id of the truss type.
- sketchPlaneId ElementId
-
Element id of a SketchPlane.
- curve 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.
Return Value
Truss
Exceptions| Exception | Condition |
|---|
| 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.
|
| ArgumentNullException |
A non-optional argument was null
|
| InvalidOperationException |
This function is only enabled in Revit Structure and Revit Architecture.
-or-
Failed to create Truss element.
|
ExampleTruss 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)
{
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);
AnalyticalElement frame1 = document.GetElement(
AnalyticalToPhysicalAssociationManager.GetAnalyticalToPhysicalAssociationManager(document)
.GetAssociatedElementId(column1.Id))
as AnalyticalElement;
XYZ centerPoint1 = (frame1.GetCurve() as Line).GetEndPoint(0);
AnalyticalElement frame2 = document.GetElement(
AnalyticalToPhysicalAssociationManager.GetAnalyticalToPhysicalAssociationManager(document)
.GetAssociatedElementId(column2.Id))
as AnalyticalElement;
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.");
}
Autodesk.Revit.DB.View view = document.ActiveView;
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;
}
See Also