589 lines
20 KiB
C#
589 lines
20 KiB
C#
using CaeGlobals;
|
||
using CaeKnowledge.Data;
|
||
using CaeKnowledge.View;
|
||
using CPSO.Forms._92_Knowledge;
|
||
using DynamicDescriptors;
|
||
using FastDeepCloner;
|
||
using JetBrains.Annotations;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Windows.Forms;
|
||
|
||
// ReSharper disable once CheckNamespace
|
||
namespace CPSO.Forms
|
||
{
|
||
public partial class FrmCutter : Form
|
||
{
|
||
public FrmCutter()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private Type _modeType;
|
||
|
||
public void PrepareForm(Type type)
|
||
{
|
||
MakeColumns(type);
|
||
|
||
_modeType = type;
|
||
|
||
if (type == typeof(ViewCutter))
|
||
{
|
||
Text = @"刀具信息表";
|
||
dataGridView1.DataSource = new BindingList<ViewCutter>();
|
||
}
|
||
else if (type == typeof(ViewCuttingParameter))
|
||
{
|
||
Text = @"切削力系数表";
|
||
dataGridView1.DataSource = new BindingList<ViewCuttingParameter>();
|
||
}
|
||
else if (type == typeof(ViewToolPosition))
|
||
{
|
||
Text = @"查看切削力";
|
||
保存SToolStripButton.Visible = false;
|
||
打开OToolStripButton.Visible = false;
|
||
dataGridView1.DataSource = new BindingList<ViewToolPosition>();
|
||
}
|
||
else
|
||
{
|
||
throw new CaeException("非法数据类型,无法创建对话框!");
|
||
}
|
||
|
||
// By Luke 加载数据库
|
||
LoadDatabase();
|
||
}
|
||
|
||
private void MakeColumns(Type type)
|
||
{
|
||
var memberList = type.GetMembers(BindingFlags.Public | BindingFlags.Instance)
|
||
.Where(m => m.MemberType == MemberTypes.Field ||
|
||
m.MemberType == MemberTypes.Property);
|
||
|
||
List<DataGridViewColumn> columns = new List<DataGridViewColumn>();
|
||
if (columns == null) throw new ArgumentNullException(nameof(columns));
|
||
|
||
foreach (var member in memberList)
|
||
{
|
||
var attribute = member.GetCustomAttributes<DisplayNameAttribute>();
|
||
|
||
var displayNameAttributes = attribute as DisplayNameAttribute[] ?? attribute.ToArray();
|
||
|
||
if (displayNameAttributes.Any())
|
||
{
|
||
var displayName = displayNameAttributes.First().DisplayName;
|
||
|
||
if (displayName != null)
|
||
{
|
||
var column = new DataGridViewTextBoxColumn
|
||
{
|
||
Name = member.Name,
|
||
HeaderText = displayName,
|
||
DataPropertyName = member.Name,
|
||
};
|
||
|
||
column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
||
column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
||
|
||
columns.Add(column);
|
||
}
|
||
}
|
||
}
|
||
|
||
dataGridView1.Columns.Clear();
|
||
dataGridView1.Columns.AddRange(columns.ToArray());
|
||
}
|
||
|
||
[UsedImplicitly]
|
||
private static DataTable LoadDataTable(Type type)
|
||
{
|
||
var memberList = type.GetMembers(BindingFlags.Public | BindingFlags.Instance)
|
||
.Where(m => m.MemberType == MemberTypes.Field ||
|
||
m.MemberType == MemberTypes.Property)
|
||
.Cast<PropertyInfo>();
|
||
|
||
DataTable dataTable = new DataTable();
|
||
|
||
List<DataColumn> columns = new List<DataColumn>();
|
||
if (columns == null) throw new ArgumentNullException(nameof(columns));
|
||
|
||
columns.Add(new DataColumn
|
||
{
|
||
Caption = "Id",
|
||
ColumnName = "Id",
|
||
DataType = CaeKnowledge.KnowledgeTools.ObjectIdType,
|
||
Unique = true,
|
||
ColumnMapping = MappingType.Hidden
|
||
});
|
||
|
||
foreach (var member in memberList)
|
||
{
|
||
var attribute = member.GetCustomAttributes<DisplayNameAttribute>();
|
||
|
||
var displayNameAttributes = attribute as DisplayNameAttribute[] ?? attribute.ToArray();
|
||
|
||
if (displayNameAttributes.Any())
|
||
{
|
||
var displayName = displayNameAttributes.First().DisplayName;
|
||
|
||
if (displayName != null)
|
||
{
|
||
var col = new DataColumn
|
||
{
|
||
Caption = displayName,
|
||
ColumnName = member.Name,
|
||
DataType = member.PropertyType,
|
||
};
|
||
|
||
if (member.Name == nameof(ViewCutter.Cid))
|
||
{
|
||
col.Unique = true;
|
||
}
|
||
|
||
columns.Add(col);
|
||
}
|
||
}
|
||
}
|
||
|
||
dataTable.Columns.Clear();
|
||
dataTable.Columns.AddRange(columns.ToArray());
|
||
|
||
return dataTable;
|
||
}
|
||
|
||
private void FrmCutter_Load(object sender, EventArgs e)
|
||
{
|
||
dataGridView1.AutoGenerateColumns = false;
|
||
dataGridView1.AllowUserToAddRows = false;
|
||
dataGridView1.AllowUserToDeleteRows = false;
|
||
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
|
||
|
||
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
|
||
dataGridView1.RowHeadersVisible = false;
|
||
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||
}
|
||
|
||
private void FrmCutter_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
if (e.CloseReason == CloseReason.UserClosing)
|
||
{
|
||
e.Cancel = true;
|
||
Hide();
|
||
}
|
||
}
|
||
|
||
private void tsbExcelImport_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
using (var openFileDialog = new OpenFileDialog())
|
||
{
|
||
openFileDialog.Filter = @"Excel 文件 (*.xls;*.xlsx)|*.xls;*xlsx";
|
||
|
||
if (openFileDialog.ShowDialog() != DialogResult.OK) return;
|
||
|
||
var fullPath = openFileDialog.FileName;
|
||
|
||
if (!string.IsNullOrEmpty(fullPath) &&
|
||
dataGridView1.DataSource is BindingList<ViewCutter> list1)
|
||
{
|
||
List<ViewCutter> listViewCutters = CaeKnowledge.KnowledgeTools.LoadViewCutters(fullPath);
|
||
|
||
if (listViewCutters.Count > 0)
|
||
{
|
||
listViewCutters.GroupBy(v => v.Cid)
|
||
.Select(g => g.First())
|
||
.ToList()
|
||
.ForEach(cutter =>
|
||
{
|
||
if (list1.Any(vc => vc.Cid == cutter.Cid))
|
||
{
|
||
throw new CaeException("已有相同库号Cid的刀具信息,添加失败!");
|
||
}
|
||
|
||
list1.Add(cutter);
|
||
});
|
||
}
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(fullPath)
|
||
&& dataGridView1.DataSource is BindingList<ViewCuttingParameter> list2)
|
||
{
|
||
List<ViewCuttingParameter> listCuttingParameters =
|
||
CaeKnowledge.KnowledgeTools.LoadCuttingParameters(fullPath);
|
||
|
||
if (listCuttingParameters.Count > 0)
|
||
{
|
||
listCuttingParameters.GroupBy(v => v.Pid)
|
||
.Select(g => g.First())
|
||
.ToList()
|
||
.ForEach(cuttingParameter =>
|
||
{
|
||
if (list2.Any(cp => cp.Pid == cuttingParameter.Pid))
|
||
{
|
||
throw new CaeException("已有相同编号Pid的切削力系数,添加失败!");
|
||
}
|
||
|
||
list2.Add(cuttingParameter);
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ExceptionTools.Show(this, ex);
|
||
}
|
||
}
|
||
|
||
private void tsbAdd_Click(object sender, EventArgs e)
|
||
{
|
||
if (_modeType == typeof(ViewCutter))
|
||
{
|
||
AddViewCutter();
|
||
}
|
||
else if (_modeType == typeof(ViewCuttingParameter))
|
||
{
|
||
AddCuttingParameter();
|
||
}
|
||
else if(_modeType == typeof(ViewToolPosition))
|
||
{
|
||
AddToolPosition();
|
||
}
|
||
else
|
||
{
|
||
throw new CaeException("非法数据类型!");
|
||
}
|
||
}
|
||
|
||
private void AddViewCutter()
|
||
{
|
||
try
|
||
{
|
||
var dialog = new FrmAddCutter();
|
||
|
||
ViewCutter cutter = new ViewCutter();
|
||
|
||
dialog.Text = @"添加刀具信息";
|
||
dialog.SetCutter(cutter);
|
||
|
||
DialogResult result = dialog.ShowDialog();
|
||
|
||
if (result == DialogResult.OK && cutter.IsValid()
|
||
&& dataGridView1.DataSource is BindingList<ViewCutter> list)
|
||
{
|
||
if (list.Any(vc => vc.Cid == cutter.Cid))
|
||
{
|
||
throw new CaeException("已有相同库号Cid的刀具信息,添加失败!");
|
||
}
|
||
|
||
list.Add(cutter);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ExceptionTools.Show(this, ex);
|
||
}
|
||
}
|
||
|
||
private void AddCuttingParameter()
|
||
{
|
||
try
|
||
{
|
||
var dialog = new FrmAddCutter();
|
||
|
||
ViewCuttingParameter cuttingParameter = new ViewCuttingParameter();
|
||
|
||
dialog.Text = @"添加切削力系数";
|
||
dialog.SetCuttingParameter(cuttingParameter);
|
||
|
||
DialogResult result = dialog.ShowDialog();
|
||
|
||
if (result == DialogResult.OK && cuttingParameter.IsValid()
|
||
&& dataGridView1.DataSource is BindingList<ViewCuttingParameter> list)
|
||
{
|
||
if (list.Any(vcp => vcp.Pid == cuttingParameter.Pid))
|
||
{
|
||
throw new CaeException("已有相同编号Pid的切削力系数,添加失败!");
|
||
}
|
||
|
||
list.Add(cuttingParameter);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ExceptionTools.Show(this, ex);
|
||
}
|
||
}
|
||
|
||
private void AddToolPosition()
|
||
{
|
||
try
|
||
{
|
||
var dialog = new FrmAddCutter();
|
||
dialog.Text = @"添加切削力";
|
||
|
||
var toolPosition = new ViewToolPosition();
|
||
dialog.SetToolPosition(toolPosition);
|
||
|
||
var result = dialog.ShowDialog();
|
||
|
||
if (result == DialogResult.OK
|
||
&& dataGridView1.DataSource is BindingList<ViewToolPosition> list)
|
||
{
|
||
list.Add(toolPosition);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ExceptionTools.Show(this, ex);
|
||
}
|
||
}
|
||
|
||
private void tsbDelete_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView1.SelectedRows.Count > 0
|
||
&& MessageBox.Show(@"Are you sure you want to delete the selected row(s)?",
|
||
@"Confirm Deletion", MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
// Iterate in reverse to avoid issues with changing row indices
|
||
for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
|
||
{
|
||
DataGridViewRow row = dataGridView1.SelectedRows[i];
|
||
dataGridView1.Rows.RemoveAt(row.Index);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
|
||
{
|
||
// Ensure a valid row was double-clicked (not a header)
|
||
if (e.RowIndex < 0) return;
|
||
|
||
// Get the selected row
|
||
using (var clickedRow = dataGridView1.Rows[e.RowIndex])
|
||
{
|
||
if (_modeType == typeof(ViewCutter))
|
||
{
|
||
EditCutter(clickedRow);
|
||
}
|
||
else if (_modeType == typeof(ViewCuttingParameter))
|
||
{
|
||
EditCuttingParameter(clickedRow);
|
||
}
|
||
else if (_modeType == typeof(ViewToolPosition))
|
||
{
|
||
EditToolPosition(clickedRow);
|
||
}
|
||
}
|
||
}
|
||
|
||
private static void EditCutter(DataGridViewRow clickedRow)
|
||
{
|
||
if (!(clickedRow.DataBoundItem is ViewCutter oldCutter))
|
||
return;
|
||
|
||
var dialog = new FrmAddCutter
|
||
{
|
||
Text = @"编辑刀具信息"
|
||
};
|
||
|
||
// FastDeepCloner
|
||
var newCutter = oldCutter.Clone();
|
||
|
||
var descriptor = DynamicDescriptor.CreateFromInstance(newCutter);
|
||
descriptor.GetDynamicProperty(nameof(ViewCutter.Cid)).SetReadOnly(true);
|
||
|
||
dialog.SetCutter(descriptor);
|
||
|
||
var result = dialog.ShowDialog();
|
||
|
||
if (result == DialogResult.OK && newCutter != null && newCutter.IsValid())
|
||
{
|
||
// FastDeepCloner
|
||
newCutter.CloneTo(oldCutter);
|
||
}
|
||
}
|
||
|
||
private static void EditCuttingParameter(DataGridViewRow clickedRow)
|
||
{
|
||
if (!(clickedRow.DataBoundItem is ViewCuttingParameter oldCuttingParameter))
|
||
return;
|
||
|
||
var dialog = new FrmAddCutter
|
||
{
|
||
Text = @"编辑切削力系数"
|
||
};
|
||
|
||
// FastDeepCloner
|
||
var newCuttingParameter = oldCuttingParameter.Clone();
|
||
|
||
var descriptor = DynamicDescriptor.CreateFromInstance(newCuttingParameter);
|
||
descriptor.GetDynamicProperty(nameof(ViewCuttingParameter.Pid)).SetReadOnly(true);
|
||
|
||
dialog.SetCuttingParameter(descriptor);
|
||
|
||
var result = dialog.ShowDialog();
|
||
|
||
if (result == DialogResult.OK
|
||
&& newCuttingParameter != null && newCuttingParameter.IsValid())
|
||
{
|
||
// FastDeepCloner
|
||
newCuttingParameter.CloneTo(oldCuttingParameter);
|
||
}
|
||
}
|
||
|
||
private static void EditToolPosition(DataGridViewRow clickedRow)
|
||
{
|
||
if (!(clickedRow.DataBoundItem is ViewToolPosition oldToolPosition))
|
||
return;
|
||
|
||
// FastDeepCloner
|
||
var newToolPosition = oldToolPosition.Clone();
|
||
|
||
var dialog = new FrmAddCutter
|
||
{
|
||
Text = @"编辑切削力"
|
||
};
|
||
|
||
dialog.SetToolPosition(newToolPosition);
|
||
|
||
var result = dialog.ShowDialog();
|
||
|
||
if (result == DialogResult.OK
|
||
&& newToolPosition != null)
|
||
{
|
||
// FastDeepCloner
|
||
newToolPosition.CloneTo(oldToolPosition);
|
||
}
|
||
}
|
||
|
||
private void 打开OToolStripButton_Click(object sender, EventArgs e)
|
||
{
|
||
LoadDatabase();
|
||
}
|
||
|
||
private void LoadDatabase()
|
||
{
|
||
string fullPath = Tools.GetLiteDatabase();
|
||
|
||
if (fullPath == null || !File.Exists(fullPath)) return;
|
||
|
||
if (dataGridView1.DataSource is BindingList<ViewCutter> list1)
|
||
{
|
||
list1.Clear();
|
||
|
||
List<Cutter> listViewCutters = CaeKnowledge.KnowledgeTools.LoadCuttersFromDatabase(fullPath);
|
||
|
||
listViewCutters.ForEach(cutter =>
|
||
{
|
||
list1.Add(new ViewCutter(cutter));
|
||
});
|
||
}
|
||
else if (dataGridView1.DataSource is BindingList<ViewCuttingParameter> list2)
|
||
{
|
||
list2.Clear();
|
||
|
||
List<CuttingParameter> listCuttingParameters =
|
||
CaeKnowledge.KnowledgeTools.LoadCuttingParametersFromDatabase(fullPath);
|
||
|
||
listCuttingParameters.ForEach(cuttingParameter =>
|
||
{
|
||
list2.Add(new ViewCuttingParameter(cuttingParameter));
|
||
});
|
||
}
|
||
}
|
||
|
||
private void 保存SToolStripButton_Click(object sender, EventArgs e)
|
||
{
|
||
string fullPath = Tools.GetLiteDatabase();
|
||
|
||
if (fullPath == null) return;
|
||
|
||
if (dataGridView1.DataSource is BindingList<ViewCutter> list1)
|
||
{
|
||
var cutters = list1.Select(x => x.GetBase()).ToList();
|
||
|
||
CaeKnowledge.KnowledgeTools.SaveDatabase(fullPath, cutters);
|
||
}
|
||
else if(dataGridView1.DataSource is BindingList<ViewCuttingParameter> list2)
|
||
{
|
||
var cuttingParameters = list2.Select(x => x.GetBase()).ToList();
|
||
|
||
CaeKnowledge.KnowledgeTools.SaveDatabase(fullPath, cuttingParameters);
|
||
}
|
||
}
|
||
|
||
public void LoadToolPositions(Controller self)
|
||
{
|
||
if (!(dataGridView1.DataSource is BindingList<ViewToolPosition> list))
|
||
return;
|
||
|
||
list.Clear();
|
||
|
||
self.ToolPositions.ForEach(tp =>
|
||
{
|
||
list.Add(tp);
|
||
});
|
||
}
|
||
|
||
public void SaveToolPositions(Controller self)
|
||
{
|
||
if (!(dataGridView1.DataSource is BindingList<ViewToolPosition> list))
|
||
return;
|
||
|
||
self.ToolPositions.Clear();
|
||
|
||
self.ToolPositions.AddRange(list);
|
||
}
|
||
|
||
private void LoadData()
|
||
{
|
||
using (var openFileDialog = new OpenFileDialog())
|
||
{
|
||
openFileDialog.Filter = @"LiteDB数据库 (*.db)|*.db";
|
||
|
||
if (openFileDialog.ShowDialog() != DialogResult.OK) return;
|
||
|
||
var fullPath = openFileDialog.FileName;
|
||
|
||
if (dataGridView1.DataSource is BindingList<ViewCutter> list)
|
||
{
|
||
list.Clear();
|
||
|
||
List<Cutter> listViewCutters = CaeKnowledge.KnowledgeTools.LoadCuttersFromDatabase(fullPath);
|
||
|
||
listViewCutters.ForEach(cutter =>
|
||
{
|
||
list.Add(new ViewCutter(cutter));
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
private void Save()
|
||
{
|
||
using (var saveFileDialog = new SaveFileDialog())
|
||
{
|
||
saveFileDialog.Filter = @"LiteDB数据库 (*.db)|*.db";
|
||
|
||
if (saveFileDialog.ShowDialog() != DialogResult.OK) return;
|
||
|
||
var fullPath = saveFileDialog.FileName;
|
||
|
||
if (dataGridView1.DataSource is BindingList<ViewCutter> list)
|
||
{
|
||
var cutters = list.Select(x => x.GetBase()).ToList();
|
||
|
||
CaeKnowledge.KnowledgeTools.SaveDatabase(fullPath, cutters);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|