编写流程示例

This commit is contained in:
tfarcraw 2026-07-05 04:26:16 +08:00
parent 7cdb879f68
commit 11a4e6da69
8 changed files with 188 additions and 65 deletions

View File

@ -190,23 +190,21 @@ namespace PLCSharp.Models
{ {
var connect = Connects.Connects.FirstOrDefault(c => c.Name == name) var connect = Connects.Connects.FirstOrDefault(c => c.Name == name)
?? throw new Exception("网络未配置!"); ?? throw new Exception("网络未配置!");
if (connect.Connected == false)
{
throw new Exception("网络未连接!");
}
return connect; return connect;
} }
/// <summary> /// <summary>
/// 获取相机 /// 获取Robot
/// </summary> /// </summary>
/// <param name="name">名称</param> /// <param name="name">名称</param>
/// <returns>返回结果</returns> /// <returns>返回结果</returns>
public CameraBase GetCamera(string name) public Robot GetRobot(string name)
{ {
return VisionsModel.Cameras.FirstOrDefault(c => c.Name == name) var robot = RobotModel.Robots.FirstOrDefault(c => c.Name == name)
?? throw new Exception("相机未配置!"); ?? throw new Exception("Robot未配置!");
return robot;
} }
/// <summary> /// <summary>
/// 获取VisionFunction /// 获取VisionFunction
/// </summary> /// </summary>

View File

@ -87,14 +87,14 @@
<DataTemplate> <DataTemplate>
<!-- 编辑状态下,展示 ComboBox 供用户选择 --> <!-- 编辑状态下,展示 ComboBox 供用户选择 -->
<ComboBox SelectedIndex="{Binding Params.Layout, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <ComboBox SelectedIndex="{Binding Params.Layout, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ComboBoxItem Content=" 左 -&gt; 右 上 -&gt; 下 " Tag = "0"/> <ComboBoxItem Content=" 左 -&gt; 右 上 -&gt; 下 " Tag="0" />
<ComboBoxItem Content=" 左 -&gt; 右 下 -&gt; 上 " Tag = "1"/> <ComboBoxItem Content=" 左 -&gt; 右 下 -&gt; 上 " Tag="1" />
<ComboBoxItem Content=" 右 -&gt; 左 上 -&gt; 下 " Tag = "2"/> <ComboBoxItem Content=" 右 -&gt; 左 上 -&gt; 下 " Tag="2" />
<ComboBoxItem Content=" 右 -&gt; 左 下 -&gt; 上 " Tag = "3"/> <ComboBoxItem Content=" 右 -&gt; 左 下 -&gt; 上 " Tag="3" />
<ComboBoxItem Content=" 上 -&gt; 下 左 -&gt; 右 " Tag = "4"/> <ComboBoxItem Content=" 上 -&gt; 下 左 -&gt; 右 " Tag="4" />
<ComboBoxItem Content=" 上 -&gt; 下 右 -&gt; 左 " Tag = "5"/> <ComboBoxItem Content=" 上 -&gt; 下 右 -&gt; 左 " Tag="5" />
<ComboBoxItem Content=" 下 -&gt; 上 左 -&gt; 右 " Tag = "6"/> <ComboBoxItem Content=" 下 -&gt; 上 左 -&gt; 右 " Tag="6" />
<ComboBoxItem Content=" 下 -&gt; 上 右 -&gt; 左 " Tag = "7"/> <ComboBoxItem Content=" 下 -&gt; 上 右 -&gt; 左 " Tag="7" />
</ComboBox> </ComboBox>
</DataTemplate> </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn.CellEditingTemplate>

View File

@ -1,4 +1,4 @@
using PLCSharp.Core.Tools; using PLCSharp.Core.Tools;
using PLCSharp.VVMs.Connects.Socket; using PLCSharp.VVMs.Connects.Socket;
using System.Threading; using System.Threading;
@ -232,7 +232,7 @@ public class EpsonRobot : Robot
case "u-": case "u-":
case "returnhere": case "returnhere":
case "runpoint": case "runpoint":
PointDone = true;
if (parts.Length >= 7) if (parts.Length >= 7)
{ {
_ = double.TryParse(parts[1], out var x); _ = double.TryParse(parts[1], out var x);
@ -312,8 +312,14 @@ public class EpsonRobot : Robot
/// </summary> /// </summary>
/// <param name="point">点位</param> /// <param name="point">点位</param>
/// <returns>返回布尔值</returns> /// <returns>返回布尔值</returns>
public override bool RunPoint(RobotPoint point) public override bool RunPoint(string pointName)
{ {
PointDone = false;
var point = Points.FirstOrDefault(p => p.Name == pointName);
if (point == null)
{
return false;
}
point.Command = "RunPoint"; point.Command = "RunPoint";
PointCommand = point.DeepCopy(); PointCommand = point.DeepCopy();
return SendCommand(PointCommand); return SendCommand(PointCommand);
@ -327,13 +333,19 @@ public class EpsonRobot : Robot
/// <param name="dist"></param> /// <param name="dist"></param>
/// <param name="rate"></param> /// <param name="rate"></param>
/// <returns></returns> /// <returns></returns>
public override bool Jog(RobotPoint pointBase, string cmd, double dist, double rate = 0) public override bool Jog(string pointName, string cmd, double dist, double rate = 0)
{ {
PointDone = false;
var cmdDone = false; var cmdDone = false;
var point = Points.FirstOrDefault(p => p.Name == pointName);
if (point == null)
{
return false;
}
PointCommand = pointBase.DeepCopy(); PointCommand = CurrPoint.DeepCopy();
if (PointCommand.ToolNum == 0) if (point.ToolNum == 0)
{ {
switch (cmd) switch (cmd)
{ {
@ -341,38 +353,38 @@ public class EpsonRobot : Robot
case "X-": case "X-":
PointCommand.Command = cmd; PointCommand.Command = cmd;
PointCommand.X = dist; PointCommand.X = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate; PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand); cmdDone = SendCommand(PointCommand);
break; break;
case "Y+": case "Y+":
case "Y-": case "Y-":
PointCommand.Command = cmd; PointCommand.Command = cmd;
PointCommand.Y = dist; PointCommand.Y = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate; PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand); cmdDone = SendCommand(PointCommand);
break; break;
case "Z+": case "Z+":
case "Z-": case "Z-":
PointCommand.Command = cmd; PointCommand.Command = cmd;
PointCommand.Z = dist; PointCommand.Z = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate; PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand); cmdDone = SendCommand(PointCommand);
break; break;
case "U+": case "U+":
case "U-": case "U-":
PointCommand.Command = cmd; PointCommand.Command = cmd;
PointCommand.U = dist; PointCommand.U = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate; PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand); cmdDone = SendCommand(PointCommand);
break; break;
} }
} }
else else
{ {
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate; PointCommand.Rate = rate > 0 ? rate : point.Rate;
PointCommand.Command = "RunPoint"; PointCommand.Command = "RunPoint";
var tool = Tools.FirstOrDefault(w => w.Key == PointCommand.ToolNum); var tool = Tools.FirstOrDefault(w => w.Key == point.ToolNum);
if (tool.Value == null) return false; if (tool.Value == null) return false;
var pointTool = ToolFrame4Axis.WorldToTool(PointCommand, tool.Value); var pointTool = ToolFrame4Axis.WorldToTool(PointCommand, tool.Value);

View File

@ -1,4 +1,4 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using PLCSharp.Core.Prism; using PLCSharp.Core.Prism;
using Prism.Commands; using Prism.Commands;
using Prism.Mvvm; using Prism.Mvvm;
@ -415,7 +415,7 @@ namespace PLCSharp.VVMs.Robots
/// </summary> /// </summary>
/// <param name="robotPoint">机器人点位</param> /// <param name="robotPoint">机器人点位</param>
/// <returns>返回布尔值</returns> /// <returns>返回布尔值</returns>
public virtual bool RunPoint(RobotPoint robotPoint) public virtual bool RunPoint(string pointName)
{ {
return false; return false;
@ -428,13 +428,20 @@ namespace PLCSharp.VVMs.Robots
/// <param name="dist">dist</param> /// <param name="dist">dist</param>
/// <param name="rate">rate</param> /// <param name="rate">rate</param>
/// <returns>返回布尔值</returns> /// <returns>返回布尔值</returns>
public virtual bool Jog(RobotPoint point, string cmd, double dist, double rate = 0) public virtual bool Jog(string pointName, string cmd, double dist, double rate = 0)
{ {
return false; return false;
} }
/// <summary>
/// 点位运行完成
/// </summary>
[NotMapped]
public bool PointDone { get; set; }
private DelegateCommand _HomeCmd; private DelegateCommand _HomeCmd;
/// <summary> /// <summary>
/// 回零Cmd /// 回零Cmd

View File

@ -309,7 +309,7 @@ namespace PLCSharp.VVMs.Robots
{ {
case "RunPoint": case "RunPoint":
SelectedRobot.RunPoint(SelectedRobot.SelectedPoint); SelectedRobot.RunPoint(SelectedRobot.SelectedPoint.Name);
break; break;
@ -324,8 +324,8 @@ namespace PLCSharp.VVMs.Robots
case "U+": case "U+":
case "U-": case "U-":
SelectedRobot.CurrPoint.ToolNum = SelectedRobot.SelectedPoint.ToolNum;
SelectedRobot.Jog(SelectedRobot.CurrPoint, cmdStr, RelativeDistance); SelectedRobot.Jog(SelectedRobot.SelectedPoint.Name, cmdStr, RelativeDistance);
break; break;
} }

View File

@ -1,6 +1,7 @@
using PLCSharp.Core.Common; using PLCSharp.Core.Common;
using PLCSharp.Models; using PLCSharp.Models;
using PLCSharp.VVMs.Connects; using PLCSharp.VVMs.Connects;
using PLCSharp.VVMs.GlobalVariables;
using PLCSharp.VVMs.Homepage; using PLCSharp.VVMs.Homepage;
using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.MotionController;
using PLCSharp.VVMs.Robots; using PLCSharp.VVMs.Robots;
@ -8,38 +9,138 @@ using PLCSharp.VVMs.Vision;
namespace PLCSharp.VVMs.Workflows namespace PLCSharp.VVMs.Workflows
{ {
/// <summary>
/// 脚本代码示例
/// </summary>
public class Extension public class Extension
{ {
Robot CurrRobot; private Robot CurrRobot;
CustomControl CurrtateControl; private CustomControl CurrtateControl;
VisionFunction CurrVF; private VisionFunction CurrVF;
Connect CurrConnect; private Connect CurrConnect;
AxisPoint CurrAxisPoint; private AxisPoint CurrAxisPoint;
private Variable CurrVariable;
private CustomControl CurrControl;
public void Run(GlobalModel globalModel, FlowModel flow) public void Run(GlobalModel globalModel, FlowModel flow)
{ {
switch (flow.Step) if (globalModel.ModeState.State.Execute)
{ {
//视觉功能 switch (flow.Step)
case 0: {
CurrVF = globalModel.GetVisionFunction("视觉功能名"); //视觉功能
CurrVF.Flow.Reset(); case 0:
flow.Step++; CurrVF = globalModel.GetVisionFunction("视觉功能名");
break; if (CurrVF != null)
case 1: {
CurrVF.Flow.Reset();
flow.Step++;
}
else
{
if (CurrVF.RunAll(CurrVF.Flow)) throw new Exception("视觉功能名称错误!");
{ }
flow.Step++; break;
}
else if (flow.CheckStepTime(3))
throw new Exception("视觉功能超时!");
break; case 1:
if (CurrVF.RunAll(CurrVF.Flow))
{
flow.Step++;
}
else if (flow.CheckStepTime(3))
throw new Exception("视觉功能超时!");
break;
case 2:
CurrVariable = globalModel.GetVariable("变量名");
if (CurrVariable != null)
{
CurrVariable.Value = 100; // 设置变量值
flow.Step++;
}
else
{
throw new Exception("机器人名称错误!");
}
break;
case 3:
CurrRobot = globalModel.GetRobot("机器人名称");
if (CurrRobot != null)
{
flow.Step++;
}
else
{
throw new Exception("机器人名称错误!");
}
break;
case 4:
if (CurrRobot.RunPoint("点位名称"))
{
flow.Step++;
}
else
{
throw new Exception("机器人点位运行失败!");
}
break;
case 5:
if (CurrRobot.PointDone) // 等待运动完成
{
flow.Step++;
}
else if (flow.CheckStepTime(3))
throw new Exception("机器人点位运动超时!");
break;
case 6:
if (CurrRobot.Jog("点位名称", "X+", 10))
{
flow.Step++;
}
else
{
throw new Exception("机器人点位运行失败!");
}
break;
case 7:
if (CurrRobot.PointDone) // 等待运动完成
{
flow.Step++;
}
else if (flow.CheckStepTime(3))
throw new Exception("机器人点位运动超时!");
break;
case 8:
//显示状态
CurrControl = globalModel.GetCustomControl("控件名称");
if (CurrControl != null)
{
CurrControl.Params.CellInfos[0].State = CellState.完成;
flow.Step++;
}
else {
throw new Exception("控件名称错误!");
}
break;
}
} }
} }
} }
} }

View File

@ -1,7 +1,7 @@
case 0:  case 0:
{ {
var variable = globalModel.GetVariable("变量名"); CurrVariable = globalModel.GetVariable("变量名");
variable.Value = 100; // 设置变量值 CurrVariable.Value = 100; // 设置变量值
flow.Step++; flow.Step++;
} }
break; break;

View File

@ -35,7 +35,8 @@
<ItemsControl.ItemTemplate> <ItemsControl.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel> <StackPanel>
<Button Margin="3" Command="{Binding DataContext.FastEntry, RelativeSource={RelativeSource AncestorType=GroupBox}}" <Button Margin="3"
Command="{Binding DataContext.FastEntry, RelativeSource={RelativeSource AncestorType=GroupBox}}"
CommandParameter="{Binding Content}" CommandParameter="{Binding Content}"
Content="{Binding Name}" Content="{Binding Name}"
ToolTip="点击插入代码片段到编辑器" /> ToolTip="点击插入代码片段到编辑器" />
@ -77,7 +78,7 @@
<TextBox Text="{Binding SelectedFlowTask.DebugLog}" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" /> <TextBox Text="{Binding SelectedFlowTask.DebugLog}" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />
</GroupBox> </GroupBox>
<!-- 状态栏 --> <!-- 状态栏 -->
<StatusBar Grid.Row="1" Grid.ColumnSpan="4" Background="LightGray"> <StatusBar Grid.Row="1" Grid.ColumnSpan="4" Background="LightGray">
<StatusBar.ItemsPanel> <StatusBar.ItemsPanel>
<ItemsPanelTemplate> <ItemsPanelTemplate>
@ -93,7 +94,7 @@
<StatusBarItem Grid.Column="0"> <StatusBarItem Grid.Column="0">
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<TextBlock x:Name="tbStatus" Text="行: 1 列: 1" FontSize="12" FontFamily="Consolas" /> <TextBlock x:Name="tbStatus" Text="行: 1 列: 1" FontSize="12" FontFamily="Consolas" />
<TextBlock Text=" | " FontSize="12" Margin="4,0" /> <TextBlock Margin="4,0" Text=" | " FontSize="12" />
<TextBlock x:Name="tbFontSize" Text="字号: 20" FontSize="12" FontFamily="Consolas" /> <TextBlock x:Name="tbFontSize" Text="字号: 20" FontSize="12" FontFamily="Consolas" />
</StackPanel> </StackPanel>
</StatusBarItem> </StatusBarItem>
@ -103,8 +104,12 @@
</StatusBar> </StatusBar>
<StackPanel Grid.Row="2" Grid.ColumnSpan="4" Orientation="Horizontal"> <StackPanel Grid.Row="2" Grid.ColumnSpan="4" Orientation="Horizontal">
<Button Margin="5" Padding="10" Command="{Binding Compile}" Content=" 编译 " ToolTip="编译代码(Ctrl+S 格式化 + 自动保存后编译)" /> <Button Margin="5" Padding="10"
<Button Width="50" Margin="10,5" Command="{Binding Run}" Content="运行" ToolTip="手动执行一次(自动运行模式禁用)" /> Command="{Binding Compile}"
Content=" 编译 " ToolTip="编译代码(Ctrl+S 格式化 + 自动保存后编译)" />
<Button Width="50" Margin="10,5"
Command="{Binding Run}"
Content="运行" ToolTip="手动执行一次(自动运行模式禁用)" />
<TextBlock Margin="5" Padding="10" <TextBlock Margin="5" Padding="10"
Text=" 已编译 " Text=" 已编译 "
HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"