169 lines
5.8 KiB
C#
169 lines
5.8 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using System.Windows.Forms;
|
|||
|
|
using CaeGlobals;
|
|||
|
|
using CaeKnowledge.Data;
|
|||
|
|
using CaeKnowledge.View;
|
|||
|
|
|
|||
|
|
namespace CPSO.Forms._92_Knowledge
|
|||
|
|
{
|
|||
|
|
public partial class FrmSimpleMaterialLibrary : Form
|
|||
|
|
{
|
|||
|
|
public FrmSimpleMaterialLibrary()
|
|||
|
|
{
|
|||
|
|
InitializeComponent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void PrepareForm()
|
|||
|
|
{
|
|||
|
|
MakeColumns(typeof(CaeKnowledge.View.ViewElasticWithDensity));
|
|||
|
|
|
|||
|
|
Text = @"材料定义";
|
|||
|
|
dataGridView1.DataSource = new BindingList<CaeKnowledge.View.ViewElasticWithDensity>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void FrmSimpleMaterialLibrary_FormClosing(object sender, FormClosingEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (e.CloseReason == CloseReason.UserClosing)
|
|||
|
|
{
|
|||
|
|
e.Cancel = true;
|
|||
|
|
Hide();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void FrmSimpleMaterialLibrary_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 toolAdd_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var dialog = new FrmAddCutter();
|
|||
|
|
|
|||
|
|
var viewElastic = new ViewElasticWithDensity();
|
|||
|
|
|
|||
|
|
dialog.Text = @"添加材料";
|
|||
|
|
dialog.SetMaterial(viewElastic);
|
|||
|
|
|
|||
|
|
DialogResult result = dialog.ShowDialog();
|
|||
|
|
|
|||
|
|
if (result == DialogResult.OK && viewElastic.IsValid()
|
|||
|
|
&& dataGridView1.DataSource is BindingList<ViewElasticWithDensity> list)
|
|||
|
|
{
|
|||
|
|
if (list.Any(mat => mat.Name == viewElastic.Name))
|
|||
|
|
{
|
|||
|
|
throw new CaeException("已有相同名称的材料定义,添加失败!");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
list.Add(viewElastic);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
ExceptionTools.Show(this, ex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void toolDelete_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 打开OToolStripButton_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
var fullPath = Tools.GetLiteDatabase();
|
|||
|
|
|
|||
|
|
if (fullPath == null || !File.Exists(fullPath)) return;
|
|||
|
|
|
|||
|
|
if (dataGridView1.DataSource is BindingList<ViewElasticWithDensity> list)
|
|||
|
|
{
|
|||
|
|
list.Clear();
|
|||
|
|
|
|||
|
|
var listViewElasticWithDensities =
|
|||
|
|
CaeKnowledge.KnowledgeTools.LoadElasticWithDensitiesFromDatabase(fullPath);
|
|||
|
|
|
|||
|
|
listViewElasticWithDensities.ForEach( obj =>
|
|||
|
|
{
|
|||
|
|
list.Add(obj);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void 保存SToolStripButton_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
string fullPath = Tools.GetLiteDatabase();
|
|||
|
|
|
|||
|
|
if (fullPath == null) return;
|
|||
|
|
|
|||
|
|
if (dataGridView1.DataSource is BindingList<ViewElasticWithDensity> list)
|
|||
|
|
{
|
|||
|
|
CaeKnowledge.KnowledgeTools.SaveDatabase(fullPath, list.ToList());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|