205 lines
7.5 KiB
C#
205 lines
7.5 KiB
C#
using CaeGlobals;
|
||
using CaeKnowledge;
|
||
using CaeKnowledge.View;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Windows.Forms;
|
||
using System.Linq;
|
||
using CaeKnowledge.Data;
|
||
|
||
namespace CPSO.Forms._92_Knowledge
|
||
{
|
||
public partial class FrmEditToolPositions : Form
|
||
{
|
||
// Variables
|
||
private readonly Controller _controller;
|
||
private bool _modified;
|
||
|
||
// Constructors
|
||
public FrmEditToolPositions(Controller controller)
|
||
{
|
||
InitializeComponent();
|
||
|
||
// dgvCommands.EnableDragAndDropRows();
|
||
dgvCommands.DataBindingComplete += DataBindingComplete;
|
||
_controller = controller;
|
||
_modified = false;
|
||
}
|
||
|
||
private void tsmiClose_Click(object sender, EventArgs e)
|
||
{
|
||
Hide();
|
||
}
|
||
|
||
private void dgvCommands_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
|
||
{
|
||
// Color blue = Color.FromArgb(225, 245, 255);
|
||
Color green = Color.FromArgb(235, 255, 235);
|
||
// Color yellow = Color.FromArgb(255, 255, 205);
|
||
// Color red = Color.FromArgb(255, 235, 215);
|
||
|
||
dgvCommands.Rows[e.RowIndex].DefaultCellStyle.BackColor = green;
|
||
}
|
||
|
||
private void btnReorganize_Click(object sender, EventArgs e)
|
||
{
|
||
}
|
||
|
||
private void btnClearAll_Click(object sender, EventArgs e)
|
||
{
|
||
dgvCommands.DataSource = null;
|
||
|
||
_viewToolPositions.Clear();
|
||
|
||
SetBinding();
|
||
|
||
_modified = true;
|
||
}
|
||
|
||
private void btnOK_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
DialogResult = DialogResult.OK;
|
||
|
||
if (_modified && dgvCommands.DataSource is BindingSource source
|
||
&& source.DataSource is List<ViewToolPosition> list)
|
||
{
|
||
_controller.ToolPositions.Clear();
|
||
_controller.ToolPositions.AddRange(list);
|
||
_modified = false;
|
||
}
|
||
|
||
Hide();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ExceptionTools.Show(this, ex);
|
||
}
|
||
}
|
||
|
||
private readonly List<ViewToolPosition> _viewToolPositions = new List<ViewToolPosition>();
|
||
|
||
// Methods
|
||
public void PrepareForm()
|
||
{
|
||
_viewToolPositions.Clear();
|
||
|
||
if (_controller.ToolPositions.Count > 0)
|
||
{
|
||
_viewToolPositions.AddRange(_controller.ToolPositions);
|
||
}
|
||
|
||
SetBinding();
|
||
}
|
||
|
||
private void SetBinding()
|
||
{
|
||
BindingSource binding = new BindingSource();
|
||
binding.DataSource = _viewToolPositions;
|
||
dgvCommands.DataSource = binding;
|
||
}
|
||
|
||
// Source - https://stackoverflow.com/a/24784960
|
||
// Posted by TFischer
|
||
private void DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
|
||
{
|
||
// Loops through each row in the DataGridView, and adds the
|
||
// row number to the header
|
||
foreach (DataGridViewRow dGVRow in dgvCommands.Rows)
|
||
{
|
||
dGVRow.HeaderCell.Value = $"{dGVRow.Index + 1}";
|
||
}
|
||
|
||
// This resizes the width of the row headers to fit the numbers
|
||
dgvCommands.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
|
||
}
|
||
|
||
private void FrmEditToolPositions_Load(object sender, EventArgs e)
|
||
{
|
||
dgvCommands.AllowUserToAddRows = false;
|
||
dgvCommands.AllowUserToDeleteRows = false;
|
||
dgvCommands.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
|
||
}
|
||
|
||
private void tsmiOpen_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
// 1. 选择要读取的TXT文件
|
||
using (OpenFileDialog openFileDialog = new OpenFileDialog())
|
||
{
|
||
openFileDialog.Filter = "CSV文件 (*.csv)|*.csv|所有文件 (*.*)|*.*";
|
||
openFileDialog.FilterIndex = 1;
|
||
openFileDialog.RestoreDirectory = true;
|
||
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
string filePath = openFileDialog.FileName;
|
||
|
||
// 2. 逐行读取文件内容
|
||
var loadedPositions = ToolPositionFileHelper.LoadToolPositionsFromFile(filePath);
|
||
|
||
_controller.ToolPositions.Clear();
|
||
foreach (var position in loadedPositions)
|
||
{
|
||
_controller.ToolPositions.Add(new ViewToolPosition(position));
|
||
}
|
||
|
||
PrepareForm();
|
||
|
||
// 3. 提示读取结果
|
||
MessageBox.Show($"✅ 读取完成!\n文件路径:{filePath}\n解析出有效数据:{loadedPositions.Count}条",
|
||
"读取成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"❌ 读取失败:{ex.Message}\n{ex.StackTrace}",
|
||
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void tsmiSaveAs_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
// 1. 创建保存文件对话框
|
||
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
|
||
{
|
||
// 设置对话框属性
|
||
saveFileDialog.Filter = "CSV文件 (*.csv)|*.csv|所有文件 (*.*)|*.*"; // 筛选只显示csv文件
|
||
saveFileDialog.FilterIndex = 1; // 默认选中csv筛选项
|
||
saveFileDialog.RestoreDirectory = true; // 关闭对话框后恢复原目录
|
||
saveFileDialog.FileName = "刀位_" + DateTime.Now.ToString("yyyyMMdd"); // 默认文件名(带时间戳)
|
||
|
||
// 2. 如果用户点击了"保存"按钮
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
// 3. 获取用户选择的文件路径
|
||
string savePath = saveFileDialog.FileName;
|
||
|
||
// 4. 将列表数据写入TXT文件
|
||
var testPositions = _controller.ToolPositions.Select(x=> x.Base).ToList();
|
||
|
||
ToolPositionFileHelper.SaveToolPositionsToFile(testPositions, savePath);
|
||
|
||
// 提示保存成功
|
||
MessageBox.Show($"数据已成功保存到:\n{savePath}", "保存成功",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// 异常处理:捕获并提示保存过程中的错误
|
||
MessageBox.Show($"保存失败:{ex.Message}", "错误",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|