diff --git a/src/Core/Collision/ClashDetectiveIntegration.cs b/src/Core/Collision/ClashDetectiveIntegration.cs
index 0782127..ac86bc2 100644
--- a/src/Core/Collision/ClashDetectiveIntegration.cs
+++ b/src/Core/Collision/ClashDetectiveIntegration.cs
@@ -1186,9 +1186,6 @@ namespace NavisworksTransport
}
- ///
- /// 构建几何对象列表缓存,一次性获取所有几何对象
- ///
///
/// 构建几何对象列表缓存,一次性获取所有几何对象
///
@@ -1197,17 +1194,17 @@ namespace NavisworksTransport
lock (_cacheLock)
{
if (_allGeometryItemsCache != null) return; // 双重检查锁定
-
+
var cacheStopwatch = new System.Diagnostics.Stopwatch();
cacheStopwatch.Start();
-
+
try
{
var allItems = Application.ActiveDocument.Models.RootItemDescendantsAndSelf
.Where(item => item.HasGeometry);
-
+
_allGeometryItemsCache = allItems.ToList();
-
+
cacheStopwatch.Stop();
LogManager.Info($"几何对象列表缓存构建完成,耗时: {cacheStopwatch.ElapsedMilliseconds}ms");
LogManager.Info($" - 缓存对象总数: {_allGeometryItemsCache.Count} 个");
@@ -1219,6 +1216,18 @@ namespace NavisworksTransport
}
}
}
+
+ ///
+ /// 获取几何对象缓存(供外部使用)
+ ///
+ /// 几何对象列表的副本,如果缓存不存在则返回null
+ public static List GetAllGeometryItemsCache()
+ {
+ lock (_cacheLock)
+ {
+ return _allGeometryItemsCache?.ToList(); // 返回副本以保证线程安全
+ }
+ }
///
/// 清除所有缓存,在模型变化时调用
diff --git a/src/Core/Spatial/SpatialIndexManager.cs b/src/Core/Spatial/SpatialIndexManager.cs
index 135cc7f..8a3f8fc 100644
--- a/src/Core/Spatial/SpatialIndexManager.cs
+++ b/src/Core/Spatial/SpatialIndexManager.cs
@@ -86,12 +86,19 @@ namespace NavisworksTransport.Core.Spatial
ClashIntegration.Instance.BuildChannelObjectsCache();
ClashIntegration.BuildAllGeometryItemsCache();
- // 1. 获取所有几何对象
- var allItems = Application.ActiveDocument.Models.RootItemDescendantsAndSelf
- .Where(item => item.HasGeometry)
- .ToList();
+ // 1. 从缓存获取所有几何对象(避免重复遍历)
+ var allItems = ClashIntegration.GetAllGeometryItemsCache();
- LogManager.Info($"[空间索引] 找到 {allItems.Count} 个几何对象(含通道)");
+ if (allItems == null)
+ {
+ // 缓存不存在,手动获取(不应该发生)
+ LogManager.Warning("[空间索引] 几何对象缓存不存在,回退到实时获取");
+ allItems = Application.ActiveDocument.Models.RootItemDescendantsAndSelf
+ .Where(item => item.HasGeometry)
+ .ToList();
+ }
+
+ LogManager.Info($"[空间索引] 从缓存获取 {allItems.Count} 个几何对象(含通道)");
if (allItems.Count == 0)
{