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

1575 lines
49 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Community;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using Model.MotionControl;
using DAL;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using MahApps.Metro.Controls.Dialogs;
using System.Windows;
using Views.Motion;
using System.Windows.Data;
using System.Windows.Media;
namespace ViewModel.MotionControl
{
public class MotionControlVM : NotificationObject
{
/// <summary>
/// 数据引擎
/// </summary>
MainWindowDAL dal = new MainWindowDAL();
/// <summary>
/// 上报异常的委托
/// </summary>
/// <param name="hv"></param>
public delegate void delegeateExcep(string hv);
/// <summary>
/// 上报异常的委托事件
/// </summary>
public event delegeateExcep eventExcepReport;
/// <summary>
/// 控制卡连接是否成功
/// </summary>
public bool CardConnection { get; set; }
#region 对应的窗口对象
private MotionControWindow myWindow;
/// <summary>
/// 对应的窗口对象
/// </summary>
public MotionControWindow MyWindow
{
get { return myWindow; }
set
{
myWindow = value;
this.RaisePropertyChanged("MyWindow");
}
}
#endregion
#region 对应的生产机种
private MachineModel machineObj;
/// <summary>
/// 对应的生产机种
/// </summary>
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);
}
this.RaisePropertyChanged("MachineObj");
}
}
#endregion
#region 显示权限
private Visibility displayLevel;
/// <summary>
/// 显示权限
/// </summary>
public Visibility DisplayLevel
{
get { return displayLevel; }
set
{
displayLevel = value;
this.RaisePropertyChanged("DisplayLevel");
}
}
#endregion
#region 选中运动控制卡对象
private Card selectCard;
/// <summary>
/// 选中运动控制卡对象
/// </summary>
public Card SelectCard
{
get { return selectCard; }
set
{
selectCard = value;
this.RaisePropertyChanged("SelectCard");
}
}
#endregion
#region 选中的轴对象
private Shaft selectShaft;
/// <summary>
/// 选中的轴对象
/// </summary>
public Shaft SelectShaft
{
get { return selectShaft; }
set
{
selectShaft = value;
this.RaisePropertyChanged("SelectShaft");
}
}
#endregion
#region 运动控制卡集合
private ObservableCollection<Card> cardList;
/// <summary>
/// 运动控制卡集合
/// </summary>
public ObservableCollection<Card> CardList
{
get { return cardList; }
set
{
cardList = value;
this.RaisePropertyChanged("CardList");
}
}
#endregion
#region 轨迹集合左 TrajListL
private ObservableCollection<MotionTrajectoryModel> trajListL;
/// <summary>
/// 轨迹集合左
/// </summary>
public ObservableCollection<MotionTrajectoryModel> TrajListL
{
get { return trajListL; }
set
{
trajListL = value;
this.RaisePropertyChanged("TrajListL");
}
}
#endregion
#region 轨迹集合右 TrajListR
private ObservableCollection<MotionTrajectoryModel> trajListR;
/// <summary>
/// 轨迹集合右
/// </summary>
public ObservableCollection<MotionTrajectoryModel> TrajListR
{
get { return trajListR; }
set
{
trajListR = value;
this.RaisePropertyChanged("TrajListR");
}
}
#endregion
#region 选中的运行轨迹
private MotionTrajectoryModel selectTrajectory;
/// <summary>
/// 选中的运行轨迹
/// </summary>
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<MotionPointModel> plist = new List<MotionPointModel>() { 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;
/// <summary>
/// 启用轨迹点位编辑
/// </summary>
public bool EnableTrajeEdit
{
get { return enableTrajeEdit; }
set
{
enableTrajeEdit = value;
this.RaisePropertyChanged("EnableTrajeEdit");
}
}
#endregion
#region 选中的运行点位
private MotionPointModel selectMotionPoint;
/// <summary>
/// 选中的运行点位
/// </summary>
public MotionPointModel SelectMotionPoint
{
get { return selectMotionPoint; }
set
{
selectMotionPoint = value;
IsEnables = false;
this.RaisePropertyChanged("SelectMotionPoint");
}
}
#endregion
#region 微调距离
private double tuningDis;
/// <summary>
/// 微调距离
/// </summary>
public double TuningDis
{
get { return tuningDis; }
set
{
tuningDis = value;
this.RaisePropertyChanged("TuningDis");
}
}
#endregion
#region 所有轴集合
private ObservableCollection<Shaft> allShaftList;
/// <summary>
/// 所有轴集合
/// </summary>
public ObservableCollection<Shaft> AllShaftList
{
get { return allShaftList; }
set
{
allShaftList = value;
this.RaisePropertyChanged("AllShaftList");
}
}
#endregion
#region 轴对象
private Shaft shaft_X;
/// <summary>
/// X轴
/// </summary>
public Shaft Shaft_X
{
get { return shaft_X; }
set
{
shaft_X = value;
this.RaisePropertyChanged("Shaft_X");
}
}
private Shaft shaft_Y;
/// <summary>
/// Y轴
/// </summary>
public Shaft Shaft_Y
{
get { return shaft_Y; }
set
{
shaft_Y = value;
this.RaisePropertyChanged("Shaft_Y");
}
}
private Shaft shaft_Z;
/// <summary>
/// Z轴
/// </summary>
public Shaft Shaft_Z
{
get { return shaft_Z; }
set
{
shaft_Z = value;
this.RaisePropertyChanged("Shaft_Z");
}
}
private Shaft shaft_U;
/// <summary>
/// U轴
/// </summary>
public Shaft Shaft_U
{
get { return shaft_U; }
set
{
shaft_U = value;
this.RaisePropertyChanged("Shaft_U");
}
}
#endregion
#region 各种方法
/// <summary>
/// 新增运动控制卡
/// </summary>
public DelegateCommand AddCardCommand { get; set; }
/// <summary>
/// 保存运动控制卡信息
/// </summary>
public DelegateCommand SaveCardCommand { get; set; }
/// <summary>
/// 新增轴
/// </summary>
public DelegateCommand AddShaftCommand { get; set; }
/// <summary>
/// 保存轴参数
/// </summary>
public DelegateCommand SaveShaftCommand { get; set; }
/// <summary>
/// 保存回原点参数
/// </summary>
public DelegateCommand SaveHomePerCommand { get; set; }
/// <summary>
/// 保存单轴运动参数
/// </summary>
public DelegateCommand SaveMovementPerCommand { get; set; }
/// <summary>
/// 鼠标左键按下时
/// </summary>
public DelegateCommand PreviewMouseLeftButtonDownCommand { get; set; }
/// <summary>
/// 鼠标左键松开时
/// </summary>
public DelegateCommand PreviewMouseLeftButtonUpCommand { get; set; }
/// <summary>
/// 编辑点位
/// </summary>
public DelegateCommand EditorPointCommand { get; set; }
/// <summary>
/// 新增点位
/// </summary>
public DelegateCommand AddPointCommand { get; set; }
/// <summary>
/// 删除点位
/// </summary>
public DelegateCommand DeletePointCommand { get; set; }
/// <summary>
/// 运行当前选中点位
/// </summary>
public DelegateCommand RunSelectPointCommand { get; set; }
/// <summary>
/// 保存运行点位坐标
/// </summary>
public DelegateCommand SaveMotionPointCommand { get; set; }
/// <summary>
/// 保存点位参数
/// </summary>
public DelegateCommand SavePointPerCommand { get; set; }
/// <summary>
/// 新增轨迹
/// </summary>
public DelegateCommand AddTrajectoryCommand { get; set; }
/// <summary>
/// 删除轨迹
/// </summary>
public DelegateCommand DeleteTrajectoryCommand { get; set; }
/// <summary>
/// 运行当前轨迹
/// </summary>
public DelegateCommand RunTrajectoryCommand { get; set; }
/// <summary>
/// 保存轨迹信息
/// </summary>
public DelegateCommand SaveTrajectoryCommand { get; set; }
#endregion
#region 构造函数
public MotionControlVM(MachineModel machine)
{
MachineObj = machine;
DisplayLevel = Visibility.Collapsed;
CardList = dal.GetCardList();
HomeStep = GengralProcessEnum.End;
CardConnection = true;
foreach (Card cd in CardList)
{
if (cd.Connect())
{
cd.GetCradInfo();
}else
{
CardConnection = false;
}
}
GetAllShaftList();
MyWindow = new MotionControWindow(AllShaftList);
MyWindow.DataContext = this;
AddCardCommand = new DelegateCommand();
AddCardCommand.ExecuteAction = new Action<object>(AddCard);
SaveCardCommand = new DelegateCommand();
SaveCardCommand.ExecuteAction = new Action<object>(SaveCard);
AddShaftCommand = new DelegateCommand();
AddShaftCommand.ExecuteAction = new Action<object>(AddShaft);
SaveShaftCommand = new DelegateCommand();
SaveShaftCommand.ExecuteAction = new Action<object>(SaveShaft);
SaveHomePerCommand = new DelegateCommand();
SaveHomePerCommand.ExecuteAction = new Action<object>(SaveHomePer);
SaveMovementPerCommand = new DelegateCommand();
SaveMovementPerCommand.ExecuteAction = new Action<object>(SaveMovementPer);
PreviewMouseLeftButtonDownCommand = new DelegateCommand();
PreviewMouseLeftButtonDownCommand.ExecuteAction = new Action<object>(PreviewMouseLeftButtonDown);
PreviewMouseLeftButtonUpCommand = new DelegateCommand();
PreviewMouseLeftButtonUpCommand.ExecuteAction = new Action<object>(PreviewMouseLeftButtonUp);
this.EditorPointCommand = new DelegateCommand();
this.EditorPointCommand.ExecuteAction = new Action<object>(EditorPoint);
this.AddPointCommand = new DelegateCommand();
this.AddPointCommand.ExecuteAction = new Action<object>(AddPoint);
this.DeletePointCommand = new DelegateCommand();
this.DeletePointCommand.ExecuteAction = new Action<object>(DeletePoint);
this.RunSelectPointCommand = new DelegateCommand();
this.RunSelectPointCommand.ExecuteAction = new Action<object>(RunSelectPoint);
this.SaveMotionPointCommand = new DelegateCommand();
this.SaveMotionPointCommand.ExecuteAction = new Action<object>(SaveMotionPoint);
this.SavePointPerCommand = new DelegateCommand();
this.SavePointPerCommand.ExecuteAction = new Action<object>(SavePointPer);
this.AddTrajectoryCommand = new DelegateCommand();
this.AddTrajectoryCommand.ExecuteAction = new Action<object>(AddTrajectory);
this.DeleteTrajectoryCommand = new DelegateCommand();
this.DeleteTrajectoryCommand.ExecuteAction = new Action<object>(DeleteTrajectory);
this.RunTrajectoryCommand = new DelegateCommand();
this.RunTrajectoryCommand.ExecuteAction = new Action<object>(RunTrajectory);
this.SaveTrajectoryCommand = new DelegateCommand();
this.SaveTrajectoryCommand.ExecuteAction = new Action<object>(SaveTrajectory);
IsEnables = false;
}
/// <summary>
/// 鼠标左键松开时发生
/// </summary>
/// <param name="obj"></param>
private void PreviewMouseLeftButtonUp(object obj)
{
StopMove();
}
/// <summary>
/// 鼠标左键按下时发生
/// </summary>
/// <param name="obj"></param>
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 保存单轴运行参数
/// <summary>
/// 保存单轴运行参数
/// </summary>
/// <param name="obj"></param>
private void SaveMovementPer(object obj)
{
if (SelectShaft == null)
{
return;
}
if (SelectShaft.ID <= 0)
{
MyMessageBox.Show("请先保存轴参数");
return;
}
SelectShaft.Standbys.AxisID = SelectShaft.ID;
int a = 0;
if (SelectShaft.Standbys.ID <= 0)
{
a = dal.AddMovementPer(SelectShaft.Standbys);
}
else
{
a = dal.UpdateMovementPer(SelectShaft.Standbys);
}
if (a > 0)
{
MyMessageBox.Show("保存成功");
SelectShaft.Standbys = dal.GetMovementPer(SelectShaft.ID);
}
else
{
MyMessageBox.Show("保存失败");
}
}
#endregion
#region 保存原点参数
/// <summary>
/// 保存原点参数
/// </summary>
/// <param name="obj"></param>
private void SaveHomePer(object obj)
{
if (SelectShaft == null)
{
return;
}
if (SelectShaft.ID <= 0)
{
MyMessageBox.Show("请先保存轴参数");
return;
}
SelectShaft.Home.AxisID = SelectShaft.ID;
int a = 0;
if (SelectShaft.Home.ID <= 0)
{
a = dal.AddHomePer(SelectShaft.Home);
}
else
{
a = dal.UpdateHomeper(SelectShaft.Home);
}
if (a > 0)
{
MyMessageBox.Show("保存成功");
SelectShaft.Home = dal.GetHomePer(SelectShaft.ID);
SelectShaft.Initialize();
}
else
{
MyMessageBox.Show("保存失败");
}
}
#endregion
#region 保存轴参数
/// <summary>
/// 保存轴参数
/// </summary>
/// <param name="obj"></param>
private void SaveShaft(object obj)
{
if (SelectShaft == null)
{
return;
}
int a = 0;
//如果运动控制卡不是Dmc 则就是lan
if (SelectCard.InterfaceType > 0)
{
SelectShaft.LanControl = true;
}
if (SelectShaft.ID <= 0)
{
a = dal.AddShaft(SelectShaft);
}
else
{
a = dal.UpdateShaft(SelectShaft);
}
if (a > 0)
{
MyMessageBox.Show("保存成功");
SelectCard.ShaftList = dal.GetShaftList(SelectCard.CardID);
GetAllShaftList();
}
else
{
MyMessageBox.Show("保存失败");
}
}
#endregion
#region 新增轴
/// <summary>
/// 新增轴
/// </summary>
/// <param name="obj"></param>
private void AddShaft(object obj)
{
if (SelectCard == null || SelectCard.ID <= 0)
{
MyMessageBox.Show("请选择要添加轴的控制卡");
return;
}
//if (CardList.Count >= SelectCard.TotalAxis)
//{
// MyMessageBox.Show("当前控制卡轴数量已到达上限");
// return;
//}
Shaft sf = new Shaft() { ShaftName = "新来的", CardID = SelectCard.CardID };
SelectCard.ShaftList.Add(sf);
}
#endregion
#region 新增运动控制卡
/// <summary>
/// 新增运动控制卡
/// </summary>
/// <param name="obj"></param>
private void AddCard(object obj)
{
Card cardnew = new Card() { CardName = "新来的" };
CardList.Add(cardnew);
}
#endregion
#region 保存运动控制卡信息
/// <summary>
/// 保存运动控制卡信息
/// </summary>
/// <param name="obj"></param>
private void SaveCard(object obj)
{
if (SelectCard == null)
{
return;
}
//if (!SelectCard.GetCradInfo())
//{
// MyMessageBox.Show("无法连接运动控制卡,请确认卡信息或硬件连接是否正确!");
// return;
//}
int a = 0;
if (SelectCard.ID > 0)
{
a = dal.UpdateCard(SelectCard);
}
else
{
a = dal.AddCaed(SelectCard);
}
if (a > 0)
{
MyMessageBox.Show("保存成功");
CardList = dal.GetCardList();
}
else
{
MyMessageBox.Show("保存失败");
}
}
#endregion
#region 点位调试的轴移动方法
/// <summary>
/// 当前调试运动轴
/// </summary>
Shaft MoveShaft { get; set; }
/// <summary>
/// 点位调试的轴移动方法
/// </summary>
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 轴移动运行
/// <summary>
/// 轴移动运行
/// </summary>
/// <param name="dir">运行方向</param>
private void ShaftMoverRun(ushort dir)
{
if (MoveShaft == null)
{
return;
}
if (TuningDis > 0)
{
if (TuningDis > 400)
{
MyMessageBox.Show("微调刻度超出限制,不应超出20MM");
}
else
if (dir == 1)
{
MoveShaft.Standbys.ActualDir = Math.Round(MoveShaft.ActualDir + TuningDis, 2);
MoveShaft.DZDC(MoveShaft.Standbys);
}
else if (dir == 0)
{
MoveShaft.Standbys.ActualDir = Math.Round(MoveShaft.ActualDir - TuningDis, 2);
MoveShaft.DZDC(MoveShaft.Standbys);
}
}
else
{
MoveShaft.ContinuousMove(dir);
}
}
#endregion
#region 停止运行
/// <summary>
/// 停止运行
/// </summary>
public void StopMove()
{
if (MoveShaft == null)
{
return;
}
if (TuningDis <= 0) //微调时 走完微调距离会自动停止
{
MoveShaft.ShaftStop();
}
}
#endregion
#region 获取所有轴
/// <summary>
/// 获取所有轴
/// </summary>
private void GetAllShaftList()
{
AllShaftList = new ObservableCollection<Shaft>();
foreach (Card c in CardList)
{
AllShaftList = new ObservableCollection<Shaft>(AllShaftList.Concat(c.ShaftList).ToList());
}
ShaftStateGrid = GetShaftStateGrid();
}
#endregion
#region 获取轴状态Grid
private Grid shaftStateGrid;
/// <summary>
/// 轴状态Grid
/// </summary>
public Grid ShaftStateGrid
{
get { return shaftStateGrid; }
set
{
shaftStateGrid = value;
this.RaisePropertyChanged("ShaftStateGrid");
}
}
/// <summary>
/// 获取轴状态Grid
/// </summary>
private Grid GetShaftStateGrid()
{
Grid g = new Grid();
int row = 0;
int column = 4;
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;//回原点按钮
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()); //添加行
}
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.DataContext = AllShaftList[number];
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控件的列
}
number++;
}
}
}
return g;
}
#endregion
#region 编辑坐标点
private bool isEnables;
/// <summary>
/// 是否启用编辑
/// </summary>
public bool IsEnables
{
get { return isEnables; }
set
{
isEnables = value;
this.RaisePropertyChanged("IsEnables");
}
}
/// <summary>
/// 编辑坐标点
/// </summary>
/// <param name="obj"></param>
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;
/// <summary>
/// 选中的点位分类
/// </summary>
public int TabSelectNumber
{
get { return tabSelectNumber; }
set
{
tabSelectNumber = value;
SelectMotionPoint = null;
SelectTrajectory = null;
this.RaisePropertyChanged("TabSelectNumber");
}
}
/// <summary>
/// 新增坐标点位
/// </summary>
/// <param name="obj"></param>
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 删除点位
/// <summary>
/// 删除点位
/// </summary>
/// <param name="obj"></param>
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 运行当前选中的坐标点
/// <summary>
/// 是否在进行运行到选择点位测试
/// </summary>
public bool RunSelectPointBool { get; set; }
/// <summary>
/// 运行当前选中的点胶点
/// </summary>
/// <param name="obj"></param>
public void RunSelectPoint(object obj)
{
if (SelectMotionPoint == null)
{
MyMessageBox.Show("请选择运行点位");
return;
}
if (!RunSelectPointBool && !RunSelectTrajectory)
{
RunSelectPointBool = true;
}
}
/// <summary>
/// 运行到选中点位
/// </summary>
public void PointRun()
{
if (!RunSelectPointBool)
{
return;
}
if (SelectMotionPoint.PointRun(AllShaftList))
{
RunSelectPointBool = false;
}
}
/// <summary>
/// 运行到指定点位
/// </summary>
/// <param name="point">点位对象</param>
/// <param name="multiple">降速倍数</param>
/// <returns></returns>
public bool PointRun(MotionPointModel point, int multiple = 1)
{
bool bl = false;
if (RunSelectPointBool)
{
return bl;
}
return point.PointRun(AllShaftList, multiple);
}
#endregion
#region 保存运行点位坐标
/// <summary>
/// 保存运行点位坐标
/// </summary>
/// <param name="obj"></param>
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.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
SelectMotionPoint.Speed_Y = Shaft_Y != null ? Shaft_Y.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
SelectMotionPoint.Speed_Z = Shaft_Z != null ? Shaft_Z.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
SelectMotionPoint.Speed_U = Shaft_U != null ? Shaft_U.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
if (dal.UpdateMotionPoint(SelectMotionPoint) > 0)
{
MyMessageBox.Show("保存成功");
}
else
{
MyMessageBox.Show("保存失败");
}
}
#endregion
#region 保存点位参数
/// <summary>
/// 保存点位参数
/// </summary>
/// <param name="obj"></param>
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.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
SelectMotionPoint.Speed_Y = Shaft_Y != null ? Shaft_Y.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
SelectMotionPoint.Speed_Z = Shaft_Z != null ? Shaft_Z.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
SelectMotionPoint.Speed_U = Shaft_U != null ? Shaft_U.Standbys.StartSpeed * SelectMotionPoint.Speed_Gear : 0;
if (dal.UpdateMotionPointPer(SelectMotionPoint) > 0)
{
MyMessageBox.Show("保存成功");
}
else
{
MyMessageBox.Show("保存失败");
}
}
#endregion
#region 所有轴回原点
/// <summary>
/// 所有轴自动回原点步骤
/// </summary>
public GengralProcessEnum HomeStep { get; set; }
/// <summary>
/// 所有轴回原点
/// </summary>
/// <param name="emg">急停信号</param>
public void OrginALL(InputSignal emg)
{
#region 监控手动回原点
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin)
{
df.HYD(emg);
}
}
#endregion
#region 自动回原点
bool bl = true;
switch (HomeStep)
{
case GengralProcessEnum.Start:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 1)
{
df.org_Successful = true;
}
}
HomeStep++;
break;
case GengralProcessEnum.Step1:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 1 && df.org_Successful)
{
bl = false;
}
}
if (bl == true)
{
HomeStep++;
}
break;
case GengralProcessEnum.Step2:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 2)
{
df.org_Successful = true;
}
}
HomeStep++;
break;
case GengralProcessEnum.Step3:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 2 && df.org_Successful)
{
bl = false;
}
}
if (bl == true)
{
HomeStep++;
}
break;
case GengralProcessEnum.Step4:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 3)
{
df.org_Successful = true;
}
}
HomeStep++;
break;
case GengralProcessEnum.Step5:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 3 && df.org_Successful)
{
bl = false;
}
}
if (bl == true)
{
HomeStep++;
}
break;
case GengralProcessEnum.Step6:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 4)
{
df.org_Successful = true;
}
}
HomeStep++;
break;
case GengralProcessEnum.Step7:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 4 && df.org_Successful)
{
bl = false;
}
}
if (bl == true)
{
HomeStep++;
}
break;
case GengralProcessEnum.Step8:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 5)
{
df.org_Successful = true;
}
}
HomeStep++;
break;
case GengralProcessEnum.Step9:
foreach (Shaft df in AllShaftList)
{
if (df.ToOrgin && df.GoHomeOrder == 5 && df.org_Successful)
{
bl = false;
}
}
if (bl == true)
{
HomeStep = GengralProcessEnum.End;
}
break;
}
#endregion
#region 所有轴状态
//所有轴状态
foreach (Shaft s in AllShaftList)
{
s.GetAllStart();
if (s.ShaftState.StatusBackground == ColorStr.Yellow)
{
eventExcepReport(s.ShaftName + "异常");//抛出异常
}
}
#endregion
}
#endregion
#region 停止所有轴
/// <summary>
/// 停止所有轴
/// </summary>
public void StopAllShaft()
{
foreach (Shaft sf in AllShaftList)
{
sf.ShaftStop();
}
}
#endregion
#region 新增运行轨迹
/// <summary>
/// 新增运行轨迹
/// </summary>
/// <param name="obj"></param>
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 删除运行轨迹
/// <summary>
/// 删除运行轨迹
/// </summary>
/// <param name="obj"></param>
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 保存运行轨迹信息
/// <summary>
/// 保存运行轨迹信息
/// </summary>
/// <param name="obj"></param>
private void SaveTrajectory(object obj)
{
if (SelectTrajectory != null)
{
if (dal.UpdateMotionTrajectory(SelectTrajectory) > 0)
{
MyMessageBox.Show("保存成功");
}
else
{
MyMessageBox.Show("保存失败");
}
}
}
#endregion
#region 运行当前轨迹
/// <summary>
/// 是否正在在运行选中轨迹
/// </summary>
bool RunSelectTrajectory { get; set; }
/// <summary>
/// 运行当前轨迹
/// </summary>
/// <param name="obj"></param>
private void RunTrajectory(object obj)
{
if (SelectTrajectory == null)
{
MyMessageBox.Show("请选择运动轨迹");
return;
}
if (!RunSelectPointBool && !RunSelectTrajectory)
{
RunSelectTrajectory = true;
}
}
/// <summary>
/// 运行选中的轨迹
/// </summary>
public void RunMotionTrajectory()
{
if (!RunSelectTrajectory)
{
return;
}
if (SelectTrajectory.TrajectoryTrype <= 0)
{
MyMessageBox.Show("点位轨迹,请使用单点运行");
return;
}
if (SelectTrajectory.TrajectoryRun(AllShaftList))
{
RunSelectTrajectory = false;
}
}
#endregion
}
}