OTSMeasureStatusWindow.cs 60 KB

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