using Community; using DAL; using HalconDotNet; using MahApps.Metro.Controls.Dialogs; using Model; using Model.MotionControl; using Model.Vision; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using System.Windows.Threading; using ViewModel.MotionControl; using ViewModel.Robots; using ViewModel.Vision; using Views; namespace ViewModel { public partial class MainWindowVM : NotificationObject { const int Y000绿灯 = 0; const int Y001红灯 = 1; const int Y002黄灯 = 2; const int Y003蜂鸣器 = 3; const int X000启动 = 0; const int X001停止 = 1; const int X002急停 = 2; #region 生成Grid状态 public (Grid, List) GetGrid(int col, int row = 0) { var handModels = new List(); Grid gd = new Grid(); for (int i = 0; i < col; i++) { gd.ColumnDefinitions.Add(new ColumnDefinition()); //添加列 HandModel sm = new HandModel() { Number = i + 1, State = new ShowInfo() { StatuInfo = (i + 1).ToString() } }; gd.Children.Add(sm.Labels); sm.Labels.SetValue(Grid.RowProperty, 0); //设置按钮所在Grid控件的行 sm.Labels.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 handModels.Add(sm); } return (gd, handModels); } #endregion public FlowModel ManulFlow { get; set; } = new FlowModel(); private DelegateCommand _ManulTest; public DelegateCommand ManulTest => _ManulTest = _ManulTest ?? new DelegateCommand() { ExecuteAction = ExecuteManulTest }; void ExecuteManulTest(object obj) { var cmd = obj.ToString(); if (testing) return; testing = true; // TorqueFlow.Step = 1; switch (cmd) { case "手动测试轴": ManulFlow.Step = 0; break; } Task.Run(() => { while (testing) { Thread.Sleep(10); switch (cmd) { case "手动测试轴": try { TestAxisRun(ManulFlow); } catch (Exception) { testing = false; } break; } } }); } private MotionPointModel GetMotionPoint(string pointName, bool copy = false) { var tempPoint = CurrentMachine.MotionPointList.Where(w => w.PointName == pointName).FirstOrDefault(); if (tempPoint != null) { if (copy) { tempPoint = Coper(tempPoint); } tempPoint.PointRunStep = GengralProcessEnum.Start; } return tempPoint; } private MotionPointModel GetMotionPoint(int pointType, bool copy = false) { var tempPoint = CurrentMachine.MotionPointList.Where(w => w.PointType == pointType).FirstOrDefault(); if (tempPoint != null) { if (copy) { tempPoint = Coper(tempPoint); } tempPoint.PointRunStep = GengralProcessEnum.Start; } return tempPoint; } bool testing; private string gridWidth; /// /// Grid的宽 /// public string GridWidth { get { return gridWidth; } set { gridWidth = value; this.RaisePropertyChanged("GridWidth"); } } #region 通讯服务器对象 private CommunicationServerVM serverComm; /// /// 通讯服务器对象 /// public CommunicationServerVM ServerComm { get { return serverComm; } set { serverComm = value; RaisePropertyChanged("ServerComm"); } } #endregion #region ERP交互管理类 private ERPManageVM eRPVM; /// /// ERP交互管理类 /// public ERPManageVM ERPVM { get { return eRPVM; } set { eRPVM = value; RaisePropertyChanged("ERPVM"); } } #endregion #region 视觉对象 private HalconVisual visualA; /// /// 视觉对象A /// public HalconVisual VisualA { get { return visualA; } set { visualA = value; RaisePropertyChanged("VisualA"); } } private HalconVisual _VisualA1; public HalconVisual VisualA1 { get { return _VisualA1; } set { _VisualA1 = value; this.RaisePropertyChanged("VisualA1"); } } private HalconVisual visualB; /// /// 视觉对象B /// public HalconVisual VisualB { get { return visualB; } set { visualB = value; RaisePropertyChanged("VisualB"); } } private HalconVisual _VisualB1; public HalconVisual VisualB1 { get { return _VisualB1; } set { _VisualB1 = value; this.RaisePropertyChanged("VisualB1"); } } private HalconVisual visualC; /// /// 视觉对象C /// public HalconVisual VisualC { get { return visualC; } set { visualC = value; RaisePropertyChanged("VisualC"); } } private HalconVisual _VisualC1; public HalconVisual VisualC1 { get { return _VisualC1; } set { _VisualC1 = value; this.RaisePropertyChanged("VisualC1"); } } private HalconVisual visualD; /// /// 视觉对象D /// public HalconVisual VisualD { get { return visualD; } set { visualD = value; RaisePropertyChanged("VisualD"); } } private HalconVisual _VisualD1; public HalconVisual VisualD1 { get { return _VisualD1; } set { _VisualD1 = value; this.RaisePropertyChanged("VisualD1"); } } private HalconVisual visualE; /// /// 视觉对象E /// public HalconVisual VisualE { get { return visualE; } set { visualE = value; RaisePropertyChanged("VisualE"); } } private HalconVisual visualF; /// /// 视觉对象F /// public HalconVisual VisualF { get { return visualF; } set { visualF = value; RaisePropertyChanged("VisualF"); } } private HalconVisual _VisualG; public HalconVisual VisualG { get { return _VisualG; } set { _VisualG = value; this.RaisePropertyChanged("VisualG"); } } #endregion #region 串口相关 private SerialComVM sCVM; /// /// 串口服务对象 /// public SerialComVM SCVM { get { return sCVM; } set { sCVM = value; this.RaisePropertyChanged("SCVM"); } } #endregion #region 机器人对象 private FeiYuRobotVM robot; /// /// 机器人对象 /// public FeiYuRobotVM RobotA { get { return robot; } set { robot = value; RaisePropertyChanged("Robot"); } } private FeiYuRobotVM robotB; /// /// 机器人对象 /// public FeiYuRobotVM RobotB { get { return robotB; } set { robotB = value; RaisePropertyChanged("RobotB"); } } #endregion #region 运动控制对象VM private MotionControlVM mCVM; /// /// 运动控制对象VM /// public MotionControlVM MCVM { get { return mCVM; } set { mCVM = value; RaisePropertyChanged("MCVM"); } } #endregion #region 软件开启时间 /// /// 软件开启时间 /// DateTime OpenTime { get; set; } /// /// 生产运行开始时间 /// DateTime RunStartTime { get; set; } private string openTimeStr; /// /// 软件开启时间 /// public string OpenTimeStr { get { return openTimeStr; } set { openTimeStr = value; this.RaisePropertyChanged("OpenTimeStr"); } } #region CT时间 private TimeSpan cTTime; /// /// CT时间 /// public TimeSpan CTTime { get { return cTTime; } set { cTTime = value; ShowCtTime = value.TotalSeconds.ToString(); this.RaisePropertyChanged("CTTime"); } } private string showCtTime; /// /// 显示CT时间 /// public string ShowCtTime { get { return showCtTime; } set { showCtTime = value; this.RaisePropertyChanged("ShowCtTime"); } } #endregion #endregion #region 图表相关 private ChartVM cVM; /// /// 图表对象 /// public ChartVM CVM { get { return cVM; } set { cVM = value; this.RaisePropertyChanged("CVM"); } } #endregion #region Ttay料盘 private TrayModel _tray1; /// /// 料盘1 /// public TrayModel Tray1 { get { return _tray1; } set { _tray1 = value; this.RaisePropertyChanged("Tray1"); } } private TrayModel _tray2; /// /// 料盘2 Blue 待放料,Green 已放料 /// public TrayModel Tray2 { get { return _tray2; } set { _tray2 = value; this.RaisePropertyChanged("Tray2"); } } private TrayModel _tray3; /// /// 料盘3 /// public TrayModel Tray3 { get { return _tray3; } set { _tray3 = value; this.RaisePropertyChanged("Tray3"); } } private TrayModel _tray4; /// /// 料盘4 /// public TrayModel Tray4 { get { return _tray4; } set { _tray4 = value; this.RaisePropertyChanged("Tray4"); } } private TrayModel _tray5; /// /// 料盘5 /// public TrayModel Tray5 { get { return _tray5; } set { _tray5 = value; this.RaisePropertyChanged("Tray5"); } } private TrayModel _tray6; /// /// 料盘6 /// public TrayModel Tray6 { get { return _tray6; } set { _tray6 = value; this.RaisePropertyChanged("Tray6"); } } private TrayModel _tray7; /// /// 料盘7 /// public TrayModel Tray7 { get { return _tray7; } set { _tray7 = value; this.RaisePropertyChanged("Tray7"); } } private TrayModel _tray8; /// /// 料盘8 /// public TrayModel Tray8 { get { return _tray8; } set { _tray8 = value; this.RaisePropertyChanged("Tray8"); } } #endregion #region 空跑模式 private bool runningMode; /// /// 空跑模式 /// public bool RunningMode { get { return runningMode; } set { runningMode = value; if (!value) { RunningModePlas = false; } this.RaisePropertyChanged("RunningMode"); } } private bool runningModePlas; /// /// 联机空跑模式 /// public bool RunningModePlas { get { return runningModePlas; } set { runningModePlas = value; if (value) { RunningMode = true; } this.RaisePropertyChanged("RunningModePlas"); } } #endregion #region 清理机台模式 private bool cleanMachine; /// /// 清理机台模式 /// public bool CleanMachine { get { return cleanMachine; } set { cleanMachine = value; this.RaisePropertyChanged("CleanMachine"); } } #endregion #region 不进行MES验证 private bool mesCheck; /// /// 不进行MES验证 /// public bool MesCheck { get { return mesCheck; } set { mesCheck = value; this.RaisePropertyChanged("MesCheck"); } } #endregion #region 不进行数据上传 private bool noUpData; /// /// 不进行数据上传 /// public bool NoUpData { get { return noUpData; } set { noUpData = value; this.RaisePropertyChanged("NoUpData"); } } #endregion #region 系统设置权限控制 private string permissionButtonName; /// /// 权限按钮名称 /// public string PermissionButtonName { get { return permissionButtonName; } set { permissionButtonName = value; this.RaisePropertyChanged("PermissionButtonName"); } } #region 管理员权限 private Visibility allVis; /// /// 管理员权限 /// public Visibility AllVis { get { return allVis; } set { allVis = value; this.RaisePropertyChanged("AllVis"); if (HVlist != null) { foreach (HalconVisual hv in HVlist) { hv.DisplayLevel = value; } } if (StandVMList != null) { foreach (var item in StandVMList) { item.IsEnable = value == Visibility.Visible ? true : false; } } //PermissionButtonName = Application.Current.Resources[value == Visibility.Collapsed ? "编辑模式" : "普通模式"].ToString(); PermissionButtonName = value == Visibility.Collapsed ? "编辑模式" : "普通模式"; if (value == Visibility.Collapsed) { SuperPrivilege = value; } } } #endregion #region 超级权限 private Visibility superPrivilege; /// /// 超级权限 /// public Visibility SuperPrivilege { get { return superPrivilege; } set { superPrivilege = value; this.RaisePropertyChanged("SuperPrivilege"); if (value == Visibility.Visible) { AllVis = value; } if (HVlist != null) { foreach (HalconVisual hv in HVlist) { hv.SuperPrivilege = value; } } if (MCVM != null) { MCVM.DisplayLevel = value; } } } #endregion #endregion #region 系统参数对象 private SystemParameter systemPar; /// /// 系统参数对象 /// public SystemParameter SystemPar { get { return systemPar; } set { systemPar = value; this.RaisePropertyChanged("SystemPar"); } } #endregion #region 数据对象 /// /// 数据对象 /// public MainWindowDAL dal = new MainWindowDAL(); /// /// 视觉数据对象 /// public VisualDAL Vdal = new VisualDAL(); #endregion #region 控制卡 /// /// Io控制卡ID /// public ushort IoCardID { get; set; } #endregion #region 输入信号集合 private List _inputList; /// /// 输入信号集合 /// public List InputList { get { return _inputList; } set { _inputList = value; if (value != null && value.Count > 0) { InputGrid = GetInputGrid(); } } } #endregion #region 输出信号集合 private List _outputList; /// /// 输出信号集合 /// public List OutputList { get { return _outputList; } set { _outputList = value; if (value != null && value.Count > 0) { OutputGrid = GetOutputGrid(); } } } #endregion #region 各线程 /// /// 状态监控线程 /// Thread ThStateMonitoring { get; set; } /// /// 运行线程 /// Thread ThRun { get; set; } /// /// 网络连接控制线程 /// Thread ThConnect { get; set; } #endregion #region 机器运行状态 private RunStatuEnum runStatu; /// /// 机器运行状态枚举 /// public RunStatuEnum RunStatu { get { return runStatu; } set { runStatu = value; switch (value) { case RunStatuEnum.Prepare: ShowStatu.StatusBackground = ColorStr.Yellow; ShowStatu.StatusForeground = ColorStr.Black; ShowStatu.StatuInfo = "准备中"; if (OutputList != null) { OutputList[Y001红灯].Switchs = false; //红灯 OutputList[Y002黄灯].Switchs = true; //黄灯 OutputList[Y000绿灯].Switchs = false; //绿灯 OutputList[Y003蜂鸣器].Switchs = false; //蜂鸣器 } break; case RunStatuEnum.Prepareations: ShowStatu.StatusBackground = ColorStr.Green; ShowStatu.StatusForeground = ColorStr.Black; ShowStatu.StatuInfo = "准备就绪"; OutputList[Y001红灯].Switchs = false; //红灯 OutputList[Y002黄灯].Switchs = true; //黄灯 OutputList[Y000绿灯].Switchs = false; //绿灯 OutputList[Y003蜂鸣器].Switchs = false; //蜂鸣器 break; case RunStatuEnum.Normal: ShowStatu.StatusBackground = ColorStr.Green; ShowStatu.StatusForeground = ColorStr.Black; ShowStatu.StatuInfo = "运行中"; OutputList[Y001红灯].Switchs = false; //红灯 OutputList[Y002黄灯].Switchs = false; //黄灯 OutputList[Y000绿灯].Switchs = true; //绿灯 OutputList[Y003蜂鸣器].Switchs = false; //蜂鸣器 break; case RunStatuEnum.Stop: ShowStatu.StatusBackground = ColorStr.Yellow; ShowStatu.StatusForeground = ColorStr.Black; ShowStatu.StatuInfo = "停止中"; OutputList[Y001红灯].Switchs = false; //红灯 OutputList[Y002黄灯].Switchs = true; //黄灯 OutputList[Y000绿灯].Switchs = false; //绿灯 OutputList[Y003蜂鸣器].Switchs = false; //蜂鸣器 break; case RunStatuEnum.EmgStop: ShowStatu.StatusBackground = ColorStr.Yellow; ShowStatu.StatusForeground = ColorStr.Black; ShowStatu.StatuInfo = "紧急停止中"; OutputList[Y001红灯].Switchs = true; //红灯 OutputList[Y002黄灯].Switchs = true; //黄灯 OutputList[Y000绿灯].Switchs = false; //绿灯 OutputList[Y003蜂鸣器].Switchs = false; //蜂鸣器 break; case RunStatuEnum.Exceptional: ShowStatu.StatusBackground = ColorStr.Red; ShowStatu.StatusForeground = ColorStr.White; OutputList[Y001红灯].Switchs = true; //红灯 OutputList[Y002黄灯].Switchs = false; //黄灯 OutputList[Y000绿灯].Switchs = false; //绿灯 OutputList[Y003蜂鸣器].Switchs = true; //蜂鸣器 break; } } } private ShowInfo showStatu; /// /// 机器状态 显示 /// public ShowInfo ShowStatu { get { return showStatu; } set { showStatu = value; this.RaisePropertyChanged("ShowStatu"); } } #endregion #region 异常信息集合 private ObservableCollection excepList; /// /// 异常信息集合 /// public ObservableCollection ExcepList { get { return excepList; } set { excepList = value; } } #endregion #region 各种运行方法 /// /// 开始运行 /// public DelegateCommand StartCommand { get; set; } /// /// 停止运行 /// public DelegateCommand StopCommand { get; set; } /// /// 重启软件 /// public DelegateCommand ResetSoftCommand { get; set; } /// /// 退出程序 /// public DelegateCommand CloseCommand { get; set; } /// /// 切换语言 /// public DelegateCommand SwitchLanguageCommand { get; set; } /// /// 保存站别参数 /// public DelegateCommand SaveStandCommand { get; set; } #endregion #region 选中的TabItem private TabItem selectTabItem; /// /// 选中的TabItem /// public TabItem SelectTabItem { get { return selectTabItem; } set { selectTabItem = value; if (AllVis == Visibility.Visible) { if (value.Tag != null && value.Tag.ToString() == "v") { GridWidth = "0"; } else { GridWidth = "30*"; } } else { GridWidth = "30*"; } this.RaisePropertyChanged("SelectTabItem"); } } #endregion #region 当前生产的机种ID private int currentMachineID; /// /// 当前生产的机种 /// public int CurrentMachineID { get { return currentMachineID; } set { currentMachineID = value; this.RaisePropertyChanged("CurrentMachineID"); if (value <= 0) { return; } CurrentMachine = AllMachineList.Where(w => w.ID == value).FirstOrDefault(); //dal.ByIDGeMachine(value); //获取当前生产机种对象 if (RobotA != null) { RobotA.MachineObj = CurrentMachine; } if (RobotB != null) { RobotB.MachineObj = CurrentMachine; } if (MCVM != null) { MCVM.MachineObj = CurrentMachine; } if (HVlist != null) { foreach (HalconVisual hv in HVlist) { hv.MachineObj = CurrentMachine; hv.QRModelInitialize = false; hv.CodeModelInitialize = false; } } //异步新增项 这是微软的BUG 只能这么麻烦了 MyDispatcher.BeginInvoke(DispatcherPriority.Background, new ParameterizedThreadStart(ShuaxingContent), info); SystemPar.DefaultMachineID = value; dal.UpdateSystemPar(SystemPar); //更新默认机种 } } private void ShuaxingContent(object obj) { Tray1 = new TrayModel(CurrentMachine, "上料料盘", 1); Tray2 = new TrayModel(CurrentMachine, "下料料盘", 2); Robot_ShuaxinPointing(0); } #endregion #region 当前生产机种对象 private MachineModel currentMachine; /// /// 当前生产机种对象 /// public MachineModel CurrentMachine { get { return currentMachine; } set { currentMachine = value; this.RaisePropertyChanged("CurrentMachine"); } } #endregion #region 各步骤 #endregion #region 动态Grid控件 private Grid inputGrid; /// /// 输入信号Grid /// public Grid InputGrid { get { return inputGrid; } set { inputGrid = value; this.RaisePropertyChanged("InputGrid"); } } private Grid outputGrid; /// /// 输出信号Grid /// public Grid OutputGrid { get { return outputGrid; } set { outputGrid = value; this.RaisePropertyChanged("OutputGrid"); } } #endregion #region 光源串口号 private string serialProt; /// /// 光源串口号 /// public string SerialProt { get { return serialProt; } set { if (!string.IsNullOrEmpty(value) && LightCOM != null) { if (!LightCOM.OpenSerialCom(value)) { // MessageBox.Show(LightCOM.ErrorMag); } else { serialProt = value; if (value != SystemPar.SerialProt) { SystemPar.SerialProt = value; try { dal.UpdateSystemPar(SystemPar); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } this.RaisePropertyChanged("SerialProt"); } } } #endregion #region 光源串口对象 private SerialCom lightCOM; /// /// 光源串口对象 /// public SerialCom LightCOM { get { return lightCOM; } set { lightCOM = value; this.RaisePropertyChanged("LightCOM"); } } #endregion #region 系统功能控件集合 private ObservableCollection tabItemList; /// /// 系统功能控件集合 /// public ObservableCollection TabItemList { get { return tabItemList; } set { tabItemList = value; this.RaisePropertyChanged("TabItemList"); } } #endregion #region 视觉对象VM集合 /// /// 视觉对象VM集合 /// List HVlist { get; set; } #endregion #region 选中的站别 private StandModel selectStand; /// /// 选中的站别 /// public StandModel SelectStand { get { return selectStand; } set { selectStand = value; this.RaisePropertyChanged("SelectStand"); } } #endregion #region 站别逻辑控制集合 private List standVMList; /// /// 站别逻辑控制集合 /// public List StandVMList { get { return standVMList; } set { standVMList = value; RaisePropertyChanged("StandVMList"); } } #endregion #region 构造函数 /// /// 构造函数 /// public MainWindowVM(Window mywd) { Mywindow = mywd; MyDispatcher = mywd.Dispatcher; GridWidth = "30*"; Initialize(); //初始化 LoadingFunctionInterface();//加载功能界面 #region 实例化各运行方法 this.StartCommand = new DelegateCommand(); this.StartCommand.ExecuteAction = new Action(this.Start); this.StopCommand = new DelegateCommand(); this.StopCommand.ExecuteAction = new Action(this.Stop); this.ResetSoftCommand = new DelegateCommand(); this.ResetSoftCommand.ExecuteAction = new Action(ResetSoft); this.CloseCommand = new DelegateCommand(); this.CloseCommand.ExecuteAction = new Action(this.Close); this.SwitchLanguageCommand = new DelegateCommand(); this.SwitchLanguageCommand.ExecuteAction = new Action(SwitchLanguage); this.SaveStandCommand = new DelegateCommand(); this.SaveStandCommand.ExecuteAction = new Action(SaveStand); #endregion GoHomeFlow = new FlowModel("回原点流程"); //开启状态监控线程 if (ThStateMonitoring == null) { ThStateMonitoring = new Thread(StateMonitoring); ThStateMonitoring.IsBackground = true; ThStateMonitoring.Start(); } } private void YaliyiConn_ReceiveCallback1(string str) { throw new NotImplementedException(); } /// /// 保存站别信息 /// /// private void SaveStand(object obj) { if (SelectStand != null) { if (dal.UpdataStandModel(SelectStand) <= 0) { MyMessageBox.Show("保存失败"); } } else { MyMessageBox.Show("请选中要保存的点位"); } } #endregion #region 1、系统初始化 /// /// 系统初始化 /// public void Initialize() { //1、初始化系统参数 SystemPar = dal.GetSystemPar(); AllVis = Visibility.Collapsed; OpenTime = DateTime.Now; ShowStatu = new ShowInfo(); RunStatu = RunStatuEnum.Prepare; ExcepList = new ObservableCollection(); //LightCOM = new SerialCom(); //SerialProt = SystemPar.SerialProt; //2、初始化输入输出信号 InputList = dal.GetInputList(0); OutputList = dal.GetOutputList(0); ERPVM = new ERPManageVM(SystemPar.ID, this.dal); SCVM = new SerialComVM(SystemPar.ID); //串口对象 ServerComm = new CommunicationServerVM(SystemPar.ID, Mywindow.Dispatcher); ServerComm.MonitorsWitch = true; List standModels = dal.GetStandList(); List list = new List(); StandVM sm; foreach (var item in standModels) { sm = new StandVM(item); sm.MyConnect = ServerComm.CommList.Where(w => w.MyType == item.ID).FirstOrDefault(); list.Add(sm); } StandVMList = list; CVM = new ChartVM();//图表对象 //机种参数 InitializeMachineEditor(); RunStartTime = DateTime.Now;//产能计时开始 //NextStation = ServerComm.CommList.Where(w => w.ShortCode == "NS").FirstOrDefault(); //NextStation.ReceiveCallback += NextStation_ReceiveCallback; //FrontStation = ServerComm.CommList.Where(w => w.ShortCode == "FS").FirstOrDefault(); //FrontStation.ReceiveCallback += FrontStation_ReceiveCallback; } #region 获取输出信号Grid /// /// 获取输出信号Grid /// /// private Grid GetOutputGrid() { Grid g = new Grid(); int row = 0; int column = 0; int count = OutputList.Count; if (count <= 16) { row = count; column = 1; } else { row = 16; column = count / 16; if (count % 16 > 0) { column = column + 1; } } int number = 0; CheckBox cb; for (int i = 0; i < column; i++) { g.ColumnDefinitions.Add(new ColumnDefinition()); //添加列 for (int j = 0; j < row; j++) { if (i == 0) { g.RowDefinitions.Add(new RowDefinition()); //添加行 } if (number < count) { cb = GetCheckBox(OutputList[number]); //加一个控件修饰控件 用于控制字体的大小 Viewbox vb = new Viewbox(); vb.Stretch = Stretch.Uniform; vb.StretchDirection = StretchDirection.Both; vb.HorizontalAlignment = HorizontalAlignment.Left; vb.Child = cb; g.Children.Add(vb); vb.SetValue(Grid.RowProperty, j);//设置按钮所在Grid控件的行 vb.SetValue(Grid.ColumnProperty, i);//设置按钮所在Grid控件的列 //g.Children.Add(cb); //cb.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 //cb.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 number++; } } } return g; } /// /// 获取CheckBox /// /// /// private CheckBox GetCheckBox(OutputSignal os) { CheckBox cb = new CheckBox(); //指定样式 cb.Style = (Style)Mywindow.FindResource("CheckBoxStyle");//CheckBoxStyle 这个样式是引用的资源文件中的样式名称 //设置数据上下文 cb.DataContext = os; //绑定背景颜色 cb.SetBinding(CheckBox.BackgroundProperty, new Binding() { Path = new PropertyPath("OutputColor"), Mode = BindingMode.TwoWay }); //绑定内容 cb.SetBinding(CheckBox.ContentProperty, new Binding() { Path = new PropertyPath("OutputMeaning"), Mode = BindingMode.TwoWay }); cb.SetBinding(CheckBox.ToolTipProperty, new Binding() { Path = new PropertyPath("IoNumber"), Mode = BindingMode.TwoWay }); //if (Application.Current.Resources[os.OutputMeaning] != null) //{ // cb.Content = Application.Current.Resources[os.OutputMeaning].ToString(); //} //else //{ // cb.Content = os.OutputMeaning; //} //绑定勾属性 cb.SetBinding(CheckBox.IsCheckedProperty, new Binding() { Path = new PropertyPath("Switchs"), Mode = BindingMode.TwoWay }); //设置前景色 cb.Foreground = new SolidColorBrush(Colors.White); //Labels.MouseDoubleClick += Labels_MouseDoubleClick; //增加鼠标双击事件 cb.FontFamily = new System.Windows.Media.FontFamily("楷体"); cb.Margin = new Thickness(1); //设置边距 cb.HorizontalContentAlignment = HorizontalAlignment.Left; cb.VerticalContentAlignment = VerticalAlignment.Center; return cb; } #endregion #region 获取输入信号Grid /// /// 获取输入信号Grid /// /// private Grid GetInputGrid() { Grid g = new Grid(); int row = 0; int coloum = 0; int count = InputList.Count; if (count <= 16) { row = count; coloum = 1; } else { row = 16; coloum = count / 16; if (count % 16 > 0) { coloum = coloum + 1; } } int number = 0; Label label; for (int i = 0; i < coloum; i++) { g.ColumnDefinitions.Add(new ColumnDefinition()); //添加列 for (int j = 0; j < row; j++) { if (i == 0) { g.RowDefinitions.Add(new RowDefinition()); //添加行 } if (number < count) { label = GetLabel(InputList[number]); g.Children.Add(label); label.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 label.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 number++; } } } return g; } private Label GetLabel(InputSignal Is) { Label Labels = new Label(); //数据样式 Labels.Style = (Style)Mywindow.FindResource("Mylabel1");//LabelStyle 这个样式是引用的资源文件中的样式名称 //设置数据上下文 Labels.DataContext = Is; //绑定背景颜色 Labels.SetBinding(Label.BackgroundProperty, new Binding() { Path = new PropertyPath("InputColor"), Mode = BindingMode.TwoWay }); //绑定内容 Labels.SetBinding(Label.ContentProperty, new Binding() { Path = new PropertyPath("InputMeaning"), Mode = BindingMode.TwoWay }); Labels.SetBinding(Label.ToolTipProperty, new Binding() { Path = new PropertyPath("IoNumber"), Mode = BindingMode.TwoWay }); //设置前景色 Labels.Foreground = new SolidColorBrush(Colors.White); //Labels.MouseDoubleClick += Labels_MouseDoubleClick; //增加鼠标双击事件 Labels.FontFamily = new System.Windows.Media.FontFamily("楷体"); Labels.Margin = new Thickness(1); //设置边距 Labels.HorizontalContentAlignment = HorizontalAlignment.Center; Labels.VerticalContentAlignment = VerticalAlignment.Center; return Labels; } #endregion #region 获取IO控制卡 /// /// 初始化IO控制卡 /// private void GetIOCard() { //IO卡ID集合 List ioCarIdList = HelperTxp.GetIoCard(); if (ioCarIdList == null) { IoCardID = 0; } else { IoCardID = ioCarIdList[0]; //获取Io控制卡ID } } #endregion #endregion #region 2、状态监控 /// /// 状态监控 /// private void StateMonitoring() { while (true) { // Thread.Sleep(10); //记录当前软件运行时间 //TimeSpan ts = DateTime.Now.Subtract(OpenTime).Duration(); //OpenTimeStr = string.Format("{0}天{1}小时{2}分{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds); if (MCVM == null || MCVM.ConnectAllCard()) { #region 1、输入信号状态 foreach (InputSignal input in InputList) { input.GetInputLever(); } if (InputList[X000启动].LightUp) { Start(0);//启动信号 } if (InputList[X001停止].LightUp) { Stop(0); //停止信号 } if (InputList[X002急停].LightUp) { EmgStop();// 急停信号 } #endregion #region 获取轴状态 if (MCVM != null) { MCVM.OrginALL(InputList[X002急停]); if (RunStatu != RunStatuEnum.Normal) { MCVM.PointRun(); mCVM.RunMotionTrajectory(); } } #endregion DualControlRun();//双控运行 GoHome();//回原点 } } } #region 回原点 /// /// 回远点流程 /// FlowModel GoHomeFlow { get; set; } bool homeing; /// /// 回原点 /// private void GoHome() { switch (GoHomeFlow.RunStep) { case GengralProcessEnum.Start: //优先机械手回原点 if (RobotA != null) { homeing = true; if (RobotA.HomeStep == GengralProcessEnum.End) { GoHomeFlow.RunStep++; } else { RobotA.RobotOrgin(); } } else { GoHomeFlow.RunStep++; } break; case GengralProcessEnum.Step1: //优先机械手回原点 if (RobotB != null) { if (RobotB.HomeStep == GengralProcessEnum.End) { GoHomeFlow.RunStep++; } else { RobotB.RobotOrgin(); } } else { GoHomeFlow.RunStep++; } break; case GengralProcessEnum.Step2: if (MCVM != null) { MCVM.HomeStep = GengralProcessEnum.Start; GoHomeFlow.RunStep++; } else { GoHomeFlow.RunStep = GengralProcessEnum.End; RunStatu = RunStatuEnum.Prepareations; //准备就绪 } break; case GengralProcessEnum.Step3: if (MCVM.HomeStep == GengralProcessEnum.End) { GoHomeFlow.RunStep++; RunStatu = RunStatuEnum.Prepareations; //准备就绪 } break; case GengralProcessEnum.Step4: GoHomeFlow.RunStep++; break; case GengralProcessEnum.Step5: GoHomeFlow.RunStep++; break; case GengralProcessEnum.Step6: homeing = false; GoHomeFlow.RunStep = GengralProcessEnum.End; RunStatu = RunStatuEnum.Prepareations; //准备就绪 break; } } #endregion #region 其他运行内容 /// /// 其他运行内容 /// private void DualControlRun() { } public DateTime FlipTime { get; set; } = DateTime.Now; #endregion #endregion #region 3、运行 /// /// 启动运行 /// private void Run() { while (true) { if (RunStatu == RunStatuEnum.Normal) { try { } catch (Exception ex) { ExcepStop(ex.Message); } } else { } } } #region 上一站连接 /// /// 上一站连接 /// public CommunicationManagementClass FrontStation { get; set; } #endregion #region 下一站连接 /// /// 下一站连接 /// public CommunicationManagementClass NextStation { get; set; } #endregion #region 发送消息给下一站 /// /// 发送载具状态的字符串 /// string SendNextStr { get; set; } DateTime SendNextStationTime { get; set; } /// /// 发送消息给下一站 /// private void SendNextStation() { if (SendNextStationTime.AddMilliseconds(500) < DateTime.Now) { NextStation.SendInfo(SendNextStr); SendNextStationTime = DateTime.Now; } } #endregion #region 发送信息给上一站 /// /// 当前发给上一站的信息 /// string CurrSendInfor { get; set; } DateTime SendFrontStationTime { get; set; } /// /// 发送信息给上一站 /// /// private void SendFrontStation(string CommandStr) { if (SendFrontStationTime.AddMilliseconds(500) < DateTime.Now) { FrontStation.SendInfo(CommandStr); CurrSendInfor = CommandStr; SendFrontStationTime = DateTime.Now; } } #endregion #endregion #region 4、加载功能界面 /// /// 加载功能界面 /// private void LoadingFunctionInterface() { TabItemList = new ObservableCollection(); //添加主页界面 MainInterface mi = new MainInterface(); TabItem tabmi = new TabItem() { Content = mi, Tag = "V" }; tabmi.Header = "主界面"; TabItemList.Add(tabmi); if (SystemPar.VisionBool) { List Camlist = Vdal.GetCameraList(); HVlist = new List(); foreach (FeiYuCamere cam in Camlist) { HalconVisual hv = new HalconVisual(CurrentMachine, LightCOM, cam); HVlist.Add(hv); TabItem tabi = new TabItem() { Content = hv.MyWindow, Tag = "v" }; //tabi.SetResourceReference(TabItem.HeaderProperty, cam.VisualName); //设定资源引用 即可进行语言的切换 tabi.Header = cam.VisualName; tabi.SetBinding(TabItem.VisibilityProperty, new Binding("AllVis")); TabItemList.Add(tabi); } if (HVlist.Count > 0) VisualA = HVlist[0]; if (HVlist.Count > 1) { VisualA1 = HVlist[1]; VisualA1.HWindowWPF2 = VisualA.HWindowWPF2; } if (HVlist.Count > 2) VisualB = HVlist[2]; if (HVlist.Count > 3) { VisualB1 = HVlist[3]; VisualB1.HWindowWPF2 = VisualB.HWindowWPF2; } if (HVlist.Count > 4) VisualC = HVlist[4]; if (HVlist.Count > 5) { VisualC1 = HVlist[5]; VisualC1.HWindowWPF2 = VisualC.HWindowWPF2; } if (HVlist.Count > 6) VisualD = HVlist[6]; //if (HVlist.Count > 7) // VisualD1 = HVlist[7]; if (HVlist.Count > 7) VisualE = HVlist[7]; if (HVlist.Count > 9) VisualF = HVlist[9]; if (HVlist.Count > 10) VisualG = HVlist[10]; } if (SystemPar.MotionControlBool) { MCVM = new MotionControlVM(CurrentMachine, InputList, OutputList); MCVM.eventExcepReport += ExcepStop; TabItem tbai = new TabItem() { Content = MCVM.MyWindow }; // tbai.SetResourceReference(TabItem.HeaderProperty, "运动控制"); //设定资源引用 即可进行语言的切换 tbai.Header = "运动控制"; tbai.SetBinding(TabItem.VisibilityProperty, new Binding("AllVis")); TabItemList.Add(tbai); } if (SystemPar.RobotBool) { switch (SystemPar.RobotBrand) { case 0: RobotA = new EpsonRobotVM(CurrentMachine, 1);//初始化爱普生机器人 RobotB = new EpsonRobotVM(CurrentMachine, 2);//初始化爱普生机器人 break; case 1: RobotA = new FanucRobotVM(CurrentMachine);//初始化发那科机器人 break; case 2: RobotA = new QkmRobotVM(CurrentMachine);//初始化李群机器人 break; case 3: RobotA = new TurinRobotVM(CurrentMachine);//图灵机器人 break; case 4: RobotA = new YamahaRobotVM(CurrentMachine);//雅马哈 break; } RobotA.eventExcepReport += ExcepStop; RobotA.ShuaxinPointing += Robot_ShuaxinPointing; TabItem tbai = new TabItem() { Content = RobotA.MyWindow }; //tbai.SetResourceReference(TabItem.HeaderProperty, "机器人"); //设定资源引用 即可进行语言的切换 tbai.Header = "上料机器人"; tbai.SetBinding(TabItem.VisibilityProperty, new Binding("AllVis")); TabItemList.Add(tbai); RobotB.eventExcepReport += ExcepStop; RobotB.ShuaxinPointing += Robot_ShuaxinPointing; tbai = new TabItem() { Content = RobotB.MyWindow }; tbai.Header = "下料机器人"; tbai.SetBinding(TabItem.VisibilityProperty, new Binding("AllVis")); TabItemList.Add(tbai); } TabItem ti = new TabItem() { Content = new ParameterSetting(ServerComm.CommList) }; // ti.SetResourceReference(TabItem.HeaderProperty, "参数设置"); //设定资源引用 即可进行语言的切换 ti.Header = "参数设置"; ti.SetBinding(TabItem.VisibilityProperty, new Binding("AllVis")); TabItemList.Add(ti); TabItem t2 = new TabItem() { Content = new LogInformation() }; //t2.SetResourceReference(TabItem.HeaderProperty, "日志信息"); //设定资源引用 即可进行语言的切换 t2.Header = "日志"; TabItemList.Add(t2); TabItem t3 = new TabItem() { Header = "输入", Content = InputGrid }; TabItemList.Add(t3); TabItem t4 = new TabItem() { Header = "输出", Content = OutputGrid }; TabItemList.Add(t4); SelectTabItem = TabItemList[0]; } #endregion #region 前站回调函数 /// /// 前站发来的字符串拆解 /// List NewsList { get; set; } /// /// 当前回流请求状态 /// string CurrHuiliuStr { get; set; } int SwitchMachineID { get; set; } /// /// 前站发来的信息 /// string FrontStationInfo { get; set; } /// /// 前站回调函数 /// /// private void FrontStation_ReceiveCallback(string str) { } #endregion #region 后站回调函数 /// /// 当前接收到的下一站信息 /// string CurrNextStr { get; set; } /// /// 后站发来的字符串拆解 /// List NextStrList { get; set; } /// /// 后站回调函数 /// /// private void NextStation_ReceiveCallback(string str) { } #endregion #region 刷新机械手阵列点位 /// /// 刷新机械手阵列点位 /// /// private void Robot_ShuaxinPointing(int type) { try { Tray1.PointListInitialization(RobotA); Tray2.PointListInitialization(RobotB); } catch { } } #endregion #region 验证是否超时 /// /// 验证是否超时 /// /// 起点时间 /// 时限(毫秒) /// 报错信息 private bool TimeOutValidation(DateTime tiem, int timeLimit, string showMsg) { if (tiem.AddMilliseconds(timeLimit) < DateTime.Now) { ExcepStop(showMsg); return true; } else { return false; } } #endregion #region 异常停止 +ExcepStop(string showMag) /// /// 异常停止 /// /// public void ExcepStop(string showMag) { if (homeing == false) { foreach (var item in CurrentMachine.MotionPointList) { item.PointStop(); } } if (RunStatu != RunStatuEnum.Stop && RunStatu != RunStatuEnum.EmgStop && RunStatu != RunStatuEnum.Exceptional) { RunStatu = RunStatuEnum.Exceptional; ShowStatu.StatuInfo = showMag; OutputList[Y001红灯].Switchs = true; //红灯 OutputList[Y002黄灯].Switchs = false; //黄灯 OutputList[Y000绿灯].Switchs = false; //绿灯 OutputList[Y003蜂鸣器].Switchs = true; //蜂鸣器 //记录异常信息 try { RecordLog(showMag); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } #endregion #region 异常不停机(无痛报警) /// /// 不停机报警中 /// bool ExcepNoIng { get; set; } /// /// 异常不停机(无痛报警) /// public void ExcepNoStop(string showMag) { try { if (ExcepNoIng) { return; } ShowStatu.StatusBackground = ColorStr.Yellow; ShowStatu.StatusForeground = ColorStr.Red; ShowStatu.StatuInfo = showMag; OutputList[Y001红灯].Switchs = false; //红灯 OutputList[Y002黄灯].Switchs = true; //黄灯 OutputList[Y000绿灯].Switchs = false; //绿灯 OutputList[Y003蜂鸣器].Switchs = true; //蜂鸣器 //记录异常信息 try { RecordLog(showMag); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } ExcepNoIng = true; } catch { } } public void ExcepNoStopClear(string showMag) { if (ShowStatu.StatuInfo == showMag) { ShowStatu.StatusForeground = ColorStr.Black; OutputList[Y001红灯].Switchs = false; //红灯 OutputList[Y002黄灯].Switchs = false; //黄灯 OutputList[Y000绿灯].Switchs = true; //绿灯 OutputList[Y003蜂鸣器].Switchs = false; //蜂鸣器 if (RunStatu == RunStatuEnum.Normal) { ShowStatu.StatusBackground = ColorStr.Green; ShowStatu.StatuInfo = "运行中"; } else { ShowStatu.StatusBackground = ColorStr.Yellow; ShowStatu.StatuInfo = "停止中"; } } } #endregion #region 启动按钮 +void Start(object per) /// /// 启动按钮 /// /// public void Start(object per) { if (GoHomeFlow.RunStep != GengralProcessEnum.End) { return; } ExcepNoIng = false; if (RunStatu == RunStatuEnum.Normal) { ShowStatu.StatusBackground = ColorStr.Green; ShowStatu.StatusForeground = ColorStr.Black; ShowStatu.StatuInfo = "运行中"; OutputList[Y001红灯].Switchs = false; //红灯 OutputList[Y002黄灯].Switchs = false; //黄灯 OutputList[Y000绿灯].Switchs = true; //绿灯 OutputList[Y003蜂鸣器].Switchs = false; //蜂鸣器 } if (RunStatu != RunStatuEnum.Stop && RunStatu != RunStatuEnum.Prepareations) { return; } try { if (ThRun == null && RunStatu != RunStatuEnum.Prepare) { RunStatu = RunStatuEnum.Normal; ThRun = new Thread(Run); ThRun.IsBackground = true; ThRun.Start(); } else if (RunStatu != RunStatuEnum.Normal && RunStatu != RunStatuEnum.Prepare) { RunStatu = RunStatuEnum.Normal; RecordLog("开始运行"); } } catch (Exception) { MessageBox.Show("开启运行线程失败,请联系系统管理员或重启软件稍后重试"); return; } } #endregion #region 停止按钮 void Stop(object per) /// /// 停止工作 /// /// public void Stop(object per) { if (homeing == false) { foreach (var item in CurrentMachine.MotionPointList) { item.PointStop(); } } testing = false; //var shalf = MCVM.AllShaftList.Where(s => s.ShaftName == "翻转轴").FirstOrDefault(); //shalf.ShaftStop(); if (RunStatu != RunStatuEnum.Stop) { RunStatu = RunStatuEnum.Stop; RecordLog("停止运行"); } } #endregion #region 重启按钮 +void ResetSoft(object obj) /// /// 重启按钮 /// /// private void ResetSoft(object obj) { if (RunStatu == RunStatuEnum.Normal) { MessageBox.Show("机器运行中,无法重启"); return; } //如果运行线程还在运行 if (ThRun != null) { ThRun.Abort(); ThRun = null; } MessageBoxResult res = MessageBox.Show("你确定要重新启动软件吗?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Asterisk); if (res == MessageBoxResult.Yes) { ResetALL(); //重置所有参数 RecordLog("重新开始"); } } /// /// 重置所有 /// private void ResetALL() { //foreach (OutputSignal os in OutputList) //{ // os.Switchs = false; //} CloseOut(); AllVis = Visibility.Collapsed; ShowStatu = new ShowInfo(); RunStatu = RunStatuEnum.Prepare; ExcepList = new ObservableCollection(); //EnableAnquanmen = true; if (MCVM != null) { foreach (var item in MCVM.AllShaftList) { item.ShaftStop(); item.org_Successful = false; item.HomeStep = GengralProcessEnum.Start; } } if (RobotA != null) { RobotA.HomeStep = GengralProcessEnum.Start; } if (RobotB != null) { RobotB.HomeStep = GengralProcessEnum.Start; } GoHomeFlow = new FlowModel("回原点流程"); //FlowList.ForEach(x => { x = new FlowModel("1"); });//重置所以流程 //XizuiList.ForEach(w => w.State.StatusBackground = ColorStr.White); } private void CloseOut() { for (int i = 0; i < OutputList.Count; i++) { OutputList[i].Switchs = false; } } #endregion #region 紧急停止按钮 +void EmgStop() /// /// 紧急停止工作 /// public void EmgStop() { if (RunStatu != RunStatuEnum.EmgStop) { if (ThRun != null) //停止线程 { ThRun.Abort(); ThRun = null; } foreach (var item in CurrentMachine.MotionPointList) { item.PointStop(); } RunStatu = RunStatuEnum.EmgStop; RecordLog("紧急停止"); } } #endregion #region 关闭程序按钮 /// /// 关闭程序确认的返回值 /// public MessageDialogResult CloseRes { get; set; } /// /// 关闭程序按钮 /// /// public void Close(object obj) { if (RunStatu == RunStatuEnum.Normal) { MyMessageBox.Show("设备运行中无法退出,请先停止运行"); return; } // LinelabelList[14].State.StatuInfo = "15"; //CurrentMachine.MyQita.LineState = LinelabelList.Select(w => w.State).ToList();//记录线体状态 dal.UpdateProductionData(CurrentMachine); //更新生产数据 Tray1.GetTrayState(CurrentMachine); Tray2.GetTrayState(CurrentMachine); dal.UpdateMachineData(CurrentMachine); dal.UpdateSystemPar(SystemPar); CloseRes = MyMessageBox.Show("你确定要退出吗?", "提示", MessageDialogStyle.AffirmativeAndNegative); if (CloseRes == MessageDialogResult.Negative) { return; } if (ThRun != null) { ThRun.Abort(); ThRun = null; } if (ThStateMonitoring != null) { ThStateMonitoring.Abort(); ThStateMonitoring = null; } foreach (OutputSignal o in OutputList) { o.Switchs = false; } //关闭相机 if (VisualA != null) { VisualA.CamReset(0); } if (VisualB != null) { VisualB.CamReset(0); } if (VisualC != null) { VisualC.CamReset(0); } if (VisualD != null) { VisualD.CamReset(0); } if (VisualE != null) { VisualE.CamReset(0); } VisualA1.CamReset(0); VisualB1.CamReset(0); VisualC1.CamReset(0); if (MCVM != null) { foreach (FeiYuCard cd in MCVM.CardList) { cd.MoveFunction_Estop(); } } if (RobotA != null) { dal.UpdateRobotModel(RobotA.RobotPer); } if (RobotB != null) { dal.UpdateRobotModel(RobotB.RobotPer); } System.Environment.Exit(0); } #endregion #region 切换系统语言 /// /// 切换系统语言 /// /// private void SwitchLanguage(object obj) { if (obj != null) { //在APP.xaml中第四个引用是语言字典资源 Application.Current.Resources.MergedDictionaries[4].Source = new Uri(@"./Language/" + SystemPar.SystemLanguage, UriKind.RelativeOrAbsolute); //切换语言 } else { if (SystemPar.SystemLanguage == "zh-cn.xaml") { Application.Current.Resources.MergedDictionaries[4].Source = new Uri(@"./Language/en-us.xaml", UriKind.RelativeOrAbsolute); SystemPar.SystemLanguage = "en-us.xaml"; } else if (SystemPar.SystemLanguage == "en-us.xaml") { Application.Current.Resources.MergedDictionaries[4].Source = new Uri(@"./Language/zh-cn.xaml", UriKind.RelativeOrAbsolute); SystemPar.SystemLanguage = "zh-cn.xaml"; } //机器状态描述语言切换 ShowStatu.StatuInfo = ShowStatu.StatuInfoTemp; //权限按钮语言切换 PermissionButtonName = Application.Current.Resources[AllVis == Visibility.Collapsed ? "编辑模式" : "普通模式"].ToString(); if (MCVM != null) { foreach (FeiYuShaft df in MCVM.AllShaftList) { df.ShaftState.StatuInfo = df.ShaftState.StatuInfoTemp; } } //输入信号语言切换 if (InputList != null && InputList.Count > 0) { InputGrid = GetInputGrid(); } //输出信号语言切换 if (OutputList != null && OutputList.Count > 0) { OutputGrid = GetOutputGrid(); } } } #endregion #region 利用反射进行对象复制 /// /// 利用反射进行对象复制 /// /// 复制对象类型 /// 被复制对象 /// public RobotPoint Coper(RobotPoint startPoint) { RobotPoint irpa = new RobotPoint(); try { var Types = startPoint.GetType();//获得类型 var Typed = irpa.GetType(); foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段 { foreach (PropertyInfo dp in Typed.GetProperties()) { if (dp.Name == sp.Name)//判断属性名是否相同 { dp.SetValue(irpa, sp.GetValue(startPoint, null), null);//获得s对象属性的值复制给d对象的属性 } } } } catch (Exception ex) { throw ex; } return irpa; } /// /// 利用反射进行对象复制 /// /// /// /// public T Coper(T Coperobj) { T a = System.Activator.CreateInstance(); try { var Types = Coperobj.GetType();//获得类型 var Typed = a.GetType(); foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段 { foreach (PropertyInfo dp in Typed.GetProperties()) { if (dp.Name == sp.Name)//判断属性名是否相同 { dp.SetValue(a, sp.GetValue(Coperobj, null), null);//获得s对象属性的值复制给d对象的属性 } } } } catch (Exception ex) { throw ex; } return a; } #endregion #region 记录日志信息 byte[] data { get; set; } /// /// 文件夹路径 /// string path { get; set; } /// /// 文件路径 /// string paths { get; set; } /// /// 记录信息对象 /// RecordInfo info { get; set; } /// /// 打开的文件流对象 /// FileStream FS { get; set; } /// /// 对应的窗体对象 /// Window Mywindow { get; set; } /// /// 这里需要用到异步新增项 /// Dispatcher MyDispatcher { get; set; } /// /// 记录日志信息到文件 /// /// private void RecordLog(string showMag) { lock (this) { info = new RecordInfo() { Number = ExcepList.Count() + 1, HappenTime = DateTime.Now, Describe = showMag }; //异步新增项 这是微软的BUG 只能这么麻烦了 MyDispatcher.BeginInvoke(DispatcherPriority.Background, new ParameterizedThreadStart(AddItem), info); path = @"D:\\SystemLog"; //文件夹路径 paths = @"D:\\SystemLog\" + DateTime.Now.ToString("D") + ".txt"; //文件路径 以年月日为名称 //创建文件夹 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //打开或创建文件 FS = File.Open(paths, FileMode.Append); //获得字节数组 data = System.Text.Encoding.Default.GetBytes(info.Number + "-" + info.HappenTime + "-" + info.Describe + "\r\n"); //开始写入 FS.Write(data, 0, data.Length); //清空缓冲区、关闭流 FS.Flush(); FS.Close(); } } void AddItem(object info) { ExcepList.Add((RecordInfo)info); } #endregion #region 手动测试轴 public int TestPos { get; set; } public void TestAxisRun(FlowModel MyFlow) { switch (MyFlow.Step) { case 0: TestPos = 1; MyFlow.Step++; break; case 1: AxisPointTemp = GetMotionPoint(TestPos); MyFlow.Step++; break; case 2: if (AxisPointTemp.PointRun(MCVM.AllShaftList)) { MyFlow.Step++; } else if (MyFlow.RunTime.AddMilliseconds(10000) < DateTime.Now) { AxisPointTemp.PointRunStep = GengralProcessEnum.Start; MyFlow.RunTime = DateTime.Now; } break; case 3: if (TestPos % 2 == 0) { if (TestPos < 8) { TestPos++; } else { TestPos = 1; } MyFlow.Step = 1; MyFlow.RunTime = DateTime.Now; } else if (MyFlow.RunTime.AddMilliseconds(1000) < DateTime.Now) { MyFlow.Step++; MyFlow.RunTime = DateTime.Now; } break; case 4: VisualA.GetImage(); VisualA.SaveImage2(TestPos.ToString()); MyFlow.Step++; MyFlow.RunTime = DateTime.Now; break; case 5: if (MyFlow.RunTime.AddMilliseconds(1000) < DateTime.Now) { MyFlow.Step = 1; MyFlow.RunTime = DateTime.Now; if (TestPos < 8) { TestPos++; } else { TestPos = 1; } } break; } } #endregion private DelegateCommand _SoftltcInit; public DelegateCommand SoftltcInit => _SoftltcInit = _SoftltcInit ?? new DelegateCommand() { ExecuteAction = ExecuteSoftltcInit }; void ExecuteSoftltcInit(object obj) { var shaft = MCVM.AllShaftList.Where(w => w.ShaftName == "轴1").FirstOrDefault(); if (shaft != null) { shaft.SoftltcInit(0, 5, 1); } } private DelegateCommand _SoftltcReset; public DelegateCommand SoftltcReset => _SoftltcReset = _SoftltcReset ?? new DelegateCommand() { ExecuteAction = ExecuteSoftltcReset }; void ExecuteSoftltcReset(object obj) { var shaft = MCVM.AllShaftList.Where(w => w.ShaftName == "轴1").FirstOrDefault(); if (shaft != null) { shaft.SoftltcReset(0); LatchPos = 0; } } private DelegateCommand _SoftltcRead; public DelegateCommand SoftltcRead => _SoftltcRead = _SoftltcRead ?? new DelegateCommand() { ExecuteAction = ExecuteSoftltcRead }; void ExecuteSoftltcRead(object obj) { var shaft = MCVM.AllShaftList.Where(w => w.ShaftName == "上料盘Z").FirstOrDefault(); if (shaft != null) { double latchPos = 0; if (shaft.SoftltcRead(0, ref latchPos)) { LatchPos = latchPos; } } } private double _LatchPos; public double LatchPos { get { return _LatchPos; } set { _LatchPos = value; this.RaisePropertyChanged("LatchPos"); } } public MotionPointModel AxisPointTemp { get; private set; } } }