Adds a new duct into the document, using two connectors and duct type.
Namespace: Autodesk.Revit.Creation
Assembly: RevitAPI (in RevitAPI.dll) Version: 16.0.0.0 (16.0.0.0)
Syntax
C# |
---|
|
Visual Basic |
---|
|
Visual C++ |
---|
|
Parameters
- connector1
- Type: Autodesk.Revit.DB Connector
The first connector to be connected to the duct.
- connector2
- Type: Autodesk.Revit.DB Connector
The second connector to be connected to the duct.
- ductType
- Type: Autodesk.Revit.DB.Mechanical DuctType
The type of the duct.
Return Value
If creation was successful then a new duct is returned, otherwise an exception with failure information will be thrown.Remarks
If the connectors are fitting or equipment connectors of the correct domain, and if the connectors' direction match the direction of the duct to be created, the connectors will be automatically connected. A transition fitting will be added at the connector(s) if necessary. If the connector's type, domain, or direction does not match the direction of the duct to be created, the position of the input connectors will govern the endpoints of the duct, but no connection will be established.
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
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;
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)
{
// 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 = uiDocument.Document.Create.NewDuct(connector1, connector2, ductType);
}
}
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 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
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 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 = uiDocument.Document.Create.NewDuct(connector1, connector2, ductType)
End If
End If
Return duct
End Function
Exceptions
Exception | Condition |
---|---|
Autodesk.Revit.Exceptions ArgumentNullException | Thrown when the input argument connector1 or connector2 is a null reference ( Nothing in Visual Basic) . |
Autodesk.Revit.Exceptions InvalidOperationException | Thrown when the duct cannot be created or regenerate fails. |
Autodesk.Revit.Exceptions ArgumentException | Thrown if the duct type does not exist in the given document. |