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
)

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