MicroscopeControlPanel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. namespace OINA.Extender.Testharness
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Windows.Forms;
  7. using OINA.Extender;
  8. using OINA.Extender.MicroscopeControl;
  9. using OINA.Extender.Testharness.Properties;
  10. /// <summary>
  11. /// Microscope Control Panel Form
  12. /// </summary>
  13. public partial class MicroscopeControlPanel : Form
  14. {
  15. /// <summary>
  16. /// IMicroscopeController object
  17. /// </summary>
  18. private IMicroscopeController microscopeController = null;
  19. /// <summary>
  20. /// MicroscopeControlPanel Constructor
  21. /// </summary>
  22. public MicroscopeControlPanel()
  23. {
  24. this.InitializeComponent();
  25. // Create microscope controller
  26. this.microscopeController = AcquireFactory.CreateMicroscopeControl();
  27. this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
  28. this.microscopeController.StageChange += this.OnMicroscopeStageChange;
  29. this.microscopeController.ColumnConnected += this.OnMicroscopeColumnChange;
  30. this.microscopeController.StageConnected += this.OnMicroscopeStageChange;
  31. // Initial UI contents
  32. this.UpdateMicroscopeColumnText();
  33. this.UpdateMicroscopeStageText();
  34. }
  35. /// <summary>
  36. /// Show a Message Box
  37. /// </summary>
  38. /// <param name="msg">MBox Message</param>
  39. /// <param name="title">MBox Title</param>
  40. private static void MsgBox(string msg, string title)
  41. {
  42. MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
  43. }
  44. /// <summary>
  45. /// Gets a double value from a textbox
  46. /// </summary>
  47. /// <param name="textBox">Textbox to get the number from</param>
  48. /// <returns>The value, or null if not a valid number</returns>
  49. private static double? GetDoubleFromTextbox(TextBox textBox)
  50. {
  51. double value;
  52. if (!double.TryParse(textBox.Text, out value))
  53. {
  54. return null;
  55. }
  56. return value;
  57. }
  58. #region Event handlers
  59. /// <summary>
  60. /// OnMicroscopeStageChange
  61. /// </summary>
  62. /// <param name="sender">sender object</param>
  63. /// <param name="e">EventArgs</param>
  64. private void OnMicroscopeStageChange(object sender, EventArgs e)
  65. {
  66. this.BeginInvoke((Action)this.UpdateMicroscopeStageText);
  67. }
  68. /// <summary>
  69. /// OnMicroscopeColumnChange
  70. /// </summary>
  71. /// <param name="sender">sender object</param>
  72. /// <param name="e">EventArgs</param>
  73. private void OnMicroscopeColumnChange(object sender, EventArgs e)
  74. {
  75. this.BeginInvoke((Action)this.UpdateMicroscopeColumnText);
  76. }
  77. #endregion
  78. #region Get microscope status and update the UI
  79. /// <summary>
  80. /// UpdateMicroscopeStageText Method
  81. /// </summary>
  82. private void UpdateMicroscopeStageText()
  83. {
  84. this.gbStage.Text = @"Stage " + this.microscopeController.StageConnectionStatus.Name;
  85. var stageCapabilities = this.microscopeController.StageCapabilities;
  86. var stageConditions = this.microscopeController.StageConditions;
  87. if (stageCapabilities.StageX.CanRead)
  88. {
  89. this.textX.Text = stageConditions.StageX.ToString(@"F2", CultureInfo.InvariantCulture);
  90. this.tbX.Text = this.textX.Text;
  91. }
  92. if (stageCapabilities.StageY.CanRead)
  93. {
  94. this.textY.Text = stageConditions.StageY.ToString(@"F2", CultureInfo.InvariantCulture);
  95. this.tbY.Text = this.textY.Text;
  96. }
  97. if (stageCapabilities.StageZ.CanRead)
  98. {
  99. this.textZ.Text = stageConditions.StageZ.ToString(@"F2", CultureInfo.InvariantCulture);
  100. this.tbZ.Text = this.textZ.Text;
  101. }
  102. if (stageCapabilities.StageT.CanRead)
  103. {
  104. this.textTilt.Text = stageConditions.StageT.ToString(@"F2", CultureInfo.InvariantCulture);
  105. this.tbTilt.Text = this.textTilt.Text;
  106. }
  107. if (stageCapabilities.StageR.CanRead)
  108. {
  109. this.textRotate.Text = stageConditions.StageR.ToString(@"F2", CultureInfo.InvariantCulture);
  110. this.tbRotate.Text = this.textRotate.Text;
  111. }
  112. this.textBacklash.Text = Convert.ToBoolean(stageConditions.BacklashOn).ToString(CultureInfo.InvariantCulture);
  113. }
  114. /// <summary>
  115. /// UpdateMicroscopeColumnText Method
  116. /// </summary>
  117. private void UpdateMicroscopeColumnText()
  118. {
  119. this.gbColumn.Text = @"Column " + this.microscopeController.ColumnConnectionStatus.Name;
  120. var columnCapabilities = this.microscopeController.ColumnCapabilities;
  121. var columnConditions = this.microscopeController.ColumnConditions;
  122. if (columnCapabilities.Magnification.CanRead)
  123. {
  124. this.textMag.Text = columnConditions.Magnification.ToString(@"F2", CultureInfo.InvariantCulture);
  125. this.tbMag.Text = this.textMag.Text;
  126. }
  127. if (columnCapabilities.WorkingDistance.CanRead)
  128. {
  129. this.textWD.Text = columnConditions.WorkingDistance.ToString(@"F2", CultureInfo.InvariantCulture);
  130. this.tbWD.Text = this.textWD.Text;
  131. }
  132. if (columnCapabilities.HighVoltage.CanRead)
  133. {
  134. this.textHV.Text = columnConditions.HighVoltage.ToString(@"F2", CultureInfo.InvariantCulture);
  135. this.tbHV.Text = this.textHV.Text;
  136. }
  137. if (columnCapabilities.BeamOn.CanRead)
  138. {
  139. this.textBeamOn.Text = Convert.ToBoolean(columnConditions.BeamOn).ToString(CultureInfo.InvariantCulture);
  140. }
  141. if (columnCapabilities.FilamentOn.CanRead)
  142. {
  143. this.textFilamentOn.Text = Convert.ToBoolean(columnConditions.FilamentOn).ToString(CultureInfo.InvariantCulture);
  144. }
  145. }
  146. #endregion
  147. #region Control Microscope from UI
  148. /// <summary>
  149. /// SetColumnConditions button click handler
  150. /// </summary>
  151. /// <param name="sender">sender object</param>
  152. /// <param name="e">EventArgs</param>
  153. private void SetColumnConditionsButton_Click(object sender, EventArgs e)
  154. {
  155. double? mag = GetDoubleFromTextbox(this.tbMag);
  156. double? wd = GetDoubleFromTextbox(this.tbWD);
  157. double? hv = GetDoubleFromTextbox(this.tbHV);
  158. if (mag + wd + hv == null)
  159. {
  160. MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
  161. return;
  162. }
  163. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  164. {
  165. { Column.Magnification, (double)mag },
  166. { Column.HighVoltage, (double)hv },
  167. { Column.WorkingDistance, (double)wd }
  168. };
  169. this.microscopeController.SetColumnConditions(columnDictionary);
  170. }
  171. /// <summary>
  172. /// Set Magnification button click handler
  173. /// </summary>
  174. /// <param name="sender">sender object</param>
  175. /// <param name="e">EventArgs</param>
  176. private void SetMagButton_Click(object sender, EventArgs e)
  177. {
  178. double? mag = GetDoubleFromTextbox(this.tbMag);
  179. if (mag == null)
  180. {
  181. MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
  182. return;
  183. }
  184. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  185. {
  186. { Column.Magnification, (double)mag }
  187. };
  188. this.microscopeController.SetColumnConditions(columnDictionary);
  189. }
  190. /// <summary>
  191. /// Set Working Distance button click handler
  192. /// </summary>
  193. /// <param name="sender">sender object</param>
  194. /// <param name="e">EventArgs</param>
  195. private void SetWDButton_Click(object sender, EventArgs e)
  196. {
  197. double? wd = GetDoubleFromTextbox(this.tbWD);
  198. if (wd == null)
  199. {
  200. MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
  201. return;
  202. }
  203. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  204. {
  205. { Column.WorkingDistance, (double)wd }
  206. };
  207. this.microscopeController.SetColumnConditions(columnDictionary);
  208. }
  209. /// <summary>
  210. /// Set High Voltage button click handler
  211. /// </summary>
  212. /// <param name="sender">sender object</param>
  213. /// <param name="e">EventArgs</param>
  214. private void SetHVButton_Click(object sender, EventArgs e)
  215. {
  216. double? hv = GetDoubleFromTextbox(this.tbHV);
  217. if (hv == null)
  218. {
  219. MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
  220. return;
  221. }
  222. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  223. {
  224. { Column.HighVoltage, (double)hv }
  225. };
  226. this.microscopeController.SetColumnConditions(columnDictionary);
  227. }
  228. /// <summary>
  229. /// SetStageConditions button click handler
  230. /// </summary>
  231. /// <param name="sender">sender object</param>
  232. /// <param name="e">EventArgs</param>
  233. private void SetStageConditionsButton_Click(object sender, EventArgs e)
  234. {
  235. double? stageX = GetDoubleFromTextbox(this.tbX);
  236. double? stageY = GetDoubleFromTextbox(this.tbY);
  237. double? stageZ = GetDoubleFromTextbox(this.tbZ);
  238. double? stageT = GetDoubleFromTextbox(this.tbTilt);
  239. double? stageR = GetDoubleFromTextbox(this.tbRotate);
  240. if (stageX + stageY + stageZ + stageT + stageR == null)
  241. {
  242. MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
  243. return;
  244. }
  245. var stageDictionary = new Dictionary<Stage, double>
  246. {
  247. { Stage.StageX, (double)stageX },
  248. { Stage.StageY, (double)stageY },
  249. { Stage.StageZ, (double)stageZ },
  250. { Stage.StageT, (double)stageT },
  251. { Stage.StageR, (double)stageR }
  252. };
  253. this.microscopeController.SetStageConditions(stageDictionary);
  254. }
  255. #endregion
  256. /// <summary>
  257. /// Raises the <see cref="E:System.Windows.Forms.Form.Closing" /> event.
  258. /// </summary>
  259. /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
  260. protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  261. {
  262. IMicroscopeController controller = this.microscopeController;
  263. if (controller != null)
  264. {
  265. controller.ColumnChange -= this.OnMicroscopeColumnChange;
  266. controller.StageChange -= this.OnMicroscopeStageChange;
  267. controller.ColumnConnected -= this.OnMicroscopeColumnChange;
  268. controller.StageConnected -= this.OnMicroscopeStageChange;
  269. }
  270. base.OnClosing(e);
  271. }
  272. }
  273. }