using Community; using DAL; using MahApps.Metro.Controls.Dialogs; using Model; using Model.MotionControl; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using Views.Motion; namespace ViewModel.MotionControl { public class MotionControlVM : NotificationObject { /// /// 数据引擎 /// MainWindowDAL dal = new MainWindowDAL(); /// /// 上报异常的委托 /// /// public delegate void delegeateExcep(string hv); /// /// 上报异常的委托事件 /// public event delegeateExcep eventExcepReport; /// /// 控制卡连接是否成功 /// public bool CardConnection { get; set; } #region 对应的窗口对象 private MotionControWindow myWindow; /// /// 对应的窗口对象 /// public MotionControWindow MyWindow { get { return myWindow; } set { myWindow = value; this.RaisePropertyChanged("MyWindow"); } } #endregion #region 对应的生产机种 private MachineModel machineObj; /// /// 对应的生产机种 /// public MachineModel MachineObj { get { return machineObj; } set { machineObj = value; if (value.ID > 0) { TrajListL = dal.GetMotionTrajectoryList(value.ID, 0); //TrajListR = dal.GetMotionTrajectoryList(value.ID, 1); MachineObj.MotionPointList = TrajListL[0].PointList; MachineObj.MotionTrajectoryList = TrajListL; } this.RaisePropertyChanged("MachineObj"); } } #endregion #region 显示权限 private Visibility displayLevel; /// /// 显示权限 /// public Visibility DisplayLevel { get { return displayLevel; } set { displayLevel = value; this.RaisePropertyChanged("DisplayLevel"); } } #endregion #region 选中运动控制卡对象 private FeiYuCard selectCard; /// /// 选中运动控制卡对象 /// public FeiYuCard SelectCard { get { return selectCard; } set { selectCard = value; this.RaisePropertyChanged("SelectCard"); } } #endregion #region 选中的轴对象 private FeiYuShaft selectShaft; /// /// 选中的轴对象 /// public FeiYuShaft SelectShaft { get { return selectShaft; } set { selectShaft = value; this.RaisePropertyChanged("SelectShaft"); } } #endregion #region 运动控制卡集合 private ObservableCollection cardList; /// /// 运动控制卡集合 /// public ObservableCollection CardList { get { return cardList; } set { cardList = value; this.RaisePropertyChanged("CardList"); } } #endregion #region 轨迹集合左 TrajListL private ObservableCollection trajListL; /// /// 轨迹集合左 /// public ObservableCollection TrajListL { get { return trajListL; } set { trajListL = value; this.RaisePropertyChanged("TrajListL"); } } #endregion #region 轨迹集合右 TrajListR private ObservableCollection trajListR; /// /// 轨迹集合右 /// public ObservableCollection TrajListR { get { return trajListR; } set { trajListR = value; this.RaisePropertyChanged("TrajListR"); } } #endregion #region 选中的运行轨迹 private MotionTrajectoryModel selectTrajectory; /// /// 选中的运行轨迹 /// public MotionTrajectoryModel SelectTrajectory { get { return selectTrajectory; } set { selectTrajectory = value; this.RaisePropertyChanged("SelectTrajectory"); if (value == null) { return; } EnableTrajeEdit = value.TrajectoryTrype >= 2 ? false : true; if (value.PointList.Count <= 0) { //如果是圆弧轨迹 则添加固定点位 if (value.TrajectoryTrype == 2) { List plist = new List() { new MotionPointModel() { MachineID = value.ID, PointName = "圆弧起点", PointType = 2 }, new MotionPointModel() { MachineID = value.ID, PointName = "圆弧中间", PointType = 2 }, new MotionPointModel() { MachineID = value.ID, PointName = "圆弧终点", PointType = 2 }}; foreach (MotionPointModel mp in plist) { dal.AddMotionPoint(mp); } } else if (value.TrajectoryTrype == 3) { } value.PointList = dal.GetMotionPointlList(value.ID); } } } #endregion #region 启用轨迹点位编辑 private bool enableTrajeEdit; /// /// 启用轨迹点位编辑 /// public bool EnableTrajeEdit { get { return enableTrajeEdit; } set { enableTrajeEdit = value; this.RaisePropertyChanged("EnableTrajeEdit"); } } #endregion #region 选中的运行点位 private MotionPointModel selectMotionPoint; /// /// 选中的运行点位 /// public MotionPointModel SelectMotionPoint { get { return selectMotionPoint; } set { if (RunSelectPointBool) return; selectMotionPoint = value; IsEnables = false; this.RaisePropertyChanged("SelectMotionPoint"); } } #endregion #region 微调距离 private double tuningDis; /// /// 微调距离 /// public double TuningDis { get { return tuningDis; } set { tuningDis = value; this.RaisePropertyChanged("TuningDis"); } } #endregion #region 所有轴集合 private ObservableCollection allShaftList; /// /// 所有轴集合 /// public ObservableCollection AllShaftList { get { return allShaftList; } set { allShaftList = value; this.RaisePropertyChanged("AllShaftList"); } } #endregion #region 轴对象 private FeiYuShaft shaft_X; /// /// X轴 /// public FeiYuShaft Shaft_X { get { return shaft_X; } set { shaft_X = value; this.RaisePropertyChanged("Shaft_X"); } } private FeiYuShaft shaft_Y; /// /// Y轴 /// public FeiYuShaft Shaft_Y { get { return shaft_Y; } set { shaft_Y = value; this.RaisePropertyChanged("Shaft_Y"); } } private FeiYuShaft shaft_Z; /// /// Z轴 /// public FeiYuShaft Shaft_Z { get { return shaft_Z; } set { shaft_Z = value; this.RaisePropertyChanged("Shaft_Z"); } } private FeiYuShaft shaft_U; /// /// U轴 /// public FeiYuShaft Shaft_U { get { return shaft_U; } set { shaft_U = value; this.RaisePropertyChanged("Shaft_U"); } } #endregion #region 各种方法 /// /// 新增运动控制卡 /// public DelegateCommand AddCardCommand { get; set; } /// /// 保存运动控制卡信息 /// public DelegateCommand SaveCardCommand { get; set; } /// /// 新增轴 /// public DelegateCommand AddShaftCommand { get; set; } /// /// 保存轴参数 /// public DelegateCommand SaveShaftCommand { get; set; } /// /// 鼠标左键按下时 /// public DelegateCommand PreviewMouseLeftButtonDownCommand { get; set; } /// /// 鼠标左键松开时 /// public DelegateCommand PreviewMouseLeftButtonUpCommand { get; set; } /// /// 编辑点位 /// public DelegateCommand EditorPointCommand { get; set; } /// /// 新增点位 /// public DelegateCommand AddPointCommand { get; set; } /// /// 删除点位 /// public DelegateCommand DeletePointCommand { get; set; } /// /// 运行当前选中点位 /// public DelegateCommand RunSelectPointCommand { get; set; } /// /// 保存运行点位坐标 /// public DelegateCommand SaveMotionPointCommand { get; set; } /// /// 保存点位参数 /// public DelegateCommand SavePointPerCommand { get; set; } /// /// 新增轨迹 /// public DelegateCommand AddTrajectoryCommand { get; set; } /// /// 删除轨迹 /// public DelegateCommand DeleteTrajectoryCommand { get; set; } /// /// 运行当前轨迹 /// public DelegateCommand RunTrajectoryCommand { get; set; } /// /// 保存轨迹信息 /// public DelegateCommand SaveTrajectoryCommand { get; set; } #endregion #region 输入信号集合 private List inputList; /// /// 输入信号集合 /// public List InputList { get { return inputList; } set { inputList = value; } } #endregion #region 输出信号集合 private List outputList; /// /// 输出信号集合 /// public List OutputList { get { return outputList; } set { outputList = value; } } #endregion #region 构造函数 public MotionControlVM(MachineModel machine, List inputList, List outputList) { MachineObj = machine; this.InputList = inputList; this.OutputList = outputList; DisplayLevel = Visibility.Collapsed; CardList = dal.GetCardList(); HomeStep = GengralProcessEnum.End; GetAllShaftList(); MyWindow = new MotionControWindow(AllShaftList); MyWindow.DataContext = this; AddCardCommand = new DelegateCommand(); AddCardCommand.ExecuteAction = new Action(AddCard); SaveCardCommand = new DelegateCommand(); SaveCardCommand.ExecuteAction = new Action(SaveCard); AddShaftCommand = new DelegateCommand(); AddShaftCommand.ExecuteAction = new Action(AddShaft); SaveShaftCommand = new DelegateCommand(); SaveShaftCommand.ExecuteAction = new Action(SaveShaft); PreviewMouseLeftButtonDownCommand = new DelegateCommand(); PreviewMouseLeftButtonDownCommand.ExecuteAction = new Action(PreviewMouseLeftButtonDown); PreviewMouseLeftButtonUpCommand = new DelegateCommand(); PreviewMouseLeftButtonUpCommand.ExecuteAction = new Action(PreviewMouseLeftButtonUp); this.EditorPointCommand = new DelegateCommand(); this.EditorPointCommand.ExecuteAction = new Action(EditorPoint); this.AddPointCommand = new DelegateCommand(); this.AddPointCommand.ExecuteAction = new Action(AddPoint); this.DeletePointCommand = new DelegateCommand(); this.DeletePointCommand.ExecuteAction = new Action(DeletePoint); this.RunSelectPointCommand = new DelegateCommand(); this.RunSelectPointCommand.ExecuteAction = new Action(RunSelectPoint); this.SaveMotionPointCommand = new DelegateCommand(); this.SaveMotionPointCommand.ExecuteAction = new Action(SaveMotionPoint); this.SavePointPerCommand = new DelegateCommand(); this.SavePointPerCommand.ExecuteAction = new Action(SavePointPer); this.AddTrajectoryCommand = new DelegateCommand(); this.AddTrajectoryCommand.ExecuteAction = new Action(AddTrajectory); this.DeleteTrajectoryCommand = new DelegateCommand(); this.DeleteTrajectoryCommand.ExecuteAction = new Action(DeleteTrajectory); this.RunTrajectoryCommand = new DelegateCommand(); this.RunTrajectoryCommand.ExecuteAction = new Action(RunTrajectory); this.SaveTrajectoryCommand = new DelegateCommand(); this.SaveTrajectoryCommand.ExecuteAction = new Action(SaveTrajectory); IsEnables = false; } /// /// 连接所有控制卡 /// /// public bool ConnectAllCard() { //如果已连接 if (CardConnection) { return CardConnection; } CardConnection = true; foreach (FeiYuCard cd in CardList) { if (cd.Connect()) { cd.GetCradInfo(); foreach (var item in InputList) { if (cd.ID == item.CardID) { item.MyCard = cd; } } foreach (var item in OutputList) { if (cd.ID == item.CardID) { item.MyCard = cd; } } } else { CardConnection = false; eventExcepReport("运动控制器【" + cd.CardName + "】未连接"); break; } } return CardConnection; } /// /// 鼠标左键松开时发生 /// /// private void PreviewMouseLeftButtonUp(object obj) { StopMove(); } /// /// 鼠标左键按下时发生 /// /// private void PreviewMouseLeftButtonDown(object obj) { try { Button btn = obj as Button; TuningDis = Convert.ToDouble(btn.ToolTip.ToString()); ShaftMove(btn.Tag.ToString()); } catch (Exception) { } } #endregion #region 保存轴参数 /// /// 保存轴参数 /// /// private void SaveShaft(object obj) { if (SelectShaft == null) { return; } if (dal.UpdateShaft(SelectShaft) > 0) { SelectShaft.Initialize(); MyMessageBox.Show("保存成功"); GetAllShaftList(); } else { MyMessageBox.Show("保存失败"); } } #endregion #region 新增轴 /// /// 新增轴 /// /// private void AddShaft(object obj) { if (SelectCard == null || SelectCard.ID <= 0) { MyMessageBox.Show("请选择要添加轴的控制卡"); return; } if (SelectCard.Brand == 1) { FeiYuShaft sf = new LeiSaiShaft() { ShaftName = "新来的", SuperiorID = SelectCard.ID, MyCard = SelectCard }; SelectCard.ShaftList.Add(sf); } else { FeiYuShaft sf = new ZmotionShaft() { ShaftName = "新来的", SuperiorID = SelectCard.ID, MyCard = SelectCard }; SelectCard.ShaftList.Add(sf); } } #endregion #region 新增运动控制卡 /// /// 新增运动控制卡 /// /// private void AddCard(object obj) { LeiSaiCard cardnew = new LeiSaiCard() { CardName = "新来的" }; CardList.Add(cardnew); } #endregion #region 保存运动控制卡信息 /// /// 保存运动控制卡信息 /// /// private void SaveCard(object obj) { if (SelectCard == null) { return; } if (dal.UpdateCard(SelectCard) > 0) { MyMessageBox.Show("保存成功"); CardList = dal.GetCardList(); } else { MyMessageBox.Show("保存失败"); } } #endregion #region 点位调试的轴移动方法 /// /// 当前调试运动轴 /// FeiYuShaft MoveShaft { get; set; } /// /// 点位调试的轴移动方法 /// public void ShaftMove(string btnTag) { if (SelectMotionPoint == null) { return; } switch (btnTag) { case "XP": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_X).FirstOrDefault();//Shaft_X; ShaftMoverRun(1); break; case "XN": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_X).FirstOrDefault(); ShaftMoverRun(0); break; case "YP": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Y).FirstOrDefault(); //Shaft_Y; ShaftMoverRun(1); break; case "YN": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Y).FirstOrDefault(); //Shaft_Y; ShaftMoverRun(0); break; case "ZP": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Z).FirstOrDefault();// Shaft_Z; ShaftMoverRun(1); break; case "ZN": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Z).FirstOrDefault();// Shaft_Z; ShaftMoverRun(0); break; case "UP": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_U).FirstOrDefault();// Shaft_U; ShaftMoverRun(1); break; case "UN": MoveShaft = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_U).FirstOrDefault();// Shaft_U; ShaftMoverRun(0); break; default: MoveShaft = null; break; } } #endregion #region 轴移动运行 /// /// 轴移动运行 /// /// 运行方向 private void ShaftMoverRun(ushort dir) { if (MoveShaft == null) { return; } if (TuningDis > 0) { if (TuningDis > 400) { MyMessageBox.Show("微调刻度超出限制,不应超出20MM"); } else if (dir == 1) { MoveShaft.MyMotionPar.ActualDir = Math.Round(MoveShaft.ActualDir + TuningDis, 2); MoveShaft.DZDC(MoveShaft.MyMotionPar); } else if (dir == 0) { MoveShaft.MyMotionPar.ActualDir = Math.Round(MoveShaft.ActualDir - TuningDis, 2); MoveShaft.DZDC(MoveShaft.MyMotionPar); } } else { MoveShaft.ContinuousMove(dir); } } #endregion #region 停止运行 /// /// 停止运行 /// public void StopMove() { if (MoveShaft == null) { return; } if (TuningDis <= 0) //微调时 走完微调距离会自动停止 { MoveShaft.ShaftStop(); } } #endregion #region 获取所有轴 /// /// 获取所有轴 /// private void GetAllShaftList() { AllShaftList = new ObservableCollection(); foreach (FeiYuCard c in CardList) { AllShaftList = new ObservableCollection(AllShaftList.Concat(c.ShaftList).ToList()); } ShaftStateGrid = GetShaftStateGrid(); } #endregion #region 获取轴状态Grid private Grid shaftStateGrid; /// /// 轴状态Grid /// public Grid ShaftStateGrid { get { return shaftStateGrid; } set { shaftStateGrid = value; this.RaisePropertyChanged("ShaftStateGrid"); } } /// /// 获取轴状态Grid /// private Grid GetShaftStateGrid() { Grid g = new Grid(); int row = 0; int column = 5; int count = AllShaftList.Count; if (count <= 12) { row = 12; } else { row = count; } int number = 0; Border bor; //轴状态颜色 Label shaftname; //轴名称 Label StatuInfo; //轴状态信息 Label shaftDir;//轴当前位置 Button goHome;//回原点按钮 Button reset;//回原点按钮 for (int i = 0; i < column; i++) { g.ColumnDefinitions.Add(new ColumnDefinition()); //添加列 number = 0; for (int j = 0; j < row; j++) { if (i == 0) { g.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) }); //添加行 } if (number < count) { if (i == 0) { bor = new Border(); bor.DataContext = AllShaftList[number].ShaftState; bor.SetBinding(Border.BackgroundProperty, new Binding("StatusBackground")); bor.Margin = new Thickness(1); //设置边距 bor.Opacity = 0.8;//设置透明度 g.Children.Add(bor); bor.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 bor.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 bor.SetValue(Grid.ColumnSpanProperty, column); shaftname = new Label(); shaftname.DataContext = AllShaftList[number]; shaftname.SetBinding(Label.ContentProperty, new Binding("ShaftName")); //shaftname.SetResourceReference(Label.ContentProperty, AllShaftList[number].ShaftName); //设定资源引用 即可进行语言的切换 shaftname.VerticalContentAlignment = VerticalAlignment.Center; shaftname.HorizontalContentAlignment = HorizontalAlignment.Center; shaftname.FontWeight = FontWeights.Bold; shaftname.FontSize = 20; g.Children.Add(shaftname); shaftname.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 shaftname.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 } if (i == 1) { StatuInfo = new Label(); StatuInfo.DataContext = AllShaftList[number].ShaftState; StatuInfo.SetBinding(Label.ContentProperty, new Binding("StatuInfo")); StatuInfo.SetBinding(Label.ForegroundProperty, new Binding("StatusForeground")); StatuInfo.VerticalContentAlignment = VerticalAlignment.Center; StatuInfo.HorizontalContentAlignment = HorizontalAlignment.Center; StatuInfo.FontWeight = FontWeights.Bold; StatuInfo.FontSize = 20; g.Children.Add(StatuInfo); StatuInfo.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 StatuInfo.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 } if (i == 2) { shaftDir = new Label(); shaftDir.DataContext = AllShaftList[number]; shaftDir.SetBinding(Label.ContentProperty, new Binding("ActualDir")); shaftDir.VerticalContentAlignment = VerticalAlignment.Center; shaftDir.HorizontalContentAlignment = HorizontalAlignment.Center; shaftDir.FontWeight = FontWeights.Bold; shaftDir.FontSize = 20; g.Children.Add(shaftDir); shaftDir.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 shaftDir.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 } if (i == 3) { goHome = new Button(); //指定样式 goHome.Style = (Style)Application.Current.FindResource("commBtn");//commBtn 这个样式是引用的资源文件中的样式名称 goHome.Tag = AllShaftList[number].ID; goHome.Click += GoHome_Click; goHome.Content = "回原点"; //goHome.SetResourceReference(Button.ContentProperty, "回原点"); //设定资源引用 即可进行语言的切换 //goHome.SetBinding(Button.CommandProperty, new Binding("GoHomeCommand")); goHome.FontSize = 20; goHome.FontWeight = FontWeights.Bold; goHome.FontFamily = new System.Windows.Media.FontFamily("楷体"); goHome.Margin = new Thickness(5); //设置边距 goHome.Opacity = 0.8;//设置透明度 g.Children.Add(goHome); goHome.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 goHome.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 } if (i == 4) { reset = new Button(); //指定样式 reset.Style = (Style)Application.Current.FindResource("commBtn");//commBtn 这个样式是引用的资源文件中的样式名称 reset.Tag = AllShaftList[number].ID; reset.Click += Reset_Click; reset.Content = "复位"; //goHome.SetResourceReference(Button.ContentProperty, "回原点"); //设定资源引用 即可进行语言的切换 //goHome.SetBinding(Button.CommandProperty, new Binding("GoHomeCommand")); reset.FontSize = 20; reset.FontWeight = FontWeights.Bold; reset.FontFamily = new System.Windows.Media.FontFamily("楷体"); reset.Margin = new Thickness(5); //设置边距 reset.Opacity = 0.8;//设置透明度 g.Children.Add(reset); reset.SetValue(Grid.RowProperty, j); //设置按钮所在Grid控件的行 reset.SetValue(Grid.ColumnProperty, i); //设置按钮所在Grid控件的列 } number++; } } } return g; } #endregion #region 单轴回原点 /// /// 单轴回原点 /// /// /// private void GoHome_Click(object sender, RoutedEventArgs e) { Button bnt = sender as Button; int id = Convert.ToInt32(bnt.Tag); FeiYuShaft shaft = AllShaftList.Where(w => w.ID == id).FirstOrDefault(); if (shaft != null) { if (shaft.InCode != "Z") { //这里Z轴防呆 List list = AllShaftList.Where(w => w.InCode == "Z").ToList(); foreach (var item in list) { if (item.ActualDir != 0) { MyMessageBox.Show(item.ShaftName + "的高度存在撞机风险"); return; } } shaft.ShaftStop(); shaft.org_Successful = true; } else { shaft.org_Successful = true; } } } #endregion private void Reset_Click(object sender, RoutedEventArgs e) { Button bnt = sender as Button; int id = Convert.ToInt32(bnt.Tag); FeiYuShaft shaft = AllShaftList.Where(w => w.ID == id).FirstOrDefault(); if (shaft != null) { shaft.Reset(); } } #region 编辑坐标点 private bool isEnables; /// /// 是否启用编辑 /// public bool IsEnables { get { return isEnables; } set { isEnables = value; this.RaisePropertyChanged("IsEnables"); } } /// /// 编辑坐标点 /// /// private void EditorPoint(object obj) { if (SelectMotionPoint == null) { MyMessageBox.Show("请选择运行点位"); return; } if (!IsEnables) { IsEnables = true; Shaft_X = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_X).FirstOrDefault(); Shaft_Y = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Y).FirstOrDefault(); Shaft_Z = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Z).FirstOrDefault(); Shaft_U = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_U).FirstOrDefault(); } } #endregion #region 新增坐标点位 private int tabSelectNumber; /// /// 选中的点位分类 /// public int TabSelectNumber { get { return tabSelectNumber; } set { tabSelectNumber = value; SelectMotionPoint = null; SelectTrajectory = null; this.RaisePropertyChanged("TabSelectNumber"); } } /// /// 新增坐标点位 /// /// private void AddPoint(object obj) { MotionPointModel p = new MotionPointModel() { MachineID = SelectTrajectory.ID, PointName = "新来的" }; if (dal.AddMotionPoint(p) > 0) { SelectTrajectory.PointList = dal.GetMotionPointlList(SelectTrajectory.ID); } } #endregion #region 删除点位 /// /// 删除点位 /// /// private void DeletePoint(object obj) { if (SelectMotionPoint == null) { MyMessageBox.Show("请选择运行点位"); return; } if (MyMessageBox.Show("你确定要删除当前点位吗", "警告", MessageDialogStyle.AffirmativeAndNegative) == MessageDialogResult.Affirmative) { if (dal.DeleteMotionPoint(SelectMotionPoint.ID) > 0) { //if (SelectMotionPoint.PointClass == 1) //{ // SelectTrajectory.PointList = dal.GetMotionPointlList(SelectTrajectory.ID, 1); //} //else if (SelectMotionPoint.PointClass == 2) //{ // MotoinPointListR = dal.GetMotionPointlList(MachineObj.ID, 2); //} SelectTrajectory.PointList = dal.GetMotionPointlList(SelectTrajectory.ID); } else { MyMessageBox.Show("删除失败"); } } } #endregion #region 运行当前选中的坐标点 /// /// 是否在进行运行到选择点位测试 /// public bool RunSelectPointBool { get; set; } /// /// 运行当前选中的点胶点 /// /// public void RunSelectPoint(object obj) { if (SelectMotionPoint == null) { MyMessageBox.Show("请选择运行点位"); return; } //if (SelectMotionPoint.PointClass == 1) //{ // for (int i = 46; i < 50; i++) // { // if (!InputList[i].LightUp) // { // MyMessageBox.Show(InputList[i].InputMeaning + "异常"); // return ; // } // } //} //else if (SelectMotionPoint.PointClass == 2) //{ // for (int i = 50; i < 54; i++) // { // if (!InputList[i].LightUp) // { // MyMessageBox.Show(InputList[i].InputMeaning + "异常"); // return ; // } // } //} if (!RunSelectPointBool && !RunSelectTrajectory) { RunSelectPointBool = true; } } /// /// 运行到选中点位 /// public void PointRun() { if (!RunSelectPointBool) { return; } if (PointRun(SelectMotionPoint)) { RunSelectPointBool = false; } } /// /// 运行到指定点位 /// /// 点位对象 /// 降速倍数 /// public bool PointRun(MotionPointModel point, int multiple = 1) { bool bl = false; //这里Z轴防呆 //List list = AllShaftList.Where(w => w.InCode == "Z" && w.ID != point.ShaftID_Z).ToList(); //foreach (var item in list) //{ // if (item.ActualDir != 0) // { // eventExcepReport(item.ShaftName + "的高度存在撞机风险"); // return bl; // } //} //if (point.PointClass == 1) //{ // for (int i = 46; i < 50; i++) // { // if (!InputList[i].LightUp) // { // eventExcepReport(InputList[i].InputMeaning + "异常"); // RunSelectPointBool = false; // return bl; // } // } //} //else if (point.PointClass == 2) //{ // for (int i = 50; i < 54; i++) // { // if (!InputList[i].LightUp) // { // eventExcepReport(InputList[i].InputMeaning + "异常"); // RunSelectPointBool = false; // return bl; // } // } //} return point.PointRun(AllShaftList, multiple); } #endregion #region 保存运行点位坐标 /// /// 保存运行点位坐标 /// /// private void SaveMotionPoint(object obj) { if (MyMessageBox.Show("确定要保存当前参数", "警告", MessageDialogStyle.AffirmativeAndNegative) == MessageDialogResult.Negative) { return; } Shaft_X = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_X).FirstOrDefault(); Shaft_Y = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Y).FirstOrDefault(); Shaft_Z = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Z).FirstOrDefault(); Shaft_U = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_U).FirstOrDefault(); SelectMotionPoint.ShaftPoint_X = Shaft_X != null ? Shaft_X.ActualDir : 0; SelectMotionPoint.ShaftPoint_Y = Shaft_Y != null ? Shaft_Y.ActualDir : 0; SelectMotionPoint.ShaftPoint_Z = Shaft_Z != null ? Shaft_Z.ActualDir : 0; SelectMotionPoint.ShaftPoint_U = Shaft_U != null ? Shaft_U.ActualDir : 0; SelectMotionPoint.Speed_X = Shaft_X != null ? Shaft_X.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; SelectMotionPoint.Speed_Y = Shaft_Y != null ? Shaft_Y.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; SelectMotionPoint.Speed_Z = Shaft_Z != null ? Shaft_Z.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; SelectMotionPoint.Speed_U = Shaft_U != null ? Shaft_U.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; if (dal.UpdateMotionPoint(SelectMotionPoint) > 0) { MyMessageBox.Show("保存成功"); } else { MyMessageBox.Show("保存失败"); } } #endregion #region 保存点位参数 /// /// 保存点位参数 /// /// private void SavePointPer(object obj) { if (SelectMotionPoint == null) { return; } //if (SelectMotionPoint.Speed_Gear > 30) //{ // MyMessageBox.Show("速度挡位超过最大值"); // return; //} Shaft_X = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_X).FirstOrDefault(); Shaft_Y = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Y).FirstOrDefault(); Shaft_Z = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_Z).FirstOrDefault(); Shaft_U = AllShaftList.Where(s => s.ID == SelectMotionPoint.ShaftID_U).FirstOrDefault(); SelectMotionPoint.Speed_X = Shaft_X != null ? Shaft_X.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; SelectMotionPoint.Speed_Y = Shaft_Y != null ? Shaft_Y.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; SelectMotionPoint.Speed_Z = Shaft_Z != null ? Shaft_Z.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; SelectMotionPoint.Speed_U = Shaft_U != null ? Shaft_U.MyMotionPar.StartSpeed * SelectMotionPoint.Speed_Gear : 0; if (dal.UpdateMotionPointPer(SelectMotionPoint) > 0) { MyMessageBox.Show("保存成功"); } else { MyMessageBox.Show("保存失败"); } } #endregion #region 所有轴回原点 /// /// 所有轴自动回原点步骤 /// public GengralProcessEnum HomeStep { get; set; } /// /// 所有轴回原点 /// /// 急停信号 public void OrginALL(InputSignal emg) { #region 监控手动回原点 foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin) { df.HYD(emg); } } #endregion #region 自动回原点 bool bl = true; switch (HomeStep) { case GengralProcessEnum.Start: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 1) { df.org_Successful = true; df.HomeStep = GengralProcessEnum.Start; } } HomeStep++; break; case GengralProcessEnum.Step1: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 1 && df.org_Successful) { bl = false; } } if (bl == true) { HomeStep++; } break; case GengralProcessEnum.Step2: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 2) { df.org_Successful = true; df.HomeStep = GengralProcessEnum.Start; } } HomeStep++; break; case GengralProcessEnum.Step3: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 2 && df.org_Successful) { bl = false; } } if (bl == true) { HomeStep++; } break; case GengralProcessEnum.Step4: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 3) { df.org_Successful = true; df.HomeStep = GengralProcessEnum.Start; } } HomeStep++; break; case GengralProcessEnum.Step5: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 3 && df.org_Successful) { bl = false; } } if (bl == true) { HomeStep++; } break; case GengralProcessEnum.Step6: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 4) { df.org_Successful = true; df.HomeStep = GengralProcessEnum.Start; } } HomeStep++; break; case GengralProcessEnum.Step7: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 4 && df.org_Successful) { bl = false; } } if (bl == true) { HomeStep++; } break; case GengralProcessEnum.Step8: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 5) { df.org_Successful = true; df.HomeStep = GengralProcessEnum.Start; } } HomeStep++; break; case GengralProcessEnum.Step9: foreach (FeiYuShaft df in AllShaftList) { if (df.ToOrgin && df.GoHomeOrder == 5 && df.org_Successful) { bl = false; } } if (bl == true) { HomeStep = GengralProcessEnum.End; } break; } #endregion #region 所有轴状态 //所有轴状态 foreach (FeiYuShaft s in AllShaftList) { if (s.ID > 0) { s.GetAllStart(); if (s.ShaftState.StatusBackground == ColorStr.Yellow) { eventExcepReport(s.ShaftName + "异常");//抛出异常 } } } #endregion } #endregion #region 停止所有轴 /// /// 停止所有轴 /// public void StopAllShaft() { foreach (FeiYuShaft sf in AllShaftList) { sf.ShaftStop(); } } #endregion #region 新增运行轨迹 /// /// 新增运行轨迹 /// /// private void AddTrajectory(object obj) { int type = Convert.ToInt32(obj.ToString()); MotionTrajectoryModel mtm = new MotionTrajectoryModel() { TraName = "新来的", SuperiorID = MachineObj.ID, ClassType = TabSelectNumber, TrajectoryTrype = type }; if (dal.AddMotionTrajectory(mtm) > 0) { if (TabSelectNumber == 0) { TrajListL = dal.GetMotionTrajectoryList(MachineObj.ID, TabSelectNumber); } else { TrajListR = dal.GetMotionTrajectoryList(MachineObj.ID, TabSelectNumber); } } else { MyMessageBox.Show("新增轨迹失败"); } } #endregion #region 删除运行轨迹 /// /// 删除运行轨迹 /// /// private void DeleteTrajectory(object obj) { if (SelectTrajectory == null) { return; } if (MyMessageBox.Show("你确定要删除当前测量项目吗?", "警告", MessageDialogStyle.AffirmativeAndNegative) == MessageDialogResult.Affirmative) { if (dal.DeleteMotionTrajectory(SelectTrajectory) > 0) { TrajListL = dal.GetMotionTrajectoryList(MachineObj.ID); } else { MyMessageBox.Show("删除失败"); } } } #endregion #region 保存运行轨迹信息 /// /// 保存运行轨迹信息 /// /// private void SaveTrajectory(object obj) { if (SelectTrajectory != null) { if (dal.UpdateMotionTrajectory(SelectTrajectory) > 0) { MyMessageBox.Show("保存成功"); } else { MyMessageBox.Show("保存失败"); } } } #endregion #region 运行当前轨迹 /// /// 是否正在在运行选中轨迹 /// bool RunSelectTrajectory { get; set; } /// /// 运行当前轨迹 /// /// private void RunTrajectory(object obj) { if (SelectTrajectory == null) { MyMessageBox.Show("请选择运动轨迹"); return; } if (!RunSelectPointBool && !RunSelectTrajectory) { RunSelectTrajectory = true; } } /// /// 运行选中的轨迹 /// public void RunMotionTrajectory() { if (!RunSelectTrajectory) { return; } if (SelectTrajectory.TrajectoryTrype <= 0) { MyMessageBox.Show("点位轨迹,请使用单点运行"); return; } if (SelectTrajectory.TrajectoryRun(AllShaftList)) { RunSelectTrajectory = false; } } #endregion #region 利用反射进行对象复制 /// /// 利用反射进行对象复制 /// /// /// /// 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 } }