Creates a new duct that connects to two connectors.
Namespace: Autodesk.Revit.DB.Mechanical
Assembly: RevitAPI (in RevitAPI.dll) Version: 22.0.0.0 (22.1.0.0)
Since: 2017
Syntax
C# |
---|
|
Visual Basic |
---|
|
Visual C++ |
---|
|
Parameters
- document
- Type: Autodesk.Revit.DB Document
The document.
- ductTypeId
- Type: Autodesk.Revit.DB ElementId
The ElementId of the new duct type.
- levelId
- Type: Autodesk.Revit.DB ElementId
The level ElementId for the new duct.
- startConnector
- Type: Autodesk.Revit.DB Connector
The first connector where the new duct starts.
- endConnector
- Type: Autodesk.Revit.DB Connector
The second point of the new duct.
Return Value
The created duct.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.
Examples

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
Exceptions
Exception | Condition |
---|---|
Autodesk.Revit.Exceptions 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. |
Autodesk.Revit.Exceptions ArgumentNullException | A non-optional argument was null |
Autodesk.Revit.Exceptions DisabledDisciplineException | None of the following disciplines is enabled: Mechanical Electrical Piping. |
Autodesk.Revit.Exceptions InvalidOperationException | Thrown when the new duct fails to connect with the connector. |