Accessing Element IDs of Elements Connected to a Hub

By Saikat Bhattacharya

Hub class, as the Revit API chm file states, represents a connection between two or more Autodesk Revit Elements. So rather than connecting the two elements directly, the connection is created between two Hubs. Elements connected via a Hub do not refer directly to each other – they each refer to the Hub that keeps all the connectivity information. And the class is contained in the Autodesk.Revit.DB.Structure namespace.

To access the element Ids of the elements that are connected to a Hub, the approach is to use the connectors using the ConnectorManager from a given Hub and then use the AllRefs to get access to all the connected connectors and use the Owner property on each of the linked/joined connectors to get access to the elements that are connected. The code snippet illustrates this approach.

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
 
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
 
namespace Revit.SDK.Samples.HelloRevit.CS
{
  [Transaction(TransactionMode.Manual)]
  public class Command : IExternalCommand
  {
    public Result Execute(ExternalCommandData commandData,
      ref string message,
      ElementSet elements)
    {
      UIApplication uiApp = commandData.Application;
      Document doc = uiApp.ActiveUIDocument.Document;
 
      FilteredElementCollector coll = new FilteredElementCollector(doc);
      String str = String.Empty;
      foreach (Autodesk.Revit.DB.Structure.Hub hub 
        in 
        coll.OfClass(typeof(Autodesk.Revit.DB.Structure.Hub)))
      {
        if (null != hub)
        {
          ConnectorManager mgr = hub.GetHubConnectorManager();
          foreach (Connector conn in mgr.Connectors)
          {
            ConnectorSet joinedConnectors = conn.AllRefs;
            foreach (Connector joinedConn in joinedConnectors)
            {
              str = str + "Hub (Id) " 
                + hub.Id.ToString() 
                + " is connected to Element (Id) " 
                + joinedConn.Owner.Id.ToString() 
                + "n";
            }
 
          }
        }
      }
      TaskDialog.Show("Hub connection details", str);
 
      return Result.Succeeded;
    }
  }         
}

The output of the code:

image


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading