FormFindLine.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using CommonMethods;
  2. using HalconDotNet;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using ViewROI;
  13. namespace FindLineTool
  14. {
  15. public partial class FormFindLine : Form
  16. {
  17. public FindLine myFindLine = null;
  18. public IToolInfo myToolInfo = new IToolInfo();
  19. public HWindowTool_Smart myHwindow = new HWindowTool_Smart();
  20. public HDrawingObject selected_drawing_object = new HDrawingObject();
  21. public FormFindLine(ref object findLine)
  22. {
  23. InitializeComponent();
  24. _instance = this;
  25. if (findLine.GetType().FullName != "System.Object")
  26. {
  27. myToolInfo = (IToolInfo)findLine;
  28. myFindLine = (FindLine)myToolInfo.tool;
  29. myFindLine.DispImage();
  30. }
  31. }
  32. /// <summary>
  33. /// 窗体对象实例
  34. /// </summary>
  35. private static FormFindLine _instance;
  36. public static FormFindLine Instance
  37. {
  38. get
  39. {
  40. if (_instance != null)
  41. {
  42. lock (_instance)
  43. {
  44. if (_instance == null)
  45. {
  46. object line = new object();
  47. _instance = new FormFindLine(ref line);
  48. }
  49. return _instance;
  50. }
  51. }
  52. else
  53. {
  54. object line = new object();
  55. _instance = new FormFindLine(ref line);
  56. return _instance;
  57. }
  58. }
  59. }
  60. private void FormFindLine2_Load(object sender, EventArgs e)
  61. {
  62. this.panel1.Controls.Add(myHwindow);
  63. myHwindow.Dock = DockStyle.Fill;
  64. InitTool();
  65. }
  66. bool isInit = false;
  67. private void InitTool()
  68. {
  69. isInit = true;
  70. this.Text = myToolInfo.toolName;
  71. Application.DoEvents();
  72. // myFindLine.Run(); //运行一下,使卡尺显示出来
  73. cbx_edgeSelect.Text = myFindLine.edgeSelect;
  74. tbx_minScore.Text = myFindLine.minScore.ToString();
  75. cbx_polarity.Text = myFindLine.polarity == "positive" ? "从暗到明" : "从明到暗";
  76. tbx_caliperNum.Text = myFindLine.cliperNum.ToString();
  77. tbx_caliperLength.Text = myFindLine.length.ToString();
  78. tbx_threshold.Text = myFindLine.threshold.ToString();
  79. tbx_Sigma.Text = myFindLine.sigma.ToString();
  80. tbx_caliperLength2.Text = myFindLine.weidth.ToString();
  81. chBDispRec.Checked = myFindLine.dispRec;
  82. chBDispCross.Checked = myFindLine.dispCross;
  83. //// 将要编辑的线显示
  84. if(myFindLine.inputPoseHomMat2D.Type != HTupleType.EMPTY)
  85. {
  86. selected_drawing_object = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.LINE, new HTuple[] { myFindLine.newExpectLineStartRow, myFindLine.newExpectLineStartCol, myFindLine.newExpectLineEndRow, myFindLine.newExpectLineEndCol });
  87. }
  88. else
  89. {
  90. selected_drawing_object = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.LINE, new HTuple[] { myFindLine.modelStartRow, myFindLine.modelStartCol, myFindLine.modelEndRow, myFindLine.modelEndCol });
  91. }
  92. GC.KeepAlive(selected_drawing_object);
  93. selected_drawing_object.OnSelect(OnSelectDrawingObject);
  94. selected_drawing_object.OnAttach(OnSelectDrawingObject);
  95. selected_drawing_object.OnResize(OnSelectDrawingObject);
  96. selected_drawing_object.OnDrag(OnSelectDrawingObject);
  97. myHwindow.DispHWindow.AttachDrawingObjectToWindow(selected_drawing_object);
  98. tsbtRunTool_Click(null, null);
  99. isInit = false;
  100. }
  101. /// <summary>
  102. /// 参数回调
  103. /// </summary>
  104. /// <param name="dobj"></param>
  105. /// <param name="hwin"></param>
  106. /// <param name="type"></param>
  107. private void OnSelectDrawingObject(HDrawingObject dobj, HWindow hwin, string type)
  108. {
  109. myFindLine.expectLineStartRow = dobj.GetDrawingObjectParams("row1");
  110. myFindLine.expectLineStartCol = dobj.GetDrawingObjectParams("column1");
  111. myFindLine.expectLineEndRow = dobj.GetDrawingObjectParams("row2");
  112. myFindLine.expectLineEndCol = dobj.GetDrawingObjectParams("column2");
  113. }
  114. private void tsbtRunTool_Click(object sender, EventArgs e)
  115. {
  116. // 运行参数
  117. myFindLine.minScore = Convert.ToDouble(tbx_minScore.Text.Trim());
  118. myFindLine.cliperNum = Convert.ToInt16(tbx_caliperNum.Text.Trim());
  119. myFindLine.threshold = Convert.ToInt16(tbx_threshold.Text.Trim());
  120. myFindLine.length = Convert.ToInt16(tbx_caliperLength.Text.Trim());
  121. myFindLine.weidth = Convert.ToInt16(tbx_caliperLength2.Text.Trim());
  122. myFindLine.polarity = cbx_polarity.SelectedItem.ToString() == "从明到暗" ? "negative" : "positive";
  123. myFindLine.edgeSelect = cbx_edgeSelect.SelectedItem.ToString();
  124. myFindLine.sigma = Convert.ToDouble(tbx_Sigma.Text.Trim());
  125. // Run
  126. myFindLine.Run(SoftwareRunState.Debug);
  127. }
  128. /// <summary>
  129. /// 设定工具运行状态
  130. /// </summary>`
  131. /// <param name="msg">运行中的信息</param>
  132. /// <param name="status">运行状态</param>
  133. public void SetToolStatus(string msg, ToolRunStatu status)
  134. {
  135. if (myFindLine != null)
  136. {
  137. myFindLine.runMessage = msg;
  138. myFindLine.toolRunStatu = status;
  139. lb_RunStatus.Text = myFindLine.toolRunStatu == ToolRunStatu.Succeed ? "工具运行成功!" : $"工具运行异常, 异常原因:{myFindLine.runMessage}";
  140. lb_RunTime.Text = myFindLine.runTime;
  141. if (myFindLine.toolRunStatu == ToolRunStatu.Succeed)
  142. {
  143. statusStrip.BackColor = Color.LimeGreen;
  144. }
  145. else
  146. {
  147. statusStrip.BackColor = Color.Red;
  148. }
  149. }
  150. }
  151. private void DispSetCheck(object sender, EventArgs e)
  152. {
  153. if(!isInit)
  154. {
  155. myFindLine.dispRec = chBDispRec.Checked ? true : false;
  156. myFindLine.dispCross = chBDispCross.Checked ? true : false;
  157. }
  158. }
  159. private void FormFindLine2_FormClosing(object sender, FormClosingEventArgs e)
  160. {
  161. myHwindow.Dispose();
  162. this.Dispose();
  163. this.Dispose();
  164. GC.Collect();
  165. }
  166. private void btnSetModelPose_Click(object sender, EventArgs e)
  167. {
  168. myFindLine.UpdateModelLineLocation();
  169. MessageBox.Show("模板线位置已更新");
  170. }
  171. }
  172. }