106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Windows;
|
|
|
|
namespace DrillTools
|
|
{
|
|
/// <summary>
|
|
/// 钻带参数文件选择窗口,列出 A01-A14 文件并以第一行描述文本展示供用户选择。
|
|
/// </summary>
|
|
public partial class DrlParameterSelectionWindow : Window
|
|
{
|
|
/// <summary>
|
|
/// 用户选中的文件完整路径;若取消则为 null。
|
|
/// </summary>
|
|
public string? SelectedFilePath { get; private set; }
|
|
|
|
public DrlParameterSelectionWindow(string paramDirectory)
|
|
{
|
|
InitializeComponent();
|
|
|
|
var items = new List<ParamFileItem>();
|
|
for (int i = 1; i <= 14; i++)
|
|
{
|
|
string fileName = $"A{i:D2}";
|
|
string filePath = Path.Combine(paramDirectory, fileName);
|
|
|
|
if (!File.Exists(filePath))
|
|
continue;
|
|
|
|
string description = ReadFirstLineDescription(filePath);
|
|
items.Add(new ParamFileItem
|
|
{
|
|
FileName = fileName,
|
|
FilePath = filePath,
|
|
Description = $"{fileName} — {description}"
|
|
});
|
|
}
|
|
|
|
FileListBox.ItemsSource = items;
|
|
OkButton.IsEnabled = false;
|
|
|
|
FileListBox.SelectionChanged += (_, _) =>
|
|
{
|
|
OkButton.IsEnabled = FileListBox.SelectedItem != null;
|
|
};
|
|
|
|
FileListBox.MouseDoubleClick += (_, _) =>
|
|
{
|
|
if (FileListBox.SelectedItem is ParamFileItem item)
|
|
{
|
|
SelectedFilePath = item.FilePath;
|
|
DialogResult = true;
|
|
Close();
|
|
}
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取参数文件的第一行描述文本(去掉前导分号)。
|
|
/// </summary>
|
|
private static string ReadFirstLineDescription(string filePath)
|
|
{
|
|
try
|
|
{
|
|
string content = CommandTypeFileReader.ReadAllText(filePath);
|
|
var lines = DrillTapeLineParser.SplitLines(content);
|
|
if (lines.Count > 0)
|
|
{
|
|
string firstLine = lines[0].Text.Trim();
|
|
if (firstLine.StartsWith(";"))
|
|
return firstLine[1..].Trim();
|
|
return firstLine;
|
|
}
|
|
return "(无法读取)";
|
|
}
|
|
catch
|
|
{
|
|
return "(无法读取)";
|
|
}
|
|
}
|
|
|
|
private void OkButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (FileListBox.SelectedItem is ParamFileItem item)
|
|
{
|
|
SelectedFilePath = item.FilePath;
|
|
DialogResult = true;
|
|
Close();
|
|
}
|
|
}
|
|
|
|
private void CancelButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DialogResult = false;
|
|
Close();
|
|
}
|
|
|
|
private class ParamFileItem
|
|
{
|
|
public string FileName { get; init; } = string.Empty;
|
|
public string FilePath { get; init; } = string.Empty;
|
|
public string Description { get; init; } = string.Empty;
|
|
}
|
|
}
|
|
}
|