OTSImageDisHelp.cs 21 KB

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