OTSMeasureStatusWindow.cs 59 KB

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