OTSMeasureStatusWindow.cs 61 KB

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