using RBFMorphing; using System; using System.ComponentModel; using System.IO; using System.Linq; using System.Windows.Forms; // ReSharper disable once CheckNamespace namespace CPSO.Forms { public partial class FrmRBFMorphAdd : Form, IFormBase { private readonly Controller _controller; private readonly BindingList _bindingList; public FrmRBFMorphAdd(Controller ctx, BindingList list) { InitializeComponent(); _controller = ctx; _bindingList = list; StartPosition = FormStartPosition.Manual; } public bool PrepareForm(string stepName, string itemToEdit) { throw new System.NotImplementedException(); } public bool PrepareForm(string title, EnumMorphMethod method) { Text = title; if (method == EnumMorphMethod.Part) { pgData.SelectedObject = new ViewBasePart(_controller.Model.Mesh); return true; } if (method == EnumMorphMethod.ElementSet) { pgData.SelectedObject = new RBFMorphing.ViewElementSet(_controller.Model.Mesh); return true; } if (method == EnumMorphMethod.NodeSet) { pgData.SelectedObject = new RBFMorphing.ViewNodeSet(_controller.Model.Mesh); return true; } if (method == EnumMorphMethod.Surface) { pgData.SelectedObject = new ViewSurface(_controller.Model.Mesh); return true; } if (method == EnumMorphMethod.ImportFile) { pgData.SelectedObject = new ViewImportFile(); return true; } return false; } private void FrmRBFMorphAdd_Load(object sender, System.EventArgs e) { gbProperties.Text = @"数据"; Icon = _controller.Form.Icon; } private void btnOk_Click(object sender, System.EventArgs e) { if (pgData.SelectedObject is ViewBasePart viewBasePart) { var mesh = _controller.Model.Mesh; if (_bindingList != null && mesh.Parts.TryGetValue(viewBasePart.Name, out var basePart)) { if (mesh != null && basePart != null) { if (_bindingList.Where(x => x.Method == EnumMorphMethod.Part).Select(x => x.Name) .Contains(viewBasePart.Name)) { MessageBox.Show(@"零件已添加"); } else { var rbfBasePart = new BasePart(mesh, basePart); _bindingList.Add(rbfBasePart); var msg = $"RBF模型中添加零部件:{rbfBasePart.Name}, 节点数:{rbfBasePart.Length}"; _controller.Form.WriteDataToOutput(msg); } } } } else if (pgData.SelectedObject is RBFMorphing.ViewElementSet viewElementSet) { var mesh = _controller.Model.Mesh; if (_bindingList != null && mesh.ElementSets.TryGetValue(viewElementSet.Name, out var elementSet)) { if (mesh != null && elementSet != null) { if (_bindingList.Where(x => x.Method == EnumMorphMethod.ElementSet) .Select(x => x.Name) .Contains(viewElementSet.Name)) { MessageBox.Show(@"单元集合已添加"); } else { var rbfElementSet = new ElementSet(mesh, elementSet); _bindingList.Add(rbfElementSet); var msg = $"RBF模型中添加单元集合:{rbfElementSet.Name}, 节点数:{rbfElementSet.Length}"; _controller.Form.WriteDataToOutput(msg); } } } } else if (pgData.SelectedObject is RBFMorphing.ViewNodeSet viewNodeSet) { var mesh = _controller.Model.Mesh; if (_bindingList != null && mesh.NodeSets.TryGetValue(viewNodeSet.Name, out var nodeSet)) { if (mesh != null && nodeSet != null) { if (_bindingList.Where(x => x.Method == EnumMorphMethod.NodeSet) .Select(x => x.Name) .Contains(viewNodeSet.Name)) { MessageBox.Show(@"节点集合已添加"); } else { var rbfNodeSet = new NodeSet(mesh, nodeSet); _bindingList.Add(rbfNodeSet); var msg = $"RBF模型中添加节点集合:{rbfNodeSet.Name}, 节点数:{rbfNodeSet.Length}"; _controller.Form.WriteDataToOutput(msg); } } } } else if (pgData.SelectedObject is ViewSurface viewSurface) { var mesh = _controller.Model.Mesh; if (_bindingList != null && mesh.Surfaces.TryGetValue(viewSurface.Name, out var surface)) { if (mesh != null && surface != null) { if (_bindingList.Where(x => x.Method == EnumMorphMethod.Surface) .Select(x => x.Name) .Contains(viewSurface.Name)) { MessageBox.Show(@"曲面集合已添加"); } else { var rbfSurface = new Surface(mesh, surface); _bindingList.Add(rbfSurface); var msg = $"RBF模型中添加节点集合:{rbfSurface.Name}, 节点数:{rbfSurface.Length}"; _controller.Form.WriteDataToOutput(msg); } } } } else if (pgData.SelectedObject is ViewImportFile viewImportFile) { if (_bindingList != null && !string.IsNullOrEmpty(viewImportFile.Name) && File.Exists(viewImportFile.FileFullPath)) { try { var importedNodes = new ImportedNodes { Name = viewImportFile.Name, }; importedNodes.LoadCsvFile(viewImportFile.FileFullPath); _bindingList.Add(importedNodes); } catch (Exception exception) { MessageBox.Show(exception.Message); } } } Close(); } private void pgData_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) { var pd = e.ChangedItem.PropertyDescriptor; if (pd != null && pd.Name.Equals(nameof(ViewImportFile.FileFullPath)) && s is PropertyGrid pg && pg.SelectedObject is ViewImportFile viewImportFile && string.IsNullOrEmpty(viewImportFile.Name) && File.Exists(viewImportFile.FileFullPath)) { viewImportFile.Name = Path.GetFileNameWithoutExtension(viewImportFile.FileFullPath); } } } }