This commit is contained in:
ldw
2025-08-12 14:03:37 +08:00
parent e6f5b275fc
commit 1e9f1052d5
13 changed files with 682 additions and 48 deletions
+446 -2
View File
@@ -1,15 +1,32 @@
using JinYuan.ControlLib;
using JinYuan.CommunicationLib.Enum;
using JinYuan.ControlLib;
using JinYuan.Helper;
using JinYuan.Models;
using JinYuan.VirtualDataLibrary;
using Microsoft.Win32;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using static Org.BouncyCastle.Math.EC.ECCurve;
using System.Drawing;
namespace LargeSquareOne
{
public partial class FrmSystemSet : Form
{
private string FdConfigFilePath = "fd_config.xlsx";
private List<FDConfig> fdConfigs;
private List<string> PLCTypes = new List<string>();
private List<string> NGLDTypes1 = new List<string>();//NG拉带类型
private List<string> NGLDTypes2 = new List<string>();//NG拉带类型
private List<string> NGLDTypes3 = new List<string>();//NG拉带类型
private bool isFDEditing = false;
private int editingFDRowIndex = -1;
private readonly string configFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config");
/// <summary>
/// 打印日志方法
/// </summary>
@@ -49,6 +66,14 @@ namespace LargeSquareOne
this.cmb_PLCType.Items.Add("Mitsubishi");
this.cmb_PLCType.Items.Add("Inovance");
FdConfigFilePath = Path.Combine(configFolderPath, "FdConfig.xlsx");
PLCTypes.AddRange(new string[] { "OmronFinsTCP", "OmronCipNet" });
NGLDTypes1.AddRange(new string[] { "面1", "面2", "面3" });
NGLDTypes2.AddRange(new string[] { "面1", "面2", "面3" });
NGLDTypes3.AddRange(new string[] { "面1", "面2", "面3" });
SetupFDDataGridView();
LoadFDConfig();
this.tog_AutoStart.IsChecked = CommonMethods.sysConfig.AutoStart;
this.tog_AutoLogin.IsChecked = CommonMethods.sysConfig.AutoLogin;
this.tog_AutoLock.IsChecked = CommonMethods.sysConfig.AutoLock;
@@ -73,7 +98,7 @@ namespace LargeSquareOne
this.up_ShowSeriesCount.ValueChanged += new System.EventHandler(this.up_ShowSeriesCount_ValueChanged);
this.devPath = devPath;
InitParam();
}
@@ -417,5 +442,424 @@ namespace LargeSquareOne
LogMessage(OKMessage, "强制结果OK日志");
}
}
#region 分档设置
private void LoadFDConfig()
{
try
{
if (File.Exists(FdConfigFilePath))
{
using (var stream = File.OpenRead(FdConfigFilePath))
{
var devices = MiniExcel.Query<FDConfig>(stream).ToList();
fdConfigs = devices.Select(device => new FDConfig
{
FdNum = device.FdNum,
OKLD1Type = device.OKLD1Type,
OKLD2Type = device.OKLD2Type,
OKLD3Type = device.OKLD3Type,
OKLD4Type = device.OKLD4Type,
NGLD5Type = device.NGLD5Type,
NGLD6Type = device.NGLD6Type,
NGLD7Type = device.NGLD7Type,
IsFDActive = device.IsFDActive,
IsNGActive = device.IsNGActive
}).ToList();
}
}
else
{
fdConfigs = new List<FDConfig>();
}
RefreshFDDataGridView();
}
catch (Exception ex)
{
MessageBox.Show($"加载配置文件失败:{ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
fdConfigs = new List<FDConfig>();
}
}
private void SetColumnsFDReadOnly(bool readOnly)
{
foreach (DataGridViewColumn col in dgvFdConfigs.Columns)
{
// PlcNum 列始终保持只读
if (col.Name == "FdNum")
{
col.ReadOnly = true;
continue;
}
// 按钮列不需要设置 ReadOnly 属性
if (col is DataGridViewButtonColumn)
continue;
// 其他列根据参数设置
col.ReadOnly = readOnly;
}
}
/// <summary>
/// 初始化分档DataGrid
/// </summary>
private void SetupFDDataGridView()
{
// 设置基本属性
dgvFdConfigs.AutoGenerateColumns = false;
dgvFdConfigs.MultiSelect = false;
// 添加数据列
dgvFdConfigs.Columns.AddRange(new DataGridViewColumn[]
{
new DataGridViewTextBoxColumn
{
Name = "FdNum",
HeaderText = "分档编号",
DataPropertyName = "FdNum",
ReadOnly = true,
Width = 80,
ValueType = typeof(int)
},
#region 分档类型
new DataGridViewTextBoxColumn
{
Name = "OKLD1Type",
HeaderText = "OK拉带类型1",
DataPropertyName = "OKLD1Type",
Width = 120,
ValueType = typeof(string)
},
new DataGridViewTextBoxColumn
{
Name = "OKLD2Type",
HeaderText = "OK拉带类型2",
DataPropertyName = "OKLD2Type",
Width = 120,
ValueType = typeof(string)
},
new DataGridViewTextBoxColumn
{
Name = "OKLD3Type",
HeaderText = "OK拉带类型3",
DataPropertyName = "OKLD3Type",
Width = 120,
ValueType = typeof(string)
},
new DataGridViewTextBoxColumn
{
Name = "OKLD4Type",
HeaderText = "OK拉带类型4",
DataPropertyName = "OKLD4Type",
Width = 120,
ValueType = typeof(string)
},
#endregion
#region NG拉带类型
new DataGridViewComboBoxColumn
{
Name = "NGLD5Type",
HeaderText = "NG拉带类型5",
DataPropertyName = "NGLD5Type",//NG拉带
DataSource = new List<string>(NGLDTypes1), // 使用新列表避免引用问题
Width = 160,
ValueType = typeof(string),
FlatStyle = FlatStyle.Flat,
DefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(14, 23, 38), // 深色背景
ForeColor = Color.White, // 白色文字
SelectionBackColor = Color.FromArgb(0, 122, 204), // 选中时的背景色
SelectionForeColor = Color.White, // 选中时的文字颜色
Font = new Font("微软雅黑", 9F) // 字体设置
}
},
new DataGridViewComboBoxColumn
{
Name = "NGLD6Type",
HeaderText = "NG拉带类型6",
DataPropertyName = "NGLD6Type",//NG拉带
DataSource = new List<string>(NGLDTypes2), // 使用新列表避免引用问题
Width = 160,
ValueType = typeof(string),
FlatStyle = FlatStyle.Flat,
DefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(14, 23, 38), // 深色背景
ForeColor = Color.White, // 白色文字
SelectionBackColor = Color.FromArgb(0, 122, 204), // 选中时的背景色
SelectionForeColor = Color.White, // 选中时的文字颜色
Font = new Font("微软雅黑", 9F) // 字体设置
}
},
new DataGridViewComboBoxColumn
{
Name = "NGLD7Type",
HeaderText = "NG拉带类型7",
DataPropertyName = "NGLD7Type",//NG拉带
DataSource = new List<string>(NGLDTypes3), // 使用新列表避免引用问题
Width = 160,
ValueType = typeof(string),
FlatStyle = FlatStyle.Flat,
DefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(14, 23, 38), // 深色背景
ForeColor = Color.White, // 白色文字
SelectionBackColor = Color.FromArgb(0, 122, 204), // 选中时的背景色
SelectionForeColor = Color.White, // 选中时的文字颜色
Font = new Font("微软雅黑", 9F) // 字体设置
}
},
#endregion
#region 开启分档
new DataGridViewCheckBoxColumn
{
Name = "IsFDActive",
HeaderText = "开启分档",
DataPropertyName = "IsFDActive",
Width = 80,
ValueType = typeof(bool),
TrueValue = true,
FalseValue = false
},
new DataGridViewCheckBoxColumn
{
Name = "IsNGActive",
HeaderText = "NG电池不分类排出",
DataPropertyName = "IsNGActive",
Width = 80,
ValueType = typeof(bool),
TrueValue = true,
FalseValue = false
},
#endregion
new DataGridViewButtonColumn
{
Name = "Edit",
HeaderText = "操作",
Text = "修改",
UseColumnTextForButtonValue = true,
Width = 80
}
});
// 设置列为只读
SetColumnsFDReadOnly(false);
// 绑定事件
dgvFdConfigs.CellClick += DgvFdConfigs_CellClick;
dgvFdConfigs.CellValidating += DgvFdConfigs_CellValidating;
dgvFdConfigs.CellValueChanged += DgvFdConfigs_CellValueChanged;
dgvFdConfigs.DataError += DgvFdConfigs_DataError;
// 设置自动调整行高
dgvFdConfigs.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgvFdConfigs.RowHeadersVisible = false;
}
private void DgvFdConfigs_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) return;
var columnName = dgvFdConfigs.Columns[e.ColumnIndex].Name;
// 如果正在编辑其他行,阻止操作
if (isFDEditing && editingFDRowIndex != e.RowIndex)
{
MessageBox.Show("请先完成当前编辑操作", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var fdConfig = fdConfigs[e.RowIndex];
switch (columnName)
{
case "Edit":
HandleFDEdit(e.RowIndex);
break;
case "CommConfig":
HandleCommFdConfig(fdConfig);
break;
case "VariableConfig":
HandleVariableFdConfig(fdConfig);
break;
case "Delete":
HandleFdDelete(e.RowIndex, fdConfig);
break;
}
}
private void HandleFDEdit(int rowIndex)
{
if (!isFDEditing)
{
// 开始编辑
isFDEditing = true;
editingFDRowIndex = rowIndex;
SetColumnsFDReadOnly(false); // 设置其他列为可编辑
// 更改编辑按钮文本为"保存"
dgvFdConfigs.Rows[rowIndex].Cells["Edit"].Value = "保存";
// 禁用其他行的按钮
foreach (DataGridViewRow row in dgvFdConfigs.Rows)
{
if (row.Index != rowIndex)
{
if (row.Cells["Edit"] is DataGridViewButtonCell editCell)
editCell.Value = "";
if (row.Cells["CommConfig"] is DataGridViewButtonCell configCell)
configCell.Value = "";
if (row.Cells["Delete"] is DataGridViewButtonCell deleteCell)
deleteCell.Value = "";
}
}
// 禁用添加按钮
//btAddPLC.Enabled = false;
}
else if (rowIndex == editingFDRowIndex)
{
// 保存更改
//if (ValidateRow(rowIndex))
{
SaveFDChanges(rowIndex);
EndFDEditing();
}
}
}
private void EndFDEditing()
{
isFDEditing = false;
editingFDRowIndex = -1;
SetColumnsFDReadOnly(true);
// 恢复所有按钮文本和状态
foreach (DataGridViewRow row in dgvFdConfigs.Rows)
{
row.Cells["Edit"].Value = "编辑";
// row.Cells["CommConfig"].Value = "通信组";
// row.Cells["Delete"].Value = "删除";
}
// 启用添加按钮
//btAddPLC.Enabled = true;
}
private void HandleCommFdConfig(FDConfig fdconfig)
{
}
private void HandleVariableFdConfig(FDConfig fdconfig)
{
}
private void HandleFdDelete(int rowIndex, FDConfig fdconfig)
{
if (MessageBox.Show($"确定要删除 分档 {fdconfig.FdNum} 吗?", "确认删除",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
fdConfigs.RemoveAt(rowIndex);
SaveFDConfig();
RefreshFDDataGridView();
}
}
private void SaveFDChanges(int rowIndex)
{
try
{
var row = dgvFdConfigs.Rows[rowIndex];
var config = fdConfigs[rowIndex];
// 更新配置对象
config.OKLD1Type = row.Cells["OKLD1Type"].Value?.ToString() ?? "";
config.OKLD2Type = row.Cells["OKLD2Type"].Value?.ToString() ?? "";
config.OKLD3Type = row.Cells["OKLD3Type"].Value?.ToString() ?? "";
config.OKLD4Type = row.Cells["OKLD4Type"].Value?.ToString() ?? "";
config.NGLD5Type = row.Cells["NGLD5Type"].Value?.ToString() ?? "";
config.NGLD6Type = row.Cells["NGLD6Type"].Value?.ToString() ?? "";
config.NGLD7Type = row.Cells["NGLD7Type"].Value?.ToString() ?? "";
config.IsFDActive = row.Cells["IsFDActive"].Value != null &&
Convert.ToBoolean(row.Cells["IsFDActive"].Value);
config.IsNGActive = row.Cells["IsNGActive"].Value != null &&
Convert.ToBoolean(row.Cells["IsNGActive"].Value);
// 保存更改
SaveFDConfig();
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
// 这里只显示错误信息,不再抛出异常
MessageBox.Show($"保存失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DgvFdConfigs_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.RowIndex < 0 || !isFDEditing || e.RowIndex != editingFDRowIndex) return;
string newValue = e.FormattedValue.ToString();
var column = dgvFdConfigs.Columns[e.ColumnIndex];
}
private void DgvFdConfigs_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) return;
dgvFdConfigs.Rows[e.RowIndex].ErrorText = string.Empty;
}
private void DgvFdConfigs_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.ThrowException = false;
string columnName = dgvFdConfigs.Columns[e.ColumnIndex].HeaderText;
MessageBox.Show($"'{columnName}'列的输入数据格式不正确", "数据错误",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void SaveFDConfig()
{
try
{
// 创建备份
if (File.Exists(FdConfigFilePath))
{
string backupFile = $"{FdConfigFilePath}.bak";
File.Copy(FdConfigFilePath, backupFile, true);
}
// 直接保存到目标文件
MiniExcel.SaveAs(FdConfigFilePath, fdConfigs, overwriteFile: true);
}
catch (IOException ex)
{
MessageBox.Show($"保存文件时发生IO错误:{ex.Message}\n请确保文件未被其他程序占用。",
"保存错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show($"没有足够的权限保存文件:{ex.Message}\n请确保有写入权限。",
"权限错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
catch (Exception ex)
{
MessageBox.Show($"保存配置时发生错误:{ex.Message}",
"保存错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
private void RefreshFDDataGridView()
{
dgvFdConfigs.DataSource = null;
dgvFdConfigs.DataSource = new BindingList<FDConfig>(fdConfigs);
}
#endregion
}
}