StageDisplayHelp.cs 14 KB

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