OTSMeasureStatusWindow.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. using OTS.WinFormsUI.Docking;
  2. using OTSCLRINTERFACE;
  3. using OTSDataType;
  4. using OTSIncAGraph.Controls;
  5. using OTSMeasureApp._0_OTSModel.OTSDataType;
  6. using OTSModelSharp;
  7. using OTSModelSharp.ServiceCenter;
  8. using OTSPeriodicTable;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Drawing.Drawing2D;
  14. using System.IO;
  15. using System.Reflection;
  16. using System.Windows.Forms;
  17. namespace OTSMeasureApp
  18. {
  19. public enum MeasureStateMenuType
  20. {
  21. //获取BSE图
  22. ReadBSEPic = 0,
  23. //设置可视化灰度
  24. SetVisualGray = 1,
  25. //显示BSE灰度曲线图
  26. SetVisualGrayForSpecialGray = 2,
  27. grayToolStripMenuItem = 3,
  28. //切换至BSE图
  29. ChangeDiffImageShow = 4,
  30. //BSE去背景图
  31. DelBSEBG = 5,
  32. ColoredBSENoBG = 6,
  33. //显示BSE去背景灰度曲线图
  34. SampleHoleBSEImage = 7,
  35. //线扫描曲线
  36. MenuItemLineScam = 8,
  37. //单点采集Xray与元素信息
  38. PointScanElementMenuItem = 9,
  39. BatchCollectXray=10,
  40. //导出采集信息报告
  41. ExportReport = 11
  42. }
  43. public partial class OTSMeasureStatusWindow : DockContent
  44. {
  45. class CrossSign
  46. {
  47. public Point position;
  48. private OTSMeasureStatusWindow mywin;
  49. public CrossSign(int x, int y, OTSMeasureStatusWindow win)
  50. {
  51. position = new Point(x, y);
  52. mywin = win;
  53. }
  54. public void OnPaint(PaintEventArgs e)
  55. {
  56. System.Drawing.SolidBrush sampleBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
  57. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //图片柔顺模式选择
  58. e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
  59. e.Graphics.CompositingQuality = CompositingQuality.HighQuality;//再加一点
  60. Pen pen = new Pen(sampleBrush, 2f);
  61. var picboxPos = GetPocboxPosByImagePos(position.X, position.Y);
  62. int X = picboxPos.X;
  63. int Y = picboxPos.Y;
  64. e.Graphics.DrawLine(pen, new Point(X - 8, Y), new Point(X + 8, Y));
  65. e.Graphics.DrawLine(pen, new Point(X, Y - 8), new Point(X, Y + 8));
  66. }
  67. private Point GetPocboxPosByImagePos(int x, int y)
  68. {
  69. var rectangle = mywin.ImgZoomRectangle;
  70. double x1 = x;
  71. double y1 = y;
  72. var picX = x1 / m_iWidth * rectangle.Width + rectangle.X;
  73. var picY = y1 / m_iHeight * rectangle.Height + rectangle.Y;
  74. return new Point((int)picX, (int)picY);
  75. }
  76. }
  77. #region 全部变量
  78. public OTSIncAMeasureAppForm m_MeasureAppForm;
  79. static int m_iWidth = 0;
  80. static int m_iHeight = 0;
  81. public byte[] originalBseData;
  82. //去背景图
  83. private byte[] removeBGBseData;
  84. private Bitmap coloredParticleImage;
  85. //绘制采集点对象
  86. private static List<CrossSign> m_ElementPointGDIObjects = new List<CrossSign>();
  87. float m_ZoomRecord = 1;
  88. float m_ZoomCoefficient = 0.2f;
  89. int m_MaxZoom = 12;
  90. NLog.Logger log;
  91. //国际化
  92. OTSCommon.Language lan;
  93. #endregion
  94. #region 在右下角状态栏中显示鼠标当前位置所需要的参数
  95. private COTSField myFld;
  96. private CBSEImgClr originalBse;
  97. Rectangle originalImgRectangle;
  98. private RectangleF ImgZoomRectangle = new RectangleF();
  99. #endregion
  100. public enum ImageType
  101. {
  102. //BSE图
  103. BSEImage = 0,
  104. //去背景图
  105. RemoveBGImage = 1,
  106. //线扫描曲线
  107. GrayLevelLineScan = 2,
  108. //
  109. ColorParticleImage = 3
  110. }
  111. //图bfResult
  112. public static bool bfResult = false;
  113. //当前默认值
  114. ImageType m_imagetype = ImageType.BSEImage;
  115. public OTSMeasureStatusWindow(OTSIncAMeasureAppForm MeasureApp)
  116. {
  117. InitializeComponent();
  118. m_MeasureAppForm = MeasureApp;
  119. //进入时首先对XRayTable进行隐藏
  120. panelXray.Visible = false;
  121. lan = new OTSCommon.Language(this);
  122. }
  123. public void ShowSingleImage(Bitmap bmpImage)
  124. {
  125. picBox.Image = bmpImage;
  126. //设置Pictrue显示位置
  127. picBox.Height = this.RestoreBounds.Height;
  128. picBox.Width = this.RestoreBounds.Width;
  129. //获取黄线坐标位置
  130. originalImgRectangle = GetPictureBoxImageSize(picBox);
  131. pointStart.X = originalImgRectangle.X;
  132. pointEnd.X = originalImgRectangle.X + originalImgRectangle.Width;
  133. pointStart.Y = originalImgRectangle.Y + (originalImgRectangle.Height) / 2;
  134. pointEnd.Y = originalImgRectangle.Y + (originalImgRectangle.Height) / 2;
  135. picBox.Left = 0;
  136. picBox.Top = 0;
  137. picBox.Show();
  138. ImgZoomRectangle.Width = originalImgRectangle.Width;
  139. ImgZoomRectangle.Height = originalImgRectangle.Height;
  140. ImgZoomRectangle.X = originalImgRectangle.X;
  141. ImgZoomRectangle.Y = originalImgRectangle.Y;
  142. pictureBox2.Visible = false;
  143. }
  144. public void ShowDoubleImage(Bitmap image1, Bitmap image2)
  145. {
  146. if (image1 != null)
  147. {
  148. picBox.Image = image1;
  149. }
  150. //设置Pictrue显示位置
  151. picBox.Height = this.RestoreBounds.Height;
  152. picBox.Width = this.RestoreBounds.Width / 2;
  153. //获取黄线坐标位置
  154. originalImgRectangle = GetPictureBoxImageSize(picBox);
  155. pointStart.X = originalImgRectangle.X;
  156. pointEnd.X = originalImgRectangle.X + originalImgRectangle.Width;
  157. pointStart.Y = originalImgRectangle.Y + (originalImgRectangle.Height) / 2;
  158. pointEnd.Y = originalImgRectangle.Y + (originalImgRectangle.Height) / 2;
  159. ImgZoomRectangle.Width = originalImgRectangle.Width;
  160. ImgZoomRectangle.Height = originalImgRectangle.Height;
  161. ImgZoomRectangle.X = originalImgRectangle.X;
  162. ImgZoomRectangle.Y = originalImgRectangle.Y;
  163. picBox.Left = 0;
  164. picBox.Top = 0;
  165. picBox.Show();
  166. if (image2 != null)
  167. {
  168. pictureBox2.Image = image2;
  169. }
  170. pictureBox2.Height = this.RestoreBounds.Height;
  171. pictureBox2.Width = this.RestoreBounds.Width / 2;
  172. pictureBox2.Left = this.RestoreBounds.Width / 2;
  173. pictureBox2.Top = 0;
  174. pictureBox2.Show();
  175. }
  176. //显示扫描曲线图方法
  177. #region 显示扫描曲线图方法
  178. private void DrawCurve(byte[] buf, Graphics g)
  179. {
  180. //创建位图
  181. Rectangle rectangle = GetPictureBoxImageSize(picBox);
  182. double rate = (double)rectangle.Height / (double)picBox.Image.Height;
  183. Pen mypenYellow = new Pen(Color.Yellow, 0.001f);//点颜色
  184. int len = buf.Length;
  185. PointF[] CurvePointF = new PointF[len];//坐标点
  186. float pointX = 0;
  187. float pointY = 0;
  188. for (int i = 0; i < buf.Length; i++)
  189. {
  190. pointX = (picBox.Width - rectangle.Width) / 2 + Convert.ToSingle(i * rate);
  191. float imagei = (int)GrayLevel.Max - Convert.ToInt32(buf[i]);
  192. float imagerate = (Convert.ToSingle(Convert.ToDouble(rectangle.Height) / 510));
  193. pointY = imagei * imagerate + (rectangle.Height) / 4 + rectangle.Top;
  194. CurvePointF[i] = new PointF(pointX, pointY);
  195. }
  196. float Tension = 0.001f;
  197. g.DrawCurve(mypenYellow, CurvePointF, Tension);//画曲线
  198. }
  199. #endregion
  200. //处理获取调试图菜单事件
  201. #region 处理获取调试图菜单事件
  202. private void AcquireBSEImage_Click(object sender, EventArgs e)
  203. {
  204. m_ElementPointGDIObjects.Clear();
  205. bool rst = AcquireBSEImage();
  206. //取图不成功就返回
  207. if (rst)
  208. {
  209. bfResult = true;
  210. }
  211. if (!rst)
  212. {
  213. if (contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled == true)
  214. {
  215. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = false;
  216. }
  217. //显示单点扫描元素信息菜单
  218. contextMenuStrip1.Items[(int)MeasureStateMenuType.PointScanElementMenuItem].Enabled = false;
  219. picBox.Hide();
  220. return;
  221. }
  222. else
  223. {
  224. //显示单点扫描元素信息菜单
  225. SetContextMenuEnabled(true);
  226. }
  227. m_imagetype = ImageType.BSEImage;
  228. Bitmap bitmap = CImageHandler.ToGrayBitmap(originalBseData, m_iWidth, m_iHeight);
  229. ShowSingleImage(bitmap);
  230. m_ZoomRecord = 1;
  231. picBox.Height = this.RestoreBounds.Height;
  232. picBox.Left = 0;
  233. picBox.Top = 0;
  234. panelXray.Visible = false;
  235. }
  236. /// <summary>
  237. /// 获取BSE图方法
  238. /// </summary>
  239. private bool AcquireBSEImage()
  240. {
  241. //建立图像数据
  242. originalBseData = new byte[m_iWidth * m_iHeight];
  243. //获取图像数据
  244. DwellTimeLevel nDwellTime = DwellTimeLevel.Low;
  245. try
  246. {
  247. int level = (int)m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().GetImageScanParam().GetScanImageSpeed();
  248. switch (level)
  249. {
  250. case 0:
  251. nDwellTime = DwellTimeLevel.Low;
  252. break;
  253. case 1:
  254. nDwellTime = DwellTimeLevel.Medium;
  255. break;
  256. case 2:
  257. nDwellTime = DwellTimeLevel.High;
  258. break;
  259. }
  260. }
  261. catch (Exception)
  262. {
  263. nDwellTime = DwellTimeLevel.Low;
  264. }
  265. CBSEImgClr img;
  266. bool rst = GetScanImage(m_iWidth, m_iHeight, nDwellTime, out img);
  267. originalBse = img;
  268. originalBseData = img.GetImageDataPtr();
  269. myFld = new COTSField();
  270. myFld.SetBSEImage(img);
  271. myFld.Sample = m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample();
  272. return rst;
  273. }
  274. private void UpdateImageAndLineScanChart()
  275. {
  276. m_imagetype = ImageType.GrayLevelLineScan;
  277. Bitmap bitmap = CImageHandler.ToGrayBitmap(originalBseData, m_iWidth, m_iHeight);
  278. ShowSingleImage(bitmap);
  279. picBox.Refresh();
  280. }
  281. public bool GetScanImage(int iWidth, int iHeight, DwellTimeLevel iDwellTime, out CBSEImgClr bseImg)
  282. {
  283. //电镜设置对象
  284. IScanController cfun = ScanController.GetScanController();
  285. ISemController sem = SemController.GetSEMController();
  286. try
  287. {
  288. //连接电镜
  289. bool IsConnec = cfun.Init();
  290. if (!IsConnec)
  291. {
  292. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.ConncetSem() = false Failed";
  293. log.Error(strErrorInfo);
  294. bseImg = null;
  295. return false;
  296. }
  297. cfun.SetImageSize(iWidth, iHeight);
  298. cfun.SetDwellTime(iDwellTime);
  299. var bse = cfun.AcquireBSEImage();
  300. bseImg = bse;
  301. sem.SetScanExternal(false);
  302. }
  303. catch (Exception ex)
  304. {
  305. log.Error(ex.Message.ToString());
  306. bseImg = null;
  307. return false;
  308. }
  309. return true;
  310. }
  311. #endregion
  312. //显示去背景图 = AUTO
  313. #region 显示去背景图
  314. private void ShowRemoveBGImage_Click(object sender, EventArgs e)
  315. {
  316. //设置右键菜单中的项 是否可编辑
  317. SetContextMenuEnabled(true);
  318. m_imagetype = ImageType.RemoveBGImage;
  319. ShowRemoveBGImage();
  320. panelXray.Visible = false;
  321. }
  322. protected void ShowRemoveBGImage()
  323. {
  324. //获取图像数据
  325. COTSSample WSample = m_MeasureAppForm.m_ProjRstData.GetWorkingSample();
  326. double pixelSize = WSample.CalculatePixelSize();
  327. var imageProcessParam = WSample.GetMsrParams().GetImageProcessParam();
  328. var imageHandle = new CImageHandler();
  329. myFld = new COTSField(new Point(0, 0), pixelSize);
  330. myFld.SetBSEImage(originalBse);
  331. myFld.Sample = WSample;
  332. bfResult = imageHandle.GetBSEImage(myFld, imageProcessParam, pixelSize, m_iWidth, m_iHeight, ref removeBGBseData);
  333. //取图不成功就返回
  334. if (!bfResult) { return; }
  335. Bitmap bitmap = CImageHandler.ToGrayBitmap(removeBGBseData, m_iWidth, m_iHeight);
  336. ShowDoubleImage((Bitmap)picBox.Image, bitmap);
  337. //切换显示至BSE图
  338. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  339. }
  340. protected void ShowColoredRemoveBGImage()
  341. {
  342. //获取图像数据
  343. COTSSample WSample = m_MeasureAppForm.m_ProjRstData.GetWorkingSample();
  344. double pixelSize = WSample.CalculatePixelSize();
  345. var imageProcessParam = WSample.GetMsrParams().GetImageProcessParam();
  346. var imageHandle = new CImageHandler();
  347. coloredParticleImage = new Bitmap(m_iWidth, m_iHeight);
  348. myFld = new COTSField(new Point(0, 0), pixelSize);
  349. myFld.SetBSEImage(originalBse);
  350. myFld.Sample = WSample;
  351. CBSEImgClr pImgNoBG = new CBSEImgClr(new Rectangle(0, 0, m_iWidth, m_iHeight));
  352. bfResult = imageHandle.GetColoredImage(myFld, imageProcessParam, pixelSize, m_iWidth, m_iHeight, ref pImgNoBG, ref coloredParticleImage);
  353. removeBGBseData = pImgNoBG.GetImageDataPtr();
  354. //取图不成功就返回
  355. if (!bfResult) { return; }
  356. ShowDoubleImage((Bitmap)picBox.Image, coloredParticleImage);
  357. picBox.Show();
  358. //切换显示至BSE图
  359. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  360. }
  361. #endregion
  362. //显示线扫描曲线
  363. #region 显示线扫描曲线
  364. private void ShowLineScanChart_Click(object sender, EventArgs e)
  365. {
  366. ShowSingleImage((Bitmap)picBox.Image);
  367. if (MenuItemLineScan.Checked)
  368. {
  369. m_imagetype = ImageType.GrayLevelLineScan;
  370. timer1.Enabled = true;
  371. }
  372. else
  373. {
  374. m_imagetype = ImageType.BSEImage;
  375. timer1.Enabled = false;
  376. }
  377. }
  378. #endregion
  379. static Rectangle GetPictureBoxImageSize(PictureBox p_PictureBox)
  380. {
  381. if (p_PictureBox != null)
  382. {
  383. PropertyInfo _ImageRectanglePropert = p_PictureBox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
  384. return (Rectangle)_ImageRectanglePropert.GetValue(p_PictureBox, null);
  385. }
  386. return new Rectangle(0, 0, 0, 0);
  387. }
  388. private void SetDoubleBufferByIsDraw()
  389. {
  390. SetStyle(ControlStyles.UserPaint, true); //没什么效果,开与关
  391. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景,关了闪
  392. SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲,关了闪
  393. //上面是必须有的
  394. SetStyle(ControlStyles.UserMouse, true); //执行自己的鼠标行为,这个打开后,在win7下鼠标操作明显改善
  395. }
  396. private void OTSMeasureStatusWindow_Load(object sender, EventArgs e)
  397. {
  398. SetDoubleBufferByIsDraw();
  399. this.control_XRayTable1 = new Control_XRayTable();
  400. this.panelXray.Controls.Add(this.control_XRayTable1);
  401. picBox.MouseClick += new MouseEventHandler(HiddenXrayTablePanel_MouseClick);
  402. pictureBox2.MouseClick += new MouseEventHandler(HiddenXrayTablePanel_MouseClick);
  403. this.picBox.MouseUp += new MouseEventHandler(this.pbBSEImage_MouseUp);
  404. this.pictureBox2.MouseUp += new MouseEventHandler(this.pbBSEImage_MouseUp);
  405. this.picBox.MouseWheel += new MouseEventHandler(this.pbBSEImage_MouseWheel);
  406. this.pictureBox2.MouseWheel += new MouseEventHandler(this.pbBSEImage_MouseWheel);
  407. //设置右键菜单中的项 是否可编辑
  408. SetContextMenuEnabled(true);
  409. log = NLog.LogManager.GetCurrentClassLogger();
  410. }
  411. private void HiddenXrayTablePanel_MouseClick(object sender, MouseEventArgs e)
  412. {
  413. if (e.Button != MouseButtons.Right)
  414. {
  415. if (m_ElementPointGDIObjects.Count > 0)
  416. {
  417. foreach (var item in m_ElementPointGDIObjects)
  418. {
  419. if (Math.Abs(e.Y - item.position.Y) < 5 && Math.Abs(e.X - item.position.X) < 5)
  420. {
  421. //显示Xray图像
  422. panelXray.Visible = true;
  423. }
  424. else
  425. {
  426. panelXray.Visible = false;
  427. }
  428. }
  429. }
  430. }
  431. }
  432. #region 设置右键菜单中的项 是否可编辑
  433. protected void SetContextMenuEnabled(bool isEnabled)
  434. {
  435. MenuItemLineScan.Enabled = true;
  436. PointScanElementMenuItem.Enabled = isEnabled;
  437. bool showExprot = m_ElementPointGDIObjects.Count > 0 ? true : false;
  438. ExportScanInfoMenuItem.Enabled = showExprot;
  439. VisualAdjustingMenu.Enabled = isEnabled;
  440. toolStripMenuItem1.Enabled = true;
  441. 批量采集ToolStripMenuItem.Enabled = true;
  442. this.Cursor = Cursors.Default;
  443. this.Validate();
  444. }
  445. #endregion
  446. #region 获取行数据
  447. private void GetLineImage(int iHeight, int iWidth, ref byte[] bdata)
  448. {
  449. Stream stream = null;
  450. stream = new MemoryStream(originalBseData);
  451. if (iHeight >= 0)
  452. {
  453. stream.Seek(iHeight * iWidth, SeekOrigin.Begin);
  454. }
  455. stream.Read(bdata, 0, iWidth);
  456. }
  457. #endregion
  458. private void contextMenuStrip1_Opened(object sender, EventArgs e)
  459. {
  460. if (bfResult)
  461. {
  462. SampleParaLock.Enabled = true;
  463. ToolStripMenuItem.Enabled = true;
  464. toolStripMenuItem2.Enabled = true;
  465. ChangeDiffImageShow.Enabled = true;
  466. PointScanElementMenuItem.Enabled = true;
  467. if (!MenuItemLineScan.Checked)
  468. {
  469. PointScanElementMenuItem.Enabled = true;
  470. ExportScanInfoMenuItem.Enabled = true;
  471. }
  472. else
  473. {
  474. PointScanElementMenuItem.Enabled = false;
  475. ExportScanInfoMenuItem.Enabled = false;
  476. return;
  477. }
  478. PointScanElementMenuItem.Enabled = true;
  479. bool showExprot = m_ElementPointGDIObjects.Count > 0 ? true : false;
  480. if (showExprot)
  481. {
  482. ExportScanInfoMenuItem.Enabled = true;
  483. }
  484. COTSSample WSample = m_MeasureAppForm.m_ProjRstData.GetWorkingSample();
  485. COTSImageProcParam ImgProcPrm = WSample.GetMsrParams().GetImageProcessParam();
  486. if (ImgProcPrm.GetBGRemoveType() == otsdataconst.OTS_BGREMOVE_TYPE.MANUAL)
  487. {
  488. VisualAdjustingMenu.Enabled = true;
  489. }
  490. else
  491. {
  492. VisualAdjustingMenu.Enabled = false;
  493. }
  494. }
  495. else
  496. {
  497. ToolStripMenuItem.Enabled = false;
  498. MenuItemLineScan.Enabled = false;
  499. }
  500. }
  501. //拖动灰度线
  502. #region 拖动灰度线
  503. int LineX = 0;
  504. int LineXEnd = 0;
  505. private Point pointStart = new Point(100, 100);
  506. private Point pointEnd = new Point(400, 100);
  507. //private bool m_bMouseDown = false;
  508. void DrawLineScan(PictureBox pic, Graphics g)
  509. {
  510. Rectangle rectangle = GetPictureBoxImageSize(pic);
  511. if (pointStart.Equals(pointEnd))
  512. {
  513. return;
  514. }
  515. double rate = (double)picBox.Image.Height / (double)rectangle.Height;
  516. //显示线扫描 黄色线的曲线数据
  517. var Imagedata = new byte[m_iWidth];
  518. int YSize = Convert.ToInt32((rate * (double)pointStart.Y));
  519. GetLineImage(YSize, m_iWidth, ref Imagedata);
  520. DrawCurve(Imagedata, g);
  521. g.DrawLine(new Pen(Color.Red), new Point(rectangle.X, rectangle.Y + (rectangle.Height) / 4), new Point(rectangle.X + rectangle.Width, rectangle.Y + (rectangle.Height) / 4));
  522. g.DrawLine(new Pen(Color.Red), new Point(rectangle.X, rectangle.Y + rectangle.Height * 3 / 4), new Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height * 3 / 4));
  523. g.DrawLine(new Pen(Color.Blue), new Point(rectangle.X, pointStart.Y), new Point(rectangle.X + rectangle.Width, pointEnd.Y));
  524. #region 绘制刻度条
  525. float offHeight = (rectangle.Y + (rectangle.Height) / 4) - (rectangle.Y + rectangle.Height * 3 / 4);
  526. float grayValue = Math.Abs(offHeight / 255);
  527. //设置文字对齐方式
  528. StringFormat sf = new StringFormat();
  529. sf.Alignment = StringAlignment.Center;
  530. sf.LineAlignment = StringAlignment.Center;
  531. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(100, Color.Red));
  532. //字体大小 根据样品孔Rectangle大小
  533. float fontSize = 15;
  534. Font font = new Font("黑体", fontSize, FontStyle.Bold);
  535. Pen pen = new Pen(Color.Red, 1);
  536. pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
  537. pen.DashPattern = new float[] { 5, 5 };
  538. int fontOffWidth = 50;
  539. int fontOffHeight = 10;
  540. int scaleLine = 0;
  541. for (int i = 25; i <= 225; i += 25)
  542. {
  543. scaleLine = (int)(grayValue * i);
  544. g.DrawLine(pen, new Point(rectangle.X, (rectangle.Y + rectangle.Height * 3 / 4) - scaleLine), new Point(rectangle.X + fontOffWidth, (rectangle.Y + rectangle.Height * 3 / 4) - scaleLine));
  545. g.DrawString(i.ToString(), font, solidBrush, rectangle.X + 20, (rectangle.Y + rectangle.Height * 3 / 4) - scaleLine - fontOffHeight, sf);
  546. }
  547. #endregion
  548. }
  549. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  550. {
  551. if (!bfResult)
  552. {
  553. return;
  554. }
  555. if (picBox.Image == null)
  556. {
  557. return;
  558. }
  559. var g = e.Graphics;
  560. g.Clear(Color.Gainsboro);
  561. g.DrawImage(picBox.Image, ImgZoomRectangle);
  562. if (m_imagetype == ImageType.GrayLevelLineScan)
  563. {
  564. DrawLineScan(picBox, e.Graphics);
  565. }
  566. else
  567. {
  568. if (m_ElementPointGDIObjects != null)
  569. {
  570. if (m_ElementPointGDIObjects.Count > 0)
  571. {
  572. //绘制采集点标记
  573. foreach (CrossSign item in m_ElementPointGDIObjects)
  574. {
  575. if (item != null)
  576. {
  577. item.OnPaint(e);
  578. }
  579. }
  580. }
  581. }
  582. }
  583. }
  584. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  585. {
  586. if (m_MeasureAppForm.m_MsrThreadWrapper.BGWorkThreadIsRunning())
  587. {
  588. return;
  589. }
  590. //如果pictureBox1中没有图像 则return退出
  591. if (picBox.Image == null)
  592. {
  593. return;
  594. }
  595. if (MoveFlag)
  596. {
  597. ImgZoomRectangle.X = Convert.ToInt32(e.X + xbias);//设置x坐标.
  598. ImgZoomRectangle.Y = Convert.ToInt32(e.Y + ybias);//设置y坐标.
  599. }
  600. var imagepos = GetImagePosByPicBoxPos(e.X, e.Y);
  601. int startimageX = imagepos.X;
  602. int startimageY = imagepos.Y;
  603. //处理灰度值
  604. int grayValue = 0;
  605. if (originalBseData != null)
  606. {
  607. //如果当前鼠标在图像Y轴中
  608. if (startimageX >= 0 && startimageX < m_iWidth && startimageY >= 0 && startimageY < m_iHeight)
  609. {
  610. var binPos = startimageX + (startimageY * m_iWidth);
  611. grayValue = originalBseData[binPos];
  612. }
  613. }
  614. string strGrayValue = " " + grayValue.ToString();
  615. //此处调接口显示灰度值
  616. m_MeasureAppForm.ShowGrayVal(strGrayValue.ToString());
  617. m_MeasureAppForm.ShowSemCoordvAL(imagepos.ToString());
  618. if (m_imagetype != ImageType.GrayLevelLineScan)
  619. {
  620. if (m_ElementPointGDIObjects != null)
  621. {
  622. if (m_ElementPointGDIObjects.Count > 0)
  623. {
  624. foreach (var item in m_ElementPointGDIObjects)
  625. {
  626. Rectangle rect = new Rectangle(e.Location, new Size(1, 1));
  627. if (Math.Abs(e.Y - item.position.Y) < 5 && Math.Abs(e.X - item.position.X) < 5)
  628. {
  629. //设置鼠标样式
  630. this.Cursor = Cursors.Hand;
  631. }
  632. else
  633. {
  634. this.Cursor = Cursors.Default;
  635. }
  636. }
  637. }
  638. }
  639. }
  640. if (e.Button != MouseButtons.Left)
  641. {
  642. return;
  643. }
  644. pointStart = new Point(pointStart.X, e.Y);
  645. pointEnd = new Point(pointEnd.X, e.Y);
  646. picBox.Refresh();
  647. pictureBox2.Refresh();
  648. }
  649. float xbias;
  650. float ybias;
  651. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  652. {
  653. if (!MenuItemLineScan.Checked)
  654. {
  655. xPos = e.X;//当前x坐标.
  656. yPos = e.Y;//当前y坐标.
  657. if (e.Button == MouseButtons.Left)
  658. {
  659. MoveFlag = true;
  660. this.Cursor = Cursors.Hand;
  661. xbias = ImgZoomRectangle.X - xPos;
  662. ybias = ImgZoomRectangle.Y - yPos;
  663. }
  664. }
  665. }
  666. #endregion
  667. private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
  668. {
  669. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  670. {
  671. var picBox = (PictureBox)sender;
  672. int XMenu = picBox.Left + e.Location.X;
  673. int YMenu = picBox.Top + e.Location.Y;
  674. Point mousePoint = new Point(XMenu, YMenu);
  675. contextMenuStrip1.Show(PointToScreen(mousePoint));
  676. SetContentMenu();
  677. }
  678. }
  679. private void ChangeDiffImageShow_Click(object sender, EventArgs e)
  680. {
  681. SetContextMenuEnabled(true);
  682. //显示BSE图
  683. if (originalBseData != null)
  684. {
  685. //用于线扫描曲线
  686. m_imagetype = ImageType.BSEImage;
  687. //在控件中显示图像
  688. Bitmap bitmap = CImageHandler.ToGrayBitmap(originalBseData, m_iWidth, m_iHeight);
  689. ShowSingleImage(bitmap);
  690. }
  691. panelXray.Visible = false;
  692. }
  693. #region 设置测量状态初始化
  694. /// <summary>
  695. /// 设置测量状态初始化
  696. /// </summary>
  697. public void SetMeasureStatusInit()
  698. {
  699. picBox.Hide();
  700. foreach (ToolStripItem item in contextMenuStrip1.Items)
  701. {
  702. if (item != contextMenuStrip1.Items[(int)MeasureStateMenuType.ReadBSEPic])
  703. {
  704. item.Enabled = false;
  705. }
  706. }
  707. }
  708. #endregion
  709. #region 设置右键菜单项是否可用
  710. /// <summary>
  711. /// 设置右键菜单项是否可用
  712. /// </summary>
  713. public void SetContentMenu()
  714. {
  715. int sampleCount = m_MeasureAppForm.m_ProjParam.GetSampleCount();
  716. if (sampleCount == 0)
  717. {
  718. foreach (ToolStripItem item in contextMenuStrip1.Items)
  719. {
  720. item.Enabled = false;
  721. }
  722. }
  723. else
  724. {
  725. if (m_MeasureAppForm.MeasureThreadRunFlag)
  726. {
  727. if (m_MeasureAppForm.m_MsrThreadWrapper.BgWorkIsPaused())
  728. {
  729. foreach (ToolStripItem item in contextMenuStrip1.Items)
  730. {
  731. item.Enabled = true;
  732. }
  733. }
  734. else
  735. {
  736. foreach (ToolStripItem item in contextMenuStrip1.Items)
  737. {
  738. item.Enabled = false;
  739. }
  740. }
  741. }
  742. double iWDistance = 0;
  743. double dMagni = 0;
  744. m_MeasureAppForm.m_ProjParam.GetWorkingSampleSEMData(ref iWDistance, ref dMagni);
  745. if (iWDistance != 0 && dMagni != 0)
  746. {
  747. contextMenuStrip1.Items[(int)MeasureStateMenuType.ReadBSEPic].Enabled = true;
  748. }
  749. }
  750. }
  751. #endregion
  752. private Point GetImagePosByPicBoxPos(int x, int y)
  753. {
  754. var rectangle = ImgZoomRectangle;
  755. double ratex = x - rectangle.X;
  756. var imageX = ratex / rectangle.Width * m_iWidth;
  757. double ratey = y - rectangle.Y;
  758. var imageY = ratey / rectangle.Height * m_iHeight;
  759. return new Point((int)imageX, (int)imageY);
  760. }
  761. private void 单点采集MenuItem_Click(object sender, EventArgs e)
  762. {
  763. MenuItemLineScan.Checked = false;
  764. //电镜设置对象
  765. //控制类对象初始化
  766. var expC = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().GetAnalyExpCount();
  767. var ifautoid = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().IfAutoId;
  768. var knownelements = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().AnalysisElements;
  769. IEDSController m_EDSHardwareMgr = EDSController.GetEDSController(m_iWidth, m_iHeight, expC, ifautoid, knownelements);
  770. //1.连接电镜
  771. if (m_EDSHardwareMgr.Connect())
  772. {
  773. COTSMsrPrjResultData cProjMgr = m_MeasureAppForm.m_ProjParam.GetResultData();
  774. //获取工作样品对象
  775. COTSSample sampleClr = cProjMgr.GetWorkingSample();
  776. //清空之前记录的标识
  777. if (m_ElementPointGDIObjects != null)
  778. {
  779. if (m_ElementPointGDIObjects.Count > 0)
  780. {
  781. //清空
  782. m_ElementPointGDIObjects.Clear();
  783. }
  784. }
  785. var mousepos = GetImagePosByPicBoxPos(xPos, yPos);
  786. //3.根据所要扫描的位置进行元素分析
  787. uint a_Milliseconds = 0;
  788. //采集时间
  789. a_Milliseconds = Convert.ToUInt32(sampleClr.GetMsrParams().GetXRayParam().GetMidAnalyAQTime());
  790. string classifyName = "";
  791. //元素名称
  792. string a_strElementName = "";
  793. string classifyColor = "";
  794. //元素Xray数据
  795. uint[] a_ElementXrayData;
  796. COTSParticleClr selpart = null;
  797. var mode = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().GetScanMode();
  798. GetSelectedParticle((int)mousepos.X, (int)mousepos.Y, ref selpart);
  799. if (selpart == null)
  800. {
  801. var part = new COTSParticleClr();
  802. part.SetXRayPos(new Point(mousepos.X, mousepos.Y));
  803. var xray = part.GetXray();
  804. xray.SetIndex(0);
  805. part.SetXray(xray);
  806. selpart = part;
  807. if (mode != otsdataconst.OTS_X_RAY_SCAN_MODE.PointMode)
  808. {
  809. log.Warn("There's no particle to be found at this position,only point mode xray collection can be done!");
  810. mode = otsdataconst.OTS_X_RAY_SCAN_MODE.PointMode;
  811. }
  812. }
  813. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  814. parts.Add(selpart);
  815. if (GetXRayAndElements(a_Milliseconds, parts, mode))
  816. {
  817. log.Trace("--获取单点扫描数据:" + "元素名称:" + a_strElementName + "'--");
  818. a_ElementXrayData = selpart.GetXray().GetXrayData();
  819. a_strElementName = selpart.GetXray().GetQuantifiedElementsStr();
  820. classifyName = selpart.GetTypeName();
  821. classifyColor = selpart.GetTypeColor();
  822. Color colorValue = ColorTranslator.FromHtml(classifyColor);
  823. CrossSign theSign = new CrossSign(mousepos.X, mousepos.Y, this);
  824. m_ElementPointGDIObjects.Add(theSign);
  825. //5.获取成功后弹出对话框
  826. //计数率
  827. double electronicCount = 0;
  828. uint[] u_ElementXrayData = new uint[2000];
  829. for (int i = 0; i < a_ElementXrayData.Length; i++)
  830. {
  831. u_ElementXrayData[i] = (uint)a_ElementXrayData[i];
  832. //累加计数率
  833. electronicCount += a_ElementXrayData[i];
  834. }
  835. //显示Xray控件
  836. panelXray.Visible = true;
  837. //获取数据后,需要对xraytable设置
  838. string[] strGroup = a_strElementName.Split('\n');
  839. //get CElementChemistryClr list
  840. List<ShowElementInfo> list_showelementinfo = new List<ShowElementInfo>();
  841. for (int i = 0; i < strGroup.Length; i++)
  842. {
  843. if (!strGroup[i].Equals(""))
  844. {
  845. ShowElementInfo ls_sei = new ShowElementInfo();
  846. ls_sei.ElementName = strGroup[i].Split(':')[0];
  847. ls_sei.Percentage = Convert.ToDouble(strGroup[i].Split(':')[1]);
  848. ls_sei.dKF = Convert.ToDouble(CListPeriodic.GetPeriodicByEleName(ls_sei.ElementName).K_Peak);
  849. double de_sx2 = 0;
  850. if (CListPeriodic.GetPeriodicByEleName(ls_sei.ElementName).L_Peak == "" || CListPeriodic.GetPeriodicByEleName(ls_sei.ElementName).L_Peak == "-")
  851. {
  852. de_sx2 = 0;
  853. }
  854. else
  855. {
  856. de_sx2 = Convert.ToDouble(CListPeriodic.GetPeriodicByEleName(ls_sei.ElementName).L_Peak);
  857. }
  858. ls_sei.dLF = de_sx2;
  859. list_showelementinfo.Add(ls_sei);
  860. }
  861. }
  862. control_XRayTable1.List_ShowElementInfo = list_showelementinfo;
  863. control_XRayTable1.GoodName = classifyName;
  864. //设置分析Xray数据
  865. control_XRayTable1.SetXRayShowLineValue(new uint[2000], u_ElementXrayData, list_showelementinfo);
  866. //需要显示计数率
  867. string strElectronicCount = string.Empty;
  868. if (electronicCount > 1000)
  869. {
  870. strElectronicCount = (Convert.ToDouble(electronicCount) / 1000).ToString("f2") + "kcps";
  871. }
  872. }
  873. var img = myFld.GetAnalysisParticleSTDColoredImage();
  874. pictureBox2.Image = img;
  875. ExportScanInfoMenuItem.Enabled = true;
  876. this.Refresh();
  877. }
  878. }
  879. public bool GetSelectedParticle(int dMouseImgX, int dMouseImgY, ref COTSParticleClr selectedPart)
  880. {
  881. var fldParts = myFld.GetListAnalysisParticles();
  882. var partPos = new Point(dMouseImgX, dMouseImgY);
  883. selectedPart = null;
  884. foreach (var p in fldParts)
  885. {
  886. Rectangle r = (Rectangle)p.GetParticleRect();
  887. if (r.Contains(partPos))
  888. {
  889. selectedPart = p;
  890. break;
  891. }
  892. }
  893. if (selectedPart == null)
  894. {
  895. return false;
  896. }
  897. else
  898. {
  899. return true;
  900. }
  901. }
  902. public bool GetXRayAndElements(uint a_nXRayAQTime, List<COTSParticleClr> selectedParts, otsdataconst.OTS_X_RAY_SCAN_MODE mode)
  903. {
  904. var expC = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().GetAnalyExpCount();
  905. var ifautoid = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().IfAutoId;
  906. var knownelements = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().AnalysisElements;
  907. var eds = EDSController.GetEDSController(m_iWidth, m_iHeight, expC, ifautoid, knownelements);
  908. //var parts = new List<COTSParticleClr>();
  909. //parts.Add(selectedPart);
  910. if (mode == otsdataconst.OTS_X_RAY_SCAN_MODE.PointMode)
  911. {
  912. eds.GetXRayByParts(selectedParts, a_nXRayAQTime, true);
  913. }
  914. else if (mode == otsdataconst.OTS_X_RAY_SCAN_MODE.FeatureMode)
  915. {
  916. eds.GetXRayByFeatures(selectedParts, a_nXRayAQTime, true);
  917. }
  918. else if (mode == otsdataconst.OTS_X_RAY_SCAN_MODE.ExpandMode)
  919. {
  920. eds.GetXRayByExpandFeatures(selectedParts, a_nXRayAQTime, true);
  921. }
  922. ClassifyParticles(selectedParts);
  923. return true;
  924. }
  925. void ClassifyParticles(List<COTSParticleClr> parts)
  926. {
  927. if (m_MeasureAppForm.m_ProjRstData.SystemTypeId == otsdataconst.OTS_SysType_ID.IncA)
  928. {
  929. var smplmeasure = new CSmplMeasureIncA("", m_MeasureAppForm.m_ProjRstData.GetWorkingSample());
  930. foreach (var p in parts)
  931. {
  932. smplmeasure.ClassifyIncAParticle(p, m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetSTDName());
  933. }
  934. }
  935. else
  936. {
  937. var smplmeasure = new CSmplMeasureCleanliness("", m_MeasureAppForm.m_ProjRstData.GetWorkingSample());
  938. smplmeasure.ClassifyParticles(parts, m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetSTDName());
  939. }
  940. }
  941. public void DelElementPointGDIObjects()
  942. {
  943. if (m_ElementPointGDIObjects != null)
  944. {
  945. if (m_ElementPointGDIObjects.Count > 0)
  946. {
  947. //删除点标记
  948. m_ElementPointGDIObjects.Clear();
  949. this.Invalidate();
  950. }
  951. }
  952. }
  953. private void ExportScanInfoMenuItem_Click(object sender, EventArgs e)
  954. {
  955. //导出图片到报告中
  956. ExportReport();
  957. }
  958. /// <summary>
  959. /// 导出Image到报告
  960. /// </summary>
  961. private bool ExportReport()
  962. {
  963. if (m_ElementPointGDIObjects.Count == 0)
  964. {
  965. return false;
  966. }
  967. string startupPath = System.Windows.Forms.Application.StartupPath + "\\TempReport\\";
  968. if (!Directory.Exists(startupPath))
  969. {
  970. Directory.CreateDirectory(startupPath);
  971. }
  972. //保存Xray图片
  973. string ImgFilePath = startupPath + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
  974. Bitmap bseBM = new Bitmap(this.picBox.Width, this.picBox.Height);
  975. this.picBox.DrawToBitmap(bseBM, new Rectangle(0, 0, this.picBox.Width, this.picBox.Height));
  976. //保存图片
  977. bseBM.Save(ImgFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  978. //释放资源
  979. bseBM.Dispose();
  980. //保存Xray图片
  981. string ImgXrayFilePath = startupPath + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
  982. int xrayWidth = control_XRayTable1.Width;
  983. int xrayHeight = control_XRayTable1.Height;
  984. Bitmap xrayBM = new Bitmap(xrayWidth, xrayHeight);
  985. this.control_XRayTable1.DrawToBitmap(xrayBM, new Rectangle(0, 0, xrayWidth, xrayHeight));
  986. //保存图片
  987. xrayBM.Save(ImgXrayFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  988. //释放资源
  989. xrayBM.Dispose();
  990. List<string> listFilePath = new List<string>();
  991. listFilePath.Add(ImgFilePath);
  992. listFilePath.Add(ImgXrayFilePath);
  993. List<ShowElementInfo> list_ElementInfo = control_XRayTable1.List_ShowElementInfo;
  994. //1.编辑信息
  995. DataTable dt = new DataTable();
  996. dt.Columns.Add("元素名称");
  997. dt.Columns.Add("原子数");
  998. dt.Columns.Add("质量百分比");
  999. dt.Columns.Add("摩尔比");
  1000. foreach (var item in list_ElementInfo)
  1001. {
  1002. //获取摩尔比与原子数
  1003. CElementChemistry cElementChemistry = new CElementChemistry();
  1004. cElementChemistry.SetName(item.ElementName);
  1005. cElementChemistry.SetPercentage(item.Percentage);
  1006. double molarValue = Math.Round(cElementChemistry.GetMolarPercentage(), 2);
  1007. int atomNum = (int)CElement.GetAtomicNum(item.ElementName);
  1008. DataRow dr = dt.NewRow();
  1009. dr[0] = item.ElementName;
  1010. dr[1] = atomNum;
  1011. dr[2] = item.Percentage;
  1012. dr[3] = molarValue;
  1013. dt.Rows.Add(dr);
  1014. }
  1015. DataGridView dgv = new DataGridView();
  1016. dgv.DataSource = dt.DefaultView;
  1017. List<DataTable> list_ElementData = new List<DataTable>();
  1018. list_ElementData.Add(dt);
  1019. //将数据插入Excel中
  1020. return InsertDataToExcelTable(list_ElementData, startupPath, listFilePath);
  1021. }
  1022. public bool InsertDataToExcelTable(List<System.Data.DataTable> a_list_ElementData, string a_filePath, List<string> a_ImgFilePath)
  1023. {
  1024. var m_xe = new ExcelEdit();
  1025. m_xe.Create();
  1026. Microsoft.Office.Interop.Excel.Worksheet ws = m_xe.GetSheet("Sheet1");
  1027. m_xe.m_ws = ws;
  1028. string str_ExcelFilePath = a_filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
  1029. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[0], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 400, 300);
  1030. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 320, 400, 100);
  1031. if (str_ExcelFilePath.IndexOf(":") < 0)
  1032. {
  1033. return false;
  1034. }
  1035. if (m_xe.m_app == null)
  1036. {
  1037. return false;
  1038. }
  1039. //数据列表 行数
  1040. int dataRow = 30;
  1041. //写入标题
  1042. foreach (var dgv in a_list_ElementData)
  1043. {
  1044. for (int i = 0; i < dgv.Columns.Count; i++)
  1045. {
  1046. m_xe.m_ws.Cells[dataRow + 1, i + 1] = dgv.Columns[i].ColumnName;
  1047. }
  1048. //写入数值
  1049. for (int r = 0; r < dgv.Rows.Count; r++)
  1050. {
  1051. for (int i = 0; i < dgv.Columns.Count; i++)
  1052. {
  1053. m_xe.m_ws.Cells[dataRow + r + 2, i + 1] = dgv.Rows[r].ItemArray[i];
  1054. }
  1055. System.Windows.Forms.Application.DoEvents();
  1056. }
  1057. }
  1058. //列宽自适应
  1059. m_xe.m_ws.Columns.EntireColumn.AutoFit();
  1060. if (str_ExcelFilePath != "")
  1061. {
  1062. try
  1063. {
  1064. m_xe.m_wb.Saved = true;
  1065. m_xe.m_wb.SaveCopyAs(str_ExcelFilePath);
  1066. }
  1067. catch (Exception ex)
  1068. {
  1069. log.Error(ex.ToString());
  1070. }
  1071. }
  1072. //关闭excel
  1073. m_xe.Close();
  1074. //强行销毁
  1075. GC.Collect();
  1076. //干掉进程里的EXCEL
  1077. foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
  1078. {
  1079. if (p.ProcessName == "EXCEL")
  1080. {
  1081. p.Kill();
  1082. }
  1083. }
  1084. //删除图片文件
  1085. foreach (string imgPath in a_ImgFilePath)
  1086. {
  1087. if (File.Exists(imgPath))
  1088. {
  1089. //如果存在则删除
  1090. File.Delete(imgPath);
  1091. }
  1092. }
  1093. System.Diagnostics.Process.Start(str_ExcelFilePath);
  1094. return true;
  1095. }
  1096. public void HideXRayTable()
  1097. {
  1098. panelXray.Visible = false;
  1099. //清空元素标记对象
  1100. if (m_ElementPointGDIObjects != null)
  1101. {
  1102. if (m_ElementPointGDIObjects.Count > 0)
  1103. {
  1104. m_ElementPointGDIObjects.Clear();
  1105. }
  1106. }
  1107. }
  1108. //在picturebox的鼠标按下事件里,记录三个变量.
  1109. int xPos;
  1110. int yPos;
  1111. bool MoveFlag = false;
  1112. //在picturebox的鼠标按下事件里.
  1113. private void pbBSEImage_MouseUp(object sender, MouseEventArgs e)
  1114. {
  1115. MoveFlag = false;
  1116. this.Cursor = Cursors.Default;
  1117. picBox.Refresh();
  1118. }
  1119. //在picturebox鼠标缩放
  1120. private void pbBSEImage_MouseWheel(object sender, MouseEventArgs e)
  1121. {
  1122. if (MenuItemLineScan.Checked)
  1123. {
  1124. return;
  1125. }
  1126. if (m_MeasureAppForm.m_MsrThreadWrapper.BGWorkThreadIsRunning())
  1127. {
  1128. return;
  1129. }
  1130. var refPoint = e.Location;
  1131. float newZoom;
  1132. float zoomDelta;
  1133. //鼠标滚动缩放 BSE图
  1134. if (e.Delta > 0)
  1135. {
  1136. newZoom = m_ZoomRecord + m_ZoomCoefficient;
  1137. }
  1138. else
  1139. {
  1140. newZoom = m_ZoomRecord - m_ZoomCoefficient;
  1141. }
  1142. if (newZoom > m_MaxZoom)
  1143. {
  1144. newZoom = m_MaxZoom;
  1145. }
  1146. if (newZoom < 1)
  1147. {
  1148. newZoom = 1;
  1149. }
  1150. zoomDelta = newZoom - m_ZoomRecord;
  1151. if (newZoom == 1)
  1152. {
  1153. ImgZoomRectangle.Width = originalImgRectangle.Width;
  1154. ImgZoomRectangle.Height = originalImgRectangle.Height;
  1155. ImgZoomRectangle.X = originalImgRectangle.X;
  1156. ImgZoomRectangle.Y = originalImgRectangle.Y;
  1157. }
  1158. else
  1159. {
  1160. float xShiftOld = (refPoint.X - ImgZoomRectangle.X);
  1161. float yShiftOld = (refPoint.Y - ImgZoomRectangle.Y);
  1162. float xShift = xShiftOld * (1 + 1 / m_ZoomRecord * zoomDelta);
  1163. float yShift = yShiftOld * (1 + 1 / m_ZoomRecord * zoomDelta);
  1164. ImgZoomRectangle.Width += Convert.ToInt32(ImgZoomRectangle.Width / m_ZoomRecord * zoomDelta);
  1165. ImgZoomRectangle.Height += Convert.ToInt32(ImgZoomRectangle.Height / m_ZoomRecord * zoomDelta);
  1166. ImgZoomRectangle.X = (refPoint.X - xShift);
  1167. ImgZoomRectangle.Y = (refPoint.Y - yShift);
  1168. }
  1169. m_ZoomRecord = newZoom;
  1170. picBox.Invalidate();
  1171. pictureBox2.Invalidate();
  1172. }
  1173. private void OTSMeasureStatusWindow_MouseClick(object sender, MouseEventArgs e)
  1174. {
  1175. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  1176. {
  1177. contextMenuStrip1.Show(PointToScreen(e.Location));
  1178. SetContentMenu();
  1179. }
  1180. }
  1181. private void ViewStripMenuItem_Click(object sender, EventArgs e)
  1182. {
  1183. ToolWindow toolWindow = new ToolWindow(m_MeasureAppForm, this);
  1184. int grayStart = 0;
  1185. int grayEnd = 0;
  1186. if (originalBse != null)
  1187. {
  1188. Bitmap bitmap = CImageHandler.ToGrayBitmap(originalBseData, m_iWidth, m_iHeight);
  1189. toolWindow.BseImg = bitmap;
  1190. toolWindow.OriginalBseData = originalBseData;
  1191. //获取去背景灰度值
  1192. grayStart = m_MeasureAppForm.m_ProjParam.GetWorkSample().GetMsrParams().GetImageProcessParam().GetBGGray().GetStart();
  1193. grayEnd = m_MeasureAppForm.m_ProjParam.GetWorkSample().GetMsrParams().GetImageProcessParam().GetBGGray().GetEnd();
  1194. //设置可视化中的属性
  1195. toolWindow.BseGrayMinValue = grayStart;
  1196. toolWindow.BseGrayMaxValue = grayEnd;
  1197. }
  1198. DialogResult dialogResult = toolWindow.ShowDialog();
  1199. if (dialogResult == DialogResult.Yes)
  1200. {
  1201. //获取去背景参数
  1202. grayStart = toolWindow.BseGrayMinValue;
  1203. grayEnd = toolWindow.BseGrayMaxValue;
  1204. //设置去背景参数
  1205. COTSImageProcParam cOTSImgProc;
  1206. cOTSImgProc = m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().GetImageProcessParam();
  1207. CIntRange cIntRange = new CIntRange();
  1208. cIntRange.SetStart(grayStart);
  1209. cIntRange.SetEnd(grayEnd);
  1210. cOTSImgProc.SetBGGray(cIntRange);
  1211. m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().SetImageProcessParam(cOTSImgProc);
  1212. //更新属性窗口
  1213. m_MeasureAppForm.UpdatePropertyVal();
  1214. //确认后进行去背景
  1215. var m_systype = m_MeasureAppForm.m_ProjParam.GetResultData().SystemTypeId;
  1216. if (m_systype != otsdataconst.OTS_SysType_ID.IncA)
  1217. {
  1218. ShowColoredRemoveBGImage();
  1219. }
  1220. else
  1221. {
  1222. ShowRemoveBGImage();
  1223. }
  1224. }
  1225. }
  1226. private void toolStripMenuItem1_Click(object sender, EventArgs e)
  1227. {
  1228. SpecialParticleForm specialParticleForm = new SpecialParticleForm(CSpecialGrayRangeParam.GetParamFileFullName(), m_MeasureAppForm, originalBseData, m_iWidth, m_iHeight, this);
  1229. DialogResult dialogResult = specialParticleForm.ShowDialog();
  1230. }
  1231. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  1232. {
  1233. //设置右键菜单中的项 是否可编辑
  1234. SetContextMenuEnabled(true);
  1235. m_imagetype = ImageType.RemoveBGImage;
  1236. ShowColoredRemoveBGImage();
  1237. //m_CurrentMultiple = 0;
  1238. panelXray.Visible = false;
  1239. }
  1240. private void timer1_Tick(object sender, EventArgs e)
  1241. {
  1242. if (!backgroundWorker1.IsBusy)
  1243. {
  1244. backgroundWorker1.RunWorkerAsync();
  1245. }
  1246. }
  1247. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  1248. {
  1249. //更新当前的图
  1250. log.Info("begin to acquire image");
  1251. bool rst = AcquireBSEImage();
  1252. if (m_imagetype == ImageType.GrayLevelLineScan)
  1253. {
  1254. log.Info("begin to show image");
  1255. UpdateImageAndLineScanChart();
  1256. }
  1257. }
  1258. private void pictureBox2_Paint(object sender, PaintEventArgs e)
  1259. {
  1260. if (pictureBox2.Image != null)
  1261. {
  1262. var g = e.Graphics;
  1263. g.Clear(Color.Gainsboro);
  1264. g.DrawImage(pictureBox2.Image, ImgZoomRectangle);
  1265. if (m_ElementPointGDIObjects != null)
  1266. {
  1267. if (m_ElementPointGDIObjects.Count > 0)
  1268. {
  1269. //绘制采集点标记
  1270. foreach (CrossSign item in m_ElementPointGDIObjects)
  1271. {
  1272. if (item != null)
  1273. {
  1274. item.OnPaint(e);
  1275. }
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. private void OTSMeasureStatusWindow_Activated(object sender, EventArgs e)
  1282. {
  1283. //获取电镜中图像大小
  1284. if (m_MeasureAppForm.m_ProjParam.GetWorkSample() != null)
  1285. {
  1286. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();
  1287. string[] sArray = str.Split('X');
  1288. if (sArray[0] != "" && sArray[1] != "")
  1289. {
  1290. m_iWidth = Convert.ToInt32(sArray[0]);
  1291. m_iHeight = Convert.ToInt32(sArray[1]);
  1292. }
  1293. }
  1294. }
  1295. private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
  1296. {
  1297. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  1298. {
  1299. int XMenu = pictureBox2.Left + e.Location.X;
  1300. int YMenu = pictureBox2.Top + e.Location.Y;
  1301. Point mousePoint = new Point(XMenu, YMenu);
  1302. contextMenuStrip1.Show(PointToScreen(mousePoint));
  1303. SetContentMenu();
  1304. }
  1305. }
  1306. private void 批量采集ToolStripMenuItem_Click(object sender, EventArgs e)
  1307. {
  1308. var expC = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().GetAnalyExpCount();
  1309. var ifautoid = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().IfAutoId;
  1310. var knownelements = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().AnalysisElements;
  1311. IEDSController m_EDSHardwareMgr = EDSController.GetEDSController(m_iWidth, m_iHeight, expC, ifautoid, knownelements);
  1312. var mode = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().GetScanMode();
  1313. if (m_EDSHardwareMgr.Connect())
  1314. {
  1315. COTSMsrPrjResultData cProjMgr = m_MeasureAppForm.m_ProjParam.GetResultData();
  1316. //获取工作样品对象
  1317. COTSSample sampleClr = cProjMgr.GetWorkingSample();
  1318. //清空之前记录的标识
  1319. if (m_ElementPointGDIObjects != null)
  1320. {
  1321. if (m_ElementPointGDIObjects.Count > 0)
  1322. {
  1323. //清空
  1324. m_ElementPointGDIObjects.Clear();
  1325. }
  1326. }
  1327. //3.根据所要扫描的位置进行元素分析
  1328. uint a_Milliseconds = 0;
  1329. //采集时间
  1330. a_Milliseconds = Convert.ToUInt32(sampleClr.GetMsrParams().GetXRayParam().GetMidAnalyAQTime());
  1331. var parts = myFld.GetListAnalysisParticles();
  1332. GetXRayAndElements(a_Milliseconds, parts, mode);
  1333. var colorImg = myFld.GetAnalysisParticleSTDColoredImage();
  1334. pictureBox2.Image = colorImg;
  1335. ExportScanInfoMenuItem.Enabled = true;
  1336. this.Refresh();
  1337. }
  1338. }
  1339. }
  1340. }