123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- namespace OINA.Extender.Testharness
- {
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Windows.Forms;
- using OINA.Extender;
- using OINA.Extender.MicroscopeControl;
- using OINA.Extender.Testharness.Properties;
- /// <summary>
- /// Microscope Control Panel Form
- /// </summary>
- public partial class MicroscopeControlPanel : Form
- {
- /// <summary>
- /// IMicroscopeController object
- /// </summary>
- private IMicroscopeController microscopeController = null;
- /// <summary>
- /// MicroscopeControlPanel Constructor
- /// </summary>
- public MicroscopeControlPanel()
- {
- this.InitializeComponent();
- // Create microscope controller
- this.microscopeController = AcquireFactory.CreateMicroscopeControl();
- this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
- this.microscopeController.StageChange += this.OnMicroscopeStageChange;
- this.microscopeController.ColumnConnected += this.OnMicroscopeColumnChange;
- this.microscopeController.StageConnected += this.OnMicroscopeStageChange;
- // Initial UI contents
- this.UpdateMicroscopeColumnText();
- this.UpdateMicroscopeStageText();
- }
- /// <summary>
- /// Show a Message Box
- /// </summary>
- /// <param name="msg">MBox Message</param>
- /// <param name="title">MBox Title</param>
- private static void MsgBox(string msg, string title)
- {
- MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
- }
- /// <summary>
- /// Gets a double value from a textbox
- /// </summary>
- /// <param name="textBox">Textbox to get the number from</param>
- /// <returns>The value, or null if not a valid number</returns>
- private static double? GetDoubleFromTextbox(TextBox textBox)
- {
- double value;
- if (!double.TryParse(textBox.Text, out value))
- {
- return null;
- }
- return value;
- }
- #region Event handlers
- /// <summary>
- /// OnMicroscopeStageChange
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">EventArgs</param>
- private void OnMicroscopeStageChange(object sender, EventArgs e)
- {
- this.BeginInvoke((Action)this.UpdateMicroscopeStageText);
- }
- /// <summary>
- /// OnMicroscopeColumnChange
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">EventArgs</param>
- private void OnMicroscopeColumnChange(object sender, EventArgs e)
- {
- this.BeginInvoke((Action)this.UpdateMicroscopeColumnText);
- }
- #endregion
- #region Get microscope status and update the UI
- /// <summary>
- /// UpdateMicroscopeStageText Method
- /// </summary>
- private void UpdateMicroscopeStageText()
- {
- this.gbStage.Text = @"Stage " + this.microscopeController.StageConnectionStatus.Name;
- var stageCapabilities = this.microscopeController.StageCapabilities;
- var stageConditions = this.microscopeController.StageConditions;
- if (stageCapabilities.StageX.CanRead)
- {
- this.textX.Text = stageConditions.StageX.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbX.Text = this.textX.Text;
- }
- if (stageCapabilities.StageY.CanRead)
- {
- this.textY.Text = stageConditions.StageY.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbY.Text = this.textY.Text;
- }
- if (stageCapabilities.StageZ.CanRead)
- {
- this.textZ.Text = stageConditions.StageZ.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbZ.Text = this.textZ.Text;
- }
- if (stageCapabilities.StageT.CanRead)
- {
- this.textTilt.Text = stageConditions.StageT.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbTilt.Text = this.textTilt.Text;
- }
- if (stageCapabilities.StageR.CanRead)
- {
- this.textRotate.Text = stageConditions.StageR.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbRotate.Text = this.textRotate.Text;
- }
- this.textBacklash.Text = Convert.ToBoolean(stageConditions.BacklashOn).ToString(CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// UpdateMicroscopeColumnText Method
- /// </summary>
- private void UpdateMicroscopeColumnText()
- {
- this.gbColumn.Text = @"Column " + this.microscopeController.ColumnConnectionStatus.Name;
- var columnCapabilities = this.microscopeController.ColumnCapabilities;
- var columnConditions = this.microscopeController.ColumnConditions;
-
- if (columnCapabilities.Magnification.CanRead)
- {
- this.textMag.Text = columnConditions.Magnification.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbMag.Text = this.textMag.Text;
- }
- if (columnCapabilities.WorkingDistance.CanRead)
- {
- this.textWD.Text = columnConditions.WorkingDistance.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbWD.Text = this.textWD.Text;
- }
- if (columnCapabilities.HighVoltage.CanRead)
- {
- this.textHV.Text = columnConditions.HighVoltage.ToString(@"F2", CultureInfo.InvariantCulture);
- this.tbHV.Text = this.textHV.Text;
- }
- if (columnCapabilities.BeamOn.CanRead)
- {
- this.textBeamOn.Text = Convert.ToBoolean(columnConditions.BeamOn).ToString(CultureInfo.InvariantCulture);
- }
- if (columnCapabilities.FilamentOn.CanRead)
- {
- this.textFilamentOn.Text = Convert.ToBoolean(columnConditions.FilamentOn).ToString(CultureInfo.InvariantCulture);
- }
- }
- #endregion
- #region Control Microscope from UI
- /// <summary>
- /// SetColumnConditions button click handler
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">EventArgs</param>
- private void SetColumnConditionsButton_Click(object sender, EventArgs e)
- {
- double? mag = GetDoubleFromTextbox(this.tbMag);
- double? wd = GetDoubleFromTextbox(this.tbWD);
- double? hv = GetDoubleFromTextbox(this.tbHV);
- if (mag + wd + hv == null)
- {
- MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
- return;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.Magnification, (double)mag },
- { Column.HighVoltage, (double)hv },
- { Column.WorkingDistance, (double)wd }
- };
- this.microscopeController.SetColumnConditions(columnDictionary);
- }
- /// <summary>
- /// Set Magnification button click handler
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">EventArgs</param>
- private void SetMagButton_Click(object sender, EventArgs e)
- {
- double? mag = GetDoubleFromTextbox(this.tbMag);
- if (mag == null)
- {
- MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
- return;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.Magnification, (double)mag }
- };
- this.microscopeController.SetColumnConditions(columnDictionary);
- }
- /// <summary>
- /// Set Working Distance button click handler
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">EventArgs</param>
- private void SetWDButton_Click(object sender, EventArgs e)
- {
- double? wd = GetDoubleFromTextbox(this.tbWD);
- if (wd == null)
- {
- MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
- return;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.WorkingDistance, (double)wd }
- };
- this.microscopeController.SetColumnConditions(columnDictionary);
- }
- /// <summary>
- /// Set High Voltage button click handler
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">EventArgs</param>
- private void SetHVButton_Click(object sender, EventArgs e)
- {
- double? hv = GetDoubleFromTextbox(this.tbHV);
- if (hv == null)
- {
- MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
- return;
- }
-
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.HighVoltage, (double)hv }
- };
- this.microscopeController.SetColumnConditions(columnDictionary);
- }
- /// <summary>
- /// SetStageConditions button click handler
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">EventArgs</param>
- private void SetStageConditionsButton_Click(object sender, EventArgs e)
- {
- double? stageX = GetDoubleFromTextbox(this.tbX);
- double? stageY = GetDoubleFromTextbox(this.tbY);
- double? stageZ = GetDoubleFromTextbox(this.tbZ);
- double? stageT = GetDoubleFromTextbox(this.tbTilt);
- double? stageR = GetDoubleFromTextbox(this.tbRotate);
- if (stageX + stageY + stageZ + stageT + stageR == null)
- {
- MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
- return;
- }
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageX, (double)stageX },
- { Stage.StageY, (double)stageY },
- { Stage.StageZ, (double)stageZ },
- { Stage.StageT, (double)stageT },
- { Stage.StageR, (double)stageR }
- };
- this.microscopeController.SetStageConditions(stageDictionary);
- }
- #endregion
- /// <summary>
- /// Raises the <see cref="E:System.Windows.Forms.Form.Closing" /> event.
- /// </summary>
- /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
- protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
- {
- IMicroscopeController controller = this.microscopeController;
- if (controller != null)
- {
- controller.ColumnChange -= this.OnMicroscopeColumnChange;
- controller.StageChange -= this.OnMicroscopeStageChange;
- controller.ColumnConnected -= this.OnMicroscopeColumnChange;
- controller.StageConnected -= this.OnMicroscopeStageChange;
- }
- base.OnClosing(e);
- }
- }
- }
|