OTSBSEImageFun.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using NSOTSController;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using OTSModelSharp.ServiceInterface;
  9. using OTSDataType;
  10. namespace OTSMeasureApp
  11. {
  12. public class OTSBSEImageFun
  13. {
  14. //OTSIncAMeasureAppForm m_MsrApp = null;
  15. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  16. CImageHandler imageHandle;
  17. public OTSBSEImageFun()
  18. {
  19. imageHandle = new CImageHandler();
  20. }
  21. public bool GetBSEImage(COTSSample WSample,byte[] BSEImage, int iHeight, int iWidth, ref byte[] BSEImageNoBG)
  22. {
  23. return imageHandle.GetBSEImage(WSample,BSEImage, iHeight, iWidth,ref BSEImageNoBG);
  24. }
  25. public bool GetBSEImage(byte[] BSEImage, int iHeight, int iWidth, int grayStart, int grayEnd, ref byte[] BSEImageNoBG)
  26. {
  27. return imageHandle.GetBSEImage(BSEImage, iHeight, iWidth, grayStart, grayEnd,ref BSEImageNoBG);
  28. }
  29. public int GetScanImage(int iWidth, int iHeigh, ref byte[] bImageData)
  30. {
  31. //电镜设置对象
  32. COTSControlFunExport cfun = COTSControlFunExport.GetControllerInstance();
  33. int GetImgCount = 0;
  34. try
  35. {
  36. //连接电镜
  37. bool IsConnec = cfun.ConncetSem();
  38. if (!IsConnec)
  39. {
  40. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.ConncetSem() = false Failed";
  41. log.Error(strErrorInfo);
  42. return 0;
  43. }
  44. //实例电镜初始化
  45. bool IsScan = cfun.ScanInit();
  46. if (!IsScan)
  47. {
  48. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.ScanInit = false Failed";
  49. log.Error(strErrorInfo);
  50. return 0;
  51. }
  52. int a_ExternalMode = 0;
  53. //获取终止模式
  54. a_ExternalMode = cfun.GetSemExternalMode();
  55. //保存初始模式变量
  56. int a_oldMode = 0;
  57. //获取初始模式
  58. if (!cfun.GetSemScanMode(ref a_oldMode))
  59. {
  60. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.GetSemScanMode = false Failed";
  61. log.Error(strErrorInfo);
  62. return 0;
  63. }
  64. //设置当前模式
  65. if (!cfun.SetSemScanMode(a_ExternalMode))
  66. {
  67. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.SetSemScanMode = false Failed";
  68. log.Error(strErrorInfo);
  69. return 0;
  70. }
  71. #region BeamBlank
  72. int a_nBeamBlank = 0;
  73. //获取参数
  74. if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
  75. {
  76. cfun.SetSemScanMode(a_oldMode);
  77. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.GetSemBeamBlank = false Failed";
  78. log.Error(strErrorInfo);
  79. return 0;
  80. }
  81. //设置参数
  82. if (!cfun.SetSemBeamBlank(0))
  83. {
  84. cfun.SetSemScanMode(a_oldMode);
  85. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.SetSemBeamBlank = false Failed";
  86. log.Error(strErrorInfo);
  87. return 0;
  88. }
  89. #endregion
  90. #region 获得放大倍数
  91. //获得放大倍数
  92. double a_dMagnification = 0;
  93. //获取参数
  94. if (!cfun.GetSemMagnification(ref a_dMagnification))
  95. {
  96. cfun.SetSemScanMode(a_oldMode);
  97. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.GetSemMagnification = false Failed";
  98. log.Error(strErrorInfo);
  99. return 0;
  100. }
  101. #endregion
  102. #region 获取 电镜 X、Y轴 与角度
  103. //获取 电镜 X、Y轴 与角度
  104. double PositionX = 0;
  105. double PositionY = 0;
  106. double PositionR = 0;
  107. //获取参数
  108. if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
  109. {
  110. cfun.SetSemScanMode(a_oldMode);
  111. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.GetSemPositionXY = false Failed";
  112. log.Error(strErrorInfo);
  113. return 0;
  114. }
  115. #endregion
  116. #region 设置图像分辨率
  117. //设置宽度
  118. if (!cfun.SetImageSize(iWidth,iHeigh))
  119. {
  120. cfun.SetSemScanMode(a_oldMode);
  121. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.SetImageSize = false Failed";
  122. log.Error(strErrorInfo);
  123. return 0;
  124. }
  125. #endregion
  126. #region 采集时间
  127. //采集时间
  128. int nDwellTime = 4;
  129. if (!cfun.SetDwellTime(nDwellTime))
  130. {
  131. cfun.SetSemScanMode(a_oldMode);
  132. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.SetDwellTime = false Failed";
  133. log.Error(strErrorInfo);
  134. return 0;
  135. }
  136. #endregion
  137. #region MatrixSize
  138. //获得放大倍数
  139. int a_MatrixSize = 0;
  140. Size size = new Size();
  141. //获取参数
  142. size = cfun.GetMatrixSize(a_MatrixSize);
  143. size = cfun.GetMatrixSize(a_MatrixSize);
  144. #endregion
  145. //获取图像数据
  146. int resultCount = iWidth * iHeigh;
  147. GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref bImageData);
  148. //记录日志
  149. if (resultCount == GetImgCount)
  150. {
  151. //设置为原始 扫描模式
  152. cfun.SetSemScanMode(a_oldMode);
  153. //Scan类结束
  154. //初始化电镜参数
  155. //if (!cfun.SetAndStartScan())
  156. //{
  157. // return 0;
  158. //}
  159. }
  160. else
  161. {
  162. cfun.SetSemScanMode(a_oldMode);
  163. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.AcquireBSEImage = 0 Failed";
  164. log.Error(strErrorInfo);
  165. //初始化电镜参数
  166. //if (!cfun.SetAndStartScan())
  167. //{
  168. // return 0;
  169. //}
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. //记录日志
  175. log.Error(ex.Message.ToString());
  176. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) Exception Error Happend";
  177. log.Error(strErrorInfo);
  178. }
  179. finally
  180. {
  181. //cfun.ScanFinishedInstance();
  182. cfun.DisconnectSem();
  183. //cfun.FreeHardware();
  184. }
  185. return GetImgCount;
  186. }
  187. }
  188. }