Create Method


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

C#
public static AnalyticalLink Create(
	Document doc,
	ElementId type,
	ElementId startHubId,
	ElementId endHubId
)
Visual Basic
Public Shared Function Create ( _
	doc As Document, _
	type As ElementId, _
	startHubId As ElementId, _
	endHubId As ElementId _
) As AnalyticalLink
Visual C++
public:
static AnalyticalLink^ Create(
	Document^ doc, 
	ElementId^ type, 
	ElementId^ startHubId, 
	ElementId^ endHubId
)

Parameters

doc
Type: Autodesk.Revit.DB Document
Document to which new AnalyticalLink should be added.
type
Type: Autodesk.Revit.DB ElementId
AnalyticalLinkType for the new AnalyticalLink.
startHubId
Type: Autodesk.Revit.DB ElementId
Hub at start of AnalyticalLink.
endHubId
Type: Autodesk.Revit.DB ElementId
Hub at end of AnalyticalLink.

Return Value

The newly created AnalyticalLink instance.

Examples

Copy C#
public void CreateLink(Document doc, FamilyInstance fi1, FamilyInstance 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.GetAnalyticalModel().Id, allHubs); 
    ElementId endHubId = GetHub(fi2.GetAnalyticalModel().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;
}
Copy VB.NET
Public Sub CreateLink(doc As Document, fi1 As FamilyInstance, fi2 As FamilyInstance)
    Dim hubCollector As New FilteredElementCollector(doc)
    hubCollector.OfClass(GetType(Hub))
    'Get all hubs
    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()
    'Get the first analytical link type.  
    ' Get hub Ids from two selected family instance items
    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()
    'Create a link between these two hubs.
    Dim createdLink As AnalyticalLink = AnalyticalLink.Create(doc, firstLinkType, startHubId, endHubId)
    tran.Commit()
End Sub

'Get the first Hub on a given AnalyticalModel element
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

Exception Condition
Autodesk.Revit.Exceptions ArgumentException 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.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was null
Autodesk.Revit.Exceptions DisabledDisciplineException None of the following disciplines is enabled: Structural.

See Also

ArchiLabs Desktop

Put AI to work inside Revit.

Give Desktop your instructions and project files to build Revit families, update model elements, and create views and schedules. Works directly in Revit and AutoCAD.

Explore Desktop →