Skip to content

Commit

Permalink
fix(Navisworks) - Improve SelectionSet extraction with error handling…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
jsdbroughton authored Aug 2, 2024
1 parent 1f2fda4 commit a6a42e5
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ private HashSet<ModelItem> 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<SelectionSet>();

// 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)
{
Expand All @@ -308,6 +313,45 @@ private HashSet<ModelItem> GetObjectsFromSavedSets()
return _uniqueModelItems;
}

/// <summary>
/// Recursively extracts SelectionSet objects from the given item.
/// </summary>
/// <param name="selection">The object to extract SelectionSets from. Can be a SelectionSet, FolderItem, or any other object.</param>
/// <returns>An IEnumerable of SelectionSet objects extracted from the item and its children (if applicable).</returns>
/// <exception cref="ArgumentNullException">Thrown if the input item is null.</exception>
static IEnumerable<SelectionSet> 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;
}
}

/// <summary>
/// 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.
Expand Down

0 comments on commit a6a42e5

Please sign in to comment.