123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- 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<PointF>;
- public class DrawMulLineA : DrawObject
- {
- public PointList pointArray;
- private const string entryLength = "Length";
- private const string entryPoint = "Point";
- /// <summary>
- /// Graphic objects for hit test
- /// </summary>
- private GraphicsPath areaPath = null;
- private Pen areaPen = null;
- private Region areaRegion = null;
- /// <summary>
- /// 长度
- /// </summary>
- private double allLength = 0.0;
- /// <summary>
- /// 测量信息矩形定义
- /// </summary>
- private Point pointL = new Point();
- public bool pointChange = true;
- private int moveKb;
- public bool configurationFile = false;
- public List<RectangleF> RectangleFList = new List<RectangleF>();
- 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<PointF> 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;
- }
- /// <summary>
- /// Clone this instance
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// Get handle pointscroll by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 用于创建一个路径或者是闭合的范围
- /// 用于响应点击选中
- /// 需要咨询用户是仅点击线还是矩形选择
- /// </summary>
- protected virtual void CreateObjects()
- {
- if (AreaPath != null)
- return;
- AreaPath = new GraphicsPath();
- AreaPath.AddRectangle(GetBoundingBox());
- AreaPath.CloseFigure();
- AreaRegion = new Region(AreaPath);
- }
- /// <summary>
- /// Invalidate object.
- /// When object is invalidated, path used for hit test
- /// is released and should be created again.
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// Draw tracker for selected object
- /// </summary>
- /// <param name="g"></param>
- 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);
- }
- /// <summary>
- /// Hit test.
- /// Return value: -1 - no hit
- /// 0 - hit anywhere
- /// > 1 - handle number
- /// </summary>
- /// <param name="pointscroll"></param>
- /// <returns></returns>
- 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<PointF> GetPoints()
- {
- return pointArray;
- }
- public override ParentStyleModel GetStyle()
- {
- return labelCircleStyleModel;
- }
- }
- }
|