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 { /// /// 机种编辑 /// partial class MainWindowVM { #region 已启用的机种集合 private ObservableCollection enableMachineList; /// /// 已启用的机种集合 /// public ObservableCollection EnableMachineList { get { return enableMachineList; } set { enableMachineList = value; this.RaisePropertyChanged("EnableMachineList"); } } #endregion #region 所有机种集合 private ObservableCollection allMachineList; /// /// 所有机种结合 /// public ObservableCollection AllMachineList { get { return allMachineList; } set { allMachineList = value; this.RaisePropertyChanged("AllMachineList"); } } #endregion #region 各种方法 /// /// 新增机种 /// public DelegateCommand AddMachineCommand { get; set; } /// /// 更新机种 /// public DelegateCommand UpdateMachineCommand { get; set; } /// /// 生产计数清零 /// public DelegateCommand CountClearedCommand { get; set; } /// /// 读取产品数据 /// public DelegateCommand ReadProductCommand { get; set; } /// /// 导出产品数据 /// public DelegateCommand ExportProductCommand { get; set; } /// /// 读取产品上一页 /// public DelegateCommand ReadPageUpProductCommand { get; set; } /// /// 读取产品下一页 /// public DelegateCommand ReadPageDownProductCommand { get; set; } #endregion #region 选中的要编辑机种 private MachineModel selectMachine; /// /// 选中的要编辑机种 /// public MachineModel SelectMachine { get { return selectMachine; } set { selectMachine = value; this.RaisePropertyChanged("SelectMachine"); } } #endregion #region 初始化机种编辑类 /// /// 初始化机种编辑类 /// public void InitializeMachineEditor() { //1、获取已启用的机种集合 AllMachineList = dal.GetALLMachineList(); EnableMachineList = new ObservableCollection(AllMachineList.Where(w => w.EnableS == true).ToList()); //获取当前生产机种 CurrentMachineID = SystemPar.DefaultMachineID; AddMachineCommand = new DelegateCommand(); AddMachineCommand.ExecuteAction = new Action(AddMachine); UpdateMachineCommand = new DelegateCommand(); UpdateMachineCommand.ExecuteAction = new Action(UpdateMachine); CountClearedCommand = new DelegateCommand(); CountClearedCommand.ExecuteAction = new Action(CountCleared); StartProductionTime = DateTime.Now; EndProductionTime = DateTime.Now; //this.ReadProductCommand = new DelegateCommand(); //this.ReadProductCommand.ExecuteAction = new Action(ReadProduct); //this.ReadPageUpProductCommand = new DelegateCommand(); //this.ReadPageUpProductCommand.ExecuteAction = new Action(ReadPageUpProduct); //this.ReadPageDownProductCommand = new DelegateCommand(); //this.ReadPageDownProductCommand.ExecuteAction = new Action(ReadPageDownProduct); //this.ExportProductCommand = new DelegateCommand(); //this.ExportProductCommand.ExecuteAction = new Action(ExportProduct); QuerySN = ""; } #endregion #region 生产计数清零 /// /// 生产计数清零 /// /// 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 更新机种 /// /// 更新机种 /// /// private void UpdateMachine(object obj) { if (MessageBox.Show("你确定更新当前选中机种吗?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No) { return; } if (dal.UpdateMachineData(SelectMachine) > 0) { EnableMachineList = new ObservableCollection(AllMachineList.Where(w => w.EnableS == true).ToList()); MessageBox.Show("更新成功"); } else { MessageBox.Show("更新失败"); } } #endregion #region 新增机种 /// /// 新增机种 /// /// 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(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; /// /// 查询开始时间 /// public DateTime StartProductionTime { get { return startProductionTime; } set { startProductionTime = value; this.RaisePropertyChanged("StartProductionTime"); } } #endregion #region 查询结束时间 private DateTime endProductionTime; /// /// 查询结束时间 /// 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; /// /// 查询的SN /// public string QuerySN { get { return querySN; } set { querySN = value; this.RaisePropertyChanged("QuerySN"); } } #endregion #region 时间段产品数据集合 private ObservableCollection productList; /// /// 时间段产品数据集合 /// public ObservableCollection ProductList { get { return productList; } set { productList = value; this.RaisePropertyChanged("ProductList"); } } #endregion //#region 读取产品数据 ///// ///// 读取产品数据 ///// ///// //private void ReadProduct(object obj) //{ // ProductList = dal.GetProductModelList(StartProductionTime, EndProductionTime, QuerySN); //} //#endregion //#region 查询上一页 ///// ///// 查询上一页 ///// ///// //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 查询下一页 ///// ///// 查询下一页 ///// ///// //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 导出生产数据 ///// ///// 生产数据类的所有属性集合 ///// //PropertyInfo[] ProInfo { get; set; } ///// ///// 导出生产数据 ///// ///// //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 } }