OTSMeasureStatusWindow.cs 57 KB

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