using OTSDataType; using OTSMeasureApp._0_OTSModel.OTSDataType; using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow; using OTSModelSharp; using OTSModelSharp.ServiceCenter; using System; using System.Collections; using System.Drawing; using System.Threading; using System.Windows.Forms; namespace OTSMeasureApp._5_OTSMeasureStatuImageFun { public partial class frmBCRegulate : Form { public PointF stdPos; public string brightphaseMaterial; public int brightphasevalue; public string darkphaseMaterial; public int darkphasevalue; public bool isToRun = false; public double initialBrightness; public double initialContrast; public int mag = 100; //放大倍数 public double workingDistance = 10.0; public AutoRegulateType autoregulateType = AutoRegulateType.EveryPeriod; //自动调节类型 public int period = 60; //minutes to run the regulation public CMeasure m_measureobj; private ISemController semController = SemController.GetSEMController(); public CSEMStageData stageData = null; CBrightnessContrastAdjust BCregulater=null; public frmBCRegulate() { InitializeComponent(); } private void frmBCRegulate_Load_1(object sender, EventArgs e) { // 国际化 OTSCommon.Language lan = new OTSCommon.Language(this); Hashtable table = lan.GetNameTable(this.Name); this.Text = table["frmcaption"].ToString(); this.label1.Text = table["stdpos"].ToString(); this.label2.Text = table["bright"].ToString(); this.label3.Text = table["dark"].ToString(); this.label4.Text = table["initbright"].ToString(); this.label5.Text = table["initcontrast"].ToString(); this.label6.Text = table["mag"].ToString(); //this.label7.Text = table["label7"].ToString(); this.button2.Text = table["autobctest"].ToString(); this.button1.Text = table["read"].ToString(); this.button4.Text = table["read"].ToString(); this.button5.Text = table["read"].ToString(); this.button6.Text = table["write"].ToString(); this.button8.Text = table["stop"].ToString(); //this.button7.Text = table["ok"].ToString(); this.button3.Text = table["record"].ToString(); this.EnableAutoBC.Text = table["enableautobc"].ToString(); this.radiobtnEveryPeriod.Text = table["everyperiod"].ToString(); this.radiobtnEverySample.Text = table["everysample"].ToString(); txtstdpos.Text = stdPos.ToString(); cbobrele.Text = brightphaseMaterial; cbodarkele.Text = darkphaseMaterial; txtbrvalue.Text = brightphasevalue.ToString(); txtdarkvalue.Text = darkphasevalue.ToString(); txtinitbr.Text = initialBrightness.ToString(); txtinitcontrast.Text = initialContrast.ToString(); txtMag.Text = mag.ToString(); radiobtnEveryPeriod.Checked = autoregulateType == AutoRegulateType.EveryPeriod; radiobtnEverySample.Checked = autoregulateType == AutoRegulateType.EverySample; txtperiod.Text = period.ToString(); txtWD.Text = workingDistance.ToString("F2"); EnableAutoBC.Checked = isToRun; label9.Text = ""; } private void button1_Click(object sender, EventArgs e) { stdPos = Point.Empty; double r = 0; double x=0, y = 0; semController.GetSemPositionXY(ref x, ref y, ref r); stageData.ConvertSEMToOTSCoord( new Point((int)x, (int)y),ref stdPos); txtstdpos.Text = stdPos.ToString(); double wd = 10; semController.GetWorkingDistance(ref wd); txtWD.Text =wd.ToString("F2"); } private void button2_Click(object sender, EventArgs e) { var param = m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam; param.stdMaterialOTSPos=new Point((int)stdPos.X,(int)stdPos.Y); param.brightphaseelement = cbobrele.Text; param.brightphaseGrayvalue = int.Parse(txtbrvalue.Text); param.darkphaseelement = cbodarkele.Text; param.darkphaseGrayvalue = int.Parse(txtdarkvalue.Text); param.mag = txtMag.Text == "" ? 100 : int.Parse(txtMag.Text); param.initialBrightness = double.Parse(txtinitbr.Text); param.initialContrast = double.Parse(txtinitcontrast.Text); param.toRun = EnableAutoBC.Checked; param.workingDistance= double.Parse(txtWD.Text); m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam = param; BCregulater = new CBrightnessContrastAdjust(m_measureobj); // disable the button while worker runs button2.Enabled = false; label9.Text = "Auto BC Adjusting..."; // start worker on a background thread and ensure callback runs on UI thread when done Thread thread = new Thread(() => { try { BCregulater.DoBrightnessContrastAdjust(); } catch (ThreadAbortException) { // respect thread aborts if any; still call callback to restore UI } catch (Exception ex) { // optionally log exception - keep UI responsive try { } catch { } } finally { OnBCAdjustFinished(); } }); thread.IsBackground = true; thread.Start(); } /// /// Callback invoked when background BC adjust finishes (success, failure or stop). /// Marshals back to UI thread and re-enables button2. /// private void OnBCAdjustFinished() { if (this.IsHandleCreated && this.InvokeRequired) { try { this.BeginInvoke((Action)(() => { try { button2.Enabled = true; label9.Text = "Auto BC Done!"; } catch { } })); } catch { /* form might be closing */ } } else { try { button2.Enabled = true; label9.Text = "Auto BC Done!"; } catch { } } } private void button3_Click(object sender, EventArgs e) { var param = m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam; param.stdMaterialOTSPos = new Point((int)stdPos.X, (int)stdPos.Y); param.brightphaseelement = cbobrele.Text; param.brightphaseGrayvalue = int.Parse(txtbrvalue.Text); param.darkphaseelement = cbodarkele.Text; param.darkphaseGrayvalue = int.Parse(txtdarkvalue.Text); param.mag = txtMag.Text == "" ? 100 : int.Parse(txtMag.Text); param.initialBrightness = double.Parse(txtinitbr.Text); param.initialContrast = double.Parse(txtinitcontrast.Text); param.toRun = EnableAutoBC.Checked; param.period= txtperiod.Text == "" ? 60 : int.Parse(txtperiod.Text); param.autoRegulateType =radiobtnEveryPeriod.Checked ? AutoRegulateType.EveryPeriod : AutoRegulateType.EverySample; param.workingDistance = double.Parse( txtWD.Text); m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam = param; Close(); } private void button4_Click(object sender, EventArgs e) { CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj); int dark = 0, bright = 0; regulater.GetCurrentTwoProminentGray(ref dark, ref bright); txtbrvalue.Text = bright.ToString(); txtdarkvalue.Text = dark.ToString(); } private void button5_Click(object sender, EventArgs e) { CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj); double br = 0, contra = 0; regulater.GetCurrentBrightnessAndContrast(ref br, ref contra); txtinitbr.Text = br.ToString(); txtinitcontrast.Text = contra.ToString(); } private void button6_Click(object sender, EventArgs e) { CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj); double br = double.Parse(txtinitbr.Text), contra =double.Parse(txtinitcontrast.Text); regulater.WriteBrightnessAndContrast(ref br,ref contra); } private void button7_Click(object sender, EventArgs e) { CBrightnessContrastAdjust regulater = new CBrightnessContrastAdjust(m_measureobj); regulater.MoveSEMToPoint(new PointF(stdPos.X, stdPos.Y)); regulater.SetWorkingDistance(Convert.ToDouble(txtWD.Text)); } private void button8_Click(object sender, EventArgs e) { if (BCregulater != null) BCregulater.StopBrightnessContrastAdjust(); } } }