using PaintDotNet.Base;
using PaintDotNet.Base.Functionodel;
using PaintDotNet.Data.Param;
using PaintDotNet.DbOpreate.DbBll;
using PaintDotNet.DbOpreate.DbModel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace PaintDotNet.Instrument
{
///
/// 脚本管理
///
internal class ScriptManageDialog : FloatingToolForm// PdnBaseForm
{
#region 控件
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(94, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(50, 23);
this.button1.TabIndex = 1;
this.button1.Text = "新建";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(151, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(50, 23);
this.button2.TabIndex = 2;
this.button2.Text = "修改";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(208, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(50, 23);
this.button3.TabIndex = 3;
this.button3.Text = "删除";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(265, 12);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(50, 23);
this.button4.TabIndex = 4;
this.button4.Text = "刷新";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(12, 12);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 5;
this.button5.Text = "执行";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(12, 42);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(303, 400);
this.listBox1.TabIndex = 6;
//
// ScriptManageDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(323, 450);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "ScriptManageDialog";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "脚本管理";
this.TopMost = false;
this.Activated += new System.EventHandler(this.ScriptManageDialog_Activated_1);
this.Controls.SetChildIndex(this.button1, 0);
this.Controls.SetChildIndex(this.button2, 0);
this.Controls.SetChildIndex(this.button3, 0);
this.Controls.SetChildIndex(this.button4, 0);
this.Controls.SetChildIndex(this.button5, 0);
this.Controls.SetChildIndex(this.listBox1, 0);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.ListBox listBox1;
#endregion
private AppWorkspace appWorkspace;
private ToolStripItemCollection collection;
private ScriptEditDialog scriptEditDialog;
//private ScriptStepDialog scriptStepDialog;
private List list;
///
/// 获取当前选中的脚本步骤
///
List steps = new List();
///
/// 获取当前选中的脚本步骤参数
///
private List paramss = new List();
///
/// 获取当前执行的脚本id和脚本参数
///
private ParamObject currentParam = null;
///
/// 当前正在执行哪个脚本
///
private int currentRunningIndex = 0;
///
/// 当前执行到哪个脚本
///
private int currentIndex = 0;
///
/// 手动退出脚本执行
///
private bool alertShutDownAction = false;
public ScriptManageDialog(AppWorkspace appWorkspace)
{
this.appWorkspace = appWorkspace;
this.collection = this.appWorkspace.ToolBar.MainMenu.Items;
this.ShowInTaskbar = false;
InitializeComponent();
this.button1.Text = PdnResources.GetString("Menu.New.text");
this.button2.Text = PdnResources.GetString("Menu.modify.Text");
this.button3.Text = PdnResources.GetString("Menu.Edit.Delete.Text");
this.button4.Text = PdnResources.GetString("Menu.Refresh.text");
this.button5.Text = PdnResources.GetString("Menu.tool.Scriptmanagement.execute.text");
this.Text = PdnResources.GetString("Menu.Tools.Script.Text");
this.Text = PdnResources.GetString("Menu.Tools.Script.Text");
}
///
/// 新建
///
///
///
private void button1_Click(object sender, EventArgs e)
{
this.appWorkspace.ScriptRecordingModel = null;
this.appWorkspace.editScriptRecording = false;
if (scriptEditDialog == null)
{
CreateScriptEditDialog();
}
else
{
if (scriptEditDialog.IsDisposed)
{
CreateScriptEditDialog();
}
else
{
scriptEditDialog.WindowState = FormWindowState.Normal;
}
}
}
private void CreateScriptEditDialog()
{
scriptEditDialog = new ScriptEditDialog(appWorkspace, false, null);
scriptEditDialog.StartPosition = FormStartPosition.CenterScreen;
scriptEditDialog.ShowDialog();
}
///
/// 设置当前执行到脚本功能的名称
///
///
private void setScriptText(string stepName)
{
if (stepName.StartsWith("-"))
stepName = stepName.TrimStart(new char[] { '-' });
appWorkspace.Widgets.RunningDialog.setScriptText(stepName, (this.currentRunningIndex == steps.Count - 1));
//if (scriptStepDialog == null || scriptStepDialog.IsDisposed)
// return;
//scriptStepDialog.setScriptText(stepName, (currentRunningIndex == steps.Count - 1));
}
///
/// 初始化脚本列表
///
private void InitScriptList()
{
this.listBox1.Items.Clear();
list = mic_script_BLL.FindAll();
foreach (var item in list)
{
this.listBox1.Items.Add(item.name);
}
}
private void ScriptManageDialog_Activated_1(object sender, EventArgs e)
{
InitScriptList();
}
///
/// 修改
///
///
///
private void button2_Click(object sender, EventArgs e)
{
this.appWorkspace.ScriptRecordingModel = null;
this.appWorkspace.editScriptRecording = false;
if (this.listBox1.SelectedItem != null)
{
scriptEditDialog = new ScriptEditDialog(appWorkspace, true, list[this.listBox1.SelectedIndex]);
scriptEditDialog.StartPosition = FormStartPosition.CenterScreen;
scriptEditDialog.ShowDialog();
}
}
///
/// 刷新
///
///
///
private void button4_Click(object sender, EventArgs e)
{
InitScriptList();
}
///
/// 删除脚本
///
///
///
private void button3_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedItem != null)
{
var toremoveItem = this.listBox1.SelectedItem;
int selectedItem = this.listBox1.SelectedIndex;
DialogResult dr = MessageBox.Show(PdnResources.GetString("Menu.reyousureyouwanteletethescript.Text")+"?", PdnResources.GetString("Menu.hint.text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question);//0930###19008
if (dr != DialogResult.OK)
return;
mic_script_BLL.Del(this.list[selectedItem].id);
this.listBox1.Items.Remove(toremoveItem);
}
InitScriptList();
}
///
/// 当前正在执行哪个脚本
///
public ParamObject ScriptCurrentParam
{
set
{
currentParam = value;
}
get
{
return currentParam;
}
}
///
/// 退出脚本执行
///
public void ShutDownScriptRunning()
{
this.alertShutDownAction = true;
this.currentIndex = steps.Count;
appWorkspace.ActiveDocumentWorkspace.ActiveTool= Annotation.Enum.DrawToolType.Pointer;
appWorkspace.ScriptStopping = false;
ResumeScriptRunning();
}
///
/// 用于标注测量后继续执行
///
public void ResumeScriptRunning()
{
if (!appWorkspace.ScriptRunning)
return;
if (appWorkspace.ScriptStopping)
return;
//循环脚本步骤
for (int index = this.currentIndex; index < steps.Count; index++)
{
mic_script_step step = steps[index];
if (!appWorkspace.ScriptRunning)
break;
this.currentRunningIndex = index;
//递归的方式
this.RecursiveData(this.collection, step, "");
if (appWorkspace.ScriptStopping)
{
this.currentIndex = index + 1;
break;
}
}
if (!appWorkspace.ScriptStopping)
appWorkspace.ScriptRunning = false;
refreshScriptRunningStatus();
}
///
/// 执行脚本
///
///
///
private void button5_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedItem != null)
{
//appWorkspace.Widgets.RunningDialog.Show();
appWorkspace.Widgets.RunningDialog.Visible = true;
////appWorkspace.Widgets.RunningDialog.setScriptText(parentName + "-" + /*list[this.listBox1.SelectedIndex]*/((PdnMenuItem)collection[i]).Text);
//scriptStepDialog = new ScriptStepDialog(appWorkspace, true, list[this.listBox1.SelectedIndex]);
//scriptStepDialog.StartPosition = FormStartPosition.CenterParent/*.CenterScreen*/;
//scriptStepDialog.Show();
}
if (this.listBox1.SelectedItem!=null)
{
this.currentIndex = 0;
steps = mic_script_step_BLL.FindAllByScripId(list[this.listBox1.SelectedIndex].id);
paramss = mic_script_step_param_BLL.FindAllByScriptId(list[this.listBox1.SelectedIndex].id);
//this.WindowState = FormWindowState.Minimized;
this.Enabled = false;
appWorkspace.ScriptRunning = false;
appWorkspace.ScriptStopping = false;
appWorkspace.ScriptRunning = true;
//appWorkspace.toolsPanel.RefreshTools();
//appWorkspace.ToolBar.ResetCustomizeTools();
//循环脚本步骤
for (int index = this.currentIndex; index < steps.Count; index++)
{
mic_script_step step = steps[index];
if (!appWorkspace.ScriptRunning)
break;
this.currentRunningIndex = index;
//递归的方式
this.RecursiveData(this.collection, step, "");
if (appWorkspace.ScriptStopping)
{
this.currentIndex = index + 1;
break;
}
//下面是根据name查找的方式,效率应该更高,不过需要改改数据结构
/**
ToolStripItem[] items = this.collection.Find(step.step_name, true);
if (items != null && items.Length>0)
{
foreach(ToolStripItem item in items)
{
if (!item.Name.Equals("OpenRecent"))
{
if (item.GetType() != typeof(ToolStripSeparator))
{
if (step.menu_id == ((PdnMenuItem)item).MenuId)
{
((PdnMenuItem)item).PerformClick();
}
}
}
}
}
**/
}
if (!appWorkspace.ScriptStopping)
appWorkspace.ScriptRunning = false;
refreshScriptRunningStatus();
}
}
///
/// 脚本执行完成给出提示并刷新显示状态
///
private void refreshScriptRunningStatus()
{
if (!appWorkspace.ScriptRunning && !appWorkspace.ScriptStopping)
{
if (this.alertShutDownAction)
this.alertShutDownAction = false;
else
MessageBox.Show(PdnResources.GetString("Menu.criptexecutioncomplet.Text"));
appWorkspace.Widgets.RunningDialog.Visible = false;
//if (scriptStepDialog != null/* && !scriptStepDialog.IsDisposed*/)
// scriptStepDialog.Close();
//this.WindowState = FormWindowState.Normal;
this.Enabled = true;
//appWorkspace.toolsPanel.RefreshTools();
//appWorkspace.ToolBar.ResetCustomizeTools();
}
}
///
/// 递归查找菜单,暂时不用,改成直接用name查找
///
///
///
private void RecursiveData(ToolStripItemCollection collection, mic_script_step step, string parentName)
{
for (int i = 0; i < collection.Count; i++)
{
if (!collection[i].Name.Equals("OpenRecent") && !collection[i].Name.Equals("CameraSelection"))
{
if (collection[i] is PdnMenuItem)
{
if (step.menu_id == ((PdnMenuItem)collection[i]).MenuId)
{
if (step.automatic == 1 && !((PdnMenuItem)collection[i]).NeedOpenDialog)
{
//在这里反射出对应功能的参数类
string className = InvariantData.path_Action + ".Action" + step.menu_id;
ParamObject param = (ParamObject)Assembly.Load(InvariantData.assembly_Data).CreateInstance(className);
if (param != null && !(new List() { "" }.Contains(param.OpenImage)) && param.Lists != null)
{
if (appWorkspace.ActiveDocumentWorkspace == null)
{
MessageBox.Show(((PdnMenuItem)collection[i]).Text + ":请打开图片");
appWorkspace.ScriptRunning = false;
break;
}
setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
if (((PdnMenuItem)collection[i]).CanUseInSenseShield)
{
//if (((PdnMenuItem)collection[i]).NeedOpenDialog)
//{
// setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
// ((PdnMenuItem)collection[i]).PerformClick();
// if (this.currentRunningIndex < steps.Count - 1 && !((PdnMenuItem)collection[i]).AutoNextScript
// && ((PdnMenuItem)collection[i]).NeedWaitKey && this.appWorkspace.GetScriptRunning())
// this.appWorkspace.SetScriptStopping(true);
//}
//else
//{
DocumentWorkspace[] scriptWorkspaces = appWorkspace.ScriptWorkspaces;
if (scriptWorkspaces == null)
autoStep(param, step.id, parentName + "-" + ((PdnMenuItem)collection[i]).Text, null);
else//循环处理多张打开的图片
{
foreach (DocumentWorkspace item in scriptWorkspaces)
{
autoStep(param, step.id, parentName + "-" + ((PdnMenuItem)collection[i]).Text, item);
item.OpenInScriptRunning = false;
}
}
}
}
else
{
this.currentParam = null;
setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
((PdnMenuItem)collection[i]).PerformClick();
if (this.currentRunningIndex < steps.Count - 1 && !((PdnMenuItem)collection[i]).AutoNextScript
&& ((PdnMenuItem)collection[i]).NeedWaitKey && this.appWorkspace.GetScriptRunning()
/* 、、待调试已确认NeedOpenDialog是否多余 && !((PdnMenuItem)collection[i]).NeedOpenDialog*/)
this.appWorkspace.SetScriptStopping(true);
//else if (((PdnMenuItem)collection[i]).NeedOpenDialog)
//{//...待调试已确认NeedOpenDialog是否多余
// this.appWorkspace.SetScriptStopping(true);
//}
}
}
else
{
//在这里反射出对应功能的参数类
string className = InvariantData.path_Action + ".Action" + step.menu_id;
this.currentParam = (ParamObject)Assembly.Load(InvariantData.assembly_Data).CreateInstance(className);
if (this.currentParam != null && this.currentParam.Lists != null)
{
List list = new List();
foreach (Args arg in this.currentParam.Lists)
{
mic_script_step_param param1 = paramss.Find(m => m.param_key.Equals(arg.Key) && m.step_id == step.id);
if (param1 != null)
{
arg.Value = param1.value != null ? param1.value : param1.param_value;
if (arg.choiseList != null && arg.choiseList.Count > 0)
{
try
{
int index = Convert.ToInt32(arg.value);
var thisArg = arg.choiseList[index];
foreach (var item in thisArg.lists)
{
mic_script_step_param thisParam = paramss.Find(m => m.param_key.Equals(item.Key) && m.step_id == step.id);
if (thisParam != null)
{
item.value = thisParam.value != null ? thisParam.value : thisParam.param_value;
list.Add(item);
}
}
}
catch (Exception)
{
continue;
}
}
}
}
this.currentParam.lists.AddRange(list);
}
setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
this.appWorkspace.ScriptAutomatic = (step.automatic == 1);
((PdnMenuItem)collection[i]).PerformClick();
if (this.currentRunningIndex < steps.Count - 1 && !((PdnMenuItem)collection[i]).AutoNextScript
&& ((PdnMenuItem)collection[i]).NeedWaitKey && this.appWorkspace.GetScriptRunning())
this.appWorkspace.SetScriptStopping(true);
}
}
if (appWorkspace.ScriptStopping)
{
//waitKey();
}
else
RecursiveData(((PdnMenuItem)collection[i]).DropDownItems, step, parentName + "-" + ((PdnMenuItem)collection[i]).Text);
}
}
}
}
///
/// 脚本自动执行的方法
///
///
///
///
private void autoStep(ParamObject param, int stepId, string stepName, DocumentWorkspace oneWorkSpace)
{
if (stepName.StartsWith("-"))
stepName = stepName.TrimStart(new char[] { '-' });
bool createNewImage = true;
foreach (Args arg in param.Lists)
{
mic_script_step_param param1 = paramss.Find(m => m.param_key.Equals(arg.Key) && m.step_id == stepId);
if (param1 != null)
arg.Value = param1.value != null ? param1.value : param1.param_value;
switch (arg.Key)
{
case "createNewImg":
createNewImage = (int)arg.Value == 0 ? true : false;
break;
}
}
OpenCvSharp.Mat imageMat;
if (oneWorkSpace == null)
imageMat = appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat();
else
imageMat = oneWorkSpace.CompositionSurface.CreatedAliasedMat();
if (param.OpenImage.Equals("current"))
{
OpenCvSharp.Mat mat = param.PerformProcess(imageMat.Clone());
DocumentWorkspace dw;
if (!createNewImage)
{
dw = appWorkspace.ActiveDocumentWorkspace;
}
else
{
dw = appWorkspace.AddNewDocumentWorkspace();
Document document = Document.FromImageMat(mat);
dw.Document = document;
dw.fileText = stepName + "-" + Startup.instance.step_length;
dw.OpenInScriptRunning = (oneWorkSpace != null);
dw.rules = appWorkspace.ActiveDocumentWorkspace.rules;
dw.xmlSaveModel = appWorkspace.ActiveDocumentWorkspace.xmlSaveModel;
this.appWorkspace.ActiveDocumentWorkspace = dw;
Startup.instance.step_length += 1;
}
}
else if (param.OpenImage.Equals("addPhase"))
{
OpenCvSharp.Mat mat = param.PerformProcess(imageMat.Clone());
List list = new List();
PhaseModel model = new PhaseModel();
model.name = PdnResources.GetString("Menu.BinaryAction.BinaryExtraction.Text");
model.choise = true;
model.mat = mat;
foreach (Args args in param.lists)
{
if (args.Key.Equals("phaseColor"))
{
model.color = (int)args.Value;
break;
}
}
list.Add(model);
DocumentWorkspace dw;
if (!createNewImage)
{
dw = appWorkspace.ActiveDocumentWorkspace;
}
else {
dw = appWorkspace.AddNewDocumentWorkspace();
Document document = Document.FromImageMat(imageMat.Clone());
dw.Document = document;
dw.fileText = stepName + "-" + Startup.instance.step_length;
dw.OpenInScriptRunning = (oneWorkSpace != null);
dw.rules = appWorkspace.ActiveDocumentWorkspace.rules;
dw.xmlSaveModel = appWorkspace.ActiveDocumentWorkspace.xmlSaveModel;
this.appWorkspace.ActiveDocumentWorkspace = dw;
Startup.instance.step_length += 1;
}
dw.PhaseModels = list;
this.appWorkspace.ActiveDocumentWorkspace.Refresh();
}
else if (param.OpenImage.Equals("currentPhase"))
{
//如果有选中的相
List phases;
if (oneWorkSpace == null)
phases = appWorkspace.ActiveDocumentWorkspace.PhaseModels.FindAll(a => a.choise == true && a.mat != null);
else
phases = oneWorkSpace.PhaseModels.FindAll(a => a.choise == true && a.mat != null);
if (phases.Count > 0)
{
//一些方法用到了微米换算
double ruleValue = 0.0;
Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out ruleValue);//获取
List phasesCopy;
if (oneWorkSpace == null)
phasesCopy = appWorkspace.ActiveDocumentWorkspace.PhaseModelsForCopy;
else
phasesCopy = oneWorkSpace.PhaseModelsForCopy;
for (int i = 0; i < phases.Count; i++)
{
if (phases[i].mat == null)
continue;
PhaseModel model = new PhaseModel();
model.choise = phases[i].choise;
model.color = phases[i].color;
model.name = phases[i].name;
model.position = phases[i].position;
model.mat = new OpenCvSharp.Mat();
phases[i].mat.CopyTo(model.mat);
phasesCopy[i].mat = param.PerformProcess(model, ruleValue);
}
DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();
Document document = Document.FromImageMat(imageMat.Clone());
dw.Document = document;
dw.fileText = stepName + "-" + Startup.instance.step_length;
dw.xmlSaveModel = appWorkspace.ActiveDocumentWorkspace.xmlSaveModel;
dw.PhaseModels = phasesCopy/*list*/;
dw.OpenInScriptRunning = (oneWorkSpace != null);
this.appWorkspace.ActiveDocumentWorkspace = dw;
Startup.instance.step_length += 1;
this.appWorkspace.ActiveDocumentWorkspace.Refresh();
}
else
{
OpenCvSharp.Mat mat = param.PerformProcess(imageMat.Clone());
if (mat != null)//###########
{
DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();
Document document = Document.FromImageMat(mat);
dw.Document = document;
dw.fileText = stepName + "-" + Startup.instance.step_length;
dw.OpenInScriptRunning = (oneWorkSpace != null);
appWorkspace.ActiveDocumentWorkspace = dw;
Startup.instance.step_length += 1;
}
}
}
}
}
}