using PaintDotNet.Annotation.Enum; using PaintDotNet.Base.SettingModel; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Globalization; using System.Windows.Forms; using PaintDotNet.Base.CommTool; using PaintDotNet.Annotation.Measure; using System.Linq; namespace PaintDotNet.Annotation.Measure { using PointList = List; /// /// 测量->平行线测量->平行多点平行线 /// public class MeasureHMulParallelLine : MeasureDrawObject { public PointList pointArray; /// /// Graphic objects for hit test /// private GraphicsPath areaPath = null; private Pen areaPen = null; private Region areaRegion = null; /// /// 总长度 /// private double Length = 0.0; /// /// 测量信息矩形定义 /// private RectangleF rectangleF1 = new RectangleF(); private RectangleF rectangleF2 = new RectangleF(); private RectangleF rectangleF3 = new RectangleF(); private RectangleF rectangleF4 = new RectangleF(); private RectangleF rectangleF5 = new RectangleF(); /// /// 文本上用于拖动的点 /// private Point pointL = new Point(); /// /// 绘制限制(第一次绘制时) /// public bool pointChange = true; /// /// 区分移动文本 /// private int moveKb; /// /// 绘制限制(从配置文件加载) /// public bool configurationFile = false; /// /// 绘制限制(更改属性时) /// private bool SavePointChange; /// /// 多点绘制时的文本矩形集合 /// public List RectangleFList = new List(); /// /// 绘制属性 /// private string[] drawingPropertiesList; /// /// 绘制属性(克隆) /// private string[] drawingPropertiesListClone; private bool drawing; /// /// 属性绘制点 /// private PointF rotationPoint; private PointF rotationPoint1; /// /// 字体角度 /// private double angleRotationP = 0; private double angleRotationL = 0; private int num = 0; /// /// 限制绘制(绘制中) /// public bool mouseUpPointChange = false; /// /// 测量样式信息model /// private MeasureStyleModel.MeasureHMulParallelLine measureStyleModel; public MeasureStyleModel.MeasureHMulParallelLine MeasureStyleModel { set { this.measureStyleModel = value; } } public MeasureHMulParallelLine(ISurfaceBox surfaceBox, int x1, int y1, bool clone) : base(surfaceBox) { this.objectType = DrawClass.Measure; this.drawToolType = DrawToolType.MeasureHMulParallelLine; measureStyleModel = surfaceBox.GetMeasureStyleModel().measureHMulParallelLine; pointArray = new PointList(); startPoint.X = x1; startPoint.Y = y1; if (clone) { drawing = true; pointArray.Add(new Point(x1, y1)); } this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]); this.unitString = surfaceBox.GetPxPerUnit()[1]; this.unit = surfaceBox.GetPxPerUnit()[2]; Initialize(); } /// /// 测量属性 /// /// public override Dictionary GetData() { if (data.ContainsKey(MeasureAttributes.MeasureMethod)) data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.ParallelLines.Text"); else data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.ParallelLines.Text")); if (data.ContainsKey(MeasureAttributes.MeasureUnitCN)) data[MeasureAttributes.MeasureUnitCN] = this.unitString; else data.Add(MeasureAttributes.MeasureUnitCN, this.unitString); if (data.ContainsKey(MeasureAttributes.MeasureUnitEN)) data[MeasureAttributes.MeasureUnitEN] = this.unit; else data.Add(MeasureAttributes.MeasureUnitEN, this.unit); if (data.ContainsKey(MeasureAttributes.PixelStartX)) data[MeasureAttributes.PixelStartX] = startPoint.X; else data.Add(MeasureAttributes.PixelStartX, startPoint.X); if (data.ContainsKey(MeasureAttributes.PixelStartY)) data[MeasureAttributes.PixelStartY] = startPoint.Y; else data.Add(MeasureAttributes.PixelStartY, startPoint.Y); if (data.ContainsKey(MeasureAttributes.PhysicalStartX)) data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces); else data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces)); if (data.ContainsKey(MeasureAttributes.PhysicalStartY)) data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces); else data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces)); if (data.ContainsKey(MeasureAttributes.PixelLength)) data[MeasureAttributes.PixelLength] = Math.Round(Length, decimalPlaces); else data.Add(MeasureAttributes.PixelLength, Math.Round(Length, decimalPlaces)); string s = Math.Round(Length * unitLength, decimalPlaces).ToString(); if (s.IndexOf(".") == -1) { for (int i = 0; i < decimalPlaces; i++) { if (i == 0) s += "."; s += "0"; } } else { int dic = s.Length - s.IndexOf(".") - 1; if (dic < decimalPlaces) { for (int i = 0; i < decimalPlaces - dic; i++) { s += "0"; } } } if (data.ContainsKey(MeasureAttributes.PhysicalLength)) data[MeasureAttributes.PhysicalLength] = s; else data.Add(MeasureAttributes.PhysicalLength, s); if (data.ContainsKey(MeasureAttributes.PixelAverageDistance)) data[MeasureAttributes.PixelAverageDistance] = Math.Round(Length / (pointArray.Count - 2), decimalPlaces); else data.Add(MeasureAttributes.PixelAverageDistance, Math.Round(Length / (pointArray.Count - 2), decimalPlaces)); s = Math.Round(Length / (pointArray.Count - 2) * unitLength, decimalPlaces).ToString(); if (s.IndexOf(".") == -1) { for (int i = 0; i < decimalPlaces; i++) { if (i == 0) s += "."; s += "0"; } } else { int a = s.Length - s.IndexOf(".") - 1; if (a < decimalPlaces) { for (int i = 0; i < decimalPlaces - a; i++) { s += "0"; } } } if (data.ContainsKey(MeasureAttributes.PhysicalAverageDistance)) data[MeasureAttributes.PhysicalAverageDistance] = s; else data.Add(MeasureAttributes.PhysicalAverageDistance, s); return data; } public MeasureHMulParallelLine(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base() { this.objectType = DrawClass.Measure; this.drawToolType = DrawToolType.MeasureHMulParallelLine; this.ISurfaceBox = surfaceBox; measureStyleModel = (MeasureStyleModel.MeasureHMulParallelLine)parentStyleModel; pointArray = DrawRulerHelper.DeepCopyListByReflect(points); startPoint = points[0]; this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]); this.unitString = surfaceBox.GetPxPerUnit()[1]; this.unit = surfaceBox.GetPxPerUnit()[2]; for (int i = 0; i < (points.Count - 2) * 6; i++) { this.RectangleFList.Add(new RectangleF()); } this.configurationFile = true; } /// /// Clone this instance /// public override DrawObject Clone() { MeasureHMulParallelLine drawPolygon = new MeasureHMulParallelLine(ISurfaceBox, (int)pointArray[0].X, (int)pointArray[0].Y, false); drawPolygon.ISurfaceBox = ISurfaceBox; foreach (PointF p in this.pointArray) { drawPolygon.pointArray.Add(p); } if (drawingPropertiesList != null) drawPolygon.drawingPropertiesListClone = drawingPropertiesList; foreach (var rectangleF in RectangleFList) { drawPolygon.RectangleFList.Add(rectangleF); } FillDrawObjectFields(drawPolygon); return drawPolygon; } public override DrawObject Clone(ISurfaceBox surfaceBox) { MeasureHMulParallelLine drawPolygon = new MeasureHMulParallelLine(surfaceBox, this.GetPoints(), this.measureStyleModel, null); if (drawingPropertiesList != null) drawPolygon.drawingPropertiesListClone = drawingPropertiesList; drawPolygon.RectangleFList = DrawRulerHelper.DeepCopyListByReflect(RectangleFList); FillDrawObjectFields(drawPolygon); return drawPolygon; } private List pointFs = new PointList(); private List pointFs1 = new PointList(); public override void Draw(Graphics g) { lengthParagraphs = new List(); pointFs.Clear(); pointFs1.Clear(); drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList); if (drawingPropertiesList == null) { drawingPropertiesList = drawingPropertiesListClone; if (!this.drawing) this.configurationFile = true; } pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange); Font textfont1 = new Font(measureStyleModel.font, measureStyleModel.fontSize); Brush textbrush1 = new SolidBrush(Color.FromArgb(measureStyleModel.textColor)); // 总长度 double allLength = 0.0; SizeF sizeF = new SizeF(); measureStyleModel = this.ISurfaceBox.GetMeasureStyleModel().measureHMulParallelLine; int a1 = 0; int a2 = 1; int a3 = 2; int a4 = 3; int a5 = 4; int a6 = 5; RectangleF rectangleF; string frontStr = string.Empty; if (this.measureStyleModel.showSuffix) { frontStr = this.measureStyleModel.suffixName; } if (this.measureStyleModel.showSerial) { #region [获取序号] List newGraphicsList = new List(); int n = ISurfaceBox.GraphicsList.Count; int num = 0; for (int i = n - 1; i >= 0; i--) { newGraphicsList.Add(ISurfaceBox.GraphicsList[i].Clone()); } var newGraphics = newGraphicsList.Where(m => m.ID.Equals(this.ID)).FirstOrDefault(); if (newGraphics != null) { num = newGraphicsList.IndexOf(newGraphics) + 1; } #endregion frontStr = $"[{ num }]{frontStr}"; } if (this.measureStyleModel.showAlias) { frontStr = this.measureStyleModel.aliasName + frontStr; } // 连续绘制 for (int a = 1; a <= HandleCount - 2; a++) { if (HandleCount - 1 >= 2) { Font textfont = new Font(measureStyleModel.font, measureStyleModel.fontSize); Brush textbrush = new SolidBrush(Color.FromArgb(measureStyleModel.textColor)); g.SmoothingMode = SmoothingMode.AntiAlias; Color color = Color.FromArgb(this.measureStyleModel.lineColor); Pen pen = new Pen(color, this.measureStyleModel.lineWidth); pen.DashStyle = (DashStyle)this.measureStyleModel.lineStyle; if (this.Selected) { if (measureStyleModel.chooseStyle != null) { textfont = new Font(measureStyleModel.chooseStyle.font, measureStyleModel.chooseStyle.fontSize); textbrush = new SolidBrush(Color.FromArgb(measureStyleModel.chooseStyle.textColor)); pen = new Pen(Color.FromArgb(measureStyleModel.chooseStyle.lineColor), measureStyleModel.chooseStyle.lineWidth); pen.DashStyle = (DashStyle)measureStyleModel.chooseStyle.lineStyle; } } if (!this.Selected) { this.pointChange = true; rectangleF1 = new RectangleF(); rectangleF2 = new RectangleF(); rectangleF3 = new RectangleF(); rectangleF4 = new RectangleF(); rectangleF5 = new RectangleF(); } if (HandleCount - 1 == 2) { g.DrawLine(pen, pointArray[0], pointArray[1]); } if (HandleCount - 1 >= 3) { // 垂足坐标 PointF footPoint = new PointF(); footPoint.X = pointArray[0].X; footPoint.Y = pointArray[a].Y; //判断第三个点相对于第一个点的象限 int x3 = (int)(pointArray[a].X - pointArray[0].X); if (x3 == 0) x3 = 1; int y3 = (int)(pointArray[a].Y - pointArray[0].Y); if (y3 == 0) y3 = 1; int i3 = 0; if (x3 > 0 && y3 > 0) //第4象限 { i3 = 4; } else if (x3 > 0 && y3 < 0) //第1象限 { i3 = 1; } else if (x3 < 0 && y3 < 0) //第2象限 { i3 = 2; } else if (x3 < 0 && y3 > 0) //第3象限 { i3 = 3; } double angle = 0.0; if (i3 == 1 || i3 == 4) { angle = 0; } else { angle = 180; } // 点到直线距离 double length = BasicCalculationHelper.GetDistance(new PointF(pointArray[0].X, pointArray[a].Y), pointArray[a], 10); PointF point = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[0].X + (int)length, pointArray[0].Y), pointArray[0], angle); PointF point1 = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[1].X + (int)length, pointArray[1].Y), pointArray[1], angle); pointFs.Add(point); pointFs1.Add(point1); g.DrawLine(pen, new PointF(pointArray[0].X, pointArray[a].Y), pointArray[a]); g.DrawLine(pen, point, point1); // 总长度 allLength = allLength + length; if (a > 1) { int offsetValuePoint = measureStyleModel.fontSize + measureStyleModel.fontSize / 2; int offsetValue1Point = measureStyleModel.lineWidth * 2 / 3; int offsetPoint = offsetValue1Point; if (drawingPropertiesList != null) { // 像素长度 if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString())) { offsetPoint += offsetValuePoint; } // 物理长度 if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString())) { offsetPoint += offsetValuePoint; } // 像素起始点X if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString())) { offsetPoint += offsetValuePoint; } // 像素起始点Y if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString())) { offsetPoint += offsetValuePoint; } // 物理起始点X if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString())) { offsetPoint += offsetValuePoint; } // 物理起始点Y if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString())) { offsetPoint += offsetValuePoint; } } if (measureStyleModel.linePositionP == 0) this.rotationPoint1 = new PointF(footPoint.X, footPoint.Y - offsetPoint ); else if (measureStyleModel.linePositionP == 1) this.rotationPoint1 = new PointF((footPoint.X + pointArray[a].X) / 2, footPoint.Y - offsetPoint ); else if (measureStyleModel.linePositionP == 2) this.rotationPoint1 = new PointF(pointArray[a].X, pointArray[a].Y - offsetPoint ); else if (measureStyleModel.linePositionP == 3) this.rotationPoint1 = new PointF( footPoint.X, footPoint.Y + offsetValue1Point); else if (measureStyleModel.linePositionP == 4) this.rotationPoint1 = new PointF((footPoint.X + pointArray[a].X) / 2, (footPoint.Y + pointArray[a].Y) / 2 + offsetValue1Point); else if (measureStyleModel.linePositionP == 5) this.rotationPoint1 = new PointF(pointArray[a].X, pointArray[a].Y + offsetValue1Point); int offsetValue = measureStyleModel.fontSize + measureStyleModel.fontSize / 2; int offsetValue1 = measureStyleModel.lineWidth * 2 / 3; int offset = 0; if (drawingPropertiesList != null) { // 像素长度 if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString())) { rectangleF = this.RectangleFList[a1]; sizeF = g.MeasureString("" + Math.Round(length, decimalPlaces) + "px", textfont1); rectangleF.Width = sizeF.Width; rectangleF.Height = sizeF.Height; if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute) { rectangleF.Location = new Point((int)this.rotationPoint1.X - (int)sizeF.Width / 2, (int)this.rotationPoint1.Y - offset); offset += offsetValue; this.RectangleFList[a1] = rectangleF; } g.DrawString("" + Math.Round(length, decimalPlaces) + "px", textfont1, textbrush1, this.RectangleFList[a1]); a1 += 6; } else { this.RectangleFList[a1] = new RectangleF(); a1 += 6; } string s = Math.Round(length * unitLength, decimalPlaces).ToString(); if (s.IndexOf(".") == -1) { for (int i = 0; i < decimalPlaces; i++) { if (i == 0) s += "."; s += "0"; } } else { int dic = s.Length - s.IndexOf(".") - 1; if (dic < decimalPlaces) { for (int i = 0; i < decimalPlaces - dic; i++) { s += "0"; } } } lengthParagraphs.Add(s); // 物理长度 if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString())) { rectangleF = this.RectangleFList[a2]; if (a == 2) { sizeF = g.MeasureString(frontStr + "" + s + this.unit, textfont1); } else { sizeF = g.MeasureString("" + s + this.unit, textfont1); } sizeF = g.MeasureString(s + this.unit, textfont1); rectangleF.Width = sizeF.Width; rectangleF.Height = sizeF.Height; if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute) { rectangleF.Location = new Point((int)this.rotationPoint1.X - (int)sizeF.Width / 2, (int)this.rotationPoint1.Y - offset); offset += offsetValue; this.RectangleFList[a2] = rectangleF; } if (a == 2) { g.DrawString(frontStr + "" + s + this.unit, textfont1, textbrush1, this.RectangleFList[a2]); } else { g.DrawString("" + s + this.unit, textfont1, textbrush1, this.RectangleFList[a2]); } a2 += 6; } else { this.RectangleFList[a2] = new RectangleF(); a2 += 6; } // 像素起始点X if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString())) { rectangleF = this.RectangleFList[a3]; sizeF = g.MeasureString("" + pointArray[a].X, textfont1); rectangleF.Width = sizeF.Width; rectangleF.Height = sizeF.Height; if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute) { rectangleF.Location = new Point((int)this.rotationPoint1.X - (int)sizeF.Width / 2, (int)this.rotationPoint1.Y - offset); offset += offsetValue; this.RectangleFList[a3] = rectangleF; } g.DrawString("" + pointArray[a].X, textfont1, textbrush1, this.RectangleFList[a3]); a3 += 6; } else { this.RectangleFList[a3] = new RectangleF(); a3 += 6; } // 像素起始点Y if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString())) { rectangleF = this.RectangleFList[a4]; sizeF = g.MeasureString("" + pointArray[a].Y, textfont1); rectangleF.Width = sizeF.Width; rectangleF.Height = sizeF.Height; if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute) { rectangleF.Location = new Point((int)this.rotationPoint1.X - (int)sizeF.Width / 2, (int)this.rotationPoint1.Y - offset); offset += offsetValue; this.RectangleFList[a4] = rectangleF; } g.DrawString("" + pointArray[a].Y, textfont1, textbrush1, this.RectangleFList[a4]); a4 += 6; } else { this.RectangleFList[a4] = new RectangleF(); a4 += 6; } // 物理起始点X if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString())) { rectangleF = this.RectangleFList[a5]; sizeF = g.MeasureString("" + Math.Round(pointArray[a].X * unitLength, decimalPlaces), textfont1); rectangleF.Width = sizeF.Width; rectangleF.Height = sizeF.Height; if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute) { rectangleF.Location = new Point((int)this.rotationPoint1.X - (int)sizeF.Width / 2, (int)this.rotationPoint1.Y - offset); offset += offsetValue; this.RectangleFList[a5] = rectangleF; } g.DrawString("" + Math.Round(pointArray[a].X * unitLength, decimalPlaces), textfont1, textbrush1, this.RectangleFList[a5]); a5 += 6; } else { this.RectangleFList[a5] = new RectangleF(); a5 += 6; } // 物理起始点Y if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString())) { rectangleF = this.RectangleFList[a6]; sizeF = g.MeasureString("" + Math.Round(pointArray[a].Y * unitLength, decimalPlaces), textfont1); rectangleF.Width = sizeF.Width; rectangleF.Height = sizeF.Height; if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute) { rectangleF.Location = new Point((int)this.rotationPoint1.X - (int)sizeF.Width / 2, (int)this.rotationPoint1.Y - offset); offset += offsetValue; this.RectangleFList[a6] = rectangleF; } g.DrawString("" + Math.Round(pointArray[a].Y * unitLength, decimalPlaces), textfont1, textbrush1, this.RectangleFList[a6]); a6 += 6; } else { this.RectangleFList[a6] = new RectangleF(); a6 += 6; } } } pen.Dispose(); } } } if(pointArray.Count > 1) { Matrix mtxSave1 = g.Transform; Matrix matrix1 = g.Transform; int count = 0; if (pointArray.Count == 2) count = 1; else count = pointArray.Count - 2; // 是否绘制 if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute) { if (measureStyleModel.linePositionL == 0) { this.rotationPoint = pointArray[1]; angleRotationL = -90; } else if (measureStyleModel.linePositionL == 1) { this.rotationPoint = new PointF((pointArray[1].X + pointArray[0].X) / 2, (pointArray[0].Y + pointArray[1].Y) / 2); angleRotationL = -90; } else if (measureStyleModel.linePositionL == 2) { this.rotationPoint = pointArray[0]; angleRotationL = -90; } else if (measureStyleModel.linePositionL == 3) { this.rotationPoint = pointArray[1]; angleRotationL = 90; } else if (measureStyleModel.linePositionL == 4) { this.rotationPoint = new PointF((pointArray[1].X + pointArray[0].X) / 2, (pointArray[0].Y + pointArray[1].Y) / 2); angleRotationL = 90; } else if (measureStyleModel.linePositionL == 5) { this.rotationPoint = pointArray[0]; angleRotationL = 90; } // 文本间隔定义 int offsetValue = measureStyleModel.fontSize + measureStyleModel.fontSize / 2; int offsetValue1 = measureStyleModel.lineWidth * 2 / 3; int offset = offsetValue + offsetValue1; this.pointL = new Point((int)this.rotationPoint.X, (int)this.rotationPoint.Y); if (drawingPropertiesList != null) { // 像素平均距离 if (drawingPropertiesList.Contains(MeasureAttributes.PixelAverageDistance.ToString())) { sizeF = g.MeasureString(frontStr + Math.Round(Length / count, decimalPlaces) + "px", textfont1); rectangleF4.Location = new Point((int)this.rotationPoint.X - (int)sizeF.Width / 2, (int)this.rotationPoint.Y - offset); offset += offsetValue; } // 物理平均距离 if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalAverageDistance.ToString())) { string s = Math.Round(Length / count * unitLength, decimalPlaces).ToString(); if (s.IndexOf(".") == -1) { for (int i = 0; i < decimalPlaces; i++) { if (i == 0) s += "."; s += "0"; } } else { int a = s.Length - s.IndexOf(".") - 1; if (a < decimalPlaces) { for (int i = 0; i < decimalPlaces - a; i++) { s += "0"; } } } sizeF = g.MeasureString(frontStr + s + this.unit, textfont1); rectangleF5.Location = new Point((int)this.rotationPoint.X - (int)sizeF.Width / 2, (int)this.rotationPoint.Y - offset); offset += offsetValue; } // 测量方式 if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString())) { sizeF = g.MeasureString(frontStr + PdnResources.GetString("Menu.MeasureAction.ParallelLines.Text"), textfont1); rectangleF1.Location = new Point((int)this.rotationPoint.X - (int)sizeF.Width / 2, (int)this.rotationPoint.Y - offset); offset += offsetValue; } // 测量单位(中文) if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString())) { sizeF = g.MeasureString(frontStr + this.unitString, textfont1); rectangleF2.Location = new Point((int)this.rotationPoint.X - (int)sizeF.Width / 2, (int)this.rotationPoint.Y - offset); offset += offsetValue; } // 测量单位(英文) if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString())) { sizeF = g.MeasureString(frontStr + this.unit, textfont1); rectangleF3.Location = new Point((int)this.rotationPoint.X - (int)sizeF.Width / 2, (int)this.rotationPoint.Y - offset); } } } matrix1.RotateAt((float)angleRotationL, rotationPoint); g.Transform = matrix1; if (drawingPropertiesList != null) { // 测量方式 if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString())) { sizeF = g.MeasureString(frontStr + PdnResources.GetString("Menu.MeasureAction.ParallelLines.Text"), textfont1); rectangleF1.Width = sizeF.Width; rectangleF1.Height = sizeF.Height; g.DrawString(frontStr + PdnResources.GetString("Menu.MeasureAction.ParallelLines.Text"), textfont1, textbrush1, rectangleF1); } else { rectangleF1 = new RectangleF(); } // 测量单位(中文) if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString())) { sizeF = g.MeasureString(frontStr + this.unitString, textfont1); rectangleF2.Width = sizeF.Width; rectangleF2.Height = sizeF.Height; g.DrawString(frontStr + this.unitString, textfont1, textbrush1, rectangleF2); } else { rectangleF2 = new RectangleF(); } // 测量单位(英文) if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString())) { sizeF = g.MeasureString(frontStr + this.unit, textfont1); rectangleF3.Width = sizeF.Width; rectangleF3.Height = sizeF.Height; g.DrawString(frontStr + this.unit, textfont1, textbrush1, rectangleF3); } else { rectangleF3 = new RectangleF(); } // 像素平均距离 if (drawingPropertiesList.Contains(MeasureAttributes.PixelAverageDistance.ToString())) { sizeF = g.MeasureString(frontStr + Math.Round(Length / count, decimalPlaces) + "px", textfont1); rectangleF4.Width = sizeF.Width; rectangleF4.Height = sizeF.Height; g.DrawString(frontStr + Math.Round(Length / count, decimalPlaces) + "px", textfont1, textbrush1, rectangleF4); } else { rectangleF4 = new RectangleF(); } // 物理平均距离 if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalAverageDistance.ToString())) { string s = Math.Round(Length / count * unitLength, decimalPlaces).ToString(); if (s.IndexOf(".") == -1) { for (int i = 0; i < decimalPlaces; i++) { if (i == 0) s += "."; s += "0"; } } else { int a = s.Length - s.IndexOf(".") - 1; if (a < decimalPlaces) { for (int i = 0; i < decimalPlaces - a; i++) { s += "0"; } } } sizeF = g.MeasureString(frontStr + s + this.unit, textfont1); rectangleF5.Width = sizeF.Width; rectangleF5.Height = sizeF.Height; g.DrawString(frontStr + s + this.unit, textfont1, textbrush1, rectangleF5); } else { rectangleF5 = new RectangleF(); } } g.Transform = mtxSave1; matrix1.Dispose(); // 长度文本显示 //g.DrawString(Math.Round(allLength * unitLength, decimalPlaces) + unit, textfont1, textbrush1, pointArray[0]); this.Length = allLength; if (this.configurationFile) this.pointChange = false; this.mouseUpAttribute = false; pointChangeObject.Remove(this.drawToolType); } } /// 停止绘制时 /// /// public override void MouseUp(bool up) { mouseUpPointChange = up; } public void AddPoint(PointF point) { pointArray.Add(point); } public override int HandleCount { get { return pointArray.Count + 1; } } /// /// Get handle pointscroll by 1-based number /// /// /// public override PointF GetHandle(int handleNumber) { if (handleNumber < 1) handleNumber = 1; if (handleNumber > pointArray.Count && handleNumber != pointArray.Count + 1) handleNumber = pointArray.Count; if (handleNumber == pointArray.Count + 1) return this.pointL; else return pointArray[handleNumber - 1]; } public override Cursor GetHandleCursor(int handleNumber) { return handleCursor; } public override void MoveHandleTo(Point point, int handleNumber) { if (handleNumber < 1) handleNumber = 1; if (handleNumber > pointArray.Count && handleNumber != pointArray.Count + 1) handleNumber = pointArray.Count; if (handleNumber == pointArray.Count + 1) { if(this.moveKb == 1 || this.moveKb == 2 || this.moveKb == 3 || this.moveKb == 4 || this.moveKb == 5) { point = SetOffsetAfterRotation(point, this.rotationPoint, angleRotationL); if (this.moveKb == 1) this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y); else if (this.moveKb == 2) this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y); else if (this.moveKb == 3) this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y); else if (this.moveKb == 4) this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y); else if (this.moveKb == 5) this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y); } else { for (int i = 0; i < this.RectangleFList.Count; i++) { num = i / 6; if (num + 1 < pointArray.Count) { if (measureStyleModel.linePositionP == 0) { this.rotationPoint1 = pointArray[num + 1]; } else if (measureStyleModel.linePositionP == 1) { this.rotationPoint1 = new PointF((pointArray[num + 1].X + pointFs[num].X) / 2, (pointFs[num].Y + pointArray[num + 1].Y) / 2); } else if (measureStyleModel.linePositionP == 2) { this.rotationPoint1 = pointFs[num]; } else if (measureStyleModel.linePositionP == 3) { this.rotationPoint1 = pointArray[num + 1]; } else if (measureStyleModel.linePositionP == 4) { this.rotationPoint1 = new PointF((pointArray[num + 1].X + pointFs[num].X) / 2, (pointFs[num].Y + pointArray[num + 1].Y) / 2); } else if (measureStyleModel.linePositionP == 5) { this.rotationPoint1 = pointArray[num]; } RectangleF rectangleF = this.RectangleFList[i]; if (this.moveKb == -i) { point = SetOffsetAfterRotation(point, this.rotationPoint1, angleRotationP); rectangleF.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y); this.RectangleFList[i] = rectangleF; break; } } } } this.pointL = point; } else { this.mouseUpPointChange = true; if (handleNumber == 1) { this.rectangleF1.Offset(pointArray[0].X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); this.rectangleF2.Offset(pointArray[0].X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); this.rectangleF3.Offset(pointArray[0].X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); this.rectangleF4.Offset(pointArray[0].X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); this.rectangleF5.Offset(pointArray[0].X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); pointArray[0] = new PointF(pointArray[0].X, point.Y); } else if (handleNumber == 2) { pointArray[1] = new PointF(pointArray[1].X, point.Y); } else { for (int i = 1; i <= this.RectangleFList.Count; i++) { if (i == handleNumber && handleNumber != 2) { RectangleF rectangleF = this.RectangleFList[i * 6 - 18]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 18] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 17]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 17] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 16]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 16] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 15]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 15] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 14]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 14] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 13]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 13] = rectangleF; } } pointArray[handleNumber - 1] = point; } } Invalidate(); } public override void Move(int deltaX, int deltaY) { int n = pointArray.Count; PointF point; for (int i = 0; i < n; i++) { point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY)); pointArray[i] = point; } this.startPoint = pointArray[0]; int x = ISurfaceBox.UnscaleScalar(deltaX); int y = ISurfaceBox.UnscaleScalar(deltaY); pointL.X += x; pointL.Y += y; this.rectangleF1.Offset(x, y); this.rectangleF2.Offset(x, y); this.rectangleF3.Offset(x, y); this.rectangleF4.Offset(x, y); this.rectangleF5.Offset(x, y); for (int i = 0; i < this.RectangleFList.Count; i++) { RectangleF rectangleF = this.RectangleFList[i]; rectangleF.Offset(x, y); this.RectangleFList[i] = rectangleF; } Invalidate(); } /// /// 用于创建一个路径或者是闭合的范围 /// 用于响应点击选中 /// 需要咨询用户是仅点击线还是矩形选择 /// protected virtual void CreateObjects() { if (AreaPath != null) return; AreaPath = new GraphicsPath(); AreaPath.AddRectangle(GetBoundingBox()); AreaPath.CloseFigure(); AreaRegion = new Region(AreaPath); } /// /// Invalidate object. /// When object is invalidated, path used for hit test /// is released and should be created again. /// protected void Invalidate() { if (AreaPath != null) { AreaPath.Dispose(); AreaPath = null; } if (AreaPen != null) { AreaPen.Dispose(); AreaPen = null; } if (AreaRegion != null) { AreaRegion.Dispose(); AreaRegion = null; } } protected GraphicsPath AreaPath { get { return areaPath; } set { areaPath = value; } } protected Pen AreaPen { get { return areaPen; } set { areaPen = value; } } protected Region AreaRegion { get { return areaRegion; } set { areaRegion = value; } } /// /// Draw tracker for selected object /// /// public override void DrawTracker(Graphics g) { if (!Selected) return; SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor)); Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth); for (int i = 1; i <= HandleCount; i++) { if (i == HandleCount) { brush = new SolidBrush(Color.Transparent); pen = new Pen(Color.Transparent); } switch (MarkpointStyle) { case 0: g.FillRectangle(brush, GetHandleRectangle(i)); g.DrawRectangle(pen, GetHandleRectangle(i)); break; case 1: g.FillEllipse(brush, GetHandleRectangle(i)); g.DrawEllipse(pen, GetHandleRectangle(i)); break; case 2: g.FillPolygon(brush, GetHandlePoint(i)); g.DrawPolygon(pen, GetHandlePoint(i)); break; } } brush.Dispose(); pen.Dispose(); } /// /// Hit test. /// Return value: -1 - no hit /// 0 - hit anywhere /// > 1 - handle number /// /// /// public override int HitTest(Point point) { if (Selected) { for (int i = 1; i <= HandleCount; i++) { if (GetHandleRectangle(i).Contains(point)) return i; } if (GetRectanglePosition(this.rectangleF1, point, this.rotationPoint, angleRotationL)) { this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, angleRotationL); moveKb = 1; return this.pointArray.Count + 1; } else if (GetRectanglePosition(this.rectangleF2, point, this.rotationPoint, angleRotationL)) { this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, angleRotationL); moveKb = 2; return this.pointArray.Count + 1; } else if (GetRectanglePosition(this.rectangleF3, point, this.rotationPoint, angleRotationL)) { this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, angleRotationL); moveKb = 3; return this.pointArray.Count + 1; } else if (GetRectanglePosition(this.rectangleF4, point, this.rotationPoint, angleRotationL)) { this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, angleRotationL); moveKb = 4; return this.pointArray.Count + 1; } else if (GetRectanglePosition(this.rectangleF5, point, this.rotationPoint, angleRotationL)) { this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, angleRotationL); moveKb = 5; return this.pointArray.Count + 1; } for (int i = 0; i < this.RectangleFList.Count; i++) { num = i / 6; RectangleF rectangleF = this.RectangleFList[i]; if (num + 1 < pointArray.Count) { if (measureStyleModel.linePositionP == 0) { this.rotationPoint1 = pointArray[num + 1]; } else if (measureStyleModel.linePositionP == 1) { this.rotationPoint1 = new PointF((pointArray[num + 1].X + pointFs[num].X) / 2, (pointFs[num].Y + pointArray[num + 1].Y) / 2); } else if (measureStyleModel.linePositionP == 2) { this.rotationPoint1 = pointFs[num]; } else if (measureStyleModel.linePositionP == 3) { this.rotationPoint1 = pointArray[num + 1]; } else if (measureStyleModel.linePositionP == 4) { this.rotationPoint1 = new PointF((pointArray[num + 1].X + pointFs[num].X) / 2, (pointFs[num].Y + pointArray[num + 1].Y) / 2); } else if (measureStyleModel.linePositionP == 5) { this.rotationPoint1 = pointArray[num]; } if (GetRectanglePosition(rectangleF, point, this.rotationPoint1, angleRotationP)) { this.pointL = SetOffsetAfterRotation(point, this.rotationPoint1, angleRotationP); moveKb = -i; return this.pointArray.Count + 1; } } } } if (PointInObject(point)) return 0; return -1; } protected override bool PointInObject(Point point) { CreateObjects(); return AreaRegion.IsVisible(point); } public override bool IntersectsWith(Rectangle rectangle) { CreateObjects(); return AreaRegion.IsVisible(rectangle); } public override RectangleF GetBoundingBox() { Rectangle rectangle; int minx = 0, maxx = 0, miny = 0, maxy = 0; for (int i = 0; i < pointArray.Count; i++) { if (i == 0) { minx = maxx = (int)pointArray[i].X; miny = maxy = (int)pointArray[i].Y; } else { if (pointArray[i].X > maxx) maxx = (int)pointArray[i].X; if (pointArray[i].X < minx) minx = (int)pointArray[i].X; if (pointArray[i].Y > maxy) maxy = (int)pointArray[i].Y; if (pointArray[i].Y < miny) miny = (int)pointArray[i].Y; } } rectangle = new Rectangle(minx, miny, maxx - minx, maxy - miny); return rectangle; } internal void setNextPoint(PointF p) { AddPoint(p); //startPoint = endPoint; //endPoint = p; } internal void setEndPoint(Point p) { endPoint = p; } public override List GetPoints() { return pointArray; } public override ParentStyleModel GetStyle() { return measureStyleModel; } } }