using OTSDataType; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using static OTSDataType.otsdataconst; namespace OTSMeasureApp._7_OTSProgMgrInfo.Stage { class StageDisplayHelp { public const int RATIO_RECT_LEFT = 22; //ratio rect left edge public const int PIC_EDGE = 12; //edge width public const int RATIO_RECT_HEIGHT = 30; //ratio rect height public const int RATIO_RECT_DOWN = 10; //ratio rect down edge public const int DOMAIN_ITEM_NUMBER = 5; public StageDisplayHelp() { } public void DrawStageBoundery(CStage a_pStage, int a_nWidth, int a_nHeight, Object pDC, double a_dPixSize) { if (a_pStage == null) { return; } CDomain pBoundery = a_pStage.GetBoundary(); otsdataconst.DOMAIN_SHAPE nShape = (otsdataconst.DOMAIN_SHAPE)(-1); nShape = pBoundery.GetShape(); Rectangle PixRect = new Rectangle(); GetPixRect(pBoundery, a_nWidth, a_nHeight, a_dPixSize, ref PixRect); DrawShape(PixRect, pDC, nShape, true); } // draw stage hole public void DrawStageHole(CStage a_pStage, int a_nWidth, int a_nHeight, Object pDC, double a_dPixSize) { if (a_pStage == null) { return; } if (a_pStage.GetHoleList().Count == 0) { return; } foreach (var pHole in a_pStage.GetHoleList()) { otsdataconst.DOMAIN_SHAPE nShape = (otsdataconst.DOMAIN_SHAPE)(-1); nShape = pHole.GetShape(); Rectangle PixRect = new Rectangle(); GetPixRect(pHole, a_nWidth, a_nHeight, a_dPixSize, ref PixRect); DrawShape(PixRect, pDC, nShape, true); int nHeight = PixRect.Height * 2 / 3; //设置文字对齐方式 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; //文字颜色 string ColorStr = "#90ee90"; Color myColor = ColorTranslator.FromHtml(ColorStr); System.Drawing.SolidBrush sampleBrush = new System.Drawing.SolidBrush(myColor); SolidBrush solidBrush = new SolidBrush(Color.FromArgb(50, Color.Black)); //字体大小 根据样品孔Rectangle大小 float fontSize = PixRect.Width / 4; Font font = new Font("宋体", fontSize, FontStyle.Regular); if (fontSize == 0) { font = new Font("宋体", fontSize, FontStyle.Regular); } //绘制文字阴影 Rectangle rectFont = PixRect; rectFont.X += 2; rectFont.Y += 2; ((Graphics)pDC).DrawString(pHole.GetName(), font, solidBrush, rectFont, sf); ((Graphics)pDC).DrawString(pHole.GetName(), font, sampleBrush, PixRect, sf); } } // draw ratio public void DrawRatio(int a_nBounderyWidth, int a_nWidth, int a_nHeight, Object pDC, IntPtr pWnd, Rectangle rc) { if (pDC == null) { return; } if (pWnd == null) { return; } int nRatio = a_nBounderyWidth * (a_nWidth / 2 - RATIO_RECT_LEFT - 10) / (a_nWidth - PIC_EDGE * 2) / 1000; //显示在标尺上的mm数 //string sRatio; Point oRatioStart = new Point(); oRatioStart.X = RATIO_RECT_LEFT; oRatioStart.Y = a_nHeight - RATIO_RECT_HEIGHT - RATIO_RECT_DOWN; Point oRatioEnd = new Point(); oRatioEnd.X = a_nWidth / 2; oRatioEnd.Y = a_nHeight - RATIO_RECT_DOWN; } // get pixle rect public bool GetPixRect(CDomain a_pDomain, int a_nWidth, int a_nHeight, double a_dPixSize, ref Rectangle PixRect) { if (a_pDomain == null) { return false; } RectangleF DomainRect = a_pDomain.GetDomainRect(); int CenterX = a_nWidth / 2; //pixle center int CenterY = a_nHeight / 2; PointF DomainCenter = a_pDomain.GetDomainCenter(); // um center Point PixCenter = new Point(); PixCenter.X = (int)((double)DomainCenter.X / a_dPixSize); PixCenter.Y = (int)((double)DomainCenter.Y / a_dPixSize); // um to pixle int PixRectCenterX = CenterX + PixCenter.X; int PixRectCenterY = CenterY - PixCenter.Y;//OTS y dirction is different with pixle direction int delteX = PixRectCenterX - PixCenter.X; int delteY = PixRectCenterY - PixCenter.Y; int Left = (int)(DomainRect.Left / a_dPixSize + delteX); int Top = (int)(DomainRect.Top / a_dPixSize + delteY); int Right = (int)(DomainRect.Right / a_dPixSize + delteX); int Bottom = (int)(DomainRect.Bottom / a_dPixSize + delteY); PixRect.Location = new Point(Left, Top); PixRect.Size = new Size(Math.Abs(Right - Left), Math.Abs(Bottom - Top)); return true; } // draw shape public void DrawShape(Rectangle a_PixRect, Object a_pDC, otsdataconst.DOMAIN_SHAPE a_nShape, bool a_bIsRand) { if (a_pDC == null) { return; } Graphics graphicsShape = (Graphics)a_pDC; switch ((int)a_nShape) { case (int)otsdataconst.DOMAIN_SHAPE.ROUND: graphicsShape.DrawEllipse(new Pen(Color.Black), a_PixRect); break; case (int)otsdataconst.DOMAIN_SHAPE.RECTANGLE: if (a_bIsRand) { //graphicsShape.DrawRectangle(new Pen(Color.Black), a_PixRect, new Point(ROUND_VALUE, ROUND_VALUE)); graphicsShape.DrawRectangle(new Pen(Color.Black), a_PixRect); } else { graphicsShape.DrawEllipse(new Pen(Color.Black), a_PixRect); } break; } } // get domain public CDomain GetDomain(String a_strValue) { // domain CDomain pDomain = null; // check value string a_strValue.Trim(); if (a_strValue == "") { return pDomain; } // split the value string List listStr = new List(a_strValue.Split(',')); pDomain = GetDomain(listStr); // return the domain pointer return pDomain; } CDomain GetDomain(List a_listString) { // domain CDomain pDomain = null; // check value string list size if ((int)a_listString.Count != DOMAIN_ITEM_NUMBER) { return pDomain; } // shape int nValue; string strValue = a_listString[0]; nValue = Convert.ToInt32(strValue); // check the shape value if (nValue < (int)DOMAIN_SHAPE.ROUND || nValue > (int)DOMAIN_SHAPE.RECTANGLE) { return pDomain; } DOMAIN_SHAPE nShape = (DOMAIN_SHAPE)nValue; // position strValue = a_listString[1]; nValue = Convert.ToInt32(strValue); int nCentreX = nValue; strValue = a_listString[2]; nValue = Convert.ToInt32(strValue); int nCentreY = nValue; // diameter/width strValue = a_listString[3]; nValue = Convert.ToInt32(strValue); int nDiameter_Width = nValue; // height/spare strValue = a_listString[4]; if (nShape == DOMAIN_SHAPE.RECTANGLE) { nValue = Convert.ToInt32(strValue); } int nDiameter_nHeight = nValue; // create domain Rectangle rectDomain = new Rectangle(0, 0, nDiameter_Width, nDiameter_nHeight); pDomain = new CDomain(nShape, rectDomain); pDomain.OffsetDomain(new Point(nCentreX - (nDiameter_Width / 2), nCentreY - (nDiameter_nHeight / 2))); // return the domain pointer return pDomain; } #region 画出X轴与Y轴 /// /// 在任意的panel里画一个坐标,坐标所在的四边形距离panel边50像素 /// /// public void DrawXY(Object a_pDC, double width,double height, double a_dPixSize,double coordinateSystemEndpointX, double coordinateSystemEndpointY) { if (a_pDC == null) { return; } Graphics g = (Graphics)a_pDC; //绘制X轴, double ObjectCenterX = width/ 2; double ObjectCenterY = height / 2; int X0 = (int)(coordinateSystemEndpointX / a_dPixSize + ObjectCenterX); int Y0 = (int)(0 / a_dPixSize + ObjectCenterY); int X1 = (int)(-coordinateSystemEndpointX / a_dPixSize + ObjectCenterX); int Y1 = (int)(0 / a_dPixSize + ObjectCenterY); Point px1 = new Point(X0, Y0); Point px2 = new Point(X1, Y1); Pen pen = new Pen(Brushes.Green, 1); g.DrawLine(pen, px1, px2); //绘制Y轴 int X3 = (int)(0 / a_dPixSize + ObjectCenterX); int Y3 = (int)(coordinateSystemEndpointY / a_dPixSize + ObjectCenterY); int X4 = (int)(0 / a_dPixSize + ObjectCenterX); int Y4 = (int)(-coordinateSystemEndpointY / a_dPixSize + ObjectCenterY); PointF py1 = new PointF(X3, Y3); PointF py2 = new PointF(X4, Y4); g.DrawLine(pen, py1, py2); g.DrawString("0", new Font("宋体 ", 9f), Brushes.Green, new PointF((float)ObjectCenterX - 12, (float)ObjectCenterY + 3)); int move = 5,len=10; for (int i = 1; i <= len; i++) //len等分Y正轴 { Point px3 = new Point((int)ObjectCenterX + move, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSize) / len * i)); Point px4 = new Point((int)ObjectCenterX, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSize) / len * i)); string sx = ((int)(-(-coordinateSystemEndpointY) / len * i)).ToString(); g.DrawLine(pen, px3, px4); StringFormat drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Far; drawFormat.LineAlignment = StringAlignment.Center; g.DrawString(sx, new Font("宋体", 8f), Brushes.Green, new Point((int)ObjectCenterX - 5, (int)(ObjectCenterY + (-coordinateSystemEndpointY / a_dPixSize) / len * i)), drawFormat); } for (int i = 1; i <= len; i++) { Point px3 = new Point((int)ObjectCenterX + move, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSize) / len * i)); Point px4 = new Point((int)ObjectCenterX, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSize) / len * i)); string sx = ((int)((-coordinateSystemEndpointY) / len * i)).ToString(); g.DrawLine(pen, px3, px4); StringFormat drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Far; drawFormat.LineAlignment = StringAlignment.Center; g.DrawString(sx, new Font("宋体", 8f), Brushes.Green, new Point((int)ObjectCenterX - 5, (int)(ObjectCenterY + (coordinateSystemEndpointY / a_dPixSize) / len * i)), drawFormat); } for (int i = 1; i <= len; i++) //len等分X正轴 { Point px3 = new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSize) / len * i) , (int)(ObjectCenterY + move)); Point px4 = new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSize) / len * i), (int)ObjectCenterY); string sx = ((int)(coordinateSystemEndpointX / len * i)).ToString(); g.DrawLine(pen, px3, px4); StringFormat drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Far; drawFormat.LineAlignment = StringAlignment.Center; g.DrawString(sx, new Font("宋体", 6f), Brushes.Green, new Point((int)(ObjectCenterX + (coordinateSystemEndpointX / a_dPixSize) / len * i)+5, (int)(ObjectCenterY + 15)), drawFormat); } for (int i = 1; i <= len; i++) { Point px3 = new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSize) / len * i), (int)(ObjectCenterY + move)); Point px4 = new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSize) / len * i), (int)(ObjectCenterY)); string sx = ((int)((-coordinateSystemEndpointX) / len * i)).ToString(); g.DrawLine(pen, px3, px4); StringFormat drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Far; drawFormat.LineAlignment = StringAlignment.Center; g.DrawString(sx, new Font("宋体", 6f), Brushes.Green, new Point((int)(ObjectCenterX + (-coordinateSystemEndpointX / a_dPixSize) / len * i)+5, (int)(ObjectCenterY + 15)), drawFormat); } //g.Dispose(); } #endregion } }