ScanController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. 
  2. using FEIApiControl;
  3. using OTSCLRINTERFACE;
  4. using OTSDataType;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace OTSModelSharp.ServiceInterface
  13. {
  14. public class ScanController : IScanController
  15. {
  16. private OTSCLRINTERFACE.COTSControlFunExport scan;
  17. static ScanController scanctrl=null;
  18. public static APIClass ApiClass = null;
  19. private static bool isFEI = false;
  20. int imageWidth = 0;
  21. int imageHeight = 0;
  22. public static ScanController GetScanController()
  23. {
  24. if (FileHelper.GetXMLInformations("SemControllerName") == "FEI")
  25. {
  26. isFEI = true;
  27. }
  28. if (scanctrl == null)
  29. {
  30. scanctrl = new ScanController();
  31. }
  32. return scanctrl;
  33. }
  34. private ScanController()
  35. {
  36. if (isFEI)
  37. {
  38. ApiClass = new APIClass();
  39. }
  40. else
  41. {
  42. this.scan = SemController.hw;
  43. }
  44. }
  45. public CBSEImgClr AcquireBSEImage(int matrixIndex, int reads, int dwell)
  46. {
  47. if (isFEI)
  48. {
  49. Rectangle r = new Rectangle();
  50. CBSEImgClr bse = new CBSEImgClr(r);
  51. bse.InitImageData(imageWidth, imageHeight);
  52. byte[] imgData = new byte[imageWidth * imageHeight];
  53. if (!ApiClass.RunAcquireImage(imageWidth, imageHeight, dwell, "", ref imgData))
  54. {
  55. return null;
  56. }
  57. bse.SetImageData(imgData, imageWidth, imageHeight);
  58. return bse;
  59. }
  60. else
  61. {
  62. if (!scan.IsConnected())
  63. {
  64. return null;
  65. }
  66. Rectangle r = new Rectangle();
  67. CBSEImgClr bse = new CBSEImgClr(r);
  68. if (!scan.AcquireBSEImage(matrixIndex, reads, dwell, ref bse))
  69. {
  70. return null;
  71. }
  72. return bse;
  73. }
  74. }
  75. public bool Init()
  76. {
  77. if (isFEI)
  78. {
  79. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  80. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  81. if (FEIIP == "" || FEIPORT == "")
  82. {
  83. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  84. return false;
  85. }
  86. if (ApiClass.isConnect())
  87. {
  88. return true;
  89. }
  90. return ApiClass.Connect(FEIIP, FEIPORT);
  91. }
  92. else
  93. {
  94. return scan.ScanInit();
  95. }
  96. }
  97. public bool SetDwellTime(long val)
  98. {
  99. if (isFEI)
  100. {
  101. return true;
  102. }
  103. else
  104. {
  105. if (!scan.IsConnected())
  106. {
  107. return false;
  108. }
  109. return scan.SetDwellTime((int)val);
  110. }
  111. }
  112. public bool SetImageSize(int width,int height)
  113. {
  114. imageWidth = width;
  115. imageHeight = height;
  116. if (isFEI)
  117. {
  118. return ApiClass.SetResolution(imageWidth, imageHeight);
  119. }
  120. else
  121. {
  122. if (!scan.IsConnected())
  123. {
  124. return false;
  125. }
  126. return scan.SetImageSize(width, height);
  127. }
  128. }
  129. public static int GetScanImage(int iWidth, int iHeigh, string DwellTime, ref byte[] bImageData)
  130. {
  131. if (FileHelper.GetXMLInformations("SemControllerName") == "FEI")
  132. {
  133. ScanController m_ScanHardwareMgr = ScanController.GetScanController();
  134. m_ScanHardwareMgr.Init();
  135. m_ScanHardwareMgr.SetImageSize(iWidth, iHeigh);
  136. bImageData = m_ScanHardwareMgr.AcquireBSEImage(0, 0, int.Parse(DwellTime)).GetImageDataPtr();
  137. return 1;
  138. }
  139. else
  140. {
  141. //电镜设置对象
  142. var cfun = COTSControlFunExport.GetControllerInstance();
  143. int GetImgCount = 0;
  144. try
  145. {
  146. //连接电镜
  147. bool IsConnec = cfun.ConncetSem();
  148. if (!IsConnec)
  149. {
  150. return 0;
  151. }
  152. //实例电镜初始化
  153. bool IsScan = cfun.ScanInit();
  154. if (!IsScan)
  155. {
  156. return 0;
  157. }
  158. int a_ExternalMode = 0;
  159. //获取终止模式
  160. a_ExternalMode = cfun.GetSemExternalMode();
  161. //保存初始模式变量
  162. int a_oldMode = 0;
  163. //获取初始模式
  164. if (!cfun.GetSemScanMode(ref a_oldMode))
  165. {
  166. return 0;
  167. }
  168. //设置当前模式
  169. if (!cfun.SetSemScanMode(a_ExternalMode))
  170. {
  171. return 0;
  172. }
  173. #region BeamBlank
  174. int a_nBeamBlank = 0;
  175. //获取参数
  176. if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
  177. {
  178. cfun.SetSemScanMode(a_oldMode);
  179. return 0;
  180. }
  181. //设置参数
  182. if (!cfun.SetSemBeamBlank(false))
  183. {
  184. cfun.SetSemScanMode(a_oldMode);
  185. return 0;
  186. }
  187. #endregion
  188. #region 获得放大倍数
  189. //获得放大倍数
  190. double a_dMagnification = 0;
  191. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----begin------");
  192. //获取参数
  193. if (!cfun.GetSemMagnification(ref a_dMagnification))
  194. {
  195. cfun.SetSemScanMode(a_oldMode);
  196. return 0;
  197. }
  198. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification_a_dMagnification:" + a_dMagnification + "------");
  199. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----end------");
  200. #endregion
  201. #region 获取 电镜 X、Y轴 与角度
  202. //获取 电镜 X、Y轴 与角度
  203. double PositionX = 0;
  204. double PositionY = 0;
  205. double PositionR = 0;
  206. //获取参数
  207. if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
  208. {
  209. cfun.SetSemScanMode(a_oldMode);
  210. return 0;
  211. }
  212. #endregion
  213. #region 设置图像分辨率
  214. //设置宽度
  215. if (!cfun.SetImageSize(iWidth, iHeigh))
  216. {
  217. cfun.SetSemScanMode(a_oldMode);
  218. return 0;
  219. }
  220. #endregion
  221. #region 采集时间
  222. //采集时间
  223. int nDwellTime = Convert.ToInt32(DwellTime);
  224. //设置采集时间
  225. if (!cfun.SetDwellTime(nDwellTime))
  226. {
  227. cfun.SetSemScanMode(a_oldMode);
  228. return 0;
  229. }
  230. #endregion
  231. #region MatrixSize
  232. //获得放大倍数
  233. int a_MatrixSize = 0;
  234. Size size = new Size();
  235. //获取参数
  236. size = cfun.GetMatrixSize(a_MatrixSize);
  237. #endregion
  238. //获取图像数据
  239. int resultCount = iWidth * iHeigh;
  240. GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref bImageData);
  241. //记录日志
  242. if (resultCount == GetImgCount)
  243. {
  244. //设置为原始 扫描模式
  245. cfun.SetSemScanMode(a_oldMode);
  246. }
  247. else
  248. {
  249. cfun.SetSemScanMode(a_oldMode);
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. NLog.LogManager.GetCurrentClassLogger().Error(ex.ToString());
  255. }
  256. return GetImgCount;
  257. }
  258. }
  259. }
  260. }