frmBCRegulate.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using OTSDataType;
  2. using OTSMeasureApp._0_OTSModel.OTSDataType;
  3. using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow;
  4. using OTSModelSharp;
  5. using OTSModelSharp.ServiceCenter;
  6. using System;
  7. using System.Collections;
  8. using System.Drawing;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. namespace OTSMeasureApp._5_OTSMeasureStatuImageFun
  12. {
  13. public partial class frmBCRegulate : Form
  14. {
  15. public PointF stdPos;
  16. public string brightphaseMaterial;
  17. public int brightphasevalue;
  18. public string darkphaseMaterial;
  19. public int darkphasevalue;
  20. public bool isToRun = false;
  21. public double initialBrightness;
  22. public double initialContrast;
  23. public int mag = 100; //放大倍数
  24. public double workingDistance = 10.0;
  25. public AutoRegulateType autoregulateType = AutoRegulateType.EveryPeriod; //自动调节类型
  26. public int period = 60; //minutes to run the regulation
  27. public CMeasure m_measureobj;
  28. private ISemController semController = SemController.GetSEMController();
  29. public CSEMStageData stageData = null;
  30. CBrightnessContrastAdjust BCregulater=null;
  31. public frmBCRegulate()
  32. {
  33. InitializeComponent();
  34. }
  35. private void frmBCRegulate_Load_1(object sender, EventArgs e)
  36. {
  37. // 国际化
  38. OTSCommon.Language lan = new OTSCommon.Language(this);
  39. Hashtable table = lan.GetNameTable(this.Name);
  40. this.Text = table["frmcaption"].ToString();
  41. this.label1.Text = table["stdpos"].ToString();
  42. this.label2.Text = table["bright"].ToString();
  43. this.label3.Text = table["dark"].ToString();
  44. this.label4.Text = table["initbright"].ToString();
  45. this.label5.Text = table["initcontrast"].ToString();
  46. this.label6.Text = table["mag"].ToString();
  47. //this.label7.Text = table["label7"].ToString();
  48. this.button2.Text = table["autobctest"].ToString();
  49. this.button1.Text = table["read"].ToString();
  50. this.button4.Text = table["read"].ToString();
  51. this.button5.Text = table["read"].ToString();
  52. this.button6.Text = table["write"].ToString();
  53. this.button8.Text = table["stop"].ToString();
  54. //this.button7.Text = table["ok"].ToString();
  55. this.button3.Text = table["record"].ToString();
  56. this.EnableAutoBC.Text = table["enableautobc"].ToString();
  57. this.radiobtnEveryPeriod.Text = table["everyperiod"].ToString();
  58. this.radiobtnEverySample.Text = table["everysample"].ToString();
  59. txtstdpos.Text = stdPos.ToString();
  60. cbobrele.Text = brightphaseMaterial;
  61. cbodarkele.Text = darkphaseMaterial;
  62. txtbrvalue.Text = brightphasevalue.ToString();
  63. txtdarkvalue.Text = darkphasevalue.ToString();
  64. txtinitbr.Text = initialBrightness.ToString();
  65. txtinitcontrast.Text = initialContrast.ToString();
  66. txtMag.Text = mag.ToString();
  67. radiobtnEveryPeriod.Checked = autoregulateType == AutoRegulateType.EveryPeriod;
  68. radiobtnEverySample.Checked = autoregulateType == AutoRegulateType.EverySample;
  69. txtperiod.Text = period.ToString();
  70. txtWD.Text = workingDistance.ToString("F2");
  71. EnableAutoBC.Checked = isToRun;
  72. label9.Text = "";
  73. }
  74. private void button1_Click(object sender, EventArgs e)
  75. {
  76. stdPos = Point.Empty;
  77. double r = 0;
  78. double x=0, y = 0;
  79. semController.GetSemPositionXY(ref x, ref y, ref r);
  80. stageData.ConvertSEMToOTSCoord( new Point((int)x, (int)y),ref stdPos);
  81. txtstdpos.Text = stdPos.ToString();
  82. double wd = 10;
  83. semController.GetWorkingDistance(ref wd);
  84. txtWD.Text =wd.ToString("F2");
  85. }
  86. private void button2_Click(object sender, EventArgs e)
  87. {
  88. var param = m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam;
  89. param.stdMaterialOTSPos=new Point((int)stdPos.X,(int)stdPos.Y);
  90. param.brightphaseelement = cbobrele.Text;
  91. param.brightphaseGrayvalue = int.Parse(txtbrvalue.Text);
  92. param.darkphaseelement = cbodarkele.Text;
  93. param.darkphaseGrayvalue = int.Parse(txtdarkvalue.Text);
  94. param.mag = txtMag.Text == "" ? 100 : int.Parse(txtMag.Text);
  95. param.initialBrightness = double.Parse(txtinitbr.Text);
  96. param.initialContrast = double.Parse(txtinitcontrast.Text);
  97. param.toRun = EnableAutoBC.Checked;
  98. param.workingDistance= double.Parse(txtWD.Text);
  99. m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam = param;
  100. BCregulater = new CBrightnessContrastAdjust(m_measureobj);
  101. // disable the button while worker runs
  102. button2.Enabled = false;
  103. label9.Text = "Auto BC Adjusting...";
  104. // start worker on a background thread and ensure callback runs on UI thread when done
  105. Thread thread = new Thread(() =>
  106. {
  107. try
  108. {
  109. BCregulater.DoBrightnessContrastAdjust();
  110. }
  111. catch (ThreadAbortException)
  112. {
  113. // respect thread aborts if any; still call callback to restore UI
  114. }
  115. catch (Exception ex)
  116. {
  117. // optionally log exception - keep UI responsive
  118. try { } catch { }
  119. }
  120. finally
  121. {
  122. OnBCAdjustFinished();
  123. }
  124. });
  125. thread.IsBackground = true;
  126. thread.Start();
  127. }
  128. /// <summary>
  129. /// Callback invoked when background BC adjust finishes (success, failure or stop).
  130. /// Marshals back to UI thread and re-enables button2.
  131. /// </summary>
  132. private void OnBCAdjustFinished()
  133. {
  134. if (this.IsHandleCreated && this.InvokeRequired)
  135. {
  136. try
  137. {
  138. this.BeginInvoke((Action)(() =>
  139. {
  140. try {
  141. button2.Enabled = true;
  142. label9.Text = "Auto BC Done!";
  143. } catch { }
  144. }));
  145. }
  146. catch { /* form might be closing */ }
  147. }
  148. else
  149. {
  150. try {
  151. button2.Enabled = true;
  152. label9.Text = "Auto BC Done!";
  153. } catch { }
  154. }
  155. }
  156. private void button3_Click(object sender, EventArgs e)
  157. {
  158. var param = m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam;
  159. param.stdMaterialOTSPos = new Point((int)stdPos.X, (int)stdPos.Y);
  160. param.brightphaseelement = cbobrele.Text;
  161. param.brightphaseGrayvalue = int.Parse(txtbrvalue.Text);
  162. param.darkphaseelement = cbodarkele.Text;
  163. param.darkphaseGrayvalue = int.Parse(txtdarkvalue.Text);
  164. param.mag = txtMag.Text == "" ? 100 : int.Parse(txtMag.Text);
  165. param.initialBrightness = double.Parse(txtinitbr.Text);
  166. param.initialContrast = double.Parse(txtinitcontrast.Text);
  167. param.toRun = EnableAutoBC.Checked;
  168. param.period= txtperiod.Text == "" ? 60 : int.Parse(txtperiod.Text);
  169. param.autoRegulateType =radiobtnEveryPeriod.Checked ? AutoRegulateType.EveryPeriod : AutoRegulateType.EverySample;
  170. param.workingDistance = double.Parse( txtWD.Text);
  171. m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam = param;
  172. Close();
  173. }
  174. private void button4_Click(object sender, EventArgs e)
  175. {
  176. CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj);
  177. int dark = 0, bright = 0;
  178. regulater.GetCurrentTwoProminentGray(ref dark, ref bright);
  179. txtbrvalue.Text = bright.ToString();
  180. txtdarkvalue.Text = dark.ToString();
  181. }
  182. private void button5_Click(object sender, EventArgs e)
  183. {
  184. CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj);
  185. double br = 0, contra = 0;
  186. regulater.GetCurrentBrightnessAndContrast(ref br, ref contra);
  187. txtinitbr.Text = br.ToString();
  188. txtinitcontrast.Text = contra.ToString();
  189. }
  190. private void button6_Click(object sender, EventArgs e)
  191. {
  192. CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj);
  193. double br = double.Parse(txtinitbr.Text), contra =double.Parse(txtinitcontrast.Text);
  194. regulater.WriteBrightnessAndContrast(ref br,ref contra);
  195. }
  196. private void button7_Click(object sender, EventArgs e)
  197. {
  198. CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj);
  199. regulater.MoveSEMToPoint(new PointF(stdPos.X, stdPos.Y));
  200. regulater.SetWorkingDistance(Convert.ToDouble(txtWD.Text));
  201. }
  202. private void button8_Click(object sender, EventArgs e)
  203. {
  204. if (BCregulater != null)
  205. BCregulater.StopBrightnessContrastAdjust();
  206. }
  207. }
  208. }