From c9e77f793e6d7dec2b96366f106396614577b4e5 Mon Sep 17 00:00:00 2001 From: Michal Pekacki Date: Wed, 17 Apr 2024 15:32:25 +0200 Subject: [PATCH 1/5] connected elements duplicate fix --- Revit_Core_Engine/Query/ConnectedElements.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Revit_Core_Engine/Query/ConnectedElements.cs b/Revit_Core_Engine/Query/ConnectedElements.cs index 02851ba3a..414b6804e 100644 --- a/Revit_Core_Engine/Query/ConnectedElements.cs +++ b/Revit_Core_Engine/Query/ConnectedElements.cs @@ -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 ConnectedElements(this Element element) @@ -61,7 +61,7 @@ public static List ConnectedElements(this Element element) return null; } - HashSet connectedElements = new HashSet(); + HashSet connectedElementIds = new HashSet(); foreach (Connector conn in connSet) { ConnectorSet allRefs = conn.AllRefs; @@ -69,11 +69,13 @@ public static List ConnectedElements(this Element element) { Element el = refConn?.Owner; if (el != null) - connectedElements.Add(el); + connectedElementIds.Add(el.Id.IntegerValue); } } - return connectedElements.ToList(); + Document doc = element.Document; + + return connectedElementIds.Select(x => doc.GetElement(new ElementId(x))).ToList(); } /***************************************************/ From 28d47987140c3b255c1d6154e7756cb36ff75666 Mon Sep 17 00:00:00 2001 From: Michal Pekacki Date: Wed, 17 Apr 2024 15:49:25 +0200 Subject: [PATCH 2/5] remove duplicates and host element fix --- Revit_Core_Engine/Query/ConnectedElements.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Revit_Core_Engine/Query/ConnectedElements.cs b/Revit_Core_Engine/Query/ConnectedElements.cs index 414b6804e..addd4b2fa 100644 --- a/Revit_Core_Engine/Query/ConnectedElements.cs +++ b/Revit_Core_Engine/Query/ConnectedElements.cs @@ -61,7 +61,7 @@ public static List ConnectedElements(this Element element) return null; } - HashSet connectedElementIds = new HashSet(); + List connectedElements = new List(); foreach (Connector conn in connSet) { ConnectorSet allRefs = conn.AllRefs; @@ -69,13 +69,14 @@ public static List ConnectedElements(this Element element) { Element el = refConn?.Owner; if (el != null) - connectedElementIds.Add(el.Id.IntegerValue); + connectedElements.Add(el); } } - Document doc = element.Document; + //remove duplicates and host element + connectedElements = connectedElements.Where(x => x.Id.IntegerValue != element.Id.IntegerValue).GroupBy(x => x.Id.IntegerValue).Select(x => x.First()).ToList(); - return connectedElementIds.Select(x => doc.GetElement(new ElementId(x))).ToList(); + return connectedElements; } /***************************************************/ From 725cdf8def0413fbaf9fe599440af84d45a3993f Mon Sep 17 00:00:00 2001 From: Michal Pekacki Date: Fri, 19 Apr 2024 12:37:24 +0200 Subject: [PATCH 3/5] connectors method added --- Revit_Core_Engine/Query/ConnectedElements.cs | 20 +---- Revit_Core_Engine/Query/Connectors.cs | 87 ++++++++++++++++++++ 2 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 Revit_Core_Engine/Query/Connectors.cs diff --git a/Revit_Core_Engine/Query/ConnectedElements.cs b/Revit_Core_Engine/Query/ConnectedElements.cs index addd4b2fa..febdc1c5d 100644 --- a/Revit_Core_Engine/Query/ConnectedElements.cs +++ b/Revit_Core_Engine/Query/ConnectedElements.cs @@ -45,24 +45,10 @@ public static List 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 connectors = element.Connectors(); List connectedElements = new List(); - foreach (Connector conn in connSet) + + foreach (Connector conn in connectors) { ConnectorSet allRefs = conn.AllRefs; foreach (Connector refConn in allRefs) diff --git a/Revit_Core_Engine/Query/Connectors.cs b/Revit_Core_Engine/Query/Connectors.cs new file mode 100644 index 000000000..2e76c0471 --- /dev/null +++ b/Revit_Core_Engine/Query/Connectors.cs @@ -0,0 +1,87 @@ +/* + * 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 . + */ + +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("cconnectors", "MEP Element connectors.")] + public static List Connectors(this Element element) + { + return SortedConnectors(element as dynamic); + } + + /***************************************************/ + + private static List SortedConnectors(this MEPCurve mepCurve) + { + XYZ startPoint = (mepCurve.Location as LocationCurve).Curve.GetEndPoint(0); + ConnectorSet connSet = mepCurve.ConnectorManager?.Connectors; + + List connList = new List(); + foreach (Connector conn in connSet) + connList.Add(conn); + + connList = connList.OrderBy(x => x.Origin.DistanceTo(startPoint)).ToList(); + + return connList; + } + + /***************************************************/ + + private static List SortedConnectors(this FamilyInstance familyInstance) + { + ConnectorSet connSet = familyInstance.MEPModel?.ConnectorManager?.Connectors; + + List connList = new List(); + foreach (Connector conn in connSet) + connList.Add(conn); + + connList = connList.OrderByDescending(x => x.GetMEPConnectorInfo().IsPrimary).ToList(); + + return connList; + } + + /***************************************************/ + + private static List SortedConnectors(this Element element) + { + BH.Engine.Base.Compute.RecordError("Input element is not supported by the method. Check if element is a valid MEP object."); + return null; + } + + /***************************************************/ + } +} + From 8f93dde2adbd429041c339e2342c99302e5b1b7c Mon Sep 17 00:00:00 2001 From: Michal Pekacki Date: Mon, 22 Apr 2024 09:26:28 +0200 Subject: [PATCH 4/5] removed sorting by primary connector --- Revit_Core_Engine/Query/Connectors.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Revit_Core_Engine/Query/Connectors.cs b/Revit_Core_Engine/Query/Connectors.cs index 2e76c0471..441e3f8d3 100644 --- a/Revit_Core_Engine/Query/Connectors.cs +++ b/Revit_Core_Engine/Query/Connectors.cs @@ -68,8 +68,6 @@ private static List SortedConnectors(this FamilyInstance familyInstan foreach (Connector conn in connSet) connList.Add(conn); - connList = connList.OrderByDescending(x => x.GetMEPConnectorInfo().IsPrimary).ToList(); - return connList; } From d23460ecd44ae0ca7a7d11627fa8d1daf197b7fe Mon Sep 17 00:00:00 2001 From: Michal Pekacki Date: Mon, 22 Apr 2024 14:46:23 +0200 Subject: [PATCH 5/5] update after @pawelbaran comments --- Revit_Core_Engine/Query/ConnectedElements.cs | 11 +++++------ Revit_Core_Engine/Query/Connectors.cs | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Revit_Core_Engine/Query/ConnectedElements.cs b/Revit_Core_Engine/Query/ConnectedElements.cs index febdc1c5d..8ecf8a5d6 100644 --- a/Revit_Core_Engine/Query/ConnectedElements.cs +++ b/Revit_Core_Engine/Query/ConnectedElements.cs @@ -46,7 +46,7 @@ public static List ConnectedElements(this Element element) } List connectors = element.Connectors(); - List connectedElements = new List(); + HashSet connectedElements = new HashSet(); foreach (Connector conn in connectors) { @@ -54,15 +54,14 @@ public static List ConnectedElements(this Element element) 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); } } - //remove duplicates and host element - connectedElements = connectedElements.Where(x => x.Id.IntegerValue != element.Id.IntegerValue).GroupBy(x => x.Id.IntegerValue).Select(x => x.First()).ToList(); + Document doc = element.Document; - return connectedElements; + return connectedElements.Select(x => doc.GetElement(x)).ToList(); } /***************************************************/ diff --git a/Revit_Core_Engine/Query/Connectors.cs b/Revit_Core_Engine/Query/Connectors.cs index 441e3f8d3..5283dea1e 100644 --- a/Revit_Core_Engine/Query/Connectors.cs +++ b/Revit_Core_Engine/Query/Connectors.cs @@ -36,20 +36,25 @@ public static partial class Query [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("cconnectors", "MEP Element connectors.")] + [Output("connectors", "MEP Element connectors.")] public static List Connectors(this Element element) { return SortedConnectors(element as dynamic); } + /***************************************************/ + /**** Private methods ****/ /***************************************************/ private static List SortedConnectors(this MEPCurve mepCurve) { XYZ startPoint = (mepCurve.Location as LocationCurve).Curve.GetEndPoint(0); ConnectorSet connSet = mepCurve.ConnectorManager?.Connectors; - List connList = new List(); + + if (connSet == null) + return connList; + foreach (Connector conn in connSet) connList.Add(conn); @@ -63,8 +68,11 @@ private static List SortedConnectors(this MEPCurve mepCurve) private static List SortedConnectors(this FamilyInstance familyInstance) { ConnectorSet connSet = familyInstance.MEPModel?.ConnectorManager?.Connectors; - List connList = new List(); + + if (connSet == null) + return connList; + foreach (Connector conn in connSet) connList.Add(conn); @@ -75,7 +83,7 @@ private static List SortedConnectors(this FamilyInstance familyInstan private static List SortedConnectors(this Element element) { - BH.Engine.Base.Compute.RecordError("Input element is not supported by the method. Check if element is a valid MEP object."); + 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; }