OTSImageDisHelp.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 GetPhysicalCoordLeftBottomPoint(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. particle.XRayData = tmpPart.XRayData;
  264. if (particle.XrayId > -1)
  265. {
  266. for (int i = 0; i < 2000; i++)
  267. {
  268. Analysis_xray[i] = BitConverter.ToUInt32(particle.XRayData, i * 4);
  269. }
  270. Search_xray = Analysis_xray;
  271. xray_id = particle.XrayId;
  272. list_celementchemistryclr = particle.ElementList;
  273. }
  274. }
  275. /// <summary>
  276. /// 传入所有的物理field坐标点,和单个物理field的宽高,返回所有field的左上角位置,和整个field组成的rect大小
  277. /// </summary>
  278. /// <param name="in_list_point"></param>
  279. /// <param name="in_width"></param>
  280. /// <param name="in_height"></param>
  281. /// <returns></returns>
  282. public Rectangle GetWlRectTopLeftAndRect(List<Point> in_list_point, int in_width, int in_height)
  283. {
  284. //分别获取整个rect的xy最小值和最大值
  285. int i_rect_x_min = 100000000;
  286. int i_rect_y_min = 100000000;
  287. int i_rect_x_max = -100000000;
  288. int i_rect_y_max = -100000000;
  289. for (int i = 0; i < in_list_point.Count; i++)
  290. {
  291. if (i_rect_x_min > in_list_point[i].X)
  292. i_rect_x_min = in_list_point[i].X;
  293. if (i_rect_y_min > in_list_point[i].Y)
  294. i_rect_y_min = in_list_point[i].Y;
  295. if (i_rect_x_max < in_list_point[i].X)
  296. i_rect_x_max = in_list_point[i].X;
  297. if (i_rect_y_max < in_list_point[i].Y)
  298. i_rect_y_max = in_list_point[i].Y;
  299. }
  300. Rectangle ret_rect = new Rectangle(i_rect_x_min, i_rect_y_min,
  301. i_rect_x_max - i_rect_x_min, i_rect_y_max - i_rect_y_min);
  302. return ret_rect;
  303. }
  304. /// <summary>
  305. /// 根据Field的ID,来获取Field列表中对应FIeld的OTS 坐标
  306. /// </summary>
  307. /// <param name="in_fieldid"></param>
  308. /// <returns></returns>
  309. public Point GetOTSPointByFieldID(List<DisplayField> in_list_dfield, int in_fieldid)
  310. {
  311. Point ret_point = new Point(0, 0);
  312. for (int i = 0; i < in_list_dfield.Count; i++)
  313. {
  314. //这里TagID先代表的是底层返回的ID
  315. if (in_list_dfield[i].FieldID == in_fieldid.ToString())
  316. {
  317. ret_point = new Point(Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.X), Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.Y));
  318. }
  319. }
  320. return ret_point;
  321. }
  322. /// <summary>
  323. /// 将OTS坐标转换为Sem 坐标
  324. /// </summary>
  325. /// <param name="POTSCoord"></param>
  326. /// <returns></returns>
  327. public Point ChangeOTSToSemCoord(Point POTSCoord)
  328. {
  329. //first if m_semstagedata is null to get stage inforation
  330. Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  331. //after obtaining stage info,calc stage point data
  332. Point ret_SEM_point = new Point();
  333. // get center point, um
  334. long xStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["start"]);
  335. long xEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["end"]);
  336. long xCenter = (xStart + xEnd) / 2;
  337. long yStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["start"]);
  338. long yEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["end"]);
  339. long yCenter = (yStart + yEnd) / 2;
  340. // delte = SEM - OTSa
  341. long deltex = xCenter - 0;
  342. long deltey = yCenter - 0;
  343. int xdir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["xAxisDir"]);
  344. int ydir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["yAxisDir"]);
  345. if (xdir == (int)OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  346. {
  347. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  348. }
  349. else if (xdir == (int)OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  350. {
  351. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  352. }
  353. if (ydir == (int)OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  354. {
  355. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  356. }
  357. else if (ydir == (int)OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  358. {
  359. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  360. }
  361. return ret_SEM_point;
  362. }
  363. #endregion
  364. /// <summary>
  365. /// 判断该点是否在多边形的范围内
  366. /// </summary>
  367. /// <param name="inPoints"></param>
  368. /// <param name="WhetherPoint"></param>
  369. /// <returns></returns>
  370. public bool WhetherInRange(DisplayParticle Part,/*PointF[] inPoints,*/ Point WhetherPoint)
  371. {
  372. var rect = Part.Rect;
  373. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  374. {
  375. var itm = (BaseObject)Part;
  376. itm.GPath = Part.GetRegionFromDSegments();
  377. PointF[] inPoints = itm.GPath.PathPoints;
  378. bool b_inrange = false;
  379. GraphicsPath myGraphicsPath = new GraphicsPath();
  380. Region myRegion = new Region();
  381. myGraphicsPath.Reset();
  382. myGraphicsPath.AddPolygon(inPoints);
  383. myRegion.MakeEmpty();
  384. myRegion.Union(myGraphicsPath);
  385. //返回判断点是否在多边形里
  386. b_inrange = myRegion.IsVisible(WhetherPoint);
  387. return b_inrange;
  388. }
  389. else
  390. {
  391. return false;
  392. }
  393. }
  394. public bool WhetherInRange(RectangleF rec,PointF[] inPoints, Point WhetherPoint)
  395. {
  396. var rect = rec;
  397. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  398. {
  399. //var itm = (BaseObject)Part;
  400. //PointF[] inPoints = itm.GPath.PathPoints;
  401. bool b_inrange = false;
  402. GraphicsPath myGraphicsPath = new GraphicsPath();
  403. Region myRegion = new Region();
  404. myGraphicsPath.Reset();
  405. myGraphicsPath.AddPolygon(inPoints);
  406. myRegion.MakeEmpty();
  407. myRegion.Union(myGraphicsPath);
  408. //返回判断点是否在多边形里
  409. b_inrange = myRegion.IsVisible(WhetherPoint);
  410. return b_inrange;
  411. }
  412. else
  413. {
  414. return false;
  415. }
  416. }
  417. /// <summary>
  418. /// 判断该点是否在多边形的范围内的float版本重载
  419. /// </summary>
  420. /// <param name="inPoints"></param>
  421. /// <param name="WhetherPoint"></param>
  422. /// <returns></returns>
  423. public bool WhetherInRange(DisplayParticle Part,/* PointF[] inPoints, */PointF WhetherPoint)
  424. {
  425. var rect = Part.Rect;
  426. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  427. {
  428. var itm = (BaseObject)Part;
  429. PointF[] inPoints = itm.GPath.PathPoints;
  430. bool b_inrange = false;
  431. GraphicsPath myGraphicsPath = new GraphicsPath();
  432. Region myRegion = new Region();
  433. myGraphicsPath.Reset();
  434. myGraphicsPath.AddPolygon(inPoints);
  435. myRegion.MakeEmpty();
  436. myRegion.Union(myGraphicsPath);
  437. //返回判断点是否在多边形里
  438. b_inrange = myRegion.IsVisible(WhetherPoint);
  439. return b_inrange;
  440. }
  441. else
  442. {
  443. return false;
  444. }
  445. }
  446. }
  447. }