Skip to content

Commit

Permalink
ConnectedElements - duplicate elements bugfix, Connectors method …
Browse files Browse the repository at this point in the history
…added (#1475)
  • Loading branch information
pawelbaran authored May 9, 2024
2 parents 1fae6b8 + d23460e commit 4bea6f1
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 21 deletions.
30 changes: 9 additions & 21 deletions Revit_Core_Engine/Query/ConnectedElements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static partial class Query
/**** Public methods ****/
/***************************************************/

[Description("Returns all elements connected to the MEP element. Works for Family Instances and MEPCurve objects.")]
[Description("Returns elements connected directly to the MEP element connectors. Works for Family Instances and MEPCurve objects.")]
[Input("element", "MEP element to get the connected elements for.")]
[Output("connectedElements", "List of the elements that are connected to the input element.")]
public static List<Element> ConnectedElements(this Element element)
Expand All @@ -45,35 +45,23 @@ public static List<Element> ConnectedElements(this Element element)
return null;
}

ConnectorSet connSet = null;
if (element is FamilyInstance)
{
connSet = (element as FamilyInstance).MEPModel?.ConnectorManager?.Connectors;
}
else if (element is MEPCurve)
{
connSet = (element as MEPCurve).ConnectorManager?.Connectors;
}

if (connSet == null)
{
BH.Engine.Base.Compute.RecordError("Input element is not supported by the method. Check if element is a valid MEP object.");
return null;
}
List<Connector> connectors = element.Connectors();
HashSet<ElementId> connectedElements = new HashSet<ElementId>();

HashSet<Element> connectedElements = new HashSet<Element>();
foreach (Connector conn in connSet)
foreach (Connector conn in connectors)
{
ConnectorSet allRefs = conn.AllRefs;
foreach (Connector refConn in allRefs)
{
Element el = refConn?.Owner;
if (el != null)
connectedElements.Add(el);
if (el != null && el.Id != element.Id)
connectedElements.Add(el.Id);
}
}

return connectedElements.ToList();
Document doc = element.Document;

return connectedElements.Select(x => doc.GetElement(x)).ToList();
}

/***************************************************/
Expand Down
93 changes: 93 additions & 0 deletions Revit_Core_Engine/Query/Connectors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using Autodesk.Revit.DB;
using BH.oM.Base.Attributes;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace BH.Revit.Engine.Core
{
public static partial class Query
{
/***************************************************/
/**** Public methods ****/
/***************************************************/

[Description("Returns connectors of the MEP element. MEPCurve connectors are sorted by distance from the startpoint, FamilyInstace connectors by IsPrimary property.")]
[Input("element", "MEP element to get the connectors from.")]
[Output("connectors", "MEP Element connectors.")]
public static List<Connector> Connectors(this Element element)
{
return SortedConnectors(element as dynamic);
}

/***************************************************/
/**** Private methods ****/
/***************************************************/

private static List<Connector> SortedConnectors(this MEPCurve mepCurve)
{
XYZ startPoint = (mepCurve.Location as LocationCurve).Curve.GetEndPoint(0);
ConnectorSet connSet = mepCurve.ConnectorManager?.Connectors;
List<Connector> connList = new List<Connector>();

if (connSet == null)
return connList;

foreach (Connector conn in connSet)
connList.Add(conn);

connList = connList.OrderBy(x => x.Origin.DistanceTo(startPoint)).ToList();

return connList;
}

/***************************************************/

private static List<Connector> SortedConnectors(this FamilyInstance familyInstance)
{
ConnectorSet connSet = familyInstance.MEPModel?.ConnectorManager?.Connectors;
List<Connector> connList = new List<Connector>();

if (connSet == null)
return connList;

foreach (Connector conn in connSet)
connList.Add(conn);

return connList;
}

/***************************************************/

private static List<Connector> SortedConnectors(this Element element)
{
BH.Engine.Base.Compute.RecordError("Input element is not supported by the connector query method. Check if element is a valid MEP object.");
return null;
}

/***************************************************/
}
}

0 comments on commit 4bea6f1

Please sign in to comment.