diff --git a/doc/design/2026/NavisworksAPI使用方法.md b/doc/design/2026/NavisworksAPI使用方法.md index 445a12f..574050e 100644 --- a/doc/design/2026/NavisworksAPI使用方法.md +++ b/doc/design/2026/NavisworksAPI使用方法.md @@ -2738,3 +2738,5 @@ models.SetModelUnitsAndTransform(model, units , newTransform3D, true); ### 如何唯一标识一个ModelItem 使用 ModelItem.GetHashCode() 作为唯一标识符 +使用 HashSet 来存储和比较碰撞对象,这样可以正确使用 ModelItem.Equals() 和 GetHashCode() 方法,避免哈希冲突和跨运行时不一致的问题 + diff --git a/src/Core/Animation/PathAnimationManager.cs b/src/Core/Animation/PathAnimationManager.cs index 913e03b..ef55059 100644 --- a/src/Core/Animation/PathAnimationManager.cs +++ b/src/Core/Animation/PathAnimationManager.cs @@ -105,7 +105,7 @@ namespace NavisworksTransport.Core.Animation private List _allCollisionResults; // 所有碰撞结果(不去重) private bool _lastHighlightState = false; // 上一帧的高亮状态 - private HashSet _lastCollisionObjectIds = new HashSet(); // 上一帧碰撞对象的ID集合 + private HashSet _lastCollisionObjects = new HashSet(); // 上一帧碰撞对象的集合 // === 路径相关 === private PathRoute _route = null; // 路径引用 @@ -978,7 +978,7 @@ namespace NavisworksTransport.Core.Animation _fpsCounterStart = DateTime.Now; _frameInterval = 1000.0 / _animationFrameRate; _lastHighlightState = false; // 重置高亮状态 - _lastCollisionObjectIds.Clear(); // 重置碰撞对象ID集合 + _lastCollisionObjects.Clear(); // 重置碰撞对象集合 // 启动动画播放 StartAnimationPlayback(); @@ -2327,8 +2327,8 @@ namespace NavisworksTransport.Core.Animation var currentFrame = _animationFrames[_currentFrameIndex]; bool currentHasCollision = currentFrame.HasCollision; - // 收集当前帧碰撞对象的ID集合 - var currentCollisionObjectIds = new HashSet(); + // 收集当前帧碰撞对象的集合 + var currentCollisionObjects = new HashSet(); if (currentHasCollision && currentFrame.Collisions != null) { foreach (var collision in currentFrame.Collisions) @@ -2337,9 +2337,9 @@ namespace NavisworksTransport.Core.Animation var item2 = collision.GetValidItem2(); if (item1 != null) - currentCollisionObjectIds.Add(item1.GetHashCode()); + currentCollisionObjects.Add(item1); if (item2 != null) - currentCollisionObjectIds.Add(item2.GetHashCode()); + currentCollisionObjects.Add(item2); } } @@ -2347,7 +2347,7 @@ namespace NavisworksTransport.Core.Animation // 1. 碰撞状态改变(有碰撞 vs 无碰撞) // 2. 碰撞对象集合改变(碰撞的物体变了) bool stateChanged = currentHasCollision != _lastHighlightState; - bool objectsChanged = !currentCollisionObjectIds.SetEquals(_lastCollisionObjectIds); + bool objectsChanged = !currentCollisionObjects.SetEquals(_lastCollisionObjects); if (stateChanged || objectsChanged) { @@ -2355,7 +2355,7 @@ namespace NavisworksTransport.Core.Animation { // 有碰撞:高亮当前帧的碰撞对象 ClashDetectiveIntegration.Instance.HighlightCollisions(currentFrame.Collisions); - LogManager.Debug($"[高亮状态] 帧{_currentFrameIndex}: 高亮碰撞 ({currentFrame.Collisions.Count}个, 对象{currentCollisionObjectIds.Count}个)"); + LogManager.Debug($"[高亮状态] 帧{_currentFrameIndex}: 高亮碰撞 ({currentFrame.Collisions.Count}个, 对象{currentCollisionObjects.Count}个)"); } else { @@ -2366,7 +2366,7 @@ namespace NavisworksTransport.Core.Animation // 更新状态记录 _lastHighlightState = currentHasCollision; - _lastCollisionObjectIds = currentCollisionObjectIds; + _lastCollisionObjects = currentCollisionObjects; } // 每次都触发碰撞事件(用于UI更新等)