From 5fe374eeffd698b06947e7e0bf018f2c32b7988b Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Sun, 12 Jul 2026 17:53:06 +0800 Subject: [PATCH] robotMatrix --- PLCSharpWpf/Models/DatasContext.cs | 4 + PLCSharpWpf/Models/GlobalModel.cs | 125 +++-------------- .../VVMs/GlobalVariables/VariablesModel.cs | 38 +++++ .../VVMs/MotionController/ControllersModel.cs | 66 ++++++++- PLCSharpWpf/VVMs/Robots/Epson/EpsonRobot.cs | 21 ++- PLCSharpWpf/VVMs/Robots/Robot.cs | 112 ++++++++++++++- PLCSharpWpf/VVMs/Robots/RobotMatrix.cs | 132 ++++++++++++++++++ PLCSharpWpf/VVMs/Robots/RobotModel.cs | 50 +++++-- PLCSharpWpf/VVMs/Robots/RobotPoint.cs | 11 ++ PLCSharpWpf/VVMs/Robots/Robots.xaml | 48 ++++++- PLCSharpWpf/VVMs/Robots/RobotsViewModel.cs | 127 +++++++++++++++++ PLCSharpWpf/VVMs/Vision/VisionsModel.cs | 33 ++++- PLCSharpWpf/VVMs/Workflows/Workflow.cs | 5 + 13 files changed, 640 insertions(+), 132 deletions(-) create mode 100644 PLCSharpWpf/VVMs/Robots/RobotMatrix.cs diff --git a/PLCSharpWpf/Models/DatasContext.cs b/PLCSharpWpf/Models/DatasContext.cs index dd5f279..e472e01 100644 --- a/PLCSharpWpf/Models/DatasContext.cs +++ b/PLCSharpWpf/Models/DatasContext.cs @@ -105,6 +105,10 @@ namespace PLCSharp.Models /// public DbSet RobotPoints { get; set; } /// + /// 机器人矩阵 + /// + public DbSet RobotMatrices { get; set; } + /// /// 错误Logs /// public DbSet ErrorLogs { get; set; } diff --git a/PLCSharpWpf/Models/GlobalModel.cs b/PLCSharpWpf/Models/GlobalModel.cs index d328873..534d62d 100644 --- a/PLCSharpWpf/Models/GlobalModel.cs +++ b/PLCSharpWpf/Models/GlobalModel.cs @@ -11,7 +11,6 @@ using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.Recipe; using PLCSharp.VVMs.Robots; using PLCSharp.VVMs.Vision; -using PLCSharp.VVMs.Vision.Camera; using PLCSharp.VVMs.Workflows; using Prism.Commands; using Prism.Dialogs; @@ -160,6 +159,7 @@ namespace PLCSharp.Models } #endregion + #region 硬件 /// /// 连接模型 @@ -190,7 +190,7 @@ namespace PLCSharp.Models { var connect = Connects.Connects.FirstOrDefault(c => c.Name == name) ?? throw new Exception("网络未配置!"); - + return connect; } /// @@ -202,7 +202,7 @@ namespace PLCSharp.Models { var robot = RobotModel.Robots.FirstOrDefault(c => c.Name == name) ?? throw new Exception("Robot未配置!"); - + return robot; } /// @@ -230,6 +230,7 @@ namespace PLCSharp.Models #endregion + #region 任务 /// /// Workflows模型 @@ -250,6 +251,7 @@ namespace PLCSharp.Models return WorkflowsModel.Workflows.FirstOrDefault(t => t.Name == name); } #endregion + #region 日志 private ObservableCollection _ErrorLogs = []; @@ -362,6 +364,7 @@ namespace PLCSharp.Models } #endregion + #region 模式与状态 private ModeState _ModeState = new(); /// @@ -403,6 +406,7 @@ namespace PLCSharp.Models } } #endregion + #region 配方 @@ -1086,116 +1090,18 @@ namespace PLCSharp.Models { Target = "UIReLoad" }); - //加载当前配方的用户变量列表 - VariablesModel.Variables.Clear(); - var variables = _DatasContext.Variables.Where(c => c.RecipeID == CurrentRecipe.ID); - foreach (var item in variables) - { - if (item.RetainPersistent) - { - - item.Value = Variable.DynamicValue(item.ValueStr, item.Type); - - } - else - { - item.Value = Variable.DynamicValue(item.DefaultValueStr, item.Type); - item.DefaultValue = item.Value; - - } - VariablesModel.Variables.Add(item); - } - //加载当前配方的系统变量列表 - VariablesModel.SystemVariables.Clear(); - var systemVariables = _DatasContext.SystemVariables.Where(c => c.RecipeID == CurrentRecipe.ID); - foreach (var item in systemVariables) - { - if (item.RetainPersistent) - { - - item.Value = Variable.DynamicValue(item.ValueStr, item.Type); - - } - else - { - item.Value = Variable.DynamicValue(item.DefaultValueStr, item.Type); - item.DefaultValue = item.Value; - - } - VariablesModel.SystemVariables.Add(item); - } - //加载当前配方的图像数据列表 - VisionsModel.ImageDatas.Clear(); - var currRecipeImageDatas = _DatasContext.ImageDatas.Where(c => c.RecipeID == CurrentRecipe.ID); - foreach (var item in currRecipeImageDatas) - { - var itemcopy = item.DeepCopy(); - itemcopy.Prompt = ""; - VisionsModel.ImageDatas.Add(itemcopy); - } + //加载当前配方的全局变量列表 + VariablesModel.LoadRecipe(CurrentRecipe.ID); + //加载当前配方的视觉功能列表 - VisionsModel.VisionFunctions.Clear(); - var currRecipeVisionFunctions = _DatasContext.VisionFunctions.Where(c => c.RecipeID == CurrentRecipe.ID); - foreach (var item in currRecipeVisionFunctions) - { - var itemcopy = item.DeepCopy(); - itemcopy.Prompt = ""; - itemcopy.VisionsModel = VisionsModel; - itemcopy.ImageDatas = VisionsModel.ImageDatas; - itemcopy.GlobalModel = this; - VisionsModel.VisionFunctions.Add(itemcopy); - } + VisionsModel.LoadRecipe(CurrentRecipe.ID); + //加载当前配方的点位列表 - ControllersModel.Stop(); - ControllersModel.AxisPoints.Clear(); - var currRecipeAxisPoints = _DatasContext.AxisPoints.Where(c => c.RecipeID == CurrentRecipe.ID); - foreach (var item in currRecipeAxisPoints) - { - var axisPoint = item.DeepCopy(); - ControllersModel.AxisPoints.Add(axisPoint); - var axisX = ControllersModel.Axes.Where(a => a.Name == axisPoint.AxisXName).FirstOrDefault(); - if (axisX != null) - axisPoint.AxisX = axisX; - var axisY = ControllersModel.Axes.Where(a => a.Name == axisPoint.AxisYName).FirstOrDefault(); - if (axisY != null) - axisPoint.AxisY = axisY; - var axisZ = ControllersModel.Axes.Where(a => a.Name == axisPoint.AxisZName).FirstOrDefault(); - if (axisZ != null) - axisPoint.AxisZ = axisZ; - var axisU = ControllersModel.Axes.Where(a => a.Name == axisPoint.AxisUName).FirstOrDefault(); - if (axisU != null) - axisPoint.AxisU = axisU; - } - //加载当前西方的矩阵列表 - ControllersModel.Matrices.Clear(); - var currRecipeMatrices = _DatasContext.Matrices.Where(c => c.RecipeID == CurrentRecipe.ID); - foreach (var item in currRecipeMatrices) - { - var matrix = item.DeepCopy(); - ControllersModel.Matrices.Add(matrix); + ControllersModel.LoadRecipe(CurrentRecipe.ID); - ControllersModel.Create(matrix); - - - } - - //加载当前配方的插补列表 - - ControllersModel.InterpolationGroups.Clear(); - var currRecipeInterpolationGroups = _DatasContext.InterpolationGroups.Where(c => c.RecipeID == CurrentRecipe.ID); - foreach (var item in currRecipeInterpolationGroups) - { - var itemcopy = item.DeepCopy(); - ControllersModel.InterpolationGroups.Add(itemcopy); - var axisX = ControllersModel.Axes.Where(a => a.Name == itemcopy.AxisXName).FirstOrDefault(); - if (axisX != null) - itemcopy.AxisX = axisX; - var axisY = ControllersModel.Axes.Where(a => a.Name == itemcopy.AxisYName).FirstOrDefault(); - if (axisY != null) - itemcopy.AxisY = axisY; - } - ControllersModel.Start(); + //加载当前配方的机器人点位和矩阵列表 + RobotModel.LoadRecipe(CurrentRecipe.ID); //加载当前配方的任务列表 WorkflowsModel.Workflows.Clear(); @@ -1324,6 +1230,7 @@ namespace PLCSharp.Models } #endregion + #region 主页面 private ObservableCollection _CustomControls = []; /// diff --git a/PLCSharpWpf/VVMs/GlobalVariables/VariablesModel.cs b/PLCSharpWpf/VVMs/GlobalVariables/VariablesModel.cs index 9bf2d5c..6c3c426 100644 --- a/PLCSharpWpf/VVMs/GlobalVariables/VariablesModel.cs +++ b/PLCSharpWpf/VVMs/GlobalVariables/VariablesModel.cs @@ -45,9 +45,47 @@ namespace PLCSharp.VVMs.GlobalVariables item._DatasContext = _DatasContext; } } + internal void LoadRecipe(Guid CurrentRecipeID) + { + Variables.Clear(); + var variables = _DatasContext.Variables.Where(c => c.RecipeID == CurrentRecipeID); + foreach (var item in variables) + { + if (item.RetainPersistent) + { + item.Value = Variable.DynamicValue(item.ValueStr, item.Type); + } + else + { + item.Value = Variable.DynamicValue(item.DefaultValueStr, item.Type); + item.DefaultValue = item.Value; + } + Variables.Add(item); + } + + //加载当前配方的系统变量列表 + SystemVariables.Clear(); + var systemVariables = _DatasContext.SystemVariables.Where(c => c.RecipeID == CurrentRecipeID); + foreach (var item in systemVariables) + { + if (item.RetainPersistent) + { + + item.Value = Variable.DynamicValue(item.ValueStr, item.Type); + + } + else + { + item.Value = Variable.DynamicValue(item.DefaultValueStr, item.Type); + item.DefaultValue = item.Value; + + } + SystemVariables.Add(item); + } + } /// /// 局部变量集合 /// diff --git a/PLCSharpWpf/VVMs/MotionController/ControllersModel.cs b/PLCSharpWpf/VVMs/MotionController/ControllersModel.cs index 99663ec..55d5c9d 100644 --- a/PLCSharpWpf/VVMs/MotionController/ControllersModel.cs +++ b/PLCSharpWpf/VVMs/MotionController/ControllersModel.cs @@ -101,7 +101,7 @@ namespace PLCSharp.VVMs.MotionController } - e.Cancel = true; // 标记任务已被取消 + } #endregion @@ -186,6 +186,15 @@ namespace PLCSharp.VVMs.MotionController } + private double _Rate = 100; + /// + /// + /// + public double Rate + { + get { return _Rate; } + set { SetProperty(ref _Rate, value); } + } /// /// DI @@ -1651,6 +1660,61 @@ namespace PLCSharp.VVMs.MotionController } + internal void LoadRecipe(Guid CurrentRecipeID) + { + + + Stop(); + AxisPoints.Clear(); + var currRecipeAxisPoints = _DatasContext.AxisPoints.Where(c => c.RecipeID == CurrentRecipeID); + foreach (var item in currRecipeAxisPoints) + { + var axisPoint = item.DeepCopy(); + AxisPoints.Add(axisPoint); + var axisX = Axes.Where(a => a.Name == axisPoint.AxisXName).FirstOrDefault(); + if (axisX != null) + axisPoint.AxisX = axisX; + var axisY = Axes.Where(a => a.Name == axisPoint.AxisYName).FirstOrDefault(); + if (axisY != null) + axisPoint.AxisY = axisY; + var axisZ = Axes.Where(a => a.Name == axisPoint.AxisZName).FirstOrDefault(); + if (axisZ != null) + axisPoint.AxisZ = axisZ; + var axisU = Axes.Where(a => a.Name == axisPoint.AxisUName).FirstOrDefault(); + if (axisU != null) + axisPoint.AxisU = axisU; + } + //加载当前配方的矩阵列表 + Matrices.Clear(); + var currRecipeMatrices = _DatasContext.Matrices.Where(c => c.RecipeID == CurrentRecipeID); + foreach (var item in currRecipeMatrices) + { + var matrix = item.DeepCopy(); + Matrices.Add(matrix); + + Create(matrix); + + + } + + //加载当前配方的插补列表 + InterpolationGroups.Clear(); + var currRecipeInterpolationGroups = _DatasContext.InterpolationGroups.Where(c => c.RecipeID == CurrentRecipeID); + foreach (var item in currRecipeInterpolationGroups) + { + var itemcopy = item.DeepCopy(); + InterpolationGroups.Add(itemcopy); + var axisX = Axes.Where(a => a.Name == itemcopy.AxisXName).FirstOrDefault(); + if (axisX != null) + itemcopy.AxisX = axisX; + var axisY = Axes.Where(a => a.Name == itemcopy.AxisYName).FirstOrDefault(); + if (axisY != null) + itemcopy.AxisY = axisY; + } + Start(); + + } + #endregion diff --git a/PLCSharpWpf/VVMs/Robots/Epson/EpsonRobot.cs b/PLCSharpWpf/VVMs/Robots/Epson/EpsonRobot.cs index bb5ed5c..ebf888b 100644 --- a/PLCSharpWpf/VVMs/Robots/Epson/EpsonRobot.cs +++ b/PLCSharpWpf/VVMs/Robots/Epson/EpsonRobot.cs @@ -119,7 +119,7 @@ public class EpsonRobot : Robot } catch (Exception ex) { - System.Diagnostics.Debug.WriteLine($"Epson状态轮询异常: {ex.Message}"); + Model.SendErr($"Epson状态轮询异常: {ex.Message}"); } } } @@ -185,7 +185,7 @@ public class EpsonRobot : Robot catch (Exception ex) { - System.Diagnostics.Debug.WriteLine($"Epson状态解析异常: {ex.Message}"); + Model.SendErr($"Epson状态解析异常: {ex.Message}"); Thread.Sleep(1000); break; } @@ -193,7 +193,7 @@ public class EpsonRobot : Robot } catch (Exception ex) { - System.Diagnostics.Debug.WriteLine($"Epson状态轮询外异常: {ex.Message}"); + Model.SendErr($"Epson状态轮询外异常: {ex.Message}"); Thread.Sleep(1000); } } @@ -319,7 +319,7 @@ public class EpsonRobot : Robot } /// - /// 点动 + /// 点动 /// /// /// @@ -329,7 +329,7 @@ public class EpsonRobot : Robot public override void Jog(string pointName, string cmd, double dist, double rate = 0) { PointDone = false; - + var point = Points.FirstOrDefault(p => p.Name == pointName) ?? throw new Exception($"机器人点位{pointName}不存在"); PointCommand = CurrPoint.DeepCopy(); @@ -413,6 +413,17 @@ public class EpsonRobot : Robot } + + public override void RunMatrixPoint(RobotMatrix matrix, int xTarget, int yTarget) + { + + var point = matrix.GetPoint(xTarget, yTarget) ?? throw new Exception($"机器人矩阵点位{xTarget} , {yTarget}不存在"); + point.Command = "RunPoint"; + PointCommand = point.DeepCopy(); + SendCommand(PointCommand); + + } + /// /// Power /// diff --git a/PLCSharpWpf/VVMs/Robots/Robot.cs b/PLCSharpWpf/VVMs/Robots/Robot.cs index 29157fa..b5e11c9 100644 --- a/PLCSharpWpf/VVMs/Robots/Robot.cs +++ b/PLCSharpWpf/VVMs/Robots/Robot.cs @@ -14,6 +14,11 @@ namespace PLCSharp.VVMs.Robots /// public class Robot : BindableBase { + [NotMapped] + + public RobotModel Model { get; set; } + + /// /// 唯一标识 /// @@ -414,10 +419,9 @@ namespace PLCSharp.VVMs.Robots /// 运行点位 /// /// 机器人点位 - /// 返回布尔值 public virtual void RunPoint(string pointName) { - + } /// @@ -428,16 +432,16 @@ namespace PLCSharp.VVMs.Robots /// dist /// rate /// 返回布尔值 - public virtual void Jog(string pointName, string cmd, double dist, double rate = 0) + public virtual void Jog(string pointName, string cmd, double dist, double rate = 0) { - + } /// /// 点位运行完成 /// [NotMapped] - + public bool PointDone { get; set; } private DelegateCommand _HomeCmd; @@ -486,5 +490,103 @@ namespace PLCSharp.VVMs.Robots } + #region RobotMatrix + + private ObservableCollection _RobotMatrices = []; + /// + /// 机器人矩阵列表 + /// + [NotMapped] + public ObservableCollection RobotMatrices + { + get { return _RobotMatrices; } + set { SetProperty(ref _RobotMatrices, value); } + } + + /// + /// 根据矩阵配置生成点位网格(平行四边形插值) + /// + /// 矩阵配置 + internal void Create(RobotMatrix matrix) + { + matrix.Points.Clear(); + + + + int xCount = matrix.XCount > 0 ? matrix.XCount : 1; + int yCount = matrix.YCount > 0 ? matrix.YCount : 1; + int total = xCount * yCount; + + // 从机器人的点位列表中查找三个角点 + var startPoint = Points.FirstOrDefault(p => p.Name == matrix.StartName); + var xEndPoint = Points.FirstOrDefault(p => p.Name == matrix.XEndName); + var yEndPoint = Points.FirstOrDefault(p => p.Name == matrix.YEndName); + + if (startPoint == null || xEndPoint == null || yEndPoint == null) + { + Model.SendInfoDialog("基准点位名称错误,创建失败!"); + return; + } + + // 计算 X/Y/Z 方向基向量(平行四边形插值) + double xBasisX = xCount > 1 ? (xEndPoint.X - startPoint.X) / (xCount - 1) : 0; + double xBasisY = xCount > 1 ? (xEndPoint.Y - startPoint.Y) / (xCount - 1) : 0; + double xBasisZ = xCount > 1 ? (xEndPoint.Z - startPoint.Z) / (xCount - 1) : 0; + double yBasisX = yCount > 1 ? (yEndPoint.X - startPoint.X) / (yCount - 1) : 0; + double yBasisY = yCount > 1 ? (yEndPoint.Y - startPoint.Y) / (yCount - 1) : 0; + double yBasisZ = yCount > 1 ? (yEndPoint.Z - startPoint.Z) / (yCount - 1) : 0; + + double originX = startPoint.X; + double originY = startPoint.Y; + double originZ = startPoint.Z; + + // 按 MatrixType 确定遍历顺序 + for (int i = 0; i < total; i++) + { + int x, y; + if (matrix.MatrixType == 0) + { + x = i % xCount; + y = i / xCount; + } + else + { + y = i % yCount; + x = i / yCount; + } + + // 平行四边形插值坐标(X/Y/Z 线性插值,U/V/W 取自起始点) + double px = originX + x * xBasisX + y * yBasisX; + double py = originY + x * xBasisY + y * yBasisY; + double pz = originZ + x * xBasisZ + y * yBasisZ; + + var point = new RobotPoint + { + RobotID = matrix.RobotID, + XIndex = x, + YIndex = y, + X = px, + Y = py, + Z = pz, + U = startPoint.U, + V = startPoint.V, + W = startPoint.W, + Rate = startPoint.Rate, + Hand = startPoint.Hand, + ToolNum = startPoint.ToolNum, + }; + + matrix.Points.Add(point); + } + } + + public virtual void RunMatrixPoint(RobotMatrix matrix, int xTarget, int yTarget) + { + + } + + #endregion + + } } diff --git a/PLCSharpWpf/VVMs/Robots/RobotMatrix.cs b/PLCSharpWpf/VVMs/Robots/RobotMatrix.cs new file mode 100644 index 0000000..134e0cd --- /dev/null +++ b/PLCSharpWpf/VVMs/Robots/RobotMatrix.cs @@ -0,0 +1,132 @@ +using Prism.Mvvm; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace PLCSharp.VVMs.Robots +{ + /// + /// 机器人矩阵(平行四边形插值生成点位网格) + /// + public class RobotMatrix : BindableBase + { + /// + /// 唯一标识 + /// + [Key] + public Guid ID { get; set; } = Guid.NewGuid(); + /// + /// 配方标识 + /// + public Guid RecipeID { get; set; } + /// + /// 所属机器人ID + /// + public Guid RobotID { get; set; } + + private string _Name; + /// + /// 名称 + /// + public string Name + { + get { return _Name; } + set { SetProperty(ref _Name, value); } + } + + private string _StartName; + /// + /// 首点名(原点 (0,0)) + /// + public string StartName + { + get { return _StartName; } + set { SetProperty(ref _StartName, value); } + } + + private string _XEndName; + /// + /// X方向尾点名((XCount-1, 0)) + /// + public string XEndName + { + get { return _XEndName; } + set { SetProperty(ref _XEndName, value); } + } + + private string _YEndName; + /// + /// Y方向尾点名((0, YCount-1)) + /// + public string YEndName + { + get { return _YEndName; } + set { SetProperty(ref _YEndName, value); } + } + + private int _XCount; + /// + /// X方向点数 + /// + public int XCount + { + get { return _XCount; } + set { SetProperty(ref _XCount, value); } + } + + private int _YCount; + /// + /// Y方向点数 + /// + public int YCount + { + get { return _YCount; } + set { SetProperty(ref _YCount, value); } + } + + private int _MatrixType; + /// + /// 矩阵元素顺序 0先X后Y, 1先Y后X + /// + public int MatrixType + { + get { return _MatrixType; } + set { SetProperty(ref _MatrixType, value); } + } + + private int _XTarget; + /// + /// 手动运行时输入的X目标 + /// + [NotMapped] + public int XTarget + { + get { return _XTarget; } + set { SetProperty(ref _XTarget, value); } + } + + private int _YTarget; + /// + /// 手动运行时输入的Y目标 + /// + [NotMapped] + public int YTarget + { + get { return _YTarget; } + set { SetProperty(ref _YTarget, value); } + } + + /// + /// 生成的点位列表 + /// + [NotMapped] + public List Points { get; set; } = []; + + /// + /// 按索引查找点位 + /// + public RobotPoint? GetPoint(int x, int y) + { + return Points.FirstOrDefault(p => p.XIndex == x && p.YIndex == y); + } + } +} diff --git a/PLCSharpWpf/VVMs/Robots/RobotModel.cs b/PLCSharpWpf/VVMs/Robots/RobotModel.cs index 4b97aca..1d3bed0 100644 --- a/PLCSharpWpf/VVMs/Robots/RobotModel.cs +++ b/PLCSharpWpf/VVMs/Robots/RobotModel.cs @@ -1,4 +1,4 @@ -using PLCSharp.Core.Prism; +using PLCSharp.Core.Prism; using PLCSharp.Core.Tools; using PLCSharp.Models; using PLCSharp.VVMs.Robots.Epson; @@ -66,13 +66,14 @@ namespace PLCSharp.VVMs.Robots GlobalModel = globalModel; foreach (var item in _DatasContext.Robots) { - + switch (item.Type) { case RobotType.Undefined: break; case RobotType.Epson: var epsonRobot = EpsonRobot.Create(item); + epsonRobot.Model = this; Robots.Add(epsonRobot); break; default: @@ -82,15 +83,48 @@ namespace PLCSharp.VVMs.Robots } - foreach (var item in _DatasContext.RobotPoints) - { - var point = item.DeepCopy(); - var robot = Robots.FirstOrDefault(r => r.ID == point.RobotID); - robot?.Points.Add(point); - } + } #endregion + public void LoadRecipe(Guid CurrentRecipeID) + { + + + foreach (var robot in Robots) + { + robot.Points.Clear(); + + foreach (var item in _DatasContext.RobotPoints) + { + if (item.RobotID == robot.ID && item.RecipeID == CurrentRecipeID) + { + + var point = item.DeepCopy(); + + robot.Points.Add(point); + } + } + + robot.RobotMatrices.Clear(); + foreach (var item in _DatasContext.RobotMatrices) + { + + + if (item.RobotID == robot.ID && item.RecipeID == CurrentRecipeID) + { + + var matrix = item.DeepCopy(); + robot.Create(matrix); + robot.RobotMatrices.Add(matrix); + } + } + + } + + + } + } diff --git a/PLCSharpWpf/VVMs/Robots/RobotPoint.cs b/PLCSharpWpf/VVMs/Robots/RobotPoint.cs index d30372d..044894d 100644 --- a/PLCSharpWpf/VVMs/Robots/RobotPoint.cs +++ b/PLCSharpWpf/VVMs/Robots/RobotPoint.cs @@ -219,6 +219,17 @@ namespace PLCSharp.VVMs.Robots [NotMapped] public string Command { get; set; } + /// + /// 在矩阵中的X索引 + /// + [NotMapped] + public int XIndex { get; internal set; } + /// + /// 在矩阵中的Y索引 + /// + [NotMapped] + public int YIndex { get; internal set; } + /// /// 点位Params /// diff --git a/PLCSharpWpf/VVMs/Robots/Robots.xaml b/PLCSharpWpf/VVMs/Robots/Robots.xaml index 5c9856a..6db0041 100644 --- a/PLCSharpWpf/VVMs/Robots/Robots.xaml +++ b/PLCSharpWpf/VVMs/Robots/Robots.xaml @@ -30,6 +30,7 @@ + @@ -167,8 +168,11 @@ - - + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PLCSharpWpf/VVMs/Robots/RobotsViewModel.cs b/PLCSharpWpf/VVMs/Robots/RobotsViewModel.cs index 53191e9..a0f7513 100644 --- a/PLCSharpWpf/VVMs/Robots/RobotsViewModel.cs +++ b/PLCSharpWpf/VVMs/Robots/RobotsViewModel.cs @@ -170,6 +170,133 @@ namespace PLCSharp.VVMs.Robots } } #endregion + + #region RobotMatrix CRUD + + private RobotMatrix _SelectedRobotMatrix; + /// + /// Selected机器人矩阵 + /// + public RobotMatrix SelectedRobotMatrix + { + get { return _SelectedRobotMatrix; } + set { SetProperty(ref _SelectedRobotMatrix, value); } + } + + private DelegateCommand _RobotMatrixManage; + /// + /// 机器人矩阵管理 + /// + public DelegateCommand RobotMatrixManage => + _RobotMatrixManage ??= new DelegateCommand(ExecuteRobotMatrixManage); + + void ExecuteRobotMatrixManage(object param) + { + + if (SelectedRobot == null) + { + SendInfoDialog("请先选择机器人"); + return; + } + var cmd = param as string; + + + switch (cmd) + { + case "New": + + var newMatrix = new RobotMatrix + { + RobotID = SelectedRobot.ID, + RecipeID = RobotModel.GlobalModel.CurrentRecipe.ID , + Name = "新矩阵", + XCount = 2, + YCount = 2, + }; + SelectedRobot.RobotMatrices.Add(newMatrix); + SelectedRobotMatrix = newMatrix; + break; + + case "Create": + + if (SelectedRobotMatrix == null) + { + SendInfoDialog("请先选择矩阵"); + return; + } + SelectedRobot.Create(SelectedRobotMatrix); + SendInfoDialog("矩阵计算完成"); + break; + + case "Save": + + var names = new List(); + foreach (var item in SelectedRobot.RobotMatrices) + { + if (string.IsNullOrEmpty(item.Name)) + { + SendInfoDialog($"保存失败,矩阵名称不能为空"); + return; + } + if (names.Contains(item.Name)) + { + SendInfoDialog($"保存失败,重复名称:{item.Name}"); + return; + } + names.Add(item.Name); + + if (!RobotModel._DatasContext.RobotMatrices.Any(h => h.ID == item.ID)) + { + RobotModel._DatasContext.RobotMatrices.Add(item); + } + else + { + var exist = RobotModel._DatasContext.RobotMatrices.FirstOrDefault(m => m.ID == item.ID); + if (exist != null) + { + exist.Name = item.Name; + exist.StartName = item.StartName; + exist.XEndName = item.XEndName; + exist.YEndName = item.YEndName; + exist.XCount = item.XCount; + exist.YCount = item.YCount; + exist.MatrixType = item.MatrixType; + } + } + } + RobotModel._DatasContext.Save(); + SendInfoDialog("矩阵保存成功"); + break; + + case "Remove": + + if (SelectedRobotMatrix != null) + { + if (RobotModel._DatasContext.RobotMatrices.Any(h => h.ID == SelectedRobotMatrix.ID)) + { + var remove = RobotModel._DatasContext.RobotMatrices.FirstOrDefault(m => m.ID == SelectedRobotMatrix.ID); + if (remove != null) + RobotModel._DatasContext.RobotMatrices.Remove(remove); + } + SelectedRobot.RobotMatrices.Remove(SelectedRobotMatrix); + RobotModel._DatasContext.Save(); + SelectedRobotMatrix = null; + } + break; + + case "RunPoint": + + if (SelectedRobotMatrix != null) { + + SelectedRobot.RunMatrixPoint(SelectedRobotMatrix,SelectedRobotMatrix.XTarget, SelectedRobotMatrix.YTarget); + } + + break; + } + } + + #endregion + #region Robot CRUD diff --git a/PLCSharpWpf/VVMs/Vision/VisionsModel.cs b/PLCSharpWpf/VVMs/Vision/VisionsModel.cs index eeeb780..4cfdb8d 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionsModel.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionsModel.cs @@ -60,6 +60,8 @@ namespace PLCSharp.VVMs.Vision /// public GlobalModel GlobalModel { get; set; } + + /// /// 初始化 /// @@ -70,6 +72,35 @@ namespace PLCSharp.VVMs.Vision Connects = GlobalModel.Connects; } + public void LoadRecipe(Guid CurrentRecipeID) + { + + //加载当前配方的图像数据列表 + ImageDatas.Clear(); + var currRecipeImageDatas = _DatasContext.ImageDatas.Where(c => c.RecipeID == CurrentRecipeID); + foreach (var item in currRecipeImageDatas) + { + var itemcopy = item.DeepCopy(); + itemcopy.Prompt = ""; + ImageDatas.Add(itemcopy); + } + + + VisionFunctions.Clear(); + var currRecipeVisionFunctions = _DatasContext.VisionFunctions.Where(c => c.RecipeID == CurrentRecipeID); + foreach (var item in currRecipeVisionFunctions) + { + var itemcopy = item.DeepCopy(); + itemcopy.Prompt = ""; + itemcopy.VisionsModel = this; + itemcopy.ImageDatas = ImageDatas; + itemcopy.GlobalModel = GlobalModel; + VisionFunctions.Add(itemcopy); + } + + } + + /// /// SearchCameras /// @@ -417,7 +448,7 @@ namespace PLCSharp.VVMs.Vision { if (string.IsNullOrEmpty(item.Name)) { - SendInfoDialog($"保存失败,名称{item.Name}不合适!" ); + SendInfoDialog($"保存失败,名称{item.Name}不合适!"); return; } diff --git a/PLCSharpWpf/VVMs/Workflows/Workflow.cs b/PLCSharpWpf/VVMs/Workflows/Workflow.cs index e1ad41b..03d438f 100644 --- a/PLCSharpWpf/VVMs/Workflows/Workflow.cs +++ b/PLCSharpWpf/VVMs/Workflows/Workflow.cs @@ -284,6 +284,10 @@ namespace PLCSharp.VVMs.Workflows if (_BackgroundWorker != null && _BackgroundWorker.IsBusy) { _BackgroundWorker.CancelAsync(); + } + while (_BackgroundWorker.IsBusy) + { + } IsRuning = false; } @@ -308,6 +312,7 @@ namespace PLCSharp.VVMs.Workflows sw.Stop(); CycleTime = sw.Elapsed.TotalSeconds; } + } GlobalModel GlobalModel; private FlowModel _Flow = new();