编写流程示例

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)
?? throw new Exception("网络未配置!");
if (connect.Connected == false)
{
throw new Exception("网络未连接!");
}
return connect;
}
/// <summary>
/// 获取相机
/// 获取Robot
/// </summary>
/// <param name="name">名称</param>
/// <returns>返回结果</returns>
public CameraBase GetCamera(string name)
public Robot GetRobot(string name)
{
return VisionsModel.Cameras.FirstOrDefault(c => c.Name == name)
?? throw new Exception("相机未配置!");
var robot = RobotModel.Robots.FirstOrDefault(c => c.Name == name)
?? throw new Exception("Robot未配置!");
return robot;
}
/// <summary>
/// 获取VisionFunction
/// </summary>

View File

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

View File

@ -1,4 +1,4 @@
using PLCSharp.Core.Tools;
using PLCSharp.Core.Tools;
using PLCSharp.VVMs.Connects.Socket;
using System.Threading;
@ -232,7 +232,7 @@ public class EpsonRobot : Robot
case "u-":
case "returnhere":
case "runpoint":
PointDone = true;
if (parts.Length >= 7)
{
_ = double.TryParse(parts[1], out var x);
@ -312,8 +312,14 @@ public class EpsonRobot : Robot
/// </summary>
/// <param name="point">点位</param>
/// <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";
PointCommand = point.DeepCopy();
return SendCommand(PointCommand);
@ -327,13 +333,19 @@ public class EpsonRobot : Robot
/// <param name="dist"></param>
/// <param name="rate"></param>
/// <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 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)
{
@ -341,38 +353,38 @@ public class EpsonRobot : Robot
case "X-":
PointCommand.Command = cmd;
PointCommand.X = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
break;
case "Y+":
case "Y-":
PointCommand.Command = cmd;
PointCommand.Y = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
break;
case "Z+":
case "Z-":
PointCommand.Command = cmd;
PointCommand.Z = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
break;
case "U+":
case "U-":
PointCommand.Command = cmd;
PointCommand.U = dist;
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
break;
}
}
else
{
PointCommand.Rate = rate > 0 ? rate : PointCommand.Rate;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
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;
var pointTool = ToolFrame4Axis.WorldToTool(PointCommand, tool.Value);

View File

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

View File

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

View File

@ -1,6 +1,7 @@
using PLCSharp.Core.Common;
using PLCSharp.Models;
using PLCSharp.VVMs.Connects;
using PLCSharp.VVMs.GlobalVariables;
using PLCSharp.VVMs.Homepage;
using PLCSharp.VVMs.MotionController;
using PLCSharp.VVMs.Robots;
@ -8,38 +9,138 @@ using PLCSharp.VVMs.Vision;
namespace PLCSharp.VVMs.Workflows
{
/// <summary>
/// 脚本代码示例
/// </summary>
public class Extension
{
Robot CurrRobot;
CustomControl CurrtateControl;
VisionFunction CurrVF;
Connect CurrConnect;
AxisPoint CurrAxisPoint;
private Robot CurrRobot;
private CustomControl CurrtateControl;
private VisionFunction CurrVF;
private Connect CurrConnect;
private AxisPoint CurrAxisPoint;
private Variable CurrVariable;
private CustomControl CurrControl;
public void Run(GlobalModel globalModel, FlowModel flow)
{
switch (flow.Step)
if (globalModel.ModeState.State.Execute)
{
//视觉功能
case 0:
CurrVF = globalModel.GetVisionFunction("视觉功能名");
CurrVF.Flow.Reset();
flow.Step++;
break;
case 1:
switch (flow.Step)
{
//视觉功能
case 0:
CurrVF = globalModel.GetVisionFunction("视觉功能名");
if (CurrVF != null)
{
CurrVF.Flow.Reset();
flow.Step++;
}
else
{
if (CurrVF.RunAll(CurrVF.Flow))
{
flow.Step++;
}
else if (flow.CheckStepTime(3))
throw new Exception("视觉功能超时!");
throw new Exception("视觉功能名称错误!");
}
break;
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("变量名");
variable.Value = 100; // 设置变量值
CurrVariable = globalModel.GetVariable("变量名");
CurrVariable.Value = 100; // 设置变量值
flow.Step++;
}
break;

View File

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