OTSImageDisHelp.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. 
  2. using OTSIncAReportApp.DataOperation.DataAccess;
  3. using OTSIncAReportApp.DataOperation.Model;
  4. using OTSIncAReportApp.SysMgrTools;
  5. using OTSIncAReportGraph.Class;
  6. using OTSIncAReportGraph.Controls;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Diagnostics;
  12. using System.Drawing;
  13. using System.Drawing.Drawing2D;
  14. using System.Linq;
  15. namespace OTSIncAReportGraph.OTSIncAReportGraphFuncation
  16. {
  17. public class OTSImageDisHelp
  18. {
  19. #region 枚举定义
  20. /// <summary>
  21. /// 样品台X轴方向
  22. /// </summary>
  23. enum OTS_X_AXIS_DIRECTION
  24. {
  25. LEFT_TOWARD = 0,
  26. RIGHT_TOWARD = 1
  27. }
  28. /// <summary>
  29. /// 样品台Y轴方向
  30. /// </summary>
  31. enum OTS_Y_AXIS_DIRECTION
  32. {
  33. UP_TOWARD = 0,
  34. DOWN_TOWARD = 1
  35. }
  36. #endregion
  37. #region 定义变量
  38. private ResultFile resultFile = null;
  39. //记录field列表的原值
  40. public List<DisplayField> m_original_list_dfield = null;
  41. //记录原值,颗粒和线段
  42. public List<BaseObject> m_original_list_baseobject = null;
  43. NLog.Logger log;
  44. //field的数量
  45. public int m_field_count = 0;
  46. //particle的数量
  47. public int m_particle_count = 0;
  48. //加载使用的时间
  49. public string m_time_str = "";
  50. //加载使用时间计算时间段2
  51. public string m_time_str2 = "";
  52. //电镜设置对象
  53. public ServiceInterface.HardwareController m_cfun = null;
  54. //是否已经连接到了电镜
  55. public bool m_SEMConnectionState = false;
  56. //连接到电镜的ID号
  57. public int m_SEM_ID = 0;
  58. #endregion
  59. #region 构造函数
  60. public OTSImageDisHelp( ResultFile result)
  61. {
  62. m_original_list_dfield = new List<DisplayField>();
  63. m_original_list_baseobject = new List<BaseObject>();
  64. resultFile = result;
  65. m_cfun = ServiceInterface.HardwareController.GetSemController();
  66. log = NLog.LogManager.GetCurrentClassLogger();
  67. }
  68. #endregion
  69. #region 封装自定义方法
  70. public Point GetOTSCoordLeftBottomPoint(List<Point> allFldPos)
  71. {
  72. //the ots coordinate system is always positive on the right and up direction.So the leftbottom point would be the smallest point.
  73. //找出最小的x,y用来做偏移运算
  74. int i_offset_x = 1000000000;
  75. int i_offset_y = 1000000000;
  76. //先取出最小的x,y
  77. for (int i = 0; i < allFldPos.Count; i++)
  78. {
  79. if (i_offset_x > allFldPos[i].X)
  80. {
  81. i_offset_x = allFldPos[i].X;
  82. }
  83. if (i_offset_y > allFldPos[i].Y)
  84. {
  85. i_offset_y = allFldPos[i].Y;
  86. }
  87. }
  88. return new Point(i_offset_x, i_offset_y);
  89. }
  90. public Point ConvertOTSCoordinateToScreenCoord(Point otsleftBottomPoint,double pixelSize, Point currenFldPos,RectangleF wholeImageRec,RectangleF singleImgRec)//
  91. {
  92. var OTSPoint=new Point(currenFldPos.X - otsleftBottomPoint.X, currenFldPos.Y - otsleftBottomPoint.Y);
  93. double screenHeight = wholeImageRec.Height + (0 - (int)(Convert.ToDouble(OTSPoint.Y) / pixelSize));//because the screen coordinate is downward rightward positive,so we need to translate the Y coordinate of the OTS system which is upward rightward positive.
  94. screenHeight = screenHeight - singleImgRec.Height;
  95. var screenPoint = new Point((int)(Convert.ToDouble(OTSPoint.X)/pixelSize), (int)screenHeight);
  96. return screenPoint;
  97. }
  98. public Rectangle ConvertAndGetMaxRect(List<Point> inPoints, int in_width, int in_height)
  99. {
  100. //首先要能确定下来,单个物理坐标的宽和高--------------------------------
  101. int i_wl_width = 0;
  102. int i_wl_height = 0;
  103. RectangleF ls_r = GetPhysicalFieldWidthAndHeight(inPoints,in_width,in_height);
  104. i_wl_width = (int)ls_r.Width;
  105. i_wl_height = (int)ls_r.Height;
  106. //-----------------------------------------------------------------------------
  107. int point_x_min = 10000000;
  108. int point_x_max = -10000000;
  109. int point_y_min = 10000000;
  110. int point_y_max = -10000000;
  111. for (int i = 0; i < inPoints.Count(); i++)
  112. {
  113. Point ls_point = inPoints[i];
  114. //取出正数最大x
  115. if (ls_point.X > point_x_max)
  116. point_x_max = ls_point.X;
  117. if (ls_point.Y > point_y_max)
  118. point_y_max = ls_point.Y;
  119. if (ls_point.X < point_x_min)
  120. point_x_min = ls_point.X;
  121. if (ls_point.Y < point_y_min)
  122. point_y_min = ls_point.Y;
  123. }
  124. //然后分别用最大值+abs(最小值),就是x,和y轴的总长值
  125. point_x_max = point_x_max - point_x_min;
  126. point_y_max = point_y_max - point_y_min;
  127. //该算法有个问题,就是不能直观的得到整个范围的大小,要除以倍数再补1能补充缺少的一个field视域**********
  128. point_x_max = ((point_x_max / i_wl_width) + 1) * i_wl_width;
  129. point_y_max = ((point_y_max / i_wl_height) + 1) * i_wl_height;
  130. //将物理宽高,变换成分辨率宽高
  131. if (i_wl_width != 0) point_x_max = (point_x_max / i_wl_width) * in_width; else point_x_max = 0;
  132. if (i_wl_height != 0) point_y_max = (point_y_max / i_wl_height) * in_height; else point_y_max = 0;
  133. Rectangle ret_rectangle = new Rectangle(0, 0, 0, 0);
  134. //判断一下防止出错,只有在有数据的情况下,进行赋值才行
  135. if (inPoints.Count > 0)
  136. {
  137. ret_rectangle = new Rectangle(0, 0, point_x_max, point_y_max);
  138. }
  139. //这样返回是物理坐标的总大小,应该返回像素坐标大小才对
  140. return ret_rectangle;
  141. }
  142. /// <summary>
  143. /// 计算单个field的物理大小 传入field的list,还有测量结果管理类对象,在无法计算出单field的物理大小的情况下,到这里取再计算得出
  144. /// </summary>
  145. /// <returns></returns>
  146. public RectangleF GetPhysicalFieldWidthAndHeight(List<Point> points,int imageWidth,int imageHeight)
  147. {
  148. int width_max = -10000000;
  149. int height_max = -10000000;
  150. int width_max2 = -10000000;
  151. int height_max2 = -10000000;
  152. //先找出最大的值,
  153. for (int i = 0; i < points.Count(); i++)
  154. {
  155. if (width_max < points[i].X)
  156. width_max = points[i].X;
  157. if (height_max < points[i].Y)
  158. height_max = points[i].Y;
  159. }
  160. //再找出第二大的值
  161. for (int i = 0; i < points.Count(); i++)
  162. {
  163. if (width_max2 < points[i].X && width_max != points[i].X)
  164. width_max2 = points[i].X;
  165. if (height_max2 < points[i].Y && height_max != points[i].Y)
  166. height_max2 = points[i].Y;
  167. }
  168. //需要针对第二大的值,获取时进行判断,感觉这里应该如果并未找到第二大的值的情况下,赋于0值,便于以后进行计算
  169. if (width_max2 == -10000000)
  170. width_max2 = width_max;
  171. if (height_max2 == -10000000)
  172. height_max2 = height_max;
  173. RectangleF ret_rect = new RectangleF(0, 0, width_max - width_max2, height_max - height_max2);
  174. //如果最后计算出的宽高有0则重新到测量数据中获取---------------------------------------
  175. if (ret_rect.Width == 0 || ret_rect.Height == 0)
  176. {
  177. //到参数中去取单个宽
  178. double d_scanFieldSize_width = Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  179. //然后再用单个宽去计算出高是多少
  180. double d_scanFieldSize_height = 0;
  181. if (d_scanFieldSize_width != 0)
  182. d_scanFieldSize_height = (d_scanFieldSize_width / Convert.ToDouble(imageWidth)) * imageHeight;
  183. ret_rect.Width = (int)d_scanFieldSize_width;
  184. ret_rect.Height = (int)d_scanFieldSize_height;
  185. }
  186. ///-----------because all the fields 's height/width=0.75 so here we make an enforce. gsp add at 2019/10/31
  187. ///sometimes the gbfields are not conform to this for the cuting and merging operation.
  188. //if (ret_rect.Height / ret_rect.Width != 0.75f)
  189. //{
  190. // ret_rect = new Rectangle(ret_rect.X, ret_rect.Y, ret_rect.Width, (int)(ret_rect.Width * 0.75f));
  191. //}
  192. return ret_rect;
  193. }
  194. #endregion
  195. #region 电镜操作相关方法
  196. /// <summary>
  197. /// 连接电镜,分布图使用
  198. /// </summary>
  199. public void ConnectToSEM()
  200. {
  201. log.Trace("(Connection_ForDrawDistrbutionImageAndBSE)" + "Connect to SEM");
  202. if (!m_SEMConnectionState)
  203. {
  204. //和电镜建立通讯连接
  205. m_SEMConnectionState = m_cfun.Connect();
  206. log.Trace("(Connection_ForDrawDistrbutionImageAndBSE)" + "Connect to SEM" + ":--" + m_SEMConnectionState + "---");
  207. }
  208. else
  209. {
  210. log.Trace("(Connection_ForDrawDistrbutionImageAndBSE)" + ":allready connected, state:" + m_SEMConnectionState);
  211. //断开电镜连接
  212. }
  213. }
  214. public void DisConnectSEM()
  215. {
  216. m_SEMConnectionState = false;
  217. m_cfun.DisConnect();
  218. }
  219. /// <summary>
  220. /// 移动电镜到指定的X,Y坐标上,R坐标使用原先的值进行移动
  221. /// </summary>
  222. /// <param name="PositionX"></param>
  223. /// <param name="PositionY"></param>
  224. public void MoveSemToPointXY(double in_PositionX, double in_PositionY)
  225. {
  226. log.Trace("Begin MoveSemToPointXY:(" +in_PositionX.ToString()+","+in_PositionY.ToString()+")");
  227. //首先获取电镜当前的位置,并记录原R值
  228. double ls_PositionX = 0;
  229. double ls_PositionY = 0;
  230. double ls_PositionR = 0;
  231. if (m_SEMConnectionState)
  232. {
  233. m_cfun.GetSemPositionXY(ref ls_PositionX, ref ls_PositionY, ref ls_PositionR);
  234. }
  235. else
  236. {
  237. log.Error("Failed to GetSemPositionXY");
  238. return;
  239. }
  240. if (m_SEMConnectionState)
  241. {
  242. m_cfun.MoveSEMToPoint(new Point((int)in_PositionX, (int)in_PositionY), ls_PositionR);
  243. }
  244. }
  245. #endregion
  246. #region //--------------------------------------颗粒分布图相关部份---------------------------------------------------------------------
  247. /// <summary>
  248. /// 传入颗粒的tagid和fieldid,来获取该颗粒下对应的xray数据
  249. /// </summary>
  250. /// <param name="in_clr_tagid"></param>
  251. /// <param name="in_clr_fieldid"></param>
  252. /// <param name="Search_xray"></param>
  253. /// <param name="Analysis_xray"></param>
  254. public void GetXrayByParticleTagIDAndFieldID_ForDrawDistrbutionImageAndBSE(int in_clr_tagid, int in_clr_fieldid, out uint[] Search_xray, out uint[] Analysis_xray, out int xray_id, out List<Element> list_celementchemistryclr)
  255. {
  256. Search_xray = new uint[2000];
  257. Analysis_xray = new uint[2000];
  258. xray_id = 0;
  259. list_celementchemistryclr = new List<Element>();
  260. //防止为空校验判断
  261. if (resultFile.List_OTSField == null)
  262. return;
  263. Particle particle = resultFile.List_OTSField.Find(x => x.FieldID == in_clr_fieldid).ParticleList.Find(x => x.ParticleId == in_clr_tagid);
  264. var tmpPart = new ParticleData(resultFile.FilePath).GetParticleXrayDataByFidAndPid(Convert.ToString(particle.FieldId), Convert.ToString(particle.XrayId));
  265. if (tmpPart != null)
  266. {
  267. particle.XRayData = tmpPart.XRayData;
  268. if (particle.XrayId > -1)
  269. {
  270. for (int i = 0; i < 2000; i++)
  271. {
  272. Analysis_xray[i] = BitConverter.ToUInt32(particle.XRayData, i * 4);
  273. }
  274. Search_xray = Analysis_xray;
  275. xray_id = particle.XrayId;
  276. list_celementchemistryclr = particle.ElementList;
  277. }
  278. }
  279. }
  280. /// <summary>
  281. /// 传入所有的物理field坐标点,和单个物理field的宽高,返回所有field的左上角位置,和整个field组成的rect大小
  282. /// </summary>
  283. /// <param name="in_list_point"></param>
  284. /// <param name="in_width"></param>
  285. /// <param name="in_height"></param>
  286. /// <returns></returns>
  287. public Rectangle GetWlRectTopLeftAndRect(List<Point> in_list_point, int in_width, int in_height)
  288. {
  289. //分别获取整个rect的xy最小值和最大值
  290. int i_rect_x_min = 100000000;
  291. int i_rect_y_min = 100000000;
  292. int i_rect_x_max = -100000000;
  293. int i_rect_y_max = -100000000;
  294. for (int i = 0; i < in_list_point.Count; i++)
  295. {
  296. if (i_rect_x_min > in_list_point[i].X)
  297. i_rect_x_min = in_list_point[i].X;
  298. if (i_rect_y_min > in_list_point[i].Y)
  299. i_rect_y_min = in_list_point[i].Y;
  300. if (i_rect_x_max < in_list_point[i].X)
  301. i_rect_x_max = in_list_point[i].X;
  302. if (i_rect_y_max < in_list_point[i].Y)
  303. i_rect_y_max = in_list_point[i].Y;
  304. }
  305. Rectangle ret_rect = new Rectangle(i_rect_x_min, i_rect_y_min,
  306. i_rect_x_max - i_rect_x_min, i_rect_y_max - i_rect_y_min);
  307. return ret_rect;
  308. }
  309. /// <summary>
  310. /// 根据Field的ID,来获取Field列表中对应FIeld的OTS 坐标
  311. /// </summary>
  312. /// <param name="in_fieldid"></param>
  313. /// <returns></returns>
  314. public Point GetOTSPointByFieldID(List<DisplayField> in_list_dfield, int in_fieldid)
  315. {
  316. Point ret_point = new Point(0, 0);
  317. for (int i = 0; i < in_list_dfield.Count; i++)
  318. {
  319. //这里TagID先代表的是底层返回的ID
  320. if (in_list_dfield[i].FieldID == in_fieldid.ToString())
  321. {
  322. ret_point = new Point(Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.X), Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.Y));
  323. }
  324. }
  325. return ret_point;
  326. }
  327. /// <summary>
  328. /// 将OTS坐标转换为Sem 坐标
  329. /// </summary>
  330. /// <param name="POTSCoord"></param>
  331. /// <returns></returns>
  332. public Point ChangeOTSToSemCoord(Point POTSCoord)
  333. {
  334. //first if m_semstagedata is null to get stage inforation
  335. Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  336. //after obtaining stage info,calc stage point data
  337. Point ret_SEM_point = new Point();
  338. // get center point, um
  339. long xStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["start"]);
  340. long xEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["end"]);
  341. long xCenter = (xStart + xEnd) / 2;
  342. long yStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["start"]);
  343. long yEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["end"]);
  344. long yCenter = (yStart + yEnd) / 2;
  345. // delte = SEM - OTSa
  346. long deltex = xCenter - 0;
  347. long deltey = yCenter - 0;
  348. int xdir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["xAxisDir"]);
  349. int ydir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["yAxisDir"]);
  350. if (xdir == (int)OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  351. {
  352. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  353. }
  354. else if (xdir == (int)OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  355. {
  356. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  357. }
  358. if (ydir == (int)OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  359. {
  360. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  361. }
  362. else if (ydir == (int)OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  363. {
  364. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  365. }
  366. return ret_SEM_point;
  367. }
  368. #endregion
  369. /// <summary>
  370. /// 判断该点是否在多边形的范围内
  371. /// </summary>
  372. /// <param name="inPoints"></param>
  373. /// <param name="WhetherPoint"></param>
  374. /// <returns></returns>
  375. public bool WhetherInRange(DisplayParticle Part,/*PointF[] inPoints,*/ Point WhetherPoint)
  376. {
  377. var rect = Part.Rect;
  378. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  379. {
  380. var itm = (BaseObject)Part;
  381. itm.GPath = Part.GetRegionFromDSegments();
  382. PointF[] inPoints = itm.GPath.PathPoints;
  383. bool b_inrange = false;
  384. GraphicsPath myGraphicsPath = new GraphicsPath();
  385. Region myRegion = new Region();
  386. myGraphicsPath.Reset();
  387. myGraphicsPath.AddPolygon(inPoints);
  388. myRegion.MakeEmpty();
  389. myRegion.Union(myGraphicsPath);
  390. //返回判断点是否在多边形里
  391. b_inrange = myRegion.IsVisible(WhetherPoint);
  392. return b_inrange;
  393. }
  394. else
  395. {
  396. return false;
  397. }
  398. }
  399. public bool WhetherInRange(RectangleF rec,PointF[] inPoints, Point WhetherPoint)
  400. {
  401. var rect = rec;
  402. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  403. {
  404. //var itm = (BaseObject)Part;
  405. //PointF[] inPoints = itm.GPath.PathPoints;
  406. bool b_inrange = false;
  407. GraphicsPath myGraphicsPath = new GraphicsPath();
  408. Region myRegion = new Region();
  409. myGraphicsPath.Reset();
  410. myGraphicsPath.AddPolygon(inPoints);
  411. myRegion.MakeEmpty();
  412. myRegion.Union(myGraphicsPath);
  413. //返回判断点是否在多边形里
  414. b_inrange = myRegion.IsVisible(WhetherPoint);
  415. return b_inrange;
  416. }
  417. else
  418. {
  419. return false;
  420. }
  421. }
  422. /// <summary>
  423. /// 判断该点是否在多边形的范围内的float版本重载
  424. /// </summary>
  425. /// <param name="inPoints"></param>
  426. /// <param name="WhetherPoint"></param>
  427. /// <returns></returns>
  428. public bool WhetherInRange(DisplayParticle Part,/* PointF[] inPoints, */PointF WhetherPoint)
  429. {
  430. var rect = Part.Rect;
  431. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  432. {
  433. var itm = (BaseObject)Part;
  434. PointF[] inPoints = itm.GPath.PathPoints;
  435. bool b_inrange = false;
  436. GraphicsPath myGraphicsPath = new GraphicsPath();
  437. Region myRegion = new Region();
  438. myGraphicsPath.Reset();
  439. myGraphicsPath.AddPolygon(inPoints);
  440. myRegion.MakeEmpty();
  441. myRegion.Union(myGraphicsPath);
  442. //返回判断点是否在多边形里
  443. b_inrange = myRegion.IsVisible(WhetherPoint);
  444. return b_inrange;
  445. }
  446. else
  447. {
  448. return false;
  449. }
  450. }
  451. }
  452. }