StageDisplayHelp.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using OTSDataType;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using static OTSDataType.otsdataconst;
  9. namespace OTSMeasureApp._7_OTSProgMgrInfo.Stage
  10. {
  11. class StageDisplayHelp
  12. {
  13. public const int RATIO_RECT_LEFT = 22; //ratio rect left edge
  14. public const int PIC_EDGE = 12; //edge width
  15. public const int RATIO_RECT_HEIGHT = 30; //ratio rect height
  16. public const int RATIO_RECT_DOWN = 10; //ratio rect down edge
  17. public const int DOMAIN_ITEM_NUMBER = 5;
  18. public StageDisplayHelp()
  19. {
  20. }
  21. public void DrawStageBoundery(CStage a_pStage, int a_nWidth, int a_nHeight, Object pDC, double a_dPixSizeX, double a_dPixSizeY)
  22. {
  23. if (a_pStage == null)
  24. {
  25. return;
  26. }
  27. CDomain pBoundery = a_pStage.GetBoundary();
  28. otsdataconst.DOMAIN_SHAPE nShape = (otsdataconst.DOMAIN_SHAPE)(-1);
  29. nShape = pBoundery.GetShape();
  30. Rectangle PixRect = new Rectangle();
  31. GetPixRect(pBoundery, a_nWidth, a_nHeight, a_dPixSizeX, a_dPixSizeY, ref PixRect);
  32. DrawShape(PixRect, pDC, nShape, true);
  33. }
  34. // draw stage hole
  35. public void DrawStageHole(CStage a_pStage, int a_nWidth, int a_nHeight, Object pDC, double a_dPixSizeX, double a_dPixSizeY)
  36. {
  37. if (a_pStage == null)
  38. {
  39. return;
  40. }
  41. if (a_pStage.GetHoleList().Count == 0)
  42. {
  43. return;
  44. }
  45. foreach (var pHole in a_pStage.GetHoleList())
  46. {
  47. otsdataconst.DOMAIN_SHAPE nShape = (otsdataconst.DOMAIN_SHAPE)(-1);
  48. nShape = pHole.GetShape();
  49. Rectangle PixRect = new Rectangle();
  50. GetPixRect(pHole, a_nWidth, a_nHeight, a_dPixSizeX, a_dPixSizeY, ref PixRect);
  51. DrawShape(PixRect, pDC, nShape, true);
  52. //设置文字对齐方式
  53. StringFormat sf = new StringFormat();
  54. sf.Alignment = StringAlignment.Center;
  55. sf.LineAlignment = StringAlignment.Center;
  56. //文字颜色
  57. string ColorStr = "#90ee90";
  58. Color myColor = ColorTranslator.FromHtml(ColorStr);
  59. System.Drawing.SolidBrush sampleBrush = new System.Drawing.SolidBrush(myColor);
  60. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(50, Color.Black));
  61. //字体大小 根据样品孔Rectangle大小
  62. float fontSize = PixRect.Width / 4;
  63. if(fontSize==0)
  64. {
  65. fontSize = 1;
  66. }
  67. Font font = new Font("宋体", fontSize, FontStyle.Regular);
  68. if (fontSize == 0)
  69. {
  70. font = new Font("宋体", fontSize, FontStyle.Regular);
  71. }
  72. //绘制文字阴影
  73. Rectangle rectFont = PixRect;
  74. rectFont.X += 2;
  75. rectFont.Y += 2;
  76. ((Graphics)pDC).DrawString(pHole.GetName(), font, solidBrush, rectFont, sf);
  77. ((Graphics)pDC).DrawString(pHole.GetName(), font, sampleBrush, PixRect, sf);
  78. }
  79. }
  80. // draw ratio
  81. public void DrawRatio(int a_nBounderyWidth, int a_nWidth, int a_nHeight, Object pDC, IntPtr pWnd, Rectangle rc)
  82. {
  83. if (pDC == null)
  84. {
  85. return;
  86. }
  87. if (pWnd == null)
  88. {
  89. return;
  90. }
  91. int nRatio = a_nBounderyWidth * (a_nWidth / 2 - RATIO_RECT_LEFT - 10) / (a_nWidth - PIC_EDGE * 2) / 1000; //显示在标尺上的mm数
  92. //string sRatio;
  93. Point oRatioStart = new Point();
  94. oRatioStart.X = RATIO_RECT_LEFT;
  95. oRatioStart.Y = a_nHeight - RATIO_RECT_HEIGHT - RATIO_RECT_DOWN;
  96. Point oRatioEnd = new Point();
  97. oRatioEnd.X = a_nWidth / 2;
  98. oRatioEnd.Y = a_nHeight - RATIO_RECT_DOWN;
  99. }
  100. // get pixle rect
  101. public bool GetPixRect(CDomain a_pDomain, int a_nWidth, int a_nHeight, double a_dPixSizeX, double a_dPixSizeY, ref Rectangle PixRect)
  102. {
  103. if (a_pDomain == null)
  104. {
  105. return false;
  106. }
  107. RectangleF DomainRect = a_pDomain.GetDomainRect();
  108. Double CenterX = a_nWidth / 2; //pixle center
  109. Double CenterY = a_nHeight / 2;
  110. PointF DomainCenter = a_pDomain.GetDomainCenter(); // um center
  111. PointF PixCenter = new Point();
  112. PixCenter.X = (float)(DomainCenter.X / a_dPixSizeX);
  113. PixCenter.Y = (float)(DomainCenter.Y / a_dPixSizeY); // um to pixle
  114. Double PixRectCenterX = CenterX + PixCenter.X;
  115. Double PixRectCenterY = CenterY - PixCenter.Y;//OTS y dirction is different with pixle direction
  116. Double delteX = PixRectCenterX - PixCenter.X;
  117. Double delteY = PixRectCenterY - PixCenter.Y;
  118. int Left = (int)Math.Round(DomainRect.Left / a_dPixSizeX + delteX);
  119. int Top = (int)Math.Round(DomainRect.Top / a_dPixSizeY + delteY);
  120. int Right = (int)Math.Round(DomainRect.Right / a_dPixSizeX + delteX);
  121. int Bottom = (int)Math.Round(DomainRect.Bottom / a_dPixSizeY + delteY);
  122. PixRect.Location = new Point(Left, Top);
  123. PixRect.Size = new Size(Math.Abs(Right - Left), Math.Abs(Bottom - Top));
  124. return true;
  125. }
  126. // draw shape
  127. public void DrawShape(Rectangle a_PixRect, Object a_pDC, otsdataconst.DOMAIN_SHAPE a_nShape, bool a_bIsRand)
  128. {
  129. if (a_pDC == null)
  130. {
  131. return;
  132. }
  133. Graphics graphicsShape = (Graphics)a_pDC;
  134. switch ((int)a_nShape)
  135. {
  136. case (int)otsdataconst.DOMAIN_SHAPE.ROUND:
  137. graphicsShape.DrawEllipse(new Pen(Color.Black), a_PixRect);
  138. break;
  139. case (int)otsdataconst.DOMAIN_SHAPE.RECTANGLE:
  140. if (a_bIsRand)
  141. {
  142. //graphicsShape.DrawRectangle(new Pen(Color.Black), a_PixRect, new Point(ROUND_VALUE, ROUND_VALUE));
  143. graphicsShape.DrawRectangle(new Pen(Color.Black), a_PixRect);
  144. }
  145. else
  146. {
  147. graphicsShape.DrawEllipse(new Pen(Color.Black), a_PixRect);
  148. }
  149. break;
  150. }
  151. }
  152. // get domain
  153. public CDomain GetDomain(String a_strValue)
  154. {
  155. // domain
  156. CDomain pDomain = null;
  157. // check value string
  158. a_strValue.Trim();
  159. if (a_strValue == "")
  160. {
  161. return pDomain;
  162. }
  163. // split the value string
  164. List<string> listStr = new List<string>(a_strValue.Split(','));
  165. pDomain = GetDomain(listStr);
  166. // return the domain pointer
  167. return pDomain;
  168. }
  169. CDomain GetDomain(List<string> a_listString)
  170. {
  171. // domain
  172. CDomain pDomain = null;
  173. // check value string list size
  174. if ((int)a_listString.Count != DOMAIN_ITEM_NUMBER)
  175. {
  176. return pDomain;
  177. }
  178. // shape
  179. int nValue;
  180. string strValue = a_listString[0];
  181. nValue = Convert.ToInt32(strValue);
  182. // check the shape value
  183. if (nValue < (int)DOMAIN_SHAPE.ROUND || nValue > (int)DOMAIN_SHAPE.RECTANGLE)
  184. {
  185. return pDomain;
  186. }
  187. DOMAIN_SHAPE nShape = (DOMAIN_SHAPE)nValue;
  188. // position
  189. strValue = a_listString[1];
  190. nValue = Convert.ToInt32(strValue);
  191. int nCentreX = nValue;
  192. strValue = a_listString[2];
  193. nValue = Convert.ToInt32(strValue);
  194. int nCentreY = nValue;
  195. // diameter/width
  196. strValue = a_listString[3];
  197. nValue = Convert.ToInt32(strValue);
  198. int nDiameter_Width = nValue;
  199. // height/spare
  200. strValue = a_listString[4];
  201. if (nShape == DOMAIN_SHAPE.RECTANGLE)
  202. {
  203. nValue = Convert.ToInt32(strValue);
  204. }
  205. int nDiameter_nHeight = nValue;
  206. // create domain
  207. Rectangle rectDomain = new Rectangle(0, 0, nDiameter_Width, nDiameter_nHeight);
  208. pDomain = new CDomain(nShape, rectDomain);
  209. pDomain.OffsetDomain(new Point(nCentreX - (nDiameter_Width / 2), nCentreY - (nDiameter_nHeight / 2)));
  210. // return the domain pointer
  211. return pDomain;
  212. }
  213. #region 画出X轴与Y轴
  214. /// <summary>
  215. /// 在任意的panel里画一个坐标,坐标所在的四边形距离panel边50像素
  216. /// </summary>
  217. /// <param name="pan"></param>
  218. public void DrawXY(Object a_pDC, double width,double height, double a_dPixSizeX, double a_dPixSizeY, double coordinateSystemEndpointX, double coordinateSystemEndpointY)
  219. {
  220. if (a_pDC == null)
  221. {
  222. return;
  223. }
  224. Graphics g = (Graphics)a_pDC;
  225. //绘制X轴,
  226. double ObjectCenterX = width/ 2;
  227. double ObjectCenterY = height / 2;
  228. int X0 = (int)(coordinateSystemEndpointX / a_dPixSizeX + ObjectCenterX);
  229. int Y0 = (int)(0 / a_dPixSizeX + ObjectCenterY);
  230. int X1 = (int)(-coordinateSystemEndpointX / a_dPixSizeX + ObjectCenterX);
  231. int Y1 = (int)(0 / a_dPixSizeX + ObjectCenterY);
  232. Point px1 = new Point(X0, Y0);
  233. Point px2 = new Point(X1, Y1);
  234. Pen pen = new Pen(Brushes.Green, 1);
  235. g.DrawLine(pen, px1, px2);
  236. //绘制Y轴
  237. int X3 = (int)(0 / a_dPixSizeY + ObjectCenterX);
  238. int Y3 = (int)(coordinateSystemEndpointY / a_dPixSizeY + ObjectCenterY);
  239. int X4 = (int)(0 / a_dPixSizeY + ObjectCenterX);
  240. int Y4 = (int)(-coordinateSystemEndpointY / a_dPixSizeY + ObjectCenterY);
  241. PointF py1 = new PointF(X3, Y3);
  242. PointF py2 = new PointF(X4, Y4);
  243. g.DrawLine(pen, py1, py2);
  244. g.DrawString("0", new Font("宋体 ", 9f), Brushes.Green, new PointF((float)ObjectCenterX - 12, (float)ObjectCenterY + 3));
  245. int move = 5,len=10;
  246. for (int i = 1; i <= len; i++) //len等分Y正轴
  247. {
  248. Point px3 = new Point((int)ObjectCenterX + move, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSizeY) / len * i));
  249. Point px4 = new Point((int)ObjectCenterX, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSizeY) / len * i));
  250. string sx = ((int)(-(-coordinateSystemEndpointY) / len * i)).ToString();
  251. g.DrawLine(pen, px3, px4);
  252. StringFormat drawFormat = new StringFormat();
  253. drawFormat.Alignment = StringAlignment.Far;
  254. drawFormat.LineAlignment = StringAlignment.Center;
  255. g.DrawString(sx, new Font("宋体", 8f), Brushes.Green, new Point((int)ObjectCenterX - 5, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSizeY) / len * i)), drawFormat);
  256. }
  257. for (int i = 1; i <= len; i++)
  258. {
  259. Point px3 = new Point((int)ObjectCenterX + move, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSizeY) / len * i));
  260. Point px4 = new Point((int)ObjectCenterX, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSizeY) / len * i));
  261. string sx = ((int)((-coordinateSystemEndpointY) / len * i)).ToString();
  262. g.DrawLine(pen, px3, px4);
  263. StringFormat drawFormat = new StringFormat();
  264. drawFormat.Alignment = StringAlignment.Far;
  265. drawFormat.LineAlignment = StringAlignment.Center;
  266. g.DrawString(sx, new Font("宋体", 8f), Brushes.Green, new Point((int)ObjectCenterX - 5, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSizeY) / len * i)), drawFormat);
  267. }
  268. for (int i = 1; i <= len; i++) //len等分X正轴
  269. {
  270. Point px3 = new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSizeX) / len * i) , (int)(ObjectCenterY + move));
  271. Point px4 = new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSizeX) / len * i), (int)ObjectCenterY);
  272. string sx = ((int)(coordinateSystemEndpointX / len * i)).ToString();
  273. g.DrawLine(pen, px3, px4);
  274. StringFormat drawFormat = new StringFormat();
  275. drawFormat.Alignment = StringAlignment.Far;
  276. drawFormat.LineAlignment = StringAlignment.Center;
  277. g.DrawString(sx, new Font("宋体", 6f), Brushes.Green, new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSizeX) / len * i)+5, (int)(ObjectCenterY + 15)), drawFormat);
  278. }
  279. for (int i = 1; i <= len; i++)
  280. {
  281. Point px3 = new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSizeX) / len * i), (int)(ObjectCenterY + move));
  282. Point px4 = new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSizeX) / len * i), (int)(ObjectCenterY));
  283. string sx = ((int)((-coordinateSystemEndpointX) / len * i)).ToString();
  284. g.DrawLine(pen, px3, px4);
  285. StringFormat drawFormat = new StringFormat();
  286. drawFormat.Alignment = StringAlignment.Far;
  287. drawFormat.LineAlignment = StringAlignment.Center;
  288. g.DrawString(sx, new Font("宋体", 6f), Brushes.Green, new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSizeX) / len * i)+5, (int)(ObjectCenterY + 15)), drawFormat);
  289. }
  290. //g.Dispose();
  291. }
  292. #endregion
  293. public void DrawStageSTD(CStage a_pStage, int a_nWidth, int a_nHeight, Object pDC, double a_dPixSizeX, double a_dPixSizeY)
  294. {
  295. if (a_pStage == null)
  296. {
  297. return;
  298. }
  299. CDomain pSTD = a_pStage.GetSTD();
  300. otsdataconst.DOMAIN_SHAPE nShape = (otsdataconst.DOMAIN_SHAPE)(-1);
  301. nShape = pSTD.GetShape();
  302. Rectangle PixRect = new Rectangle();
  303. GetPixRect(pSTD, a_nWidth, a_nHeight, a_dPixSizeX, a_dPixSizeY, ref PixRect);
  304. DrawShape(PixRect, pDC, nShape, false);
  305. }
  306. }
  307. }