Skip to content

Commit

Permalink
Rename DisplayPathHelper.cs to HierarchyHelper.cs
Browse files Browse the repository at this point in the history
- Renamed the file from "DisplayPathHelper.cs" to "HierarchyHelper.cs"
- Updated all references to the renamed file

Refactor helper class for generating display paths
- Refactored the helper class to extract hierarchical context from Navisworks model items in a single traversal
- Changed the method name from "GenerateDisplayPath" to "ExtractContext"
- Modified the method signature to return a tuple of strings (Name, Path)
- Updated the implementation logic accordingly

Improve traversal and extraction logic
- Improved the traversal logic by collecting both name and path information while traversing up the tree once
- Extracted meaningful name and path information from model items in a more efficient way
- Handled cases where model item has no meaningful name or is a geometry node
  • Loading branch information
jsdbroughton committed Jan 16, 2025
1 parent 5339a52 commit ad1b520
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 61 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace Speckle.Converter.Navisworks.Helpers;

/// <summary>
/// Helper class for extracting hierarchical context from Navisworks model items in a single traversal.
/// </summary>
public static class HierarchyHelper
{
private const char PATH_DELIMITER = '>';

/// <summary>
/// Extracts the meaningful name and path from a ModelItem in a single traversal.
/// </summary>
public static (string Name, string Path) ExtractContext(NAV.ModelItem modelItem)
{
if (modelItem == null)
{
throw new ArgumentNullException(nameof(modelItem));
}

var ancestors = new List<string>();
var meaningfulName = string.Empty;
var current = modelItem;

// Start with the root document name if available
if (modelItem.HasModel && !string.IsNullOrEmpty(modelItem.Model.FileName))
{
ancestors.Add(Path.GetFileNameWithoutExtension(modelItem.Model.FileName));
}

// Traverse up the tree once, collecting both name and path information
while (current != null)
{
if (IsMeaningfulNode(current))
{
// First meaningful name we find is our object name (if we haven't found one yet)
if (string.IsNullOrEmpty(meaningfulName))
{
meaningfulName = current.DisplayName;
}
// Add to ancestors if it's not a duplicate
else if (ancestors.Count == 0 || ancestors.Last() != current.DisplayName)
{
ancestors.Add(current.DisplayName);
}
}
current = current.Parent;
}

// Build path excluding the name we found
ancestors.Reverse();

var path = string.Join($" {PATH_DELIMITER} ", ancestors);

return (meaningfulName, path);
}

private static bool IsMeaningfulNode(NAV.ModelItem item) =>
!string.IsNullOrEmpty(item.DisplayName) && (!item.HasGeometry || !string.IsNullOrEmpty(item.ClassDisplayName));
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Geometries\Primitives.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometries\TransformMatrix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ColorConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DisplayPathHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HierarchyHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ElementSelectionHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\PrimitiveProcessor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\PropertyHelpers.cs" />
Expand Down

0 comments on commit ad1b520

Please sign in to comment.