Duct.Create(Document, ElementId, ElementId, Connector, Connector) Method

DuctCreate(Document, ElementId, ElementId, Connector, Connector) Method

Creates a new duct that connects to two connectors.

Namespace: Autodesk.Revit.DB.Mechanical
Assembly: RevitAPI (in RevitAPI.dll) Version: 25.0.0.0 (25.0.0.0)
Syntax
public static Duct Create(
	Document document,
	ElementId ductTypeId,
	ElementId levelId,
	Connector startConnector,
	Connector endConnector
)
Public Shared Function Create ( 
	document As Document,
	ductTypeId As ElementId,
	levelId As ElementId,
	startConnector As Connector,
	endConnector As Connector
) As Duct
public:
static Duct^ Create(
	Document^ document, 
	ElementId^ ductTypeId, 
	ElementId^ levelId, 
	Connector^ startConnector, 
	Connector^ endConnector
)
static member Create : 
        document : Document * 
        ductTypeId : ElementId * 
        levelId : ElementId * 
        startConnector : Connector * 
        endConnector : Connector -> Duct 

Parameters

document  Document
The document.
ductTypeId  ElementId
The ElementId of the new duct type.
levelId  ElementId
The level ElementId for the new duct.
startConnector  Connector
The first connector where the new duct starts.
endConnector  Connector
The second point of the new duct.

Return Value

Duct
The created duct.
Exceptions
ExceptionCondition
ArgumentException The duct type ductTypeId is not valid duct type. -or- The ElementId levelId is not a Level. -or- The connector's domain is not Domain.​DomainHvac. -or- The points of startConnector and endConnector are too close: for MEPCurve, the minimum length is 1/10 inch.
ArgumentNullException A non-optional argument was null
InvalidOperationException Thrown when the new duct fails to connect with the connector.
Remarks
The new duct will have the same diameter and system type as the start connector. The creation will also connect the new duct to two component who owns the specified connectors. If necessary, additional fitting(s) are included to make a valid connection. If the new duct can not be connected to the next component (e.g., mismatched direction, no valid fitting, and etc), the new duct will still be created at the specified connector position, and an InvalidOperationException is thrown.
Example
public Duct CreateDuctBetweenConnectors(UIDocument uiDocument)
{
    // prior to running this example
    // select some mechanical equipment with a supply air connector
    // and an elbow duct fitting with a connector in line with that connector
    ElementId levelId = ElementId.InvalidElementId;
    Connector connector1 = null, connector2 = null;
    ConnectorSetIterator csi = null;
    ICollection<ElementId> selectedIds = uiDocument.Selection.GetElementIds();
    Document document = uiDocument.Document;
    // First find the selected equipment and get the correct connector
    foreach (ElementId id in selectedIds)
    {
        Element e = document.GetElement(id);
        if (e is FamilyInstance)
        {
            FamilyInstance fi = e as FamilyInstance;
            Family family = fi.Symbol.Family;
            if (family.FamilyCategory.Name == "Mechanical Equipment")
            {
                csi = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
                while (csi.MoveNext())
                {
                    Connector conn = csi.Current as Connector;
                    if (conn.Direction == FlowDirectionType.Out && 
                        conn.DuctSystemType == DuctSystemType.SupplyAir)
                    {
                        connector1 = conn;
                        levelId = family.LevelId;
                        break;
                    }
                }
            }
        }
    }
    // next find the second selected item to connect to
    foreach (ElementId id in selectedIds)
    {
        Element e = document.GetElement(id);
        if (e is FamilyInstance)
        {
            FamilyInstance fi = e as FamilyInstance;
            Family family = fi.Symbol.Family;
            if (family.FamilyCategory.Name != "Mechanical Equipment")
            {
                csi = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
                while (csi.MoveNext())
                {
                    if (null == connector2)
                    {
                        Connector conn = csi.Current as Connector;

                        // make sure to choose the connector in line with the first connector
                        if (Math.Abs(conn.Origin.Y - connector1.Origin.Y) < 0.001)
                        {
                            connector2 = conn;
                            break;
                        }
                    }
                }
            }
        }
    }

    Duct duct = null;
    if (null != connector1 && null != connector2 && levelId != ElementId.InvalidElementId)
    {
        // find a duct type
        FilteredElementCollector collector = new FilteredElementCollector(uiDocument.Document);
        collector.OfClass(typeof(DuctType));

        // Use Linq query to make sure it is one of the rectangular duct types
        var query = from element in collector
                    where element.Name.Contains("Mitered Elbows") == true
                    select element;

        // use extension methods to get first duct type
        DuctType ductType = collector.Cast<DuctType>().First<DuctType>();

        if (null != ductType)
        {
            duct = Duct.Create(document, ductType.Id, levelId, connector1, connector2);
        }
    }

    return duct;
}
Public Function CreateDuctBetweenConnectors(uiDocument As UIDocument) As Duct
   ' prior to running this example
   ' select some mechanical equipment with a supply air connector
   ' and an elbow duct fitting with a connector in line with that connector
   Dim levelId As ElementId = ElementId.InvalidElementId
   Dim connector1 As Connector = Nothing, connector2 As Connector = Nothing
   Dim csi As ConnectorSetIterator = Nothing
   Dim selectedIds As ICollection(Of ElementId) = uiDocument.Selection.GetElementIds()
   Dim document As Document = uiDocument.Document
   ' First find the selected equipment and get the correct connector
   For Each id As ElementId In selectedIds
      Dim e As Element = document.GetElement(id)
      If TypeOf e Is FamilyInstance Then
         Dim fi As FamilyInstance = TryCast(e, FamilyInstance)
         Dim family As Family = fi.Symbol.Family
         If family.FamilyCategory.Name = "Mechanical Equipment" Then
            csi = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator()
            While csi.MoveNext()
               Dim conn As Connector = TryCast(csi.Current, Connector)
               If conn.Direction = FlowDirectionType.Out AndAlso conn.DuctSystemType = DuctSystemType.SupplyAir Then
                  levelId = fi.LevelId
                  connector1 = conn
                  Exit While
               End If
            End While
         End If
      End If
   Next
   ' next find the second selected item to connect to
   For Each id As ElementId In selectedIds
      Dim e As Element = document.GetElement(id)
      If TypeOf e Is FamilyInstance Then
         Dim fi As FamilyInstance = TryCast(e, FamilyInstance)
         Dim family As Family = fi.Symbol.Family
         If family.FamilyCategory.Name <> "Mechanical Equipment" Then
            csi = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator()
            While csi.MoveNext()
               If connector2 Is Nothing Then
                  Dim conn As Connector = TryCast(csi.Current, Connector)

                  ' make sure to choose the connector in line with the first connector
                  If Math.Abs(conn.Origin.Y - connector1.Origin.Y) < 0.001 Then
                     connector2 = conn
                     Exit While
                  End If
               End If
            End While
         End If
      End If
   Next

   Dim duct As Duct = Nothing
   If connector1 IsNot Nothing AndAlso connector2 IsNot Nothing AndAlso levelId IsNot ElementId.InvalidElementId Then
      ' find a duct type
      Dim collector As New FilteredElementCollector(uiDocument.Document)
      collector.OfClass(GetType(DuctType))

      ' Use Linq query to make sure it is one of the rectangular duct types
      Dim query As System.Collections.Generic.IEnumerable(Of Autodesk.Revit.DB.Element)
      query = From element In collector _
              Where element.Name.Contains("Mitered Elbows") = True _
              Select element

      ' use extension methods to get first duct type
      Dim ductType As DuctType = collector.Cast(Of DuctType)().First()

      If ductType IsNot Nothing Then
         duct = duct.Create(document, ductType.Id, levelId, connector1, connector2)
      End If
   End If

   Return duct
End Function

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

See Also