FormCaliper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using CommonMethods;
  11. using ChoiceTech.Halcon.Control;
  12. using HalconDotNet;
  13. using ViewROI;
  14. namespace CaliperTool
  15. {
  16. public partial class FormCaliper : Form
  17. {
  18. public Caliper myCaliper = null;
  19. public IToolInfo myToolInfo = null;
  20. public HWindowTool_Smart myHwindow = new HWindowTool_Smart();
  21. private static FormCaliper _instance;
  22. public FormCaliper(ref object caliper)
  23. {
  24. InitializeComponent();
  25. _instance = this;
  26. if (caliper.GetType().FullName != "System.Object")
  27. {
  28. myToolInfo = (IToolInfo)caliper;
  29. myCaliper = (Caliper)myToolInfo.tool;
  30. myCaliper.DispImage();
  31. }
  32. }
  33. public static FormCaliper Instance
  34. {
  35. get
  36. {
  37. if (_instance != null)
  38. {
  39. lock (_instance)
  40. {
  41. if (_instance == null)
  42. {
  43. object calib = new object();
  44. _instance = new FormCaliper(ref calib);
  45. }
  46. return _instance;
  47. }
  48. }
  49. else
  50. {
  51. object line = new object();
  52. _instance = new FormCaliper(ref line);
  53. return _instance;
  54. }
  55. }
  56. }
  57. private void FormCaliper_Load(object sender, EventArgs e)
  58. {
  59. this.panel1.Controls.Add(myHwindow);
  60. myHwindow.Dock = DockStyle.Fill;
  61. InitTool();
  62. }
  63. private void InitTool()
  64. {
  65. this.Text = myToolInfo.toolName;
  66. Application.DoEvents();
  67. // 预期参数
  68. tbx_caliperLength1.Text = myCaliper.length1.ToString();
  69. tbx_caliperLength2.Text = myCaliper.length2.ToString();
  70. cbx_edgeSelect.Text = myCaliper.edgeSelect;
  71. cbx_polarity.Text = myCaliper.polarity == "positive" ? "从暗到明" : "从明到暗";
  72. tbx_threshold.Text = myCaliper.threshold.ToString();
  73. tbx_Sigma.Text = myCaliper.sigma.ToString();
  74. // 显示
  75. chBDispRec.Checked = myCaliper.dispRec;
  76. chBDispCross.Checked = myCaliper.dispCross;
  77. chBDispCaliperROI.Checked = myCaliper.LineDisp;
  78. }
  79. private void btn_moveCliperRegion_Click(object sender, EventArgs e)
  80. {
  81. myCaliper.UpdateImage();
  82. myCaliper.DrawExpectLine(myHwindow.DispHWindow);
  83. }
  84. private void btn_runCaliperool_Click(object sender, EventArgs e)
  85. {
  86. // 运行参数
  87. myCaliper.threshold = Convert.ToInt16(tbx_threshold.Text.Trim());
  88. myCaliper.length1 = Convert.ToDouble(tbx_caliperLength1.Text.Trim());
  89. myCaliper.length2 = Convert.ToDouble(tbx_caliperLength2.Text.Trim());
  90. myCaliper.polarity = cbx_polarity.SelectedItem.ToString() == "从明到暗" ? "negative" : "positive";
  91. myCaliper.edgeSelect = cbx_edgeSelect.SelectedItem.ToString();
  92. myCaliper.sigma = Convert.ToDouble(tbx_Sigma.Text.Trim());
  93. myCaliper.Run(SoftwareRunState.Debug);
  94. }
  95. /// <summary>
  96. /// 设定工具运行状态
  97. /// </summary>
  98. /// <param name="msg">运行中的信息</param>
  99. /// <param name="status">运行状态</param>
  100. public void SetToolStatus(string msg, ToolRunStatu status)
  101. {
  102. if (myCaliper != null)
  103. {
  104. myCaliper.runMessage = msg;
  105. myCaliper.toolRunStatu = status;
  106. lb_RunStatus.Text = myCaliper.toolRunStatu == ToolRunStatu.Succeed ? "工具运行成功!" : $"工具运行异常, 异常原因:{myCaliper.runMessage}";
  107. lb_RunTime.Text = myCaliper.runTime;
  108. if (myCaliper.toolRunStatu == ToolRunStatu.Succeed)
  109. {
  110. statusStrip.BackColor = Color.LimeGreen;
  111. }
  112. else
  113. {
  114. statusStrip.BackColor = Color.Red;
  115. }
  116. }
  117. }
  118. private void tsbtRunTool_Click(object sender, EventArgs e)
  119. {
  120. }
  121. }
  122. }