OTSMeasureStatusWindow.cs 58 KB

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