StageDisplayHelp.cs 14 KB

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