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 System.Net.NetworkInformation; using System.Collections; using System.Linq; using PaintDotNet.Annotation.Measure; namespace PaintDotNet.Annotation.PhysicalPhaseAction { using static PaintDotNet.Base.SettingModel.LabelStyleModel; using PointList = List; public class DrawMulLineA : DrawObject { public PointList pointArray; private const string entryLength = "Length"; private const string entryPoint = "Point"; /// /// Graphic objects for hit test /// private GraphicsPath areaPath = null; private Pen areaPen = null; private Region areaRegion = null; /// /// 长度 /// private double allLength = 0.0; /// /// 测量信息矩形定义 /// private Point pointL = new Point(); public bool pointChange = true; private int moveKb; public bool configurationFile = false; public List RectangleFList = new List(); public DrawAnalysisModel labelCircleStyleModel; public DrawMulLineA(ISurfaceBox surfaceBox, int x1, int y1) : base() { this.objectType = DrawClass.Label; this.drawToolType = DrawToolType.DrawMulLineA; labelCircleStyleModel = surfaceBox.AnalysisStyleModel; pointArray = new PointList(); startPoint.X = x1; startPoint.Y = y1; pointArray.Add(new Point(x1, y1)); Initialize(); } public DrawMulLineA(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base() { this.objectType = DrawClass.Label; this.drawToolType = DrawToolType.DrawMulLineA; this.ISurfaceBox = surfaceBox; labelCircleStyleModel = (DrawAnalysisModel)parentStyleModel; pointArray = points; this.configurationFile = true; } /// /// Clone this instance /// public override DrawObject Clone() { DrawMulLineA drawPolygon = new DrawMulLineA(ISurfaceBox, 0, 0); drawPolygon.ISurfaceBox = ISurfaceBox; foreach (PointF p in this.pointArray) { drawPolygon.pointArray.Add(p); } FillDrawObjectFields(drawPolygon); return drawPolygon; } public override void Draw(Graphics g) { Matrix mtxSave1 = g.Transform; Matrix matrix1 = g.Transform; double alllength = 0.0; double length = 0.0; int pxPerUnit = 1; if (this.pointChange) { this.pointL = new Point((int)pointArray[0].X, (int)pointArray[0].Y); } for (int i = 1; i <= pointArray.Count - 1; i++) { Pen linePen = new Pen(Color.FromArgb(labelCircleStyleModel.lineColor), labelCircleStyleModel.lineWidth); linePen.DashStyle = (DashStyle)labelCircleStyleModel.lineStyle; g.SmoothingMode = SmoothingMode.AntiAlias; length = BasicCalculationHelper.GetDistance(pointArray[i - 1], pointArray[i], 10) / pxPerUnit; g.DrawLine(linePen, pointArray[i - 1], pointArray[i]); alllength = alllength + length; linePen.Dispose(); } this.allLength = alllength; if (this.configurationFile) this.pointChange = false; } public void AddPoint(Point 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) { switch (handleNumber) { case 1: return Cursors.SizeNWSE; case 2: return Cursors.SizeNS; case 3: return Cursors.SizeNESW; case 4: return Cursors.SizeWE; case 5: return Cursors.SizeNWSE; case 6: return Cursors.SizeNS; case 7: return Cursors.SizeNESW; case 8: return Cursors.SizeWE; default: return Cursors.Default; } } public override void MoveHandleTo(Point point, int handleNumber) { double k = 0.0; if (handleNumber < 1) handleNumber = 1; if (handleNumber > pointArray.Count && handleNumber != pointArray.Count + 1) handleNumber = pointArray.Count; if (handleNumber > 2) { if (handleNumber == pointArray.Count + 1) { //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++) // { // RectangleF rectangleF = this.RectangleFList[i]; // if (this.moveKb == -i) // rectangleF.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y); // this.RectangleFList[i] = rectangleF; // } //} //this.pointL = point; } else { // 求直线斜率 if (pointArray[0].X != pointArray[1].X && pointArray[0].Y != pointArray[1].Y) { k = (double)(pointArray[1].Y - pointArray[0].Y) / (pointArray[1].X - pointArray[0].X); point.Y = (int)(k * (point.X - pointArray[handleNumber - 2].X) + pointArray[handleNumber - 2].Y); for (int i = 1; i <= this.RectangleFList.Count; i++) { if (i == handleNumber) { RectangleF rectangleF = this.RectangleFList[i * 6 - 12]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 12] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 11]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 11] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 10]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 10] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 9]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 9] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 8]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 8] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 7]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 7] = rectangleF; } } pointArray[handleNumber - 1] = point; } if (pointArray[0].X == pointArray[1].X) { point.X = (int)pointArray[0].X; for (int i = 1; i <= this.RectangleFList.Count; i++) { if (i == handleNumber) { RectangleF rectangleF = this.RectangleFList[i * 6 - 12]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 12] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 11]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 11] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 10]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 10] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 9]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 9] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 8]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 8] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 7]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 7] = rectangleF; } } pointArray[handleNumber - 1] = point; } if (pointArray[0].Y == pointArray[1].Y) { point.Y = (int)pointArray[0].Y; for (int i = 1; i <= this.RectangleFList.Count; i++) { if (i == handleNumber) { RectangleF rectangleF = this.RectangleFList[i * 6 - 12]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 12] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 11]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 11] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 10]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 10] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 9]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 9] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 8]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 8] = rectangleF; rectangleF = this.RectangleFList[i * 6 - 7]; rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y); this.RectangleFList[i * 6 - 7] = rectangleF; } } pointArray[handleNumber - 1] = point; } } } else { if (handleNumber == 1) { //this.rectangleF1.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); //this.rectangleF2.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); //this.rectangleF3.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); //this.rectangleF4.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); //this.rectangleF5.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y); } else if (handleNumber == 2) { RectangleF rectangleF = this.RectangleFList[0]; rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y); this.RectangleFList[0] = rectangleF; rectangleF = this.RectangleFList[1]; rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y); this.RectangleFList[1] = rectangleF; rectangleF = this.RectangleFList[2]; rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y); this.RectangleFList[2] = rectangleF; rectangleF = this.RectangleFList[3]; rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y); this.RectangleFList[3] = rectangleF; rectangleF = this.RectangleFList[4]; rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y); this.RectangleFList[4] = rectangleF; rectangleF = this.RectangleFList[5]; rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y); this.RectangleFList[5] = rectangleF; } pointArray[handleNumber - 1] = point; } this.startPoint = pointArray[0]; Invalidate(); } public override void Move(int deltaX, int deltaY) { //for (int i = 0; i < pointArray.Count; i++) //{ // PointF 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(); } public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber) { info.AddValue( String.Format(CultureInfo.InvariantCulture, "{0}{1}", entryLength, orderNumber), pointArray.Count); int i = 0; foreach (PointF p in pointArray) { info.AddValue( String.Format(CultureInfo.InvariantCulture, "{0}{1}-{2}", entryPoint, orderNumber, i++), p); } base.SaveToStream(info, orderNumber); // ?? } public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber) { Point point; int n = info.GetInt32( String.Format(CultureInfo.InvariantCulture, "{0}{1}", entryLength, orderNumber)); for (int i = 0; i < n; i++) { point = (Point)info.GetValue( String.Format(CultureInfo.InvariantCulture, "{0}{1}-{2}", entryPoint, orderNumber, i), typeof(Point)); pointArray.Add(point); } base.LoadFromStream(info, orderNumber); } /// /// 用于创建一个路径或者是闭合的范围 /// 用于响应点击选中 /// 需要咨询用户是仅点击线还是矩形选择 /// 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.Black); for (int i = 1; i <= HandleCount; i++) { if (i == HandleCount) brush = new SolidBrush(Color.Transparent); g.FillRectangle(brush, GetHandleRectangle(i)); } //g.DrawRectangle(new Pen(Color.White), GetBoundingBox()); brush.Dispose(); RectangleF r = GetBoundingBox(); g.DrawRectangle(new Pen(Color.White), r.X, r.Y, r.Width, r.Height); } /// /// 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; } for (int i = 0; i < this.RectangleFList.Count; i++) { RectangleF rectangleF = this.RectangleFList[i]; if (rectangleF.Contains(point)) { this.pointL = point; moveKb = -i; return this.pointArray.Count; } } } 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(Point 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 labelCircleStyleModel; } } }