ScriptManageDialog.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. using PaintDotNet.Base;
  2. using PaintDotNet.Base.Functionodel;
  3. using PaintDotNet.Data.Param;
  4. using PaintDotNet.DbOpreate.DbBll;
  5. using PaintDotNet.DbOpreate.DbModel;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Reflection;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.Instrument
  12. {
  13. /// <summary>
  14. /// 脚本管理
  15. /// </summary>
  16. internal class ScriptManageDialog : FloatingToolForm// PdnBaseForm
  17. {
  18. #region 控件
  19. /// <summary>
  20. /// Required designer variable.
  21. /// </summary>
  22. private System.ComponentModel.IContainer components = null;
  23. /// <summary>
  24. /// Clean up any resources being used.
  25. /// </summary>
  26. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  27. protected override void Dispose(bool disposing)
  28. {
  29. if (disposing && (components != null))
  30. {
  31. components.Dispose();
  32. }
  33. base.Dispose(disposing);
  34. }
  35. #region Windows Form Designer generated code
  36. /// <summary>
  37. /// Required method for Designer support - do not modify
  38. /// the contents of this method with the code editor.
  39. /// </summary>
  40. private void InitializeComponent()
  41. {
  42. this.button1 = new System.Windows.Forms.Button();
  43. this.button2 = new System.Windows.Forms.Button();
  44. this.button3 = new System.Windows.Forms.Button();
  45. this.button4 = new System.Windows.Forms.Button();
  46. this.button5 = new System.Windows.Forms.Button();
  47. this.listBox1 = new System.Windows.Forms.ListBox();
  48. this.SuspendLayout();
  49. //
  50. // button1
  51. //
  52. this.button1.Location = new System.Drawing.Point(94, 12);
  53. this.button1.Name = "button1";
  54. this.button1.Size = new System.Drawing.Size(50, 23);
  55. this.button1.TabIndex = 1;
  56. this.button1.Text = "新建";
  57. this.button1.UseVisualStyleBackColor = true;
  58. this.button1.Click += new System.EventHandler(this.button1_Click);
  59. //
  60. // button2
  61. //
  62. this.button2.Location = new System.Drawing.Point(151, 12);
  63. this.button2.Name = "button2";
  64. this.button2.Size = new System.Drawing.Size(50, 23);
  65. this.button2.TabIndex = 2;
  66. this.button2.Text = "修改";
  67. this.button2.UseVisualStyleBackColor = true;
  68. this.button2.Click += new System.EventHandler(this.button2_Click);
  69. //
  70. // button3
  71. //
  72. this.button3.Location = new System.Drawing.Point(208, 12);
  73. this.button3.Name = "button3";
  74. this.button3.Size = new System.Drawing.Size(50, 23);
  75. this.button3.TabIndex = 3;
  76. this.button3.Text = "删除";
  77. this.button3.UseVisualStyleBackColor = true;
  78. this.button3.Click += new System.EventHandler(this.button3_Click);
  79. //
  80. // button4
  81. //
  82. this.button4.Location = new System.Drawing.Point(265, 12);
  83. this.button4.Name = "button4";
  84. this.button4.Size = new System.Drawing.Size(50, 23);
  85. this.button4.TabIndex = 4;
  86. this.button4.Text = "刷新";
  87. this.button4.UseVisualStyleBackColor = true;
  88. this.button4.Click += new System.EventHandler(this.button4_Click);
  89. //
  90. // button5
  91. //
  92. this.button5.Location = new System.Drawing.Point(12, 12);
  93. this.button5.Name = "button5";
  94. this.button5.Size = new System.Drawing.Size(75, 23);
  95. this.button5.TabIndex = 5;
  96. this.button5.Text = "执行";
  97. this.button5.UseVisualStyleBackColor = true;
  98. this.button5.Click += new System.EventHandler(this.button5_Click);
  99. //
  100. // listBox1
  101. //
  102. this.listBox1.FormattingEnabled = true;
  103. this.listBox1.ItemHeight = 12;
  104. this.listBox1.Location = new System.Drawing.Point(12, 42);
  105. this.listBox1.Name = "listBox1";
  106. this.listBox1.Size = new System.Drawing.Size(303, 400);
  107. this.listBox1.TabIndex = 6;
  108. //
  109. // ScriptManageDialog
  110. //
  111. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  112. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  113. this.ClientSize = new System.Drawing.Size(323, 450);
  114. this.Controls.Add(this.listBox1);
  115. this.Controls.Add(this.button5);
  116. this.Controls.Add(this.button4);
  117. this.Controls.Add(this.button3);
  118. this.Controls.Add(this.button2);
  119. this.Controls.Add(this.button1);
  120. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  121. this.MaximizeBox = false;
  122. this.Name = "ScriptManageDialog";
  123. this.ShowInTaskbar = false;
  124. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  125. this.Text = "脚本管理";
  126. this.TopMost = false;
  127. this.Activated += new System.EventHandler(this.ScriptManageDialog_Activated_1);
  128. this.Controls.SetChildIndex(this.button1, 0);
  129. this.Controls.SetChildIndex(this.button2, 0);
  130. this.Controls.SetChildIndex(this.button3, 0);
  131. this.Controls.SetChildIndex(this.button4, 0);
  132. this.Controls.SetChildIndex(this.button5, 0);
  133. this.Controls.SetChildIndex(this.listBox1, 0);
  134. this.ResumeLayout(false);
  135. }
  136. #endregion
  137. private System.Windows.Forms.Button button1;
  138. private System.Windows.Forms.Button button2;
  139. private System.Windows.Forms.Button button3;
  140. private System.Windows.Forms.Button button4;
  141. private System.Windows.Forms.Button button5;
  142. private System.Windows.Forms.ListBox listBox1;
  143. #endregion
  144. private AppWorkspace appWorkspace;
  145. private ToolStripItemCollection collection;
  146. private ScriptEditDialog scriptEditDialog;
  147. //private ScriptStepDialog scriptStepDialog;
  148. private List<mic_script> list;
  149. /// <summary>
  150. /// 获取当前选中的脚本步骤
  151. /// </summary>
  152. List<mic_script_step> steps = new List<mic_script_step>();
  153. /// <summary>
  154. /// 获取当前选中的脚本步骤参数
  155. /// </summary>
  156. private List<mic_script_step_param> paramss = new List<mic_script_step_param>();
  157. /// <summary>
  158. /// 获取当前执行的脚本id和脚本参数
  159. /// </summary>
  160. private ParamObject currentParam = null;
  161. /// <summary>
  162. /// 当前正在执行哪个脚本
  163. /// </summary>
  164. private int currentRunningIndex = 0;
  165. /// <summary>
  166. /// 当前执行到哪个脚本
  167. /// </summary>
  168. private int currentIndex = 0;
  169. /// <summary>
  170. /// 手动退出脚本执行
  171. /// </summary>
  172. private bool alertShutDownAction = false;
  173. public ScriptManageDialog(AppWorkspace appWorkspace)
  174. {
  175. this.appWorkspace = appWorkspace;
  176. this.collection = this.appWorkspace.ToolBar.MainMenu.Items;
  177. this.ShowInTaskbar = false;
  178. InitializeComponent();
  179. this.button1.Text = PdnResources.GetString("Menu.New.text");
  180. this.button2.Text = PdnResources.GetString("Menu.modify.Text");
  181. this.button3.Text = PdnResources.GetString("Menu.Edit.Delete.Text");
  182. this.button4.Text = PdnResources.GetString("Menu.Refresh.text");
  183. this.button5.Text = PdnResources.GetString("Menu.tool.Scriptmanagement.execute.text");
  184. this.Text = PdnResources.GetString("Menu.Tools.Script.Text");
  185. this.Text = PdnResources.GetString("Menu.Tools.Script.Text");
  186. }
  187. /// <summary>
  188. /// 新建
  189. /// </summary>
  190. /// <param name="sender"></param>
  191. /// <param name="e"></param>
  192. private void button1_Click(object sender, EventArgs e)
  193. {
  194. this.appWorkspace.ScriptRecordingModel = null;
  195. this.appWorkspace.editScriptRecording = false;
  196. if (scriptEditDialog == null)
  197. {
  198. CreateScriptEditDialog();
  199. }
  200. else
  201. {
  202. if (scriptEditDialog.IsDisposed)
  203. {
  204. CreateScriptEditDialog();
  205. }
  206. else
  207. {
  208. scriptEditDialog.WindowState = FormWindowState.Normal;
  209. }
  210. }
  211. }
  212. private void CreateScriptEditDialog()
  213. {
  214. scriptEditDialog = new ScriptEditDialog(appWorkspace, false, null);
  215. scriptEditDialog.StartPosition = FormStartPosition.CenterScreen;
  216. scriptEditDialog.ShowDialog();
  217. }
  218. /// <summary>
  219. /// 设置当前执行到脚本功能的名称
  220. /// </summary>
  221. /// <param name="text"></param>
  222. private void setScriptText(string stepName)
  223. {
  224. if (stepName.StartsWith("-"))
  225. stepName = stepName.TrimStart(new char[] { '-' });
  226. appWorkspace.Widgets.RunningDialog.setScriptText(stepName, (this.currentRunningIndex == steps.Count - 1));
  227. //if (scriptStepDialog == null || scriptStepDialog.IsDisposed)
  228. // return;
  229. //scriptStepDialog.setScriptText(stepName, (currentRunningIndex == steps.Count - 1));
  230. }
  231. /// <summary>
  232. /// 初始化脚本列表
  233. /// </summary>
  234. private void InitScriptList()
  235. {
  236. this.listBox1.Items.Clear();
  237. list = mic_script_BLL.FindAll();
  238. foreach (var item in list)
  239. {
  240. this.listBox1.Items.Add(item.name);
  241. }
  242. }
  243. private void ScriptManageDialog_Activated_1(object sender, EventArgs e)
  244. {
  245. InitScriptList();
  246. }
  247. /// <summary>
  248. /// 修改
  249. /// </summary>
  250. /// <param name="sender"></param>
  251. /// <param name="e"></param>
  252. private void button2_Click(object sender, EventArgs e)
  253. {
  254. this.appWorkspace.ScriptRecordingModel = null;
  255. this.appWorkspace.editScriptRecording = false;
  256. if (this.listBox1.SelectedItem != null)
  257. {
  258. scriptEditDialog = new ScriptEditDialog(appWorkspace, true, list[this.listBox1.SelectedIndex]);
  259. scriptEditDialog.StartPosition = FormStartPosition.CenterScreen;
  260. scriptEditDialog.ShowDialog();
  261. }
  262. }
  263. /// <summary>
  264. /// 刷新
  265. /// </summary>
  266. /// <param name="sender"></param>
  267. /// <param name="e"></param>
  268. private void button4_Click(object sender, EventArgs e)
  269. {
  270. InitScriptList();
  271. }
  272. /// <summary>
  273. /// 删除脚本
  274. /// </summary>
  275. /// <param name="sender"></param>
  276. /// <param name="e"></param>
  277. private void button3_Click(object sender, EventArgs e)
  278. {
  279. if (this.listBox1.SelectedItem != null)
  280. {
  281. var toremoveItem = this.listBox1.SelectedItem;
  282. int selectedItem = this.listBox1.SelectedIndex;
  283. DialogResult dr = MessageBox.Show(PdnResources.GetString("Menu.reyousureyouwanteletethescript.Text")+"?", PdnResources.GetString("Menu.hint.text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question);//0930###19008
  284. if (dr != DialogResult.OK)
  285. return;
  286. mic_script_BLL.Del(this.list[selectedItem].id);
  287. this.listBox1.Items.Remove(toremoveItem);
  288. }
  289. InitScriptList();
  290. }
  291. /// <summary>
  292. /// 当前正在执行哪个脚本
  293. /// </summary>
  294. public ParamObject ScriptCurrentParam
  295. {
  296. set
  297. {
  298. currentParam = value;
  299. }
  300. get
  301. {
  302. return currentParam;
  303. }
  304. }
  305. /// <summary>
  306. /// 退出脚本执行
  307. /// </summary>
  308. public void ShutDownScriptRunning()
  309. {
  310. this.alertShutDownAction = true;
  311. this.currentIndex = steps.Count;
  312. appWorkspace.ActiveDocumentWorkspace.ActiveTool= Annotation.Enum.DrawToolType.Pointer;
  313. appWorkspace.ScriptStopping = false;
  314. ResumeScriptRunning();
  315. }
  316. /// <summary>
  317. /// 用于标注测量后继续执行
  318. /// </summary>
  319. public void ResumeScriptRunning()
  320. {
  321. if (!appWorkspace.ScriptRunning)
  322. return;
  323. if (appWorkspace.ScriptStopping)
  324. return;
  325. //循环脚本步骤
  326. for (int index = this.currentIndex; index < steps.Count; index++)
  327. {
  328. mic_script_step step = steps[index];
  329. if (!appWorkspace.ScriptRunning)
  330. break;
  331. this.currentRunningIndex = index;
  332. //递归的方式
  333. this.RecursiveData(this.collection, step, "");
  334. if (appWorkspace.ScriptStopping)
  335. {
  336. this.currentIndex = index + 1;
  337. break;
  338. }
  339. }
  340. if (!appWorkspace.ScriptStopping)
  341. appWorkspace.ScriptRunning = false;
  342. refreshScriptRunningStatus();
  343. }
  344. /// <summary>
  345. /// 执行脚本
  346. /// </summary>
  347. /// <param name="sender"></param>
  348. /// <param name="e"></param>
  349. private void button5_Click(object sender, EventArgs e)
  350. {
  351. if (this.listBox1.SelectedItem != null)
  352. {
  353. //appWorkspace.Widgets.RunningDialog.Show();
  354. appWorkspace.Widgets.RunningDialog.Visible = true;
  355. ////appWorkspace.Widgets.RunningDialog.setScriptText(parentName + "-" + /*list[this.listBox1.SelectedIndex]*/((PdnMenuItem)collection[i]).Text);
  356. //scriptStepDialog = new ScriptStepDialog(appWorkspace, true, list[this.listBox1.SelectedIndex]);
  357. //scriptStepDialog.StartPosition = FormStartPosition.CenterParent/*.CenterScreen*/;
  358. //scriptStepDialog.Show();
  359. }
  360. if (this.listBox1.SelectedItem!=null)
  361. {
  362. this.currentIndex = 0;
  363. steps = mic_script_step_BLL.FindAllByScripId(list[this.listBox1.SelectedIndex].id);
  364. paramss = mic_script_step_param_BLL.FindAllByScriptId(list[this.listBox1.SelectedIndex].id);
  365. //this.WindowState = FormWindowState.Minimized;
  366. this.Enabled = false;
  367. appWorkspace.ScriptRunning = false;
  368. appWorkspace.ScriptStopping = false;
  369. appWorkspace.ScriptRunning = true;
  370. //appWorkspace.toolsPanel.RefreshTools();
  371. //appWorkspace.ToolBar.ResetCustomizeTools();
  372. //循环脚本步骤
  373. for (int index = this.currentIndex; index < steps.Count; index++)
  374. {
  375. mic_script_step step = steps[index];
  376. if (!appWorkspace.ScriptRunning)
  377. break;
  378. this.currentRunningIndex = index;
  379. //递归的方式
  380. this.RecursiveData(this.collection, step, "");
  381. if (appWorkspace.ScriptStopping)
  382. {
  383. this.currentIndex = index + 1;
  384. break;
  385. }
  386. //下面是根据name查找的方式,效率应该更高,不过需要改改数据结构
  387. /**
  388. ToolStripItem[] items = this.collection.Find(step.step_name, true);
  389. if (items != null && items.Length>0)
  390. {
  391. foreach(ToolStripItem item in items)
  392. {
  393. if (!item.Name.Equals("OpenRecent"))
  394. {
  395. if (item.GetType() != typeof(ToolStripSeparator))
  396. {
  397. if (step.menu_id == ((PdnMenuItem)item).MenuId)
  398. {
  399. ((PdnMenuItem)item).PerformClick();
  400. }
  401. }
  402. }
  403. }
  404. }
  405. **/
  406. }
  407. if (!appWorkspace.ScriptStopping)
  408. appWorkspace.ScriptRunning = false;
  409. refreshScriptRunningStatus();
  410. }
  411. }
  412. /// <summary>
  413. /// 脚本执行完成给出提示并刷新显示状态
  414. /// </summary>
  415. private void refreshScriptRunningStatus()
  416. {
  417. if (!appWorkspace.ScriptRunning && !appWorkspace.ScriptStopping)
  418. {
  419. if (this.alertShutDownAction)
  420. this.alertShutDownAction = false;
  421. else
  422. MessageBox.Show(PdnResources.GetString("Menu.criptexecutioncomplet.Text"));
  423. appWorkspace.Widgets.RunningDialog.Visible = false;
  424. //if (scriptStepDialog != null/* && !scriptStepDialog.IsDisposed*/)
  425. // scriptStepDialog.Close();
  426. //this.WindowState = FormWindowState.Normal;
  427. this.Enabled = true;
  428. //appWorkspace.toolsPanel.RefreshTools();
  429. //appWorkspace.ToolBar.ResetCustomizeTools();
  430. }
  431. }
  432. /// <summary>
  433. /// 递归查找菜单,暂时不用,改成直接用name查找
  434. /// </summary>
  435. /// <param name="collection"></param>
  436. /// <param name="step"></param>
  437. private void RecursiveData(ToolStripItemCollection collection, mic_script_step step, string parentName)
  438. {
  439. for (int i = 0; i < collection.Count; i++)
  440. {
  441. if (!collection[i].Name.Equals("OpenRecent") && !collection[i].Name.Equals("CameraSelection"))
  442. {
  443. if (collection[i] is PdnMenuItem)
  444. {
  445. if (step.menu_id == ((PdnMenuItem)collection[i]).MenuId)
  446. {
  447. if (step.automatic == 1 && !((PdnMenuItem)collection[i]).NeedOpenDialog)
  448. {
  449. //在这里反射出对应功能的参数类
  450. string className = InvariantData.path_Action + ".Action" + step.menu_id;
  451. ParamObject param = (ParamObject)Assembly.Load(InvariantData.assembly_Data).CreateInstance(className);
  452. if (param != null && !(new List<string>() { "" }.Contains(param.OpenImage)) && param.Lists != null)
  453. {
  454. if (appWorkspace.ActiveDocumentWorkspace == null)
  455. {
  456. MessageBox.Show(((PdnMenuItem)collection[i]).Text + ":请打开图片");
  457. appWorkspace.ScriptRunning = false;
  458. break;
  459. }
  460. setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
  461. if (((PdnMenuItem)collection[i]).CanUseInSenseShield)
  462. {
  463. //if (((PdnMenuItem)collection[i]).NeedOpenDialog)
  464. //{
  465. // setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
  466. // ((PdnMenuItem)collection[i]).PerformClick();
  467. // if (this.currentRunningIndex < steps.Count - 1 && !((PdnMenuItem)collection[i]).AutoNextScript
  468. // && ((PdnMenuItem)collection[i]).NeedWaitKey && this.appWorkspace.GetScriptRunning())
  469. // this.appWorkspace.SetScriptStopping(true);
  470. //}
  471. //else
  472. //{
  473. DocumentWorkspace[] scriptWorkspaces = appWorkspace.ScriptWorkspaces;
  474. if (scriptWorkspaces == null)
  475. autoStep(param, step.id, parentName + "-" + ((PdnMenuItem)collection[i]).Text, null);
  476. else//循环处理多张打开的图片
  477. {
  478. foreach (DocumentWorkspace item in scriptWorkspaces)
  479. {
  480. autoStep(param, step.id, parentName + "-" + ((PdnMenuItem)collection[i]).Text, item);
  481. item.OpenInScriptRunning = false;
  482. }
  483. }
  484. }
  485. }
  486. else
  487. {
  488. this.currentParam = null;
  489. setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
  490. ((PdnMenuItem)collection[i]).PerformClick();
  491. if (this.currentRunningIndex < steps.Count - 1 && !((PdnMenuItem)collection[i]).AutoNextScript
  492. && ((PdnMenuItem)collection[i]).NeedWaitKey && this.appWorkspace.GetScriptRunning()
  493. /* 、、待调试已确认NeedOpenDialog是否多余 && !((PdnMenuItem)collection[i]).NeedOpenDialog*/)
  494. this.appWorkspace.SetScriptStopping(true);
  495. //else if (((PdnMenuItem)collection[i]).NeedOpenDialog)
  496. //{//...待调试已确认NeedOpenDialog是否多余
  497. // this.appWorkspace.SetScriptStopping(true);
  498. //}
  499. }
  500. }
  501. else
  502. {
  503. //在这里反射出对应功能的参数类
  504. string className = InvariantData.path_Action + ".Action" + step.menu_id;
  505. this.currentParam = (ParamObject)Assembly.Load(InvariantData.assembly_Data).CreateInstance(className);
  506. if (this.currentParam != null && this.currentParam.Lists != null)
  507. {
  508. List<Args> list = new List<Args>();
  509. foreach (Args arg in this.currentParam.Lists)
  510. {
  511. mic_script_step_param param1 = paramss.Find(m => m.param_key.Equals(arg.Key) && m.step_id == step.id);
  512. if (param1 != null)
  513. {
  514. arg.Value = param1.value != null ? param1.value : param1.param_value;
  515. if (arg.choiseList != null && arg.choiseList.Count > 0)
  516. {
  517. try
  518. {
  519. int index = Convert.ToInt32(arg.value);
  520. var thisArg = arg.choiseList[index];
  521. foreach (var item in thisArg.lists)
  522. {
  523. mic_script_step_param thisParam = paramss.Find(m => m.param_key.Equals(item.Key) && m.step_id == step.id);
  524. if (thisParam != null)
  525. {
  526. item.value = thisParam.value != null ? thisParam.value : thisParam.param_value;
  527. list.Add(item);
  528. }
  529. }
  530. }
  531. catch (Exception)
  532. {
  533. continue;
  534. }
  535. }
  536. }
  537. }
  538. this.currentParam.lists.AddRange(list);
  539. }
  540. setScriptText(parentName + "-" + ((PdnMenuItem)collection[i]).Text);
  541. this.appWorkspace.ScriptAutomatic = (step.automatic == 1);
  542. ((PdnMenuItem)collection[i]).PerformClick();
  543. if (this.currentRunningIndex < steps.Count - 1 && !((PdnMenuItem)collection[i]).AutoNextScript
  544. && ((PdnMenuItem)collection[i]).NeedWaitKey && this.appWorkspace.GetScriptRunning())
  545. this.appWorkspace.SetScriptStopping(true);
  546. }
  547. }
  548. if (appWorkspace.ScriptStopping)
  549. {
  550. //waitKey();
  551. }
  552. else
  553. RecursiveData(((PdnMenuItem)collection[i]).DropDownItems, step, parentName + "-" + ((PdnMenuItem)collection[i]).Text);
  554. }
  555. }
  556. }
  557. }
  558. /// <summary>
  559. /// 脚本自动执行的方法
  560. /// </summary>
  561. /// <param name="param"></param>
  562. /// <param name="stepId"></param>
  563. /// <param name="stepName"></param>
  564. private void autoStep(ParamObject param, int stepId, string stepName, DocumentWorkspace oneWorkSpace)
  565. {
  566. if (stepName.StartsWith("-"))
  567. stepName = stepName.TrimStart(new char[] { '-' });
  568. bool createNewImage = true;
  569. foreach (Args arg in param.Lists)
  570. {
  571. mic_script_step_param param1 = paramss.Find(m => m.param_key.Equals(arg.Key) && m.step_id == stepId);
  572. if (param1 != null)
  573. arg.Value = param1.value != null ? param1.value : param1.param_value;
  574. switch (arg.Key)
  575. {
  576. case "createNewImg":
  577. createNewImage = (int)arg.Value == 0 ? true : false;
  578. break;
  579. }
  580. }
  581. OpenCvSharp.Mat imageMat;
  582. if (oneWorkSpace == null)
  583. imageMat = appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat();
  584. else
  585. imageMat = oneWorkSpace.CompositionSurface.CreatedAliasedMat();
  586. if (param.OpenImage.Equals("current"))
  587. {
  588. OpenCvSharp.Mat mat = param.PerformProcess(imageMat.Clone());
  589. DocumentWorkspace dw;
  590. if (!createNewImage)
  591. {
  592. dw = appWorkspace.ActiveDocumentWorkspace;
  593. }
  594. else
  595. {
  596. dw = appWorkspace.AddNewDocumentWorkspace();
  597. Document document = Document.FromImageMat(mat);
  598. dw.Document = document;
  599. dw.fileText = stepName + "-" + Startup.instance.step_length;
  600. dw.OpenInScriptRunning = (oneWorkSpace != null);
  601. dw.rules = appWorkspace.ActiveDocumentWorkspace.rules;
  602. dw.xmlSaveModel = appWorkspace.ActiveDocumentWorkspace.xmlSaveModel;
  603. this.appWorkspace.ActiveDocumentWorkspace = dw;
  604. Startup.instance.step_length += 1;
  605. }
  606. }
  607. else if (param.OpenImage.Equals("addPhase"))
  608. {
  609. OpenCvSharp.Mat mat = param.PerformProcess(imageMat.Clone());
  610. List<PhaseModel> list = new List<PhaseModel>();
  611. PhaseModel model = new PhaseModel();
  612. model.name = PdnResources.GetString("Menu.BinaryAction.BinaryExtraction.Text");
  613. model.choise = true;
  614. model.mat = mat;
  615. foreach (Args args in param.lists)
  616. {
  617. if (args.Key.Equals("phaseColor"))
  618. {
  619. model.color = (int)args.Value;
  620. break;
  621. }
  622. }
  623. list.Add(model);
  624. DocumentWorkspace dw;
  625. if (!createNewImage)
  626. {
  627. dw = appWorkspace.ActiveDocumentWorkspace;
  628. }
  629. else {
  630. dw = appWorkspace.AddNewDocumentWorkspace();
  631. Document document = Document.FromImageMat(imageMat.Clone());
  632. dw.Document = document;
  633. dw.fileText = stepName + "-" + Startup.instance.step_length;
  634. dw.OpenInScriptRunning = (oneWorkSpace != null);
  635. dw.rules = appWorkspace.ActiveDocumentWorkspace.rules;
  636. dw.xmlSaveModel = appWorkspace.ActiveDocumentWorkspace.xmlSaveModel;
  637. this.appWorkspace.ActiveDocumentWorkspace = dw;
  638. Startup.instance.step_length += 1;
  639. }
  640. dw.PhaseModels = list;
  641. this.appWorkspace.ActiveDocumentWorkspace.Refresh();
  642. }
  643. else if (param.OpenImage.Equals("currentPhase"))
  644. {
  645. //如果有选中的相
  646. List<PhaseModel> phases;
  647. if (oneWorkSpace == null)
  648. phases = appWorkspace.ActiveDocumentWorkspace.PhaseModels.FindAll(a => a.choise == true && a.mat != null);
  649. else
  650. phases = oneWorkSpace.PhaseModels.FindAll(a => a.choise == true && a.mat != null);
  651. if (phases.Count > 0)
  652. {
  653. //一些方法用到了微米换算
  654. double ruleValue = 0.0;
  655. Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out ruleValue);//获取
  656. List<PhaseModel> phasesCopy;
  657. if (oneWorkSpace == null)
  658. phasesCopy = appWorkspace.ActiveDocumentWorkspace.PhaseModelsForCopy;
  659. else
  660. phasesCopy = oneWorkSpace.PhaseModelsForCopy;
  661. for (int i = 0; i < phases.Count; i++)
  662. {
  663. if (phases[i].mat == null)
  664. continue;
  665. PhaseModel model = new PhaseModel();
  666. model.choise = phases[i].choise;
  667. model.color = phases[i].color;
  668. model.name = phases[i].name;
  669. model.position = phases[i].position;
  670. model.mat = new OpenCvSharp.Mat();
  671. phases[i].mat.CopyTo(model.mat);
  672. phasesCopy[i].mat = param.PerformProcess(model, ruleValue);
  673. }
  674. DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();
  675. Document document = Document.FromImageMat(imageMat.Clone());
  676. dw.Document = document;
  677. dw.fileText = stepName + "-" + Startup.instance.step_length;
  678. dw.xmlSaveModel = appWorkspace.ActiveDocumentWorkspace.xmlSaveModel;
  679. dw.PhaseModels = phasesCopy/*list*/;
  680. dw.OpenInScriptRunning = (oneWorkSpace != null);
  681. this.appWorkspace.ActiveDocumentWorkspace = dw;
  682. Startup.instance.step_length += 1;
  683. this.appWorkspace.ActiveDocumentWorkspace.Refresh();
  684. }
  685. else
  686. {
  687. OpenCvSharp.Mat mat = param.PerformProcess(imageMat.Clone());
  688. if (mat != null)//###########
  689. {
  690. DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();
  691. Document document = Document.FromImageMat(mat);
  692. dw.Document = document;
  693. dw.fileText = stepName + "-" + Startup.instance.step_length;
  694. dw.OpenInScriptRunning = (oneWorkSpace != null);
  695. appWorkspace.ActiveDocumentWorkspace = dw;
  696. Startup.instance.step_length += 1;
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }