| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 | using NLog.Fluent;using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;using System.Windows.Forms;using Point = System.Drawing.Point;using Rectangle = System.Drawing.Rectangle;namespace OTSMeasureApp._4_OTSSamplespaceGraphicsPanel.VisualGDIObjects{    public class CMeasureArea : CDisplayGDIObject    {        private ShapeType myshape;        private List<PointF> m_PolygonPoints = new List<PointF>();        //绘制时与移动缩放时记录的位置        private List<PointF> m_originalPolygonPoints = new List<PointF>();        private PointF startPoint;        private PointF endPoint;        protected List<CVisualFieldGDIObject> subItems = new List<CVisualFieldGDIObject>();        public CMeasureArea()        {        }        public CMeasureArea(RectangleF rectMeasure, ShapeType shape, string name, string sampleName, Color selColor)        {            m_OrigineRegionF = rectMeasure;            SetInitRegionF(m_OrigineRegionF);            SampleName = sampleName;            HoleNo = name;            myshape = shape;            SelColor = selColor;                                  ID = System.Guid.NewGuid().ToString();            OTSX = -1;            OTSY = -1;        }        public CMeasureArea(List<PointF> mPoint, ShapeType shape, string name, string sampleName, Color selColor)        {            ID = System.Guid.NewGuid().ToString();            this.SetOriginalPolygonPointFList(mPoint);            base.HoleNo=name;            base.sampleName=sampleName;            myshape = shape;            SelColor = selColor;        }        public List<CVisualFieldGDIObject> SubItems()        {            if (subItems == null) subItems = new List<CVisualFieldGDIObject>();            return subItems;        }        virtual public void AddSubItems(CVisualFieldGDIObject item)        {            if (subItems == null) subItems = new List<CVisualFieldGDIObject>();            item.SequenceNum = subItems.Count ;            subItems.Add(item);        }        public void ClearSubItems()        {            subItems.Clear();        }        public override void OnPaint(PaintEventArgs e)        {            Color myColor = selColor;            System.Drawing.SolidBrush sampleBrush = new System.Drawing.SolidBrush(myColor);            Pen p = new Pen(myColor, 1);            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;  //图片柔顺模式选择            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;//再加一点            switch (myshape)            {                case ShapeType.CIRCLE:                    e.Graphics.DrawEllipse(p, m_RegionF);                    break;                case ShapeType.RECTANGLE:                    var m_Region = new System.Drawing.Rectangle();                    m_Region.X = (int)m_RegionF.X;                    m_Region.Y = (int)m_RegionF.Y;                    m_Region.Width = (int)m_RegionF.Width;                    m_Region.Height = (int)m_RegionF.Height;                    e.Graphics.DrawRectangle(p, m_Region);                    break;                case ShapeType.POLYGON:                    myColor = selColor;                                     Pen pen = new Pen(myColor, 1);                    if (GetPolygonPointFList().Count > 0)                    {                        List<PointF> PolygonPointF = new List<PointF>();                        foreach (var item in m_PolygonPoints)                        {                            PolygonPointF.Add(item);                        }                        if (PolygonDrawingEndPoint.X != 0 && PolygonDrawingEndPoint.Y != 0)                        {                            PolygonPointF.Add(PolygonDrawingEndPoint);                        }                        if (PolygonPointF.Count > 1)                        {                            e.Graphics.DrawLines(pen, PolygonPointF.ToArray());                        }                                           }                    break;            }            if (subItems.Count != 0)            {                foreach (var item in subItems)                {                    item.OnPaint(e);                }            }        }        override public PointF DraggingPoint        {            get { return m_DraggingPoint; }            set            {                m_DraggingPoint = value;                if (subItems.Count != 0)                {                    foreach (var item in subItems)                    {                        item.DraggingPoint = m_DraggingPoint;                    }                }            }        }        override public void PositionAltering(PointF location, bool ifZoomCoord)        {            if (myshape == ShapeType.POLYGON)            {                PointF offset = new PointF(location.X - DraggingPoint.X, location.Y - DraggingPoint.Y);                PointF realShift;                if (ifZoomCoord)                {                    realShift = new PointF(offset.X / m_zoomNum, offset.Y / m_zoomNum);                }                else                {                    realShift = new PointF(offset.X, offset.Y);                }                m_OrigineRegionF = new RectangleF(m_OrigineRegionF.X + realShift.X, m_OrigineRegionF.Y + realShift.Y, m_OrigineRegionF.Width, m_OrigineRegionF.Height);                SetInitRegionF(m_OrigineRegionF);                var scalePs = new List<PointF>();                var OriginalPs = new List<PointF>();                foreach (var p in m_PolygonPoints)                {                    float x, y;                    x = (p.X + offset.X);                    y = (p.Y + offset.Y);                    scalePs.Add(new PointF(x, y));                }                m_PolygonPoints = scalePs;                foreach (var p in m_originalPolygonPoints)                {                    OriginalPs.Add(new PointF(p.X + realShift.X, p.Y + realShift.Y));                }                m_originalPolygonPoints = OriginalPs;                m_DraggingPoint = new Point((int)location.X, (int)location.Y);                            }            else            {                base.PositionAltering(location, ifZoomCoord);            }            if (subItems.Count != 0)            {                                foreach (var g in subItems)                {                    g.PositionAltering(location, ifZoomCoord);                }            }        }        override public void Move(PointF location)        {            if (myshape == ShapeType.POLYGON)            {                PointF offset = new PointF(location.X - DraggingPoint.X, location.Y - DraggingPoint.Y);                var scalePs = new List<PointF>();                foreach (var p in m_PolygonPoints)                {                    float x, y;                    x = (p.X + offset.X);                    y = (p.Y + offset.Y);                    scalePs.Add(new PointF(x, y));                }                m_PolygonPoints = scalePs;                               m_DraggingPoint = new Point((int)location.X, (int)location.Y);            }            else            {                base.Move(location);            }            if (this.subItems.Count != 0)            {                foreach (var g in subItems)                {                    g.Move(location);                }            }        }        override public void Zoom(PointF mousePoint, float zoomNum)        {            if (myshape == ShapeType.POLYGON)            {                float curZoom = m_zoomNum;                float deltaZoom = zoomNum - curZoom;                if (zoomNum == 1)                {                    m_zoomNum = 1;                    m_RegionF = m_OrigineRegionF;                    m_refPoint = new PointF(0, 0);                    m_PolygonPoints = m_originalPolygonPoints;                                  }                else                {                    m_refPoint.X = (m_refPoint.X - mousePoint.X) / curZoom * deltaZoom + m_refPoint.X;                    m_refPoint.Y = (m_refPoint.Y - mousePoint.Y) / curZoom * deltaZoom + m_refPoint.Y;                    var scalePs = new List<PointF>();                    foreach (var p in m_PolygonPoints)                    {                        float x, y;                        x = (p.X - mousePoint.X) / curZoom * deltaZoom + p.X;                        y = (p.Y - mousePoint.Y) / curZoom * deltaZoom + p.Y;                        scalePs.Add(new PointF(x, y));                    }                    m_PolygonPoints = scalePs;                    m_zoomNum = zoomNum;                }            }            else            {                base.Zoom(mousePoint, zoomNum);            }            if (this.subItems.Count != 0)            {                foreach (var g in subItems)                {                    g.Zoom(mousePoint, zoomNum);                }            }        }        override public void SetZoomNumber(float value)        {            base.SetZoomNumber(value);            if (m_originalPolygonPoints.Count != 0)            {                this.SetPolygonPointFList(m_originalPolygonPoints);            }        }        public override GraphicsPath GetGPath()        {                        //重新绘制测量区域路径            GraphicsPath GPath ;            if (myshape == ShapeType.POLYGON)            {                GraphicsPath PolygonMeasurePath = new GraphicsPath();                PolygonMeasurePath.AddPolygon(this.GetPolygonPointFList().ToArray());                GPath = PolygonMeasurePath;            }            else            {                GPath= base.GetGPath();            }            return GPath;        }        public List<PointF> GetOriginalPolygonPointFList()        { return m_originalPolygonPoints; }        public void SetOriginalPolygonPointFList(List<PointF> value)        {            if (m_zoomNum != 1)            {                var ps = new List<PointF>();                foreach (var p in value)                {                    var p1 = new PointF();                    p1.X = p.X * m_zoomNum + m_refPoint.X;                    p1.Y = p.Y * m_zoomNum + m_refPoint.Y;                    ps.Add(p1);                }                m_PolygonPoints = ps;                m_originalPolygonPoints = value;            }            else            {                m_originalPolygonPoints = value;                m_PolygonPoints = value;            }        }        public List<PointF> GetPolygonPointFList()        { return m_PolygonPoints; }        public void SetPolygonPointFList(List<PointF> value)        {            if (m_zoomNum != 1)            {                var ps = new List<PointF>();                foreach (var p in value)                {                    var p1 = new PointF();                    p1.X = (p.X - m_refPoint.X) / m_zoomNum;                    p1.Y = (p.Y - m_refPoint.Y) / m_zoomNum;                    ps.Add(p1);                }                m_originalPolygonPoints = ps;                m_PolygonPoints = value;            }            else            {                m_originalPolygonPoints = value;                m_PolygonPoints = value;            }            var region = GetMinRectangleOfPolygon(m_PolygonPoints);            SetZoomedRegionF(region);        }        private Rectangle GetMinRectangleOfPolygon(List<PointF> polygonPointList)        {            if (polygonPointList != null)            {                if (polygonPointList.Count > 0)                {                    int pCount = polygonPointList.Count;                    float minX = polygonPointList[0].X;                    float minY = polygonPointList[0].Y;                    float maxX = polygonPointList[0].X;                    float maxY = polygonPointList[0].Y;                    //获取最小X,Y 最大X,Y                    for (int i = 0; i < pCount; i++)                    {                        minX = Math.Min(minX, polygonPointList[i].X);                        minY = Math.Min(minY, polygonPointList[i].Y);                        maxX = Math.Max(maxX, polygonPointList[i].X);                        maxY = Math.Max(maxY, polygonPointList[i].Y);                    }                    //创建外接矩形                    Rectangle rect = new Rectangle();                    rect.Location = new Point((int)minX, (int)minY);                    rect.Size = new Size((int)maxX - (int)minX, (int)maxY - (int)minY);                    return rect;                }            }            return new Rectangle();        }        public PointF PolygonDrawingEndPoint        {            get { return endPoint; }            set { endPoint = value; }        }        public ShapeType Myshape { get => myshape; set => myshape = value; }    }}
 |