StageDisplayHelp.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. //Pen pen = new Pen(Color.Black, 10f);
  98. //((Graphics)pDC).DrawLine(pen,new Point(oRatioStart.X + 5, oRatioStart.Y + 5),new Point(oRatioStart.X + 5, oRatioEnd.Y - 15));
  99. //((Graphics)pDC).DrawLine(pen, new Point(oRatioStart.X + 5, oRatioStart.Y + 10),new Point(oRatioEnd.X - 5, oRatioStart.Y + 10));
  100. //((Graphics)pDC).DrawLine(pen, new Point(oRatioEnd.X - 5, oRatioStart.Y + 5),new Point(oRatioEnd.X - 5, oRatioEnd.Y - 15));
  101. //pDC->MoveTo(CPoint(oRatioStart.x + 5, oRatioStart.y + 5));
  102. //pDC->LineTo(CPoint(oRatioStart.x + 5, oRatioEnd.y - 15));
  103. //pDC->MoveTo(CPoint(oRatioStart.x + 5, oRatioStart.y + 10));
  104. //pDC->LineTo(CPoint(oRatioEnd.x - 5, oRatioStart.y + 10));
  105. //pDC->MoveTo(CPoint(oRatioEnd.x - 5, oRatioStart.y + 5));
  106. //pDC->LineTo(CPoint(oRatioEnd.x - 5, oRatioEnd.y - 15));
  107. //CFont font;
  108. //VERIFY(font.CreateFont(
  109. // 12, // nHeight
  110. // 0, // nWidth
  111. // 0, // nEscapement
  112. // 0, // nOrientation
  113. // FW_THIN, // nWeight
  114. // FALSE, // bItalic
  115. // FALSE, // bUnderline
  116. // 0, // cStrikeOut
  117. // ANSI_CHARSET, // nCharSet
  118. // OUT_DEFAULT_PRECIS, // nOutPrecision
  119. // CLIP_DEFAULT_PRECIS, // nClipPrecision
  120. // DEFAULT_QUALITY, // nQuality
  121. // DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
  122. // _T("Arial"))); // lpszFacename
  123. //CFont* def_font = pDC->SelectObject(&font);
  124. //pDC->TextOut(oRatioStart.x + 70, oRatioStart.y + 15, sRatio);
  125. //pDC->SelectObject(def_font);
  126. //// Done with the font. Delete the font object.
  127. //font.DeleteObject();
  128. //CRect oRect(oRatioStart, oRatioEnd);
  129. //HDC hDC;
  130. //hDC = ::GetDC(pWnd->m_hWnd);
  131. //HDC hBitmapDC = CreateCompatibleDC(hDC);
  132. //HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rc.Width(), rc.Height());
  133. //SelectObject(hBitmapDC, hBitmap);
  134. //BLENDFUNCTION bf;
  135. //bf.BlendOp = AC_SRC_OVER;
  136. //bf.BlendFlags = 0;
  137. //bf.AlphaFormat = 0;
  138. //bf.SourceConstantAlpha = 10;
  139. //AlphaBlend(hDC, oRect.left, oRect.top, oRect.Width(), oRect.Height(), hBitmapDC, 0, 0, oRect.Width(), oRect.Height(), bf);
  140. }
  141. // get pixle rect
  142. public bool GetPixRect(CDomain a_pDomain, int a_nWidth, int a_nHeight, double a_dPixSize, ref Rectangle PixRect)
  143. {
  144. if (a_pDomain == null)
  145. {
  146. return false;
  147. }
  148. Rectangle DomainRect = a_pDomain.GetDomainRect();
  149. int CenterX = a_nWidth / 2; //pixle center
  150. int CenterY = a_nHeight / 2;
  151. Point DomainCenter = a_pDomain.GetDomainCenter(); // um center
  152. Point PixCenter = new Point();
  153. PixCenter.X = (int)((double)DomainCenter.X / a_dPixSize);
  154. PixCenter.Y = (int)((double)DomainCenter.Y / a_dPixSize); // um to pixle
  155. int PixRectCenterX = CenterX + PixCenter.X;
  156. int PixRectCenterY = CenterY - PixCenter.Y;//OTS y dirction is different with pixle direction
  157. int delteX = PixRectCenterX - PixCenter.X;
  158. int delteY = PixRectCenterY - PixCenter.Y;
  159. int Left = (int)(DomainRect.Left / a_dPixSize + delteX);
  160. int Top = (int)(DomainRect.Top / a_dPixSize + delteY);
  161. int Right = (int)(DomainRect.Right / a_dPixSize + delteX);
  162. int Bottom = (int)(DomainRect.Bottom / a_dPixSize + delteY);
  163. PixRect.Location = new Point(Left, Top);
  164. PixRect.Size = new Size(Math.Abs(Right - Left), Math.Abs(Bottom - Top));
  165. return true;
  166. }
  167. // draw shape
  168. public void DrawShape(Rectangle a_PixRect, Object a_pDC, otsdataconst.DOMAIN_SHAPE a_nShape, bool a_bIsRand)
  169. {
  170. if (a_pDC == null)
  171. {
  172. return;
  173. }
  174. Graphics graphicsShape = (Graphics)a_pDC;
  175. switch ((int)a_nShape)
  176. {
  177. case (int)otsdataconst.DOMAIN_SHAPE.ROUND:
  178. graphicsShape.DrawEllipse(new Pen(Color.Black), a_PixRect);
  179. break;
  180. case (int)otsdataconst.DOMAIN_SHAPE.RECTANGLE:
  181. if (a_bIsRand)
  182. {
  183. //graphicsShape.DrawRectangle(new Pen(Color.Black), a_PixRect, new Point(ROUND_VALUE, ROUND_VALUE));
  184. graphicsShape.DrawRectangle(new Pen(Color.Black), a_PixRect);
  185. }
  186. else
  187. {
  188. graphicsShape.DrawEllipse(new Pen(Color.Black), a_PixRect);
  189. }
  190. break;
  191. }
  192. }
  193. // get domain
  194. public CDomain GetDomain(String a_strValue)
  195. {
  196. // domain
  197. CDomain pDomain = null;
  198. // check value string
  199. a_strValue.Trim();
  200. if (a_strValue == "")
  201. {
  202. return pDomain;
  203. }
  204. // split the value string
  205. List<string> listStr = new List<string>(a_strValue.Split(','));
  206. pDomain = GetDomain(listStr);
  207. // return the domain pointer
  208. return pDomain;
  209. }
  210. CDomain GetDomain(List<string> a_listString)
  211. {
  212. // domain
  213. CDomain pDomain = null;
  214. // check value string list size
  215. if ((int)a_listString.Count != DOMAIN_ITEM_NUMBER)
  216. {
  217. return pDomain;
  218. }
  219. // shape
  220. int nValue;
  221. string strValue = a_listString[0];
  222. nValue = Convert.ToInt32(strValue);
  223. // check the shape value
  224. if (nValue < (int)DOMAIN_SHAPE.ROUND || nValue > (int)DOMAIN_SHAPE.RECTANGLE)
  225. {
  226. return pDomain;
  227. }
  228. DOMAIN_SHAPE nShape = (DOMAIN_SHAPE)nValue;
  229. // position
  230. strValue = a_listString[1];
  231. nValue = Convert.ToInt32(strValue);
  232. int nCentreX = nValue;
  233. strValue = a_listString[2];
  234. nValue = Convert.ToInt32(strValue);
  235. int nCentreY = nValue;
  236. // diameter/width
  237. strValue = a_listString[3];
  238. nValue = Convert.ToInt32(strValue);
  239. int nDiameter_Width = nValue;
  240. // height/spare
  241. strValue = a_listString[4];
  242. if (nShape == DOMAIN_SHAPE.RECTANGLE)
  243. {
  244. nValue = Convert.ToInt32(strValue);
  245. }
  246. int nDiameter_nHeight = nValue;
  247. // create domain
  248. Rectangle rectDomain = new Rectangle(0, 0, nDiameter_Width, nDiameter_nHeight);
  249. pDomain = new CDomain(nShape, rectDomain);
  250. pDomain.OffsetDomain(new Point(nCentreX - (nDiameter_Width / 2), nCentreY - (nDiameter_nHeight / 2)));
  251. // return the domain pointer
  252. return pDomain;
  253. }
  254. #region 画出X轴与Y轴
  255. /// <summary>
  256. /// 在任意的panel里画一个坐标,坐标所在的四边形距离panel边50像素
  257. /// </summary>
  258. /// <param name="pan"></param>
  259. public void DrawXY(Object a_pDC, double width,double height, double a_dPixSize,double coordinateSystemEndpointX, double coordinateSystemEndpointY)
  260. {
  261. if (a_pDC == null)
  262. {
  263. return;
  264. }
  265. Graphics g = (Graphics)a_pDC;
  266. //绘制X轴,
  267. double ObjectCenterX = width/ 2;
  268. double ObjectCenterY = height / 2;
  269. int X0 = (int)(coordinateSystemEndpointX / a_dPixSize + ObjectCenterX);
  270. int Y0 = (int)(0 / a_dPixSize + ObjectCenterY);
  271. int X1 = (int)(-coordinateSystemEndpointX / a_dPixSize + ObjectCenterX);
  272. int Y1 = (int)(0 / a_dPixSize + ObjectCenterY);
  273. Point px1 = new Point(X0, Y0);
  274. Point px2 = new Point(X1, Y1);
  275. Pen pen = new Pen(Brushes.Green, 1);
  276. g.DrawLine(pen, px1, px2);
  277. //绘制Y轴
  278. int X3 = (int)(0 / a_dPixSize + ObjectCenterX);
  279. int Y3 = (int)(coordinateSystemEndpointY / a_dPixSize + ObjectCenterY);
  280. int X4 = (int)(0 / a_dPixSize + ObjectCenterX);
  281. int Y4 = (int)(-coordinateSystemEndpointY / a_dPixSize + ObjectCenterY);
  282. PointF py1 = new PointF(X3, Y3);
  283. PointF py2 = new PointF(X4, Y4);
  284. g.DrawLine(pen, py1, py2);
  285. g.DrawString("0", new Font("宋体 ", 9f), Brushes.Green, new PointF((float)ObjectCenterX - 12, (float)ObjectCenterY + 3));
  286. int move = 5,len=10;
  287. for (int i = 1; i <= len; i++) //len等分Y正轴
  288. {
  289. Point px3 = new Point((int)ObjectCenterX + move, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSize) / len * i));
  290. Point px4 = new Point((int)ObjectCenterX, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSize) / len * i));
  291. string sx = ((int)(-(-coordinateSystemEndpointY) / len * i)).ToString();
  292. g.DrawLine(pen, px3, px4);
  293. StringFormat drawFormat = new StringFormat();
  294. drawFormat.Alignment = StringAlignment.Far;
  295. drawFormat.LineAlignment = StringAlignment.Center;
  296. g.DrawString(sx, new Font("宋体", 8f), Brushes.Green, new Point((int)ObjectCenterX - 5, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSize) / len * i)), drawFormat);
  297. }
  298. for (int i = 1; i <= len; i++)
  299. {
  300. Point px3 = new Point((int)ObjectCenterX + move, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSize) / len * i));
  301. Point px4 = new Point((int)ObjectCenterX, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSize) / len * i));
  302. string sx = ((int)((-coordinateSystemEndpointY) / len * i)).ToString();
  303. g.DrawLine(pen, px3, px4);
  304. StringFormat drawFormat = new StringFormat();
  305. drawFormat.Alignment = StringAlignment.Far;
  306. drawFormat.LineAlignment = StringAlignment.Center;
  307. g.DrawString(sx, new Font("宋体", 8f), Brushes.Green, new Point((int)ObjectCenterX - 5, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSize) / len * i)), drawFormat);
  308. }
  309. for (int i = 1; i <= len; i++) //len等分X正轴
  310. {
  311. Point px3 = new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSize) / len * i) , (int)(ObjectCenterY + move));
  312. Point px4 = new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSize) / len * i), (int)ObjectCenterY);
  313. string sx = ((int)(coordinateSystemEndpointX / len * i)).ToString();
  314. g.DrawLine(pen, px3, px4);
  315. StringFormat drawFormat = new StringFormat();
  316. drawFormat.Alignment = StringAlignment.Far;
  317. drawFormat.LineAlignment = StringAlignment.Center;
  318. g.DrawString(sx, new Font("宋体", 6f), Brushes.Green, new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSize) / len * i)+5, (int)(ObjectCenterY + 15)), drawFormat);
  319. }
  320. for (int i = 1; i <= len; i++)
  321. {
  322. Point px3 = new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSize) / len * i), (int)(ObjectCenterY + move));
  323. Point px4 = new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSize) / len * i), (int)(ObjectCenterY));
  324. string sx = ((int)((-coordinateSystemEndpointX) / len * i)).ToString();
  325. g.DrawLine(pen, px3, px4);
  326. StringFormat drawFormat = new StringFormat();
  327. drawFormat.Alignment = StringAlignment.Far;
  328. drawFormat.LineAlignment = StringAlignment.Center;
  329. g.DrawString(sx, new Font("宋体", 6f), Brushes.Green, new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSize) / len * i)+5, (int)(ObjectCenterY + 15)), drawFormat);
  330. }
  331. //g.Dispose();
  332. }
  333. #endregion
  334. }
  335. }