快捷代码插入按钮改为从Script文件夹下自动找文件生成

This commit is contained in:
tfarcraw 2026-07-05 00:59:29 +08:00
parent 890d27712a
commit 7cdb879f68
11 changed files with 177 additions and 175 deletions

View File

@ -52,6 +52,9 @@
<None Update="SDK\MotionControl\SMC\LTSMC.lib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="VVMs\Workflows\Script\*.spt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />

View File

@ -0,0 +1,18 @@
namespace PLCSharp.VVMs.Workflows.Script
{
/// <summary>
/// 快捷代码片段项
/// </summary>
public class SnippetItem
{
/// <summary>
/// 按钮显示名称
/// </summary>
public string Name { get; set; } = "";
/// <summary>
/// 要插入的代码内容
/// </summary>
public string Content { get; set; } = "";
}
}

View File

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

View File

@ -0,0 +1,12 @@
case 0:
{
// 开始等待
flow.Step++;
}
break;
case 1:
if (flow.CheckStepTime(2)) // 等待2秒
{
flow.Step++;
}
break;

View File

@ -0,0 +1,7 @@
case 0:
{
var stateControl = globalModel.GetCustomControl("控件名称");
stateControl.Params.CellInfos[0].State = CellState.OK;
flow.Step++;
}
break;

View File

@ -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;

View File

@ -0,0 +1,17 @@
case 0:
CurrConnect = globalModel.GetConnect("网络连接名");
_ = CurrConnect.SendAsync("要发送的消息");
flow.Step++;
break;
case 1:
if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
{
flow.Step++;
}
break;

View File

@ -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;

View File

@ -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;

View File

@ -31,22 +31,19 @@
</Grid.ColumnDefinitions>
<GroupBox Grid.Column="0" Margin="3" Header="快捷输入">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<Button Margin="3" Command="{Binding FastEntry}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Content="网络收发" ToolTip="插入网络收发代码模板(连接→发送→接收)" />
<Separator Margin="3" />
<Button Margin="3" Command="{Binding FastEntry}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Content="显示状态" ToolTip="插入控件状态更新代码模板(设置 CellState)" />
<Separator Margin="3" />
<Button Margin="3" Command="{Binding FastEntry}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Content="视觉功能" ToolTip="插入视觉功能调用模板(获取→重置→运行,含超时检测)" />
<Separator Margin="3" />
<Button Margin="3" Command="{Binding FastEntry}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Content="机器人运动" ToolTip="插入机器人点位运动模板(获取→运行→等待完成)" />
<Separator Margin="3" />
<Button Margin="3" Command="{Binding FastEntry}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Content="运动控制" ToolTip="插入运动控制点位模板(获取点位→运行→等待完成,含超时检测)" />
<Separator Margin="3" />
<Button Margin="3" Command="{Binding FastEntry}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Content="延时等待" ToolTip="插入延时等待模板(使用 CheckStepTime 实现固定时长等待)" />
<Separator Margin="3" />
<Button Margin="3" Command="{Binding FastEntry}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Content="延时等待" />
</StackPanel>
<ItemsControl ItemsSource="{Binding SnippetItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Margin="3" Command="{Binding DataContext.FastEntry, RelativeSource={RelativeSource AncestorType=GroupBox}}"
CommandParameter="{Binding Content}"
Content="{Binding Name}"
ToolTip="点击插入代码片段到编辑器" />
<Separator Margin="3" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</GroupBox>
<GroupBox Grid.Column="1" Margin="3" Header="{Binding SelectedFlowTask.Name}">

View File

@ -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); }
}
/// <summary>
/// 快捷代码片段列表
/// </summary>
public ObservableCollection<SnippetItem> SnippetItems { get; set; } = [];
/// <summary>
/// 打开对话框后要执行的
/// </summary>
@ -70,6 +78,35 @@ namespace PLCSharp.VVMs.Workflows
public override void OnDialogOpened(IDialogParameters parameters)
{
SelectedFlowTask = parameters.GetValue<Workflow>("SelectedTask");
LoadSnippets();
}
/// <summary>
/// 从 Script 目录加载 .spt 文件
/// </summary>
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<string> FastEntry =>
_FastEntry ??= new DelegateCommand<string>(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);
}
/// <summary>
/// CustomTextEditor
/// </summary>
public CustomTextEditor CustomTextEditor { get; set; }
int index = 0;
CustomControl stateControl;
/// <summary>
/// Demo
/// </summary>
/// <param name="globalModel">全局模型</param>
/// <param name="flow">流程状态模型</param>
public void Demo(GlobalModel globalModel, FlowModel flow)
{
switch (flow.Step)
{
}
}
}
}