2027 Method
Changes 0
M

AnalyticalLink.Create

Description:
Creates a new instance of a AnalyticalLink element between two Hubs.
public static AnalyticalLink Create(
	Document doc,
	ElementId type,
	ElementId startHubId,
	ElementId endHubId
)
  • doc
    Document to which new AnalyticalLink should be added.
  • type
    AnalyticalLinkType for the new AnalyticalLink.
  • startHubId
    Hub at start of AnalyticalLink.
  • endHubId
    Hub at end of AnalyticalLink.
Return Value AnalyticalLink The newly created AnalyticalLink instance.
public void CreateLink(Document doc, AnalyticalElement fi1, AnalyticalElement fi2)
{
    FilteredElementCollector hubCollector = new FilteredElementCollector(doc);
    hubCollector.OfClass(typeof(Hub));  //Get all hubs
    ICollection<Element> allHubs = hubCollector.ToElements();
    FilteredElementCollector linktypeCollector = new FilteredElementCollector(doc);
    linktypeCollector.OfClass(typeof(AnalyticalLinkType));
    ElementId firstLinkType = linktypeCollector.ToElementIds().First();  //Get the first analytical link type.  
    // Get hub Ids from two selected family instance items
    ElementId startHubId = GetHub(fi1.Id, allHubs); 
    ElementId endHubId = GetHub(fi2.Id, allHubs);
    Transaction tran = new Transaction(doc, "Create Link");
    tran.Start();
    //Create a link between these two hubs.
    AnalyticalLink createdLink = AnalyticalLink.Create(doc, firstLinkType, startHubId, endHubId);  
    tran.Commit();
}

//Get the first Hub on a given AnalyticalModel element
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;
}
  • startHubId is not a valid Hub ID for an AnalyticalLink element. -or- endHubId is not a valid Hub ID for an AnalyticalLink element. -or- Thrown if startHubId or endHubId do not represent ids of Hubs. -or- Thrown if startHubId == endHubId. -or- Thrown if type does not represent an id of an AnalyticalLinkType.
  • A non-optional argument was null