4.4 KiB
4.4 KiB
常见使用场景示例
场景1:创建并显示对话框
using NavisworksTransport.Utils;
public class MyViewModel
{
private MyDialog _dialog;
public void ShowDialog()
{
// 防止重复打开
if (_dialog != null && _dialog.IsVisible)
{
_dialog.Activate();
return;
}
// 创建对话框
_dialog = new MyDialog();
// ✅ 使用 DialogHelper 设置 Owner
DialogHelper.SetOwnerSafely(_dialog);
// 显示非模态对话框
_dialog.Show();
// 清理引用
_dialog.Closed += (s, e) => _dialog = null;
}
}
场景2:带单位转换的计算
using NavisworksTransport.Utils;
public class PathPlanner
{
/// <summary>
/// 设置网格大小
/// </summary>
/// <param name="cellSizeInMeters">网格单元大小(米)</param>
public void SetCellSize(double cellSizeInMeters)
{
// ✅ 转换为模型单位存储
double cellSize = UnitsConverter.ConvertFromMeters(cellSizeInMeters);
// 后续计算使用 cellSize(模型单位)
_grid = new GridMap(cellSize);
}
/// <summary>
/// 获取路径长度(用于显示)
/// </summary>
public string GetPathLengthDisplay(PathRoute route)
{
// ✅ 转换为米显示
double lengthInMeters = UnitsConverter.ConvertToMeters(route.TotalLength);
return $"{lengthInMeters:F2} 米";
}
}
场景3:完整的操作日志记录
using NavisworksTransport.Utils;
public class PathService
{
public async Task<PathRoute> CreatePathAsync(PathParameters parameters)
{
LogManager.Info("[路径创建] === 开始创建路径 ===");
LogManager.Info($"[路径创建] 参数: {parameters}");
try
{
// 验证参数
if (!ValidateParameters(parameters))
{
LogManager.Warning("[路径创建] 参数验证失败");
return null;
}
// 执行创建
var route = await GeneratePathAsync(parameters);
LogManager.Info($"[路径创建] 成功: {route.Name}, ID: {route.Id}");
LogManager.Info($"[路径创建] 长度: {route.TotalLength:F2} 米, 点数: {route.Points.Count}");
return route;
}
catch (Exception ex)
{
// ✅ 记录完整异常信息
LogManager.Error($"[路径创建] 失败: {ex.Message}", ex);
throw;
}
finally
{
LogManager.Info("[路径创建] === 创建完成 ===");
}
}
}
场景4:几何计算
using NavisworksTransport.Utils;
public class PathAnalyzer
{
/// <summary>
/// 计算路径总长度
/// </summary>
public double CalculateTotalLength(List<Point3D> points)
{
double totalLength = 0;
for (int i = 0; i < points.Count - 1; i++)
{
// ✅ 使用 GeometryHelper
totalLength += GeometryHelper.Distance(points[i], points[i + 1]);
}
return totalLength;
}
/// <summary>
/// 检查转弯是否过急
/// </summary>
public bool IsTurnTooSharp(Point3D prev, Point3D current, Point3D next, double maxAngle)
{
Vector3D v1 = GeometryHelper.Subtract(current, prev);
Vector3D v2 = GeometryHelper.Subtract(next, current);
double angle = GeometryHelper.AngleBetweenDegrees(v1, v2);
return angle > maxAngle;
}
}
场景5:确认对话框
using NavisworksTransport.Utils;
using System.Windows;
public class PathViewModel
{
public void DeletePath(PathRoute route)
{
// ✅ 使用 DialogHelper 显示确认对话框
var result = DialogHelper.ShowMessageBox(
$"确定要删除路径 \"{route.Name}\" 吗?\n此操作不可撤销。",
"确认删除",
MessageBoxButton.YesNo,
MessageBoxImage.Warning
);
if (result != MessageBoxResult.Yes)
{
LogManager.Info($"[删除] 用户取消删除: {route.Name}");
return;
}
// 执行删除
_pathPlanningManager.DeleteRoute(route.Id);
LogManager.Info($"[删除] 路径已删除: {route.Name}");
}
}