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 m_PolygonPoints = new List(); //绘制时与移动缩放时记录的位置 private List m_originalPolygonPoints = new List(); private PointF startPoint; private PointF endPoint; protected List subItems = new List(); 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 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 SubItems() { if (subItems == null) subItems = new List(); return subItems; } virtual public void AddSubItems(CVisualFieldGDIObject item) { if (subItems == null) subItems = new List(); 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 PolygonPointF = new List(); 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(); var OriginalPs = new List(); 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(); 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(); 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 GetOriginalPolygonPointFList() { return m_originalPolygonPoints; } public void SetOriginalPolygonPointFList(List value) { if (m_zoomNum != 1) { var ps = new List(); 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 GetPolygonPointFList() { return m_PolygonPoints; } public void SetPolygonPointFList(List value) { if (m_zoomNum != 1) { var ps = new List(); 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 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; } } }