NavisworksTransport/src/UI/WPF/Views/GenerateNavigationMapDialog.xaml.cs

229 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Controls;
using Autodesk.Navisworks.Api;
namespace NavisworksTransport.UI.WPF.Views
{
/// <summary>
/// 生成导航地图对话框的交互逻辑
/// </summary>
public partial class GenerateNavigationMapDialog : Window
{
/// <summary>
/// 获取或设置图像宽度
/// </summary>
public int ImageWidth { get; private set; }
/// <summary>
/// 获取或设置图像高度
/// </summary>
public int ImageHeight { get; private set; }
/// <summary>
/// 获取或设置图像格式
/// </summary>
public ImageFormat ImageFormat { get; private set; }
/// <summary>
/// 获取或设置渲染样式
/// </summary>
public ImageGenerationStyle RenderStyle { get; private set; }
/// <summary>
/// 获取用户是否点击了确定按钮
/// </summary>
public bool IsConfirmed { get; private set; }
public GenerateNavigationMapDialog()
{
InitializeComponent();
// 延迟初始化,确保所有控件都已创建
Loaded += (sender, e) => InitializeDefaults();
}
/// <summary>
/// 初始化默认值
/// </summary>
private void InitializeDefaults()
{
try
{
// 设置默认值
ImageWidth = 1920;
ImageHeight = 1080;
ImageFormat = ImageFormat.Png;
RenderStyle = ImageGenerationStyle.ScenePlusOverlay;
IsConfirmed = false;
// 设置默认选择
FormatComboBox.SelectedIndex = 1; // PNG
PresetSizeComboBox.SelectedIndex = 2; // 1920x1080 (因为添加了4K选项索引变为2)
WidthTextBox.Text = ImageWidth.ToString();
HeightTextBox.Text = ImageHeight.ToString();
}
catch (Exception ex)
{
LogManager.Error($"初始化导航地图对话框失败: {ex.Message}", ex);
}
}
/// <summary>
/// 预设尺寸选择变更事件
/// </summary>
private void PresetSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
// 确保所有控件都已初始化
if (WidthTextBox == null || HeightTextBox == null)
{
return;
}
ComboBoxItem selectedItem = PresetSizeComboBox.SelectedItem as ComboBoxItem;
if (selectedItem?.Tag?.ToString() == "Custom")
{
// 自定义模式,启用文本框
WidthTextBox.IsEnabled = true;
HeightTextBox.IsEnabled = true;
return;
}
// 预设尺寸模式,禁用文本框
WidthTextBox.IsEnabled = false;
HeightTextBox.IsEnabled = false;
// 解析预设尺寸
string sizeTag = selectedItem?.Tag?.ToString();
if (!string.IsNullOrEmpty(sizeTag) && sizeTag.Contains("x"))
{
string[] parts = sizeTag.Split('x');
if (parts.Length == 2 &&
int.TryParse(parts[0], out int width) &&
int.TryParse(parts[1], out int height))
{
WidthTextBox.Text = width.ToString();
HeightTextBox.Text = height.ToString();
}
}
}
catch (Exception ex)
{
LogManager.Error($"预设尺寸选择失败: {ex.Message}", ex);
}
}
/// <summary>
/// 确定按钮点击事件
/// </summary>
private void OkButton_Click(object sender, RoutedEventArgs e)
{
try
{
// 验证输入
if (!ValidateInputs())
{
return;
}
// 获取图像尺寸
if (!int.TryParse(WidthTextBox.Text, out int width) || width <= 0)
{
MessageBox.Show("请输入有效的图像宽度", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
WidthTextBox.Focus();
return;
}
if (!int.TryParse(HeightTextBox.Text, out int height) || height <= 0)
{
MessageBox.Show("请输入有效的图像高度", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
HeightTextBox.Focus();
return;
}
// 检查尺寸合理性
if (width > 10000 || height > 10000)
{
MessageBox.Show("图像尺寸过大请输入小于10000像素的值", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
// 设置输出参数
ImageWidth = width;
ImageHeight = height;
// 获取图像格式
ImageFormat = GetSelectedImageFormat();
// 固定使用包含覆盖层的渲染样式
RenderStyle = ImageGenerationStyle.ScenePlusOverlay;
IsConfirmed = true;
DialogResult = true;
Close();
}
catch (Exception ex)
{
LogManager.Error($"确认导航地图参数失败: {ex.Message}", ex);
MessageBox.Show($"参数确认失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 取消按钮点击事件
/// </summary>
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
IsConfirmed = false;
DialogResult = false;
Close();
}
/// <summary>
/// 验证输入
/// </summary>
private bool ValidateInputs()
{
// 验证宽度
if (string.IsNullOrWhiteSpace(WidthTextBox.Text))
{
MessageBox.Show("请输入图像宽度", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
WidthTextBox.Focus();
return false;
}
// 验证高度
if (string.IsNullOrWhiteSpace(HeightTextBox.Text))
{
MessageBox.Show("请输入图像高度", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
HeightTextBox.Focus();
return false;
}
return true;
}
/// <summary>
/// 获取选择的图像格式
/// </summary>
private ImageFormat GetSelectedImageFormat()
{
switch (FormatComboBox.SelectedIndex)
{
case 0:
return ImageFormat.Jpeg; // JPEG
case 1:
return ImageFormat.Png; // PNG
case 2:
return ImageFormat.Bmp; // Windows 位图
default:
return ImageFormat.Png; // 默认PNG
}
}
}
}