From 7cdb879f68d294f13104845de589334592b32050 Mon Sep 17 00:00:00 2001
From: tfarcraw <123211521@163.com>
Date: Sun, 5 Jul 2026 00:59:29 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E4=BB=A3=E7=A0=81=E6=8F=92?=
=?UTF-8?q?=E5=85=A5=E6=8C=89=E9=92=AE=E6=94=B9=E4=B8=BA=E4=BB=8EScript?=
=?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E4=B8=8B=E8=87=AA=E5=8A=A8=E6=89=BE?=
=?UTF-8?q?=E6=96=87=E4=BB=B6=E7=94=9F=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
PLCSharpWpf/PLCSharp.csproj | 3 +
.../VVMs/Workflows/Script/SnippetItem.cs | 18 ++
.../VVMs/Workflows/Script/变量赋值.spt | 7 +
.../VVMs/Workflows/Script/延时等待.spt | 12 ++
.../VVMs/Workflows/Script/显示状态.spt | 7 +
.../VVMs/Workflows/Script/机器人运动.spt | 20 ++
.../VVMs/Workflows/Script/网络收发.spt | 17 ++
.../VVMs/Workflows/Script/视觉功能.spt | 19 ++
.../VVMs/Workflows/Script/运动控制.spt | 21 ++
.../VVMs/Workflows/WorkflowConfig.xaml | 29 ++-
.../VVMs/Workflows/WorkflowConfigViewModel.cs | 199 ++++--------------
11 files changed, 177 insertions(+), 175 deletions(-)
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/SnippetItem.cs
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/变量赋值.spt
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/延时等待.spt
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/显示状态.spt
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/机器人运动.spt
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/网络收发.spt
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/视觉功能.spt
create mode 100644 PLCSharpWpf/VVMs/Workflows/Script/运动控制.spt
diff --git a/PLCSharpWpf/PLCSharp.csproj b/PLCSharpWpf/PLCSharp.csproj
index fb4820d..8e1ce88 100644
--- a/PLCSharpWpf/PLCSharp.csproj
+++ b/PLCSharpWpf/PLCSharp.csproj
@@ -52,6 +52,9 @@
PreserveNewest
+
+ PreserveNewest
+
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/SnippetItem.cs b/PLCSharpWpf/VVMs/Workflows/Script/SnippetItem.cs
new file mode 100644
index 0000000..8ddb02d
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/SnippetItem.cs
@@ -0,0 +1,18 @@
+namespace PLCSharp.VVMs.Workflows.Script
+{
+ ///
+ /// 快捷代码片段项
+ ///
+ public class SnippetItem
+ {
+ ///
+ /// 按钮显示名称
+ ///
+ public string Name { get; set; } = "";
+
+ ///
+ /// 要插入的代码内容
+ ///
+ public string Content { get; set; } = "";
+ }
+}
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/变量赋值.spt b/PLCSharpWpf/VVMs/Workflows/Script/变量赋值.spt
new file mode 100644
index 0000000..4a59ac4
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/变量赋值.spt
@@ -0,0 +1,7 @@
+ case 0:
+ {
+ var variable = globalModel.GetVariable("变量名");
+ variable.Value = 100; // 设置变量值
+ flow.Step++;
+ }
+ break;
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/延时等待.spt b/PLCSharpWpf/VVMs/Workflows/Script/延时等待.spt
new file mode 100644
index 0000000..e793617
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/延时等待.spt
@@ -0,0 +1,12 @@
+ case 0:
+ {
+ // 开始等待
+ flow.Step++;
+ }
+ break;
+ case 1:
+ if (flow.CheckStepTime(2)) // 等待2秒
+ {
+ flow.Step++;
+ }
+ break;
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/显示状态.spt b/PLCSharpWpf/VVMs/Workflows/Script/显示状态.spt
new file mode 100644
index 0000000..9e209e4
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/显示状态.spt
@@ -0,0 +1,7 @@
+ case 0:
+ {
+ var stateControl = globalModel.GetCustomControl("控件名称");
+ stateControl.Params.CellInfos[0].State = CellState.OK;
+ flow.Step++;
+ }
+ break;
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/机器人运动.spt b/PLCSharpWpf/VVMs/Workflows/Script/机器人运动.spt
new file mode 100644
index 0000000..e3f1535
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/机器人运动.spt
@@ -0,0 +1,20 @@
+ case 0:
+ {
+ var robot = globalModel.GetRobot("机器人名称");
+ if (robot != null)
+ {
+ var point = robot.Points.FirstOrDefault(p => p.Name == "点位名称");
+ if (point != null)
+ {
+ robot.RunPoint(point);
+ flow.Step++;
+ }
+ }
+ }
+ break;
+ case 1:
+ if (robot.) // 等待运动完成
+ {
+ flow.Step++;
+ }
+ break;
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/网络收发.spt b/PLCSharpWpf/VVMs/Workflows/Script/网络收发.spt
new file mode 100644
index 0000000..58f492f
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/网络收发.spt
@@ -0,0 +1,17 @@
+ case 0:
+
+ CurrConnect = globalModel.GetConnect("网络连接名");
+ _ = CurrConnect.SendAsync("要发送的消息");
+ flow.Step++;
+
+ break;
+ case 1:
+
+
+ if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
+ {
+
+ flow.Step++;
+
+ }
+ break;
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/视觉功能.spt b/PLCSharpWpf/VVMs/Workflows/Script/视觉功能.spt
new file mode 100644
index 0000000..f591a7b
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/视觉功能.spt
@@ -0,0 +1,19 @@
+ case 0:
+
+ CurrVF= globalModel.GetVisionFunction("视觉功能名");
+ CurrVF.Flow.Reset();
+ flow.Step++;
+
+ break;
+ case 1:
+ {
+ if(CurrVF.RunAll(CurrVF.Flow))
+ {
+ flow.Step++;
+
+ }else if (flow.CheckStepTime(3))
+ throw new Exception("视觉功能超时!");
+ }
+ }
+
+ break;
diff --git a/PLCSharpWpf/VVMs/Workflows/Script/运动控制.spt b/PLCSharpWpf/VVMs/Workflows/Script/运动控制.spt
new file mode 100644
index 0000000..492b2e9
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Workflows/Script/运动控制.spt
@@ -0,0 +1,21 @@
+ case 0:
+ {
+ var point = globalModel.ControllersModel.AxisPoints.FirstOrDefault(p => p.Name == "点位名称");
+ if (point != null)
+ {
+ point.Run();
+ flow.Step++;
+ }
+ }
+ break;
+ case 1:
+ {
+ var point = globalModel.ControllersModel.AxisPoints.FirstOrDefault(p => p.Name == "点位名称");
+ if (point != null && point.IsDone())
+ {
+ flow.Step++;
+ }
+ else if (flow.CheckStepTime(10))
+ throw new Exception("点位运动超时!");
+ }
+ break;
diff --git a/PLCSharpWpf/VVMs/Workflows/WorkflowConfig.xaml b/PLCSharpWpf/VVMs/Workflows/WorkflowConfig.xaml
index 5dddaf2..a2005e9 100644
--- a/PLCSharpWpf/VVMs/Workflows/WorkflowConfig.xaml
+++ b/PLCSharpWpf/VVMs/Workflows/WorkflowConfig.xaml
@@ -31,22 +31,19 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
diff --git a/PLCSharpWpf/VVMs/Workflows/WorkflowConfigViewModel.cs b/PLCSharpWpf/VVMs/Workflows/WorkflowConfigViewModel.cs
index 858b09a..1569d88 100644
--- a/PLCSharpWpf/VVMs/Workflows/WorkflowConfigViewModel.cs
+++ b/PLCSharpWpf/VVMs/Workflows/WorkflowConfigViewModel.cs
@@ -7,6 +7,9 @@ using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
+using System.Collections.ObjectModel;
+using System.IO;
+using System.Linq;
namespace PLCSharp.VVMs.Workflows
{
@@ -63,6 +66,11 @@ namespace PLCSharp.VVMs.Workflows
set { SetProperty(ref _SelectedFlowTask, value); }
}
+ ///
+ /// 快捷代码片段列表
+ ///
+ public ObservableCollection SnippetItems { get; set; } = [];
+
///
/// 打开对话框后要执行的
///
@@ -70,6 +78,35 @@ namespace PLCSharp.VVMs.Workflows
public override void OnDialogOpened(IDialogParameters parameters)
{
SelectedFlowTask = parameters.GetValue("SelectedTask");
+ LoadSnippets();
+ }
+
+ ///
+ /// 从 Script 目录加载 .spt 文件
+ ///
+ private void LoadSnippets()
+ {
+ SnippetItems.Clear();
+
+ // 从程序集所在目录逐级向上查找 Script 目录
+ var dir = AppDomain.CurrentDomain.BaseDirectory;
+ string scriptDir;
+ while (true)
+ {
+ scriptDir = Path.Combine(dir, @"VVMs\Workflows\Script");
+ if (Directory.Exists(scriptDir)) break;
+
+ var parent = Directory.GetParent(dir);
+ if (parent == null) return;
+ dir = parent.FullName;
+ }
+
+ foreach (var file in Directory.GetFiles(scriptDir, "*.spt").OrderBy(f => f))
+ {
+ var name = Path.GetFileNameWithoutExtension(file);
+ var content = File.ReadAllText(file);
+ SnippetItems.Add(new SnippetItem { Name = name, Content = content });
+ }
}
private DelegateCommand _Run;
@@ -100,171 +137,15 @@ namespace PLCSharp.VVMs.Workflows
public DelegateCommand FastEntry =>
_FastEntry ??= new DelegateCommand(ExecuteFastEntry);
- void ExecuteFastEntry(string cmd)
+ void ExecuteFastEntry(string content)
{
- switch (cmd)
- {
- case "网络收发":
- CustomTextEditor.Insert(@"
- case 0:
-
- CurrConnect = globalModel.GetConnect(""网络连接名"");
- _ = CurrConnect.SendAsync(""要发送的消息"");
- flow.Step++;
-
- break;
- case 1:
-
-
- if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
- {
-
- flow.Step++;
-
- }
- break;
- ");
- break;
-
- case "视觉功能":
- CustomTextEditor.Insert(@"
- case 0:
-
- CurrVF= globalModel.GetVisionFunction(""视觉功能名"");
- CurrVF.Flow.Reset();
- flow.Step++;
-
- break;
- case 1:
- {
- if(CurrVF.RunAll(CurrVF.Flow))
- {
- flow.Step++;
-
- }else if (flow.CheckStepTime(3))
- throw new Exception(""视觉功能超时!"");
- }
- }
-
- break;
- ");
- break;
- case "显示状态":
- CustomTextEditor.Insert(@"
- case 0:
- {
- var stateControl = globalModel.GetCustomControl(""控件名称"");
- stateControl.Params.CellInfos[0].State = CellState.OK;
- flow.Step++;
- }
- break;
- ");
- break;
-
- case "运动控制":
- CustomTextEditor.Insert(@"
- case 0:
- {
- var point = globalModel.ControllersModel.AxisPoints.FirstOrDefault(p => p.Name == ""点位名称"");
- if (point != null)
- {
- point.Run();
- flow.Step++;
- }
- }
- break;
- case 1:
- {
- var point = globalModel.ControllersModel.AxisPoints.FirstOrDefault(p => p.Name == ""点位名称"");
- if (point != null && point.IsDone())
- {
- flow.Step++;
- }
- else if (flow.CheckStepTime(10))
- throw new Exception(""点位运动超时!"");
- }
- break;
- ");
- break;
- case "变量赋值":
- CustomTextEditor.Insert(@"
- case 0:
- {
- var variable = globalModel.GetVariable(""变量名"");
- variable.Value = 100; // 设置变量值
- flow.Step++;
- }
- break;
- ");
- break;
-
- case "延时等待":
- CustomTextEditor.Insert(@"
- case 0:
- {
- // 开始等待
- flow.Step++;
- }
- break;
- case 1:
- if (flow.CheckStepTime(2)) // 等待2秒
- {
- flow.Step++;
- }
- break;
- ");
- break;
-
- case "机器人运动":
- CustomTextEditor.Insert(@"
- case 0:
- {
- var robot = globalModel.GetRobot(""机器人名称"");
- if (robot != null)
- {
- var point = robot.Points.FirstOrDefault(p => p.Name == ""点位名称"");
- if (point != null)
- {
- robot.RunPoint(point);
- flow.Step++;
- }
- }
- }
- break;
- case 1:
- if (robot.) // 等待运动完成
- {
- flow.Step++;
- }
- break;
- ");
- break;
-
- }
-
+ if (!string.IsNullOrEmpty(content))
+ CustomTextEditor.Insert(content);
}
///
/// CustomTextEditor
///
public CustomTextEditor CustomTextEditor { get; set; }
- int index = 0;
- CustomControl stateControl;
- ///
- /// Demo
- ///
- /// 全局模型
- /// 流程状态模型
- public void Demo(GlobalModel globalModel, FlowModel flow)
- {
- switch (flow.Step)
- {
-
-
- }
-
-
- }
}
-
}