Creates a new instance of a AnalyticalLink element between two Hubs.
Namespace: Autodesk.Revit.DB.Structure
Assembly: RevitAPI (in RevitAPI.dll) Version: 22.0.0.0 (22.1.0.0)
Since:
2013
Syntax
Return Value
The newly created AnalyticalLink instance.
Examples
CopyC#
public void CreateLink(Document doc, FamilyInstance fi1, FamilyInstance fi2)
{
FilteredElementCollector hubCollector = new FilteredElementCollector(doc);
hubCollector.OfClass(typeof(Hub));
ICollection<Element> allHubs = hubCollector.ToElements();
FilteredElementCollector linktypeCollector = new FilteredElementCollector(doc);
linktypeCollector.OfClass(typeof(AnalyticalLinkType));
ElementId firstLinkType = linktypeCollector.ToElementIds().First();
ElementId startHubId = GetHub(fi1.GetAnalyticalModel().Id, allHubs);
ElementId endHubId = GetHub(fi2.GetAnalyticalModel().Id, allHubs);
Transaction tran = new Transaction(doc, "Create Link");
tran.Start();
AnalyticalLink createdLink = AnalyticalLink.Create(doc, firstLinkType, startHubId, endHubId);
tran.Commit();
}
private ElementId GetHub(ElementId hostId, ICollection<Element> allHubs)
{
foreach (Element ehub in allHubs)
{
Hub hub = ehub as Hub;
ConnectorManager manager = hub.GetHubConnectorManager();
ConnectorSet connectors = manager.Connectors;
foreach (Connector connector in connectors)
{
ConnectorSet refConnectors = connector.AllRefs;
foreach (Connector refConnector in refConnectors)
{
if (refConnector.Owner.Id == hostId)
{
return hub.Id;
}
}
}
}
return ElementId.InvalidElementId;
}
CopyVB.NET
Public Sub CreateLink(doc As Document, fi1 As FamilyInstance, fi2 As FamilyInstance)
Dim hubCollector As New FilteredElementCollector(doc)
hubCollector.OfClass(GetType(Hub))
Dim allHubs As ICollection(Of Element) = hubCollector.ToElements()
Dim linktypeCollector As New FilteredElementCollector(doc)
linktypeCollector.OfClass(GetType(AnalyticalLinkType))
Dim firstLinkType As ElementId = linktypeCollector.ToElementIds().First()
Dim startHubId As ElementId = GetHub(fi1.GetAnalyticalModel().Id, allHubs)
Dim endHubId As ElementId = GetHub(fi2.GetAnalyticalModel().Id, allHubs)
Dim tran As New Transaction(doc, "Create Link")
tran.Start()
Dim createdLink As AnalyticalLink = AnalyticalLink.Create(doc, firstLinkType, startHubId, endHubId)
tran.Commit()
End Sub
Private Function GetHub(hostId As ElementId, allHubs As ICollection(Of Element)) As ElementId
For Each ehub As Element In allHubs
Dim hub As Hub = TryCast(ehub, Hub)
Dim manager As ConnectorManager = hub.GetHubConnectorManager()
Dim connectors As ConnectorSet = manager.Connectors
For Each connector As Connector In connectors
Dim refConnectors As ConnectorSet = connector.AllRefs
For Each refConnector As Connector In refConnectors
If refConnector.Owner.Id = hostId Then
Return hub.Id
End If
Next
Next
Next
Return ElementId.InvalidElementId
End Function
Exceptions
See Also