OTSImageData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. /*这个引用包含作图方法*/
  3. /*这个引用包含作图的Bitmap*/
  4. using System.Drawing;
  5. using System.Collections.Generic;
  6. using NSOTSController;
  7. using System.Data;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using OTSDataType;
  11. namespace OTSMeasureApp
  12. {
  13. public enum idLine
  14. {
  15. //曲线
  16. GrayLine = 0,
  17. //灰度图
  18. GrayImage = 1
  19. }
  20. class OTSImageData
  21. {
  22. COTSControlFunExport cfun = null;
  23. OTSMeasureStatusWindow m_MeasureStatuWindow = null;
  24. OTSIncAMeasureAppForm m_MeasureApp = null;
  25. //当前默认值
  26. public idLine Line = idLine.GrayImage;
  27. //String SEMName = "";
  28. int ImageValue = 0;
  29. static ExcelEdit m_xe = new ExcelEdit();
  30. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  31. public OTSImageData(OTSMeasureStatusWindow MeasureStatuWindow, OTSIncAMeasureAppForm oTSIncAMeasureAppForm)
  32. {
  33. try
  34. {
  35. m_MeasureStatuWindow = MeasureStatuWindow;
  36. m_MeasureApp = oTSIncAMeasureAppForm;
  37. }
  38. catch (Exception ex)
  39. {
  40. log.Error("(OTSMeasureStatusWindow.ShowGrayLevelLine_Click) " + ex.ToString());
  41. }
  42. }
  43. /// <summary>
  44. /// 计算调试图灰度分布数据
  45. /// </summary>
  46. /// <param name="Imagedata"></param>
  47. /// <param name="GrayLevelData"></param>
  48. //Imagedata 是图像 GrayLevelData是Y轴的数
  49. public double[] GetGaryData(byte[] Imagedata, double[] GrayLevelData)
  50. {
  51. try
  52. {
  53. //获得下标每一点的灰度值,并在数组里加一
  54. for (int i = 0; i < Imagedata.Length; i++)
  55. {
  56. byte graylevel = Imagedata[i];
  57. if (graylevel > 0 && graylevel < 255)
  58. {
  59. GrayLevelData[graylevel] += 1;
  60. }
  61. }
  62. return GrayLevelData;
  63. }
  64. catch (Exception ex)
  65. {
  66. log.Error("(OTSMeasureStatusWindow.GetGaryData) " + ex.ToString());
  67. return null;
  68. }
  69. }
  70. public static double[] GetStaticGaryData(byte[] Imagedata, double[] GrayLevelData)
  71. {
  72. try
  73. {
  74. //获得下标每一点的灰度值,并在数组里加一
  75. for (int i = 0; i < Imagedata.Length; i++)
  76. {
  77. byte graylevel = Imagedata[i];
  78. if (graylevel > 0 && graylevel < 255)
  79. {
  80. GrayLevelData[graylevel] += 1;
  81. }
  82. }
  83. return GrayLevelData;
  84. }
  85. catch (Exception)
  86. {
  87. return null;
  88. }
  89. }
  90. /// <summary>
  91. /// 计算去背景BSE图灰度分布数据
  92. /// </summary>
  93. /// <param name="Imagedata"></param>
  94. /// <param name="GrayAbandonLevelData"></param>
  95. //Imagedata 是图像 GrayLevelData是Y轴的数
  96. public double[] GetGrayAbandonData(byte[] Imagedata, double[] GrayAbandonLevelData)
  97. {
  98. try
  99. {
  100. //获得下标每一点的灰度值,并在数组里加一
  101. for (int i = 0; i < Imagedata.Length; i++)
  102. {
  103. byte graylevel = Imagedata[i];
  104. if (graylevel > 0 && graylevel < 255)
  105. {
  106. GrayAbandonLevelData[graylevel] += 1;
  107. }
  108. }
  109. return GrayAbandonLevelData;
  110. }
  111. catch (Exception ex)
  112. {
  113. log.Error("(OTSMeasureStatusWindow.GetGaryData) " + ex.ToString());
  114. return null;
  115. }
  116. }
  117. /// <summary>
  118. /// 获取调试图
  119. /// </summary>
  120. /// <param name="iWidth"></param>
  121. /// <param name="iHeight"></param>
  122. /// <param name="bData"></param>
  123. /// <returns></returns>
  124. public bool GetImagData(int iWidth, int iHeight, ref byte[] bData)
  125. {
  126. try
  127. {
  128. //获取图像数据
  129. int resultCount = iHeight * iWidth;
  130. //获取电镜中图像大小
  131. string str = m_MeasureApp.m_ProjParam.GetBSEImageSize();
  132. string[] sArray = str.Split('X');
  133. if (sArray[0] != "" && sArray[1] != "")
  134. {
  135. iWidth = Convert.ToInt32(sArray[0]);
  136. iHeight = Convert.ToInt32(sArray[1]);
  137. }
  138. //设置获取图像的Byte数组
  139. bData = new byte[iWidth * iHeight];
  140. ImageValue = m_MeasureApp.GetImageData(iWidth, iHeight, ref bData, true);
  141. if (resultCount == ImageValue)
  142. {
  143. return true;
  144. }
  145. else
  146. {
  147. return false;
  148. }
  149. }
  150. catch (Exception)
  151. {
  152. //记录日志
  153. return false;
  154. }
  155. }
  156. //去背景图
  157. public bool ShowRemoveBGImage(COTSSample WSample, int cWidth, int cHeight, byte[] cData, ref byte[] BSEImageNoBG)
  158. {
  159. try
  160. {
  161. var ImageFun = new OTSBSEImageFun();
  162. return ImageFun.GetBSEImage(WSample,cData, cHeight, cWidth, ref BSEImageNoBG);
  163. }
  164. catch (Exception ex)
  165. {
  166. return false;
  167. }
  168. }
  169. #region 获取工作距离与放大倍数
  170. /// <summary>
  171. /// 放大倍数
  172. /// </summary>
  173. public double GetMagnification()
  174. {
  175. //获取放大倍数
  176. return cfun.GetMagnification();
  177. }
  178. public void GetSemWorkingDistance(ref double a_dWorkingDistance)
  179. {
  180. //获取工作距离
  181. cfun.GetSemWorkingDistance(ref a_dWorkingDistance);
  182. }
  183. #endregion
  184. #region 获取SEM位置
  185. /// <summary>
  186. /// 获取SEM位置
  187. /// </summary>
  188. /// <returns></returns>
  189. public bool GetSemPositionXY(ref double a_dPositionX, ref double a_dPositionY, ref double a_dPositionR)
  190. {
  191. return cfun.GetSemPositionXY(ref a_dPositionX, ref a_dPositionY, ref a_dPositionR);
  192. }
  193. #endregion
  194. #region 电镜连接
  195. public bool ConnectionOTS()
  196. {
  197. if (cfun != null)
  198. {
  199. if (cfun.ConncetSem())
  200. {
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. #endregion
  207. #region 关闭电镜连接 关闭C++ 对象
  208. public void CloseOTSControllerDLL()
  209. {
  210. if (cfun != null)
  211. {
  212. //cfun.DisConnectSem();
  213. cfun.FreeHardware();
  214. }
  215. }
  216. #endregion
  217. #region 导出Excel 图片与数据
  218. /// <summary>
  219. /// 向Excel中插入图片与数据列表
  220. /// </summary>
  221. /// <param name="dtList"></param>
  222. /// <returns></returns>
  223. public bool InsertDataToExcelTable(List<System.Data.DataTable> a_list_ElementData,string a_filePath,List<string> a_ImgFilePath)
  224. {
  225. m_xe.Create();
  226. Microsoft.Office.Interop.Excel.Worksheet ws = m_xe.GetSheet("Sheet1");
  227. m_xe.m_ws = ws;
  228. string str_ExcelFilePath = a_filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  229. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[0], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 400, 300);
  230. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 320, 400, 100);
  231. if (str_ExcelFilePath.IndexOf(":") < 0)
  232. {
  233. return false;
  234. }
  235. if (m_xe.m_app == null)
  236. {
  237. return false;
  238. }
  239. //数据列表 行数
  240. int dataRow = 30;
  241. //写入标题
  242. foreach (var dgv in a_list_ElementData)
  243. {
  244. for (int i = 0; i < dgv.Columns.Count; i++)
  245. {
  246. m_xe.m_ws.Cells[dataRow+1, i + 1] = dgv.Columns[i].ColumnName;
  247. }
  248. //写入数值
  249. for (int r = 0; r < dgv.Rows.Count; r++)
  250. {
  251. for (int i = 0; i < dgv.Columns.Count; i++)
  252. {
  253. m_xe.m_ws.Cells[dataRow+r + 2, i + 1] = dgv.Rows[r].ItemArray[i];
  254. }
  255. System.Windows.Forms.Application.DoEvents();
  256. }
  257. }
  258. //列宽自适应
  259. m_xe.m_ws.Columns.EntireColumn.AutoFit();
  260. if (str_ExcelFilePath != "")
  261. {
  262. try
  263. {
  264. m_xe.m_wb.Saved = true;
  265. m_xe.m_wb.SaveCopyAs(str_ExcelFilePath);
  266. }
  267. catch (Exception ex)
  268. {
  269. log.Error("InsertDataToExcelTable错误日志: " + ex.ToString());
  270. }
  271. }
  272. //关闭excel
  273. m_xe.Close();
  274. //强行销毁
  275. GC.Collect();
  276. //干掉进程里的EXCEL
  277. foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
  278. {
  279. if (p.ProcessName == "EXCEL")
  280. {
  281. p.Kill();
  282. }
  283. }
  284. //删除图片文件
  285. foreach (string imgPath in a_ImgFilePath)
  286. {
  287. if (File.Exists(imgPath))
  288. {
  289. //如果存在则删除
  290. File.Delete(imgPath);
  291. }
  292. }
  293. System.Diagnostics.Process.Start(str_ExcelFilePath);
  294. return true;
  295. }
  296. #endregion
  297. }
  298. }