纠正碰撞对不显示虚拟物体的问题
This commit is contained in:
parent
dc1380d7fe
commit
9751287884
55
AGENTS.md
55
AGENTS.md
@ -336,20 +336,43 @@ double distance = GeometryHelper.Distance(pointA, pointB);
|
||||
|
||||
**所有网格地图和路径规划计算必须使用模型单位,严禁混用米制单位。**
|
||||
|
||||
#### 变量命名铁律(强制执行)
|
||||
|
||||
| 单位类型 | 命名规则 | 示例 |
|
||||
|---------|---------|------|
|
||||
| **米单位** | 变量名必须以 `InMeters` 结尾 | `lengthInMeters`, `heightInMeters` |
|
||||
| **模型单位** | 变量名**无后缀** | `length`, `height`, `cellSize` |
|
||||
|
||||
```csharp
|
||||
// ✅ 正确:在函数入口统一转换
|
||||
public GridMap GenerateFromBIM(BoundingBox3D bounds, double cellSizeMeters, ...)
|
||||
// ✅ 正确:严格遵循命名规范
|
||||
public void SetSize(double lengthInMeters, double widthInMeters, double heightInMeters)
|
||||
{
|
||||
double factor = UnitsConverter.GetMetersToUnitsConversionFactor(Application.ActiveDocument.Units);
|
||||
double cellSizeInModelUnits = cellSizeMeters * factor;
|
||||
// 后续所有计算使用 cellSizeInModelUnits
|
||||
double metersToUnits = UnitsConverter.GetMetersToUnitsConversionFactor(...);
|
||||
|
||||
// 米单位 → 模型单位(无后缀)
|
||||
double length = lengthInMeters * metersToUnits;
|
||||
double width = widthInMeters * metersToUnits;
|
||||
double height = heightInMeters * metersToUnits;
|
||||
|
||||
// 后续计算使用模型单位(无后缀)
|
||||
boundingBox = new BoundingBox3D(0, 0, 0, length, width, height);
|
||||
}
|
||||
|
||||
// ❌ 错误:混用单位
|
||||
private const double MAX_HEIGHT_DIFF = 0.35; // 这是米!
|
||||
if (heightDiff > MAX_HEIGHT_DIFF) // 单位不匹配,严重Bug!
|
||||
// ❌ 错误:命名不规范,导致单位混乱
|
||||
public void SetSize(double length, double width, double height) // 参数是米还是模型单位?
|
||||
{
|
||||
double scale = length / baseSize; // 单位不明确!
|
||||
}
|
||||
|
||||
// ❌ 错误:后缀使用不一致
|
||||
public void SetSize(double lengthMeters, double widthMeters, double heightMeters) // 不要用Meters后缀
|
||||
{
|
||||
double lengthInModelUnits = lengthMeters * factor; // 不要用InModelUnits后缀
|
||||
}
|
||||
```
|
||||
|
||||
#### 单位转换方法
|
||||
|
||||
`UnitsConverter` 提供以下方法:
|
||||
|
||||
- `GetUnitsToMetersConversionFactor()` - 文档单位转米
|
||||
@ -357,6 +380,22 @@ if (heightDiff > MAX_HEIGHT_DIFF) // 单位不匹配,严重Bug!
|
||||
- `ConvertToMeters(distance)` - 转换距离为米
|
||||
- `ConvertFromMeters(distanceInMeters)` - 从米转换距离
|
||||
|
||||
#### 完整示例
|
||||
|
||||
```csharp
|
||||
// ✅ 正确:在函数入口统一转换,严格遵循命名规范
|
||||
public GridMap GenerateFromBIM(BoundingBox3D bounds, double cellSizeInMeters, ...)
|
||||
{
|
||||
double factor = UnitsConverter.GetMetersToUnitsConversionFactor(Application.ActiveDocument.Units);
|
||||
double cellSize = cellSizeInMeters * factor; // 模型单位,无后缀
|
||||
// 后续所有计算使用 cellSize(模型单位)
|
||||
}
|
||||
|
||||
// ❌ 错误:混用单位
|
||||
private const double MAX_HEIGHT_DIFF = 0.35; // 这是米!
|
||||
if (heightDiff > MAX_HEIGHT_DIFF) // 单位不匹配,严重Bug!
|
||||
```
|
||||
|
||||
### 错误处理
|
||||
|
||||
```csharp
|
||||
|
||||
@ -2439,6 +2439,25 @@ namespace NavisworksTransport.UI.WPF.ViewModels
|
||||
return;
|
||||
}
|
||||
|
||||
// 🔥 关键修复:如果运动物体是虚拟物体,确保它是可见的(可能被之前的切换隐藏了)
|
||||
if (selectedResult?.Record?.IsVirtualObject == true)
|
||||
{
|
||||
try
|
||||
{
|
||||
double unitsToMeters = UnitsConverter.GetUnitsToMetersConversionFactor(Autodesk.Navisworks.Api.Application.ActiveDocument.Units);
|
||||
// 显示虚拟物体(如果已存在且尺寸相同,会显示;如果尺寸不同,会更新尺寸)
|
||||
VirtualObjectManager.Instance.ShowVirtualObject(
|
||||
selectedResult.VirtualObjectLength * unitsToMeters,
|
||||
selectedResult.VirtualObjectWidth * unitsToMeters,
|
||||
selectedResult.VirtualObjectHeight * unitsToMeters);
|
||||
LogManager.Debug("[碰撞构件] 虚拟物体已确保可见");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogManager.Warning($"[碰撞构件] 显示虚拟物体失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 首次查看时保存动画物体状态
|
||||
|
||||
@ -6,6 +6,7 @@ using System.Windows.Input;
|
||||
using Autodesk.Navisworks.Api;
|
||||
using NavisworksTransport.Utils;
|
||||
using NavisworksTransport.Commands;
|
||||
using NavisworksTransport.Core;
|
||||
using NavisworksTransport.Core.Animation;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
@ -1323,6 +1324,17 @@ namespace NavisworksTransport.UI.WPF.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
// 🔥 关键修复:如果运动物体是虚拟物体,确保它是可见的
|
||||
if (_currentAnimatedObject != null &&
|
||||
VirtualObjectManager.Instance.IsVirtualObjectActive &&
|
||||
VirtualObjectManager.Instance.CurrentVirtualObject == _currentAnimatedObject)
|
||||
{
|
||||
// 虚拟物体已存在但可能被隐藏,需要显示
|
||||
// 使用默认尺寸 2x1x2 米(ShowVirtualObject 内部会处理尺寸更新)
|
||||
VirtualObjectManager.Instance.ShowVirtualObject(2.0, 1.0, 2.0);
|
||||
LogManager.Debug("[碰撞报告] 虚拟物体已确保可见");
|
||||
}
|
||||
|
||||
// 使用通用方法移动并聚焦到碰撞位置
|
||||
CollisionSceneHelper.MoveToCollisionAndFocus(collisionData, _currentAnimatedObject);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user