TFLH/ViewModel/MachineEditorVM.cs
2026-08-06 09:50:46 +08:00

490 lines
17 KiB
C#

using Community;
using Microsoft.Win32;
using Model;
using Model.MotionControl;
using Model.Vision;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ViewModel.Vision;
namespace ViewModel
{
/// <summary>
/// 机种编辑
/// </summary>
partial class MainWindowVM
{
#region 已启用的机种集合
private ObservableCollection<MachineModel> enableMachineList;
/// <summary>
/// 已启用的机种集合
/// </summary>
public ObservableCollection<MachineModel> EnableMachineList
{
get { return enableMachineList; }
set
{
enableMachineList = value;
this.RaisePropertyChanged("EnableMachineList");
}
}
#endregion
#region 所有机种集合
private ObservableCollection<MachineModel> allMachineList;
/// <summary>
/// 所有机种结合
/// </summary>
public ObservableCollection<MachineModel> AllMachineList
{
get { return allMachineList; }
set
{
allMachineList = value;
this.RaisePropertyChanged("AllMachineList");
}
}
#endregion
#region 各种方法
/// <summary>
/// 新增机种
/// </summary>
public DelegateCommand AddMachineCommand { get; set; }
/// <summary>
/// 更新机种
/// </summary>
public DelegateCommand UpdateMachineCommand { get; set; }
/// <summary>
/// 生产计数清零
/// </summary>
public DelegateCommand CountClearedCommand { get; set; }
/// <summary>
/// 读取产品数据
/// </summary>
public DelegateCommand ReadProductCommand { get; set; }
/// <summary>
/// 导出产品数据
/// </summary>
public DelegateCommand ExportProductCommand { get; set; }
/// <summary>
/// 读取产品上一页
/// </summary>
public DelegateCommand ReadPageUpProductCommand { get; set; }
/// <summary>
/// 读取产品下一页
/// </summary>
public DelegateCommand ReadPageDownProductCommand { get; set; }
#endregion
#region 选中的要编辑机种
private MachineModel selectMachine;
/// <summary>
/// 选中的要编辑机种
/// </summary>
public MachineModel SelectMachine
{
get { return selectMachine; }
set
{
selectMachine = value;
this.RaisePropertyChanged("SelectMachine");
}
}
#endregion
#region 初始化机种编辑类
/// <summary>
/// 初始化机种编辑类
/// </summary>
public void InitializeMachineEditor()
{
//1、获取已启用的机种集合
AllMachineList = dal.GetALLMachineList();
EnableMachineList = new ObservableCollection<MachineModel>(AllMachineList.Where(w => w.EnableS == true).ToList());
//获取当前生产机种
CurrentMachineID = SystemPar.DefaultMachineID;
AddMachineCommand = new DelegateCommand();
AddMachineCommand.ExecuteAction = new Action<object>(AddMachine);
UpdateMachineCommand = new DelegateCommand();
UpdateMachineCommand.ExecuteAction = new Action<object>(UpdateMachine);
CountClearedCommand = new DelegateCommand();
CountClearedCommand.ExecuteAction = new Action<object>(CountCleared);
StartProductionTime = DateTime.Now;
EndProductionTime = DateTime.Now;
//this.ReadProductCommand = new DelegateCommand();
//this.ReadProductCommand.ExecuteAction = new Action<object>(ReadProduct);
//this.ReadPageUpProductCommand = new DelegateCommand();
//this.ReadPageUpProductCommand.ExecuteAction = new Action<object>(ReadPageUpProduct);
//this.ReadPageDownProductCommand = new DelegateCommand();
//this.ReadPageDownProductCommand.ExecuteAction = new Action<object>(ReadPageDownProduct);
//this.ExportProductCommand = new DelegateCommand();
//this.ExportProductCommand.ExecuteAction = new Action<object>(ExportProduct);
QuerySN = "";
}
#endregion
#region 生产计数清零
/// <summary>
/// 生产计数清零
/// </summary>
/// <param name="obj"></param>
private void CountCleared(object obj)
{
if (MessageBox.Show("你确定要对当前机种生产计数清零?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) == MessageBoxResult.OK)
{
CurrentMachine.Bads = 0;
CurrentMachine.Quantitys = 0;
CurrentMachine.RejectRatio = 0;
CurrentMachine.Produce = 0;
dal.UpdateProductionData(CurrentMachine);
}
}
#endregion
#region 更新机种
/// <summary>
/// 更新机种
/// </summary>
/// <param name="obj"></param>
private void UpdateMachine(object obj)
{
if (MessageBox.Show("你确定更新当前选中机种吗?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
{
return;
}
if (dal.UpdateMachineData(SelectMachine) > 0)
{
EnableMachineList = new ObservableCollection<MachineModel>(AllMachineList.Where(w => w.EnableS == true).ToList());
MessageBox.Show("更新成功");
}
else
{
MessageBox.Show("更新失败");
}
}
#endregion
#region 新增机种
/// <summary>
/// 新增机种
/// </summary>
/// <param name="obj"></param>
private void AddMachine(object obj)
{
if (MessageBox.Show("你确定要新增机种吗?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
{
return;
}
try
{
MachineModel mt = Coper(SelectMachine); //new MachineModel() { MachineName = "新来的", EnableS = true };
mt.MachineName = "新来的";
if (dal.AddMachineData(mt) > 0)
{
dal.UpdateMachineData(mt);//更新机种参数
//添加机器人点位
if (SystemPar.RobotBool && Robot != null)
{
//新增机器人点位
if (Robot.RobotPointList != null && Robot.RobotPointList.Count > 0)
{
foreach (RobotPoint rp in Robot.RobotPointList)
{
rp.MachineID = mt.ID;
dal.AddRobotPoint(rp);
rp.MachineID = CurrentMachineID;//还原ID
}
}
}
//添加运动控制点位
if (SystemPar.MotionControlBool && MCVM != null)
{
if (MCVM.TrajListL != null && MCVM.TrajListL.Count > 0)
{
foreach (MotionTrajectoryModel item in MCVM.TrajListL)
{
item.SuperiorID = mt.ID;
if (dal.AddMotionTrajectory(item) > 0)
{
int id = dal.GetInsertID(); //获取插入数据的ID
foreach (MotionPointModel mp in item.PointList)
{
mp.MachineID = id;
dal.AddMotionPoint(mp);
mp.MachineID = item.ID; //还原ID
}
}
item.SuperiorID = CurrentMachineID; //还原ID
}
}
}
//添加视觉
if (SystemPar.VisionBool && HVlist != null)
{
foreach (HalconVisual hv in HVlist)
{
foreach (VisualEngineering ve in hv.VisualEngineeringList)
{
ve.MachineID = mt.ID;
if (Vdal.AddVisualEngineering(ve) > 0)
{
int id = dal.GetInsertID(); //获取插入数据的ID
foreach (VisualEngineeringItems vei in ve.VisualEngineeringItemsList)
{
vei.VisualEngineeringID = id;
Vdal.AddVisualEngineeringItems(vei);
vei.VisualEngineeringID = ve.ID;
}
}
ve.MachineID = CurrentMachineID; //还原ID
}
}
}
AllMachineList = dal.GetALLMachineList();
EnableMachineList = new ObservableCollection<MachineModel>(AllMachineList.Where(w => w.EnableS == true).ToList());
CurrentMachineID = SystemPar.DefaultMachineID;
MessageBox.Show("新增机种成功");
}
else
{
MessageBox.Show("新增机种失败");
}
}
catch (Exception ex)
{
MessageBox.Show("新增机种失败" + ex.ToString());
}
}
#endregion
#region 查询开始时间
private DateTime startProductionTime;
/// <summary>
/// 查询开始时间
/// </summary>
public DateTime StartProductionTime
{
get { return startProductionTime; }
set
{
startProductionTime = value;
this.RaisePropertyChanged("StartProductionTime");
}
}
#endregion
#region 查询结束时间
private DateTime endProductionTime;
/// <summary>
/// 查询结束时间
/// </summary>
public DateTime EndProductionTime
{
get { return endProductionTime; }
set
{
endProductionTime = value;
//使选中的时间包含时间
endProductionTime = new DateTime(value.Year, value.Month, value.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
this.RaisePropertyChanged("EndProductionTime");
}
}
#endregion
#region 查询的SN
private string querySN;
/// <summary>
/// 查询的SN
/// </summary>
public string QuerySN
{
get { return querySN; }
set
{
querySN = value;
this.RaisePropertyChanged("QuerySN");
}
}
#endregion
#region 时间段产品数据集合
private ObservableCollection<ProductModel> productList;
/// <summary>
/// 时间段产品数据集合
/// </summary>
public ObservableCollection<ProductModel> ProductList
{
get { return productList; }
set
{
productList = value;
this.RaisePropertyChanged("ProductList");
}
}
#endregion
//#region 读取产品数据
///// <summary>
///// 读取产品数据
///// </summary>
///// <param name="obj"></param>
//private void ReadProduct(object obj)
//{
// ProductList = dal.GetProductModelList(StartProductionTime, EndProductionTime, QuerySN);
//}
//#endregion
//#region 查询上一页
///// <summary>
///// 查询上一页
///// </summary>
///// <param name="obj"></param>
//private void ReadPageUpProduct(object obj)
//{
// if (ProductList != null && ProductList.Count > 0)
// {
// int id = ProductList.FirstOrDefault().ID;//获取第一个元素的ID
// ProductList = dal.GetProductModelList(StartProductionTime, EndProductionTime, id, true);
// }
// else
// {
// ReadProduct(0);
// }
//}
//#endregion
//#region 查询下一页
///// <summary>
///// 查询下一页
///// </summary>
///// <param name="obj"></param>
//private void ReadPageDownProduct(object obj)
//{
// if (ProductList != null && ProductList.Count > 0)
// {
// int id = ProductList.LastOrDefault().ID;//获取最后一个元素的ID
// ProductList = dal.GetProductModelList(StartProductionTime, EndProductionTime, id, false);
// }
// else
// {
// ReadProduct(0);
// }
//}
//#endregion
//#region 导出生产数据
///// <summary>
///// 生产数据类的所有属性集合
///// </summary>
//PropertyInfo[] ProInfo { get; set; }
///// <summary>
///// 导出生产数据
///// </summary>
///// <param name="obj"></param>
//private void ExportProduct(object obj)
//{
// ProductModel p = new ProductModel() { SnCode = "123", VisionValue = "57,57,10.51,10.051,9.75,10.677,10.303,9.986" };
// ExportProductionData1(p);
// p = new ProductModel() { SnCode = "456", VisionValue = "57,57,10.51,10.051,9.75,10.677,10.303,9.986" };
// ExportProductionData1(p);
// if (ProductList == null || ProductList.Count <= 0)
// {
// return;
// }
// SaveFileDialog sf = new SaveFileDialog();
// sf.Title = "导出生产数据";
// sf.DefaultExt = "txt";
// sf.Filter = "ini files (*.CSV)|*.CSV|All files (*.*)|*.*";
// sf.FilterIndex = 1;
// sf.RestoreDirectory = true;
// if ((bool)sf.ShowDialog())
// {
// //实例化字符串流
// StringBuilder sb = new StringBuilder();
// foreach (var item in ProInfo)
// {
// string n = item.Name;
// if (!n.Contains("Message"))
// {
// sb.Append(item.Name).Append(",");
// }
// }
// sb.Append(",\r");
// using (FileStream fs = new FileStream(sf.FileName, FileMode.Append))
// {
// using (StreamWriter sw = new StreamWriter(fs, UnicodeEncoding.GetEncoding("GB2312")))
// {
// if (ProductList != null && ProductList.Count > 0)
// {
// foreach (ProductModel item in ProductList)
// {
// foreach (var pr in ProInfo)
// {
// if (!pr.Name.Contains("Message"))
// {
// string s = pr.GetValue(item, null).ToString();
// if (s.Contains(","))//如果字符本身包含“,”,则要在外围加上双引号 不然会出意想不到的效果
// {
// s = "\"" + s + "\"";
// }
// sb.Append(s).Append(",");
// }
// }
// sb.Append(",\r");
// }
// }
// //将字符串流数据写入文件
// sw.WriteLine(sb);
// //刷新文件流
// sw.Flush();
// }
// }
// }
//}
//#endregion
}
}