纠正ModelItem比较的使用方法

This commit is contained in:
tian 2026-01-06 13:00:48 +08:00
parent be174ab6bb
commit aa0fdc2cec
2 changed files with 11 additions and 9 deletions

View File

@ -2738,3 +2738,5 @@ models.SetModelUnitsAndTransform(model, units , newTransform3D, true);
### 如何唯一标识一个ModelItem
使用 ModelItem.GetHashCode() 作为唯一标识符
使用 HashSet<ModelItem> 来存储和比较碰撞对象,这样可以正确使用 ModelItem.Equals() 和 GetHashCode() 方法,避免哈希冲突和跨运行时不一致的问题

View File

@ -105,7 +105,7 @@ namespace NavisworksTransport.Core.Animation
private List<CollisionResult> _allCollisionResults; // 所有碰撞结果(不去重)
private bool _lastHighlightState = false; // 上一帧的高亮状态
private HashSet<int> _lastCollisionObjectIds = new HashSet<int>(); // 上一帧碰撞对象的ID集合
private HashSet<ModelItem> _lastCollisionObjects = new HashSet<ModelItem>(); // 上一帧碰撞对象的集合
// === 路径相关 ===
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<int>();
// 收集当前帧碰撞对象的集合
var currentCollisionObjects = new HashSet<ModelItem>();
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更新等