OTSMeasureStatusWindow.cs 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  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. foreach (int i in RulerVal)
  1230. {
  1231. double obj = Convert.ToInt32(control_Ruler.RulerWidth * i / 100);
  1232. if (obj > 75)
  1233. {
  1234. control_Ruler.RulerWidth = (int)obj;
  1235. control_Ruler.Value = i;
  1236. break;
  1237. }
  1238. }
  1239. control_Ruler.SetValue(control_Ruler.RulerWidth, control_Ruler.Value.ToString("0") + "um");
  1240. picBox.Invalidate();
  1241. pictureBox2.Invalidate();
  1242. }
  1243. private void OTSMeasureStatusWindow_MouseClick(object sender, MouseEventArgs e)
  1244. {
  1245. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  1246. {
  1247. contextMenuStrip1.Show(PointToScreen(e.Location));
  1248. SetContentMenu();
  1249. }
  1250. }
  1251. private void ViewStripMenuItem_Click(object sender, EventArgs e)
  1252. {
  1253. ToolWindow toolWindow = new ToolWindow(m_MeasureAppForm, this);
  1254. int grayStart = 0;
  1255. int grayEnd = 0;
  1256. if (originalBse != null)
  1257. {
  1258. Bitmap bitmap = CImageHandler.ToGrayBitmap(originalBseData, GetIWidth(), GetIHeight());
  1259. toolWindow.BseImg = bitmap;
  1260. toolWindow.OriginalBseData = originalBseData;
  1261. //获取去背景灰度值
  1262. grayStart = m_MeasureAppForm.m_ProjParam.GetWorkSample().GetMsrParams().GetImageProcessParam().GetBGGray().GetStart();
  1263. grayEnd = m_MeasureAppForm.m_ProjParam.GetWorkSample().GetMsrParams().GetImageProcessParam().GetBGGray().GetEnd();
  1264. //设置可视化中的属性
  1265. toolWindow.BseGrayMinValue = grayStart;
  1266. toolWindow.BseGrayMaxValue = grayEnd;
  1267. }
  1268. DialogResult dialogResult = toolWindow.ShowDialog();
  1269. if (dialogResult == DialogResult.Yes)
  1270. {
  1271. //获取去背景参数
  1272. grayStart = toolWindow.BseGrayMinValue;
  1273. grayEnd = toolWindow.BseGrayMaxValue;
  1274. //设置去背景参数
  1275. COTSImageProcParam cOTSImgProc;
  1276. cOTSImgProc = m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().GetImageProcessParam();
  1277. CIntRange cIntRange = new CIntRange();
  1278. cIntRange.SetStart(grayStart);
  1279. cIntRange.SetEnd(grayEnd);
  1280. cOTSImgProc.SetBGGray(cIntRange);
  1281. m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().CopyImageProcessParam(cOTSImgProc);
  1282. //更新属性窗口
  1283. m_MeasureAppForm.UpdatePropertyVal();
  1284. //确认后进行去背景
  1285. var m_systype = m_MeasureAppForm.m_ProjParam.GetResultData().SystemTypeId;
  1286. ShowColoredRemoveBGImage();
  1287. }
  1288. }
  1289. private void toolStripMenuItem1_Click(object sender, EventArgs e)
  1290. {
  1291. COTSSample WSample = m_MeasureAppForm.m_ProjRstData.GetWorkingSample();
  1292. SpecialParticleForm specialParticleForm = new SpecialParticleForm( m_MeasureAppForm, originalBseData, GetIWidth(), GetIHeight(), WSample);
  1293. DialogResult dialogResult = specialParticleForm.ShowDialog();
  1294. }
  1295. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  1296. {
  1297. //设置右键菜单中的项 是否可编辑
  1298. SetContextMenuEnabled(true);
  1299. m_imagetype = ImageType.RemoveBGImage;
  1300. ShowColoredRemoveBGImage();
  1301. //m_CurrentMultiple = 0;
  1302. panelXray.Visible = false;
  1303. }
  1304. private void timer1_Tick(object sender, EventArgs e)
  1305. {
  1306. if (!backgroundWorker1.IsBusy)
  1307. {
  1308. backgroundWorker1.RunWorkerAsync();
  1309. }
  1310. }
  1311. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  1312. {
  1313. //更新当前的图
  1314. log.Info("begin to acquire image");
  1315. bool rst = AcquireBSEImage();
  1316. if (m_imagetype == ImageType.GrayLevelLineScan)
  1317. {
  1318. log.Info("begin to show image");
  1319. UpdateImageAndLineScanChart();
  1320. }
  1321. }
  1322. private void pictureBox2_Paint(object sender, PaintEventArgs e)
  1323. {
  1324. if (pictureBox2.Image != null)
  1325. {
  1326. var g = e.Graphics;
  1327. g.Clear(Color.Gainsboro);
  1328. g.DrawImage(pictureBox2.Image, ImgZoomRectangle);
  1329. if (m_ElementPointGDIObjects != null)
  1330. {
  1331. if (m_ElementPointGDIObjects.Count > 0)
  1332. {
  1333. //绘制采集点标记
  1334. foreach (CrossSign item in m_ElementPointGDIObjects)
  1335. {
  1336. if (item != null)
  1337. {
  1338. item.OnPaint(e);
  1339. }
  1340. }
  1341. }
  1342. }
  1343. }
  1344. }
  1345. private void OTSMeasureStatusWindow_Activated(object sender, EventArgs e)
  1346. {
  1347. //获取电镜中图像大小
  1348. if (m_MeasureAppForm.m_ProjParam.GetWorkSample() != null)
  1349. {
  1350. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();
  1351. string[] sArray = str.Split('X');
  1352. if (sArray[0] != "" && sArray[1] != "")
  1353. {
  1354. SetIWidth(Convert.ToInt32(sArray[0]));
  1355. SetIHeight(Convert.ToInt32(sArray[1]));
  1356. }
  1357. }
  1358. }
  1359. private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
  1360. {
  1361. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  1362. {
  1363. int XMenu = pictureBox2.Left + e.Location.X;
  1364. int YMenu = pictureBox2.Top + e.Location.Y;
  1365. Point mousePoint = new Point(XMenu, YMenu);
  1366. contextMenuStrip1.Show(PointToScreen(mousePoint));
  1367. SetContentMenu();
  1368. }
  1369. }
  1370. private void 批量采集ToolStripMenuItem_Click(object sender, EventArgs e)
  1371. {
  1372. var expC = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().GetAnalyExpCount();
  1373. var ifautoid = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().IfAutoId;
  1374. var knownelements = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().AnalysisElements;
  1375. IEDSController m_EDSHardwareMgr = EDSController.GetEDSController(GetIWidth(), GetIHeight(), expC, ifautoid, knownelements);
  1376. var mode = m_MeasureAppForm.m_ProjRstData.GetWorkingSample().GetMsrParams().GetXRayParam().GetScanMode();
  1377. if (m_EDSHardwareMgr.Connect())
  1378. {
  1379. COTSMsrPrjResultData cProjMgr = m_MeasureAppForm.m_ProjParam.GetResultData();
  1380. //获取工作样品对象
  1381. COTSSample sampleClr = cProjMgr.GetWorkingSample();
  1382. //清空之前记录的标识
  1383. if (m_ElementPointGDIObjects != null)
  1384. {
  1385. if (m_ElementPointGDIObjects.Count > 0)
  1386. {
  1387. //清空
  1388. m_ElementPointGDIObjects.Clear();
  1389. }
  1390. }
  1391. //3.根据所要扫描的位置进行元素分析
  1392. uint a_Milliseconds = 0;
  1393. //采集时间
  1394. a_Milliseconds = Convert.ToUInt32(sampleClr.GetMsrParams().GetXRayParam().GetMidAnalyAQTime());
  1395. var parts = myFld.GetListAnalysisParticles();
  1396. if (GetXRayAndElements(a_Milliseconds, parts, mode))
  1397. {
  1398. var colorImg = myFld.GetAnalysisParticleSTDColoredImage();
  1399. pictureBox2.Image = colorImg;
  1400. ExportScanInfoMenuItem.Enabled = true;
  1401. this.Refresh();
  1402. }
  1403. else
  1404. {
  1405. log.Error("get xray failed");
  1406. }
  1407. }
  1408. }
  1409. #region 显示标尺
  1410. int[] RulerVal = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };
  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. foreach (int i in RulerVal)
  1418. {
  1419. double obj = Convert.ToInt32(f_ruler_size * i / 100);
  1420. if (obj + 20 >75)
  1421. {
  1422. control_Ruler.RulerWidth = (int)obj;
  1423. control_Ruler.Value = i;
  1424. break;
  1425. }
  1426. }
  1427. control_Ruler.SetValue(control_Ruler.RulerWidth, control_Ruler.Value.ToString()+ "um");
  1428. }
  1429. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  1430. {
  1431. }
  1432. public static Rectangle GetZoomedImageRect(PictureBox pbox)
  1433. {
  1434. if (pbox.Image == null) return Rectangle.Empty;
  1435. Size imgSize = pbox.Image.Size;
  1436. Size boxSize = pbox.ClientSize;
  1437. if (pbox.SizeMode != PictureBoxSizeMode.Zoom)
  1438. return new Rectangle(Point.Empty, imgSize);
  1439. float imgRatio = (float)imgSize.Width / imgSize.Height;
  1440. float boxRatio = (float)boxSize.Width / boxSize.Height;
  1441. int width, height;
  1442. if (boxRatio > imgRatio)
  1443. {
  1444. height = boxSize.Height; width = (int)(imgSize.Width * height / imgSize.Height);
  1445. }
  1446. else
  1447. {
  1448. width = boxSize.Width; height = (int)(imgSize.Height * width / imgSize.Width);
  1449. }
  1450. int x = (boxSize.Width - width) / 2;
  1451. int y = (boxSize.Height - height) / 2;
  1452. return new Rectangle(x, y, width, height);
  1453. }
  1454. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  1455. {
  1456. frmBCRegulate frmBC = new frmBCRegulate();
  1457. frmBC.m_measureobj = m_MeasureAppForm.m_MsrThreadWrapper.m_measure;
  1458. //var param = m_MeasureAppForm.m_ProjParam.GetDefaultParam().BrightnessContrastRegulationParam;
  1459. var param = frmBC.m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam;
  1460. frmBC.brightphaseMaterial = param.brightphaseelement;
  1461. frmBC.darkphaseMaterial = param.darkphaseelement;
  1462. frmBC.brightphasevalue = param.brightphaseGrayvalue;
  1463. frmBC.darkphasevalue = param.darkphaseGrayvalue;
  1464. frmBC.stdPos = param.stdMaterialOTSPos;
  1465. frmBC.mag=param.mag;
  1466. frmBC.initialBrightness = param.initialBrightness;
  1467. frmBC.initialContrast = param.initialContrast;
  1468. frmBC.autoregulateType = param.autoRegulateType;
  1469. frmBC.period = param.period;
  1470. frmBC.stageData = m_MeasureAppForm.m_ProjParam.GetDefaultParam().GetStageDataParam();
  1471. frmBC.isToRun=param.toRun;
  1472. frmBC.Show();
  1473. }
  1474. #endregion
  1475. }
  1476. }