OTSMeasureStatusWindow.cs 63 KB

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