纠正物体移动方法,确保先回到CAD原始位置再进行目标位置和朝向的变换

This commit is contained in:
tian 2026-02-11 02:30:52 +08:00
parent 7ccb0ef6b0
commit bdb3ed23db

View File

@ -103,7 +103,7 @@ namespace NavisworksTransport.Utils
}
/// <summary>
/// 将物体从当前位置移动到指定位置和朝向(增量模式
/// 将物体移动到指定位置和朝向先回到CAD原始位置再移动
/// 适用于碰撞位置还原等场景
/// </summary>
/// <param name="item">要移动的物体</param>
@ -114,32 +114,35 @@ namespace NavisworksTransport.Utils
var doc = Application.ActiveDocument;
var modelItems = new ModelItemCollection { item };
// 获取当前状态
var currentBounds = item.BoundingBox();
var currentGroundPos = new Point3D(
currentBounds.Center.X,
currentBounds.Center.Y,
currentBounds.Min.Z
);
var currentYaw = GetYawFromTransform(item.Transform);
// 🔥 关键先回到CAD原始位置确保从已知状态开始计算
doc.Models.ResetPermanentTransform(modelItems);
// 计算位置和旋转增量
// 获取CAD原始状态
var originalBounds = item.BoundingBox();
var originalGroundPos = new Point3D(
originalBounds.Center.X,
originalBounds.Center.Y,
originalBounds.Min.Z
);
var originalYaw = GetYawFromTransform(item.Transform);
// 计算从CAD原始位置到目标位置的增量
var deltaPos = new Vector3D(
targetPosition.X - currentGroundPos.X,
targetPosition.Y - currentGroundPos.Y,
targetPosition.Z - currentGroundPos.Z
targetPosition.X - originalGroundPos.X,
targetPosition.Y - originalGroundPos.Y,
targetPosition.Z - originalGroundPos.Z
);
double deltaYaw = targetYaw - currentYaw;
double deltaYaw = targetYaw - originalYaw;
// 应用增量变换和ClashDetective中的做法一致
// 应用增量变换
Transform3D transform;
if (Math.Abs(deltaYaw) > 0.001)
{
// 有旋转:需要补偿绕原点旋转带来的位置偏移
double cos = Math.Cos(deltaYaw);
double sin = Math.Sin(deltaYaw);
double rotatedX = currentGroundPos.X * cos - currentGroundPos.Y * sin;
double rotatedY = currentGroundPos.X * sin + currentGroundPos.Y * cos;
double rotatedX = originalGroundPos.X * cos - originalGroundPos.Y * sin;
double rotatedY = originalGroundPos.X * sin + originalGroundPos.Y * cos;
var compensatedTranslation = new Vector3D(
targetPosition.X - rotatedX,