From a6a42e5507d1ea3d125c2578e27bfd12d8d5bf7c Mon Sep 17 00:00:00 2001 From: Jonathon Broughton <760691+jsdbroughton@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:08:08 +0100 Subject: [PATCH] fix(Navisworks) - Improve SelectionSet extraction with error handling and documentation (#3608) Improve SelectionSet extraction with error handling and documentation - Refactor `ExtractSelectionSets` method for better reusability - Add comprehensive XML documentation for clarity - Implement null checks to prevent potential exceptions - Optimize LINQ query for `SelectionSet` extraction from GUIDs --- .../Other/SelectionBuilder.cs | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/ConnectorNavisworks/ConnectorNavisworks/Other/SelectionBuilder.cs b/ConnectorNavisworks/ConnectorNavisworks/Other/SelectionBuilder.cs index 80fcc3b514..9cae7b675a 100644 --- a/ConnectorNavisworks/ConnectorNavisworks/Other/SelectionBuilder.cs +++ b/ConnectorNavisworks/ConnectorNavisworks/Other/SelectionBuilder.cs @@ -288,7 +288,12 @@ private HashSet GetObjectsFromSavedSets() // Saved Sets filter stores Guids of the selection sets. This can be converted to ModelItem pseudoIds var selections = _filter.Selection.Select(guid => new Guid(guid)).ToList(); - var savedItems = selections.Select(Application.ActiveDocument.SelectionSets.ResolveGuid).OfType(); + + // Resolve the saved items and extract the inner selection sets when folder items are encountered + var savedItems = selections + .Select(guid => Application.ActiveDocument.SelectionSets.ResolveGuid(guid)) + .SelectMany(ExtractSelectionSets) + .ToList(); foreach (var item in savedItems) { @@ -308,6 +313,45 @@ private HashSet GetObjectsFromSavedSets() return _uniqueModelItems; } + /// + /// Recursively extracts SelectionSet objects from the given item. + /// + /// The object to extract SelectionSets from. Can be a SelectionSet, FolderItem, or any other object. + /// An IEnumerable of SelectionSet objects extracted from the item and its children (if applicable). + /// Thrown if the input item is null. + static IEnumerable ExtractSelectionSets(object selection) + { + if (selection == null) + { + throw new ArgumentNullException(nameof(selection), "Input item cannot be null."); + } + + switch (selection) + { + case SelectionSet selectionSet: + yield return selectionSet; + break; + case FolderItem folderItem: + if (folderItem.Children == null) + { + yield break; + } + foreach (var childItem in folderItem.Children) + { + if (childItem == null) + { + continue; + } + + foreach (var extractedSet in ExtractSelectionSets(childItem)) + { + yield return extractedSet; + } + } + break; + } + } + /// /// Populates the hierarchy by adding ancestor and descendant items to the unique model items. /// The unique model items have already been processed to validate that they are not hidden.