using OTSCLRINTERFACE; using OTSDataType; using OTSCommon; using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using OTSModelSharp.ServiceInterface; using OTSModelSharp.ServiceCenter; using OTSMeasureApp._4_OTSSamplespaceGraphicsPanel; namespace OTSMeasureApp { public class OTSSamplespaceGraphicsPanelFun { #region 鼠标移动位置 public static CRectangleGDIObject GetMouseMoveLocation(CRectangleGDIObject item, MouseEventArgs e) { item.Region = new Rectangle( item.Region.Left + e.X - item.DraggingPoint.X, item.Region.Top + e.Y - item.DraggingPoint.Y, item.Region.Width, item.Region.Height ); item.DraggingPoint = e.Location; return item; } #endregion #region 鼠标方法操作 public static RectangleF MouseWheelFunctionF(CRectangleGDIObject RectangleGDIObject, RectangleF itemRect, float globalZoomNum) { //缩放比例 float X = itemRect.X * globalZoomNum; float Y = itemRect.Y * globalZoomNum; float Width = itemRect.Width * globalZoomNum; float Height = itemRect.Height * globalZoomNum; return new RectangleF(X, Y, Width, Height); } const int Band = 5; #endregion #region 根据颜色枚举 获取颜色值 public static string GetColorValue(ColorType ColorEnum) { string ColorStr = string.Empty; switch (ColorEnum) { case ColorType.SingleColor: ColorStr = "#fd8f8f";//帧图fd8f8f break; case ColorType.RoundRectangleColor: ColorStr = "#c8c8c8";//c8c8c8圆角矩形 break; case ColorType.FontColor: ColorStr = "#90ee90";//90ee90文字颜色 break; case ColorType.SampleColor: ColorStr = "#f4f4f4";//f4f4f4 样品未选择 break; case ColorType.SampleSelColor: ColorStr = "#505050";//505050 样品选择后 break; } return ColorStr; } #endregion #region 根据list中的帧图信息 获取中心位置 public static PointF GetSingleCenterLocation(List m_SingleGDIObjects) { float MinX = 0f; float MinY = 0f; float MaxX = 0f; float MaxY = 0f; float width = 0f; float height = 0f; //获取最小XY 与最大XY MinX = m_SingleGDIObjects[0].RegionF.X; MinY = m_SingleGDIObjects[0].RegionF.Y; MaxX = m_SingleGDIObjects[0].RegionF.X; MaxY = m_SingleGDIObjects[0].RegionF.Y; width = m_SingleGDIObjects[0].RegionF.Width; height = m_SingleGDIObjects[0].RegionF.Height; for (int i = 0; i < m_SingleGDIObjects.Count; i++) { MinX = Math.Min(m_SingleGDIObjects[i].RegionF.X, MinX); MinY = Math.Min(m_SingleGDIObjects[i].RegionF.Y, MinY); MaxX = Math.Max(m_SingleGDIObjects[i].RegionF.X, MaxX); MaxY = Math.Max(m_SingleGDIObjects[i].RegionF.Y, MaxY); } //获取帧图的外框矩形 RectangleF SingleRect = new RectangleF(MinX, MinY, MaxX- MinX+ width, MaxY- MinY+ height); //计算外框矩形的中心点 float PointFX = SingleRect.X + SingleRect.Width / 2; float PointFY = SingleRect.Y + SingleRect.Height / 2; PointF SingleCenterPoint = new PointF(PointFX, PointFY); return SingleCenterPoint; } public static PointF GetSingleDrawCenterLocation(List m_SingleGDIObjects) { float MinX = 0f; float MinY = 0f; float MaxX = 0f; float MaxY = 0f; float width = 0f; float height = 0f; //获取最小XY 与最大XY MinX = m_SingleGDIObjects[0].DrawRegionF.X; MinY = m_SingleGDIObjects[0].DrawRegionF.Y; MaxX = m_SingleGDIObjects[0].DrawRegionF.X; MaxY = m_SingleGDIObjects[0].DrawRegionF.Y; width = m_SingleGDIObjects[0].DrawRegionF.Width; height = m_SingleGDIObjects[0].DrawRegionF.Height; for (int i = 0; i < m_SingleGDIObjects.Count; i++) { MinX = Math.Min(m_SingleGDIObjects[i].DrawRegionF.X, MinX); MinY = Math.Min(m_SingleGDIObjects[i].DrawRegionF.Y, MinY); MaxX = Math.Max(m_SingleGDIObjects[i].DrawRegionF.X, MaxX); MaxY = Math.Max(m_SingleGDIObjects[i].DrawRegionF.Y, MaxY); } //获取帧图的外框矩形 RectangleF SingleRect = new RectangleF(MinX, MinY, MaxX - MinX + width, MaxY - MinY + height); //计算外框矩形的中心点 float PointFX = SingleRect.X + SingleRect.Width / 2; float PointFY = SingleRect.Y + SingleRect.Height / 2; PointF SingleCenterPoint = new PointF(PointFX, PointFY); return SingleCenterPoint; } #endregion #region 将所有帧图移植测量区域位置 public static void SetSingleLocationToMeasureLocation(List m_ObjectListGDIObjects, CRectangleGDIObject m_ObjectGDIObjects) { //获取帧图中心与测量区域中心 PointF singleCenterPoinF= GetSingleCenterLocation(m_ObjectListGDIObjects); PointF singleDrawCenterPoinF = GetSingleDrawCenterLocation(m_ObjectListGDIObjects); //测量区域 已改变为倍数的尺寸 中心点 PointF measureCenterPoin = m_ObjectGDIObjects. GetCenterPoint(); //测量区域 正常尺寸 中心点 PointF measureCenterPoinF = m_ObjectGDIObjects. GetCenterPoint(); //计算相差的XY距离 float moveX = measureCenterPoin.X - singleCenterPoinF.X; float moveY = measureCenterPoin.Y - singleCenterPoinF.Y; float moveFX = measureCenterPoinF.X - singleDrawCenterPoinF.X; float moveFY = measureCenterPoinF.Y - singleDrawCenterPoinF.Y; //需要移动的位置 PointF movePoint = new PointF(moveX, moveY); PointF movePointF = new PointF(moveFX, moveFY); foreach (var ObjectItem in m_ObjectListGDIObjects) { //计算移动后的位置 float X = ObjectItem.RegionF.X + movePoint.X; float Y = ObjectItem.RegionF.Y + movePoint.Y; float Width = ObjectItem.RegionF.Width; float Height = ObjectItem.RegionF.Height; ObjectItem.Region = new Rectangle((int)X, (int)Y, (int)Width, (int)Height); ObjectItem.RegionF = new RectangleF(X, Y, Width, Height); ObjectItem.DrawRegionF = new RectangleF(ObjectItem.DrawRegionF.X + movePointF.X, ObjectItem.DrawRegionF.Y + movePointF.Y, ObjectItem.DrawRegionF.Width, ObjectItem.DrawRegionF.Height); } } #endregion #region 判断当前位置是否在不规则形状里面 /// /// 判断当前位置是否在不规则形状里面 /// /// 不规则形状坐标集合 /// 当前鼠标坐标 /// public static bool PositionPnpoly(List polygonPointList, Point currentMouseLocation) { double testx = currentMouseLocation.X; double testy = currentMouseLocation.Y; int nvert = polygonPointList.Count; List vertx = new List(); List verty = new List(); for (int pointIndex = 0; pointIndex < polygonPointList.Count; pointIndex++) { vertx.Add(polygonPointList[pointIndex].X); verty.Add(polygonPointList[pointIndex].Y); } int i, j, c = 0; for (i = 0, j = nvert - 1; i < nvert; j = i++) { if (((verty[i] > testy) != (verty[j] > testy)) && (testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i])) { c = 1 + c; ; } } if (c % 2 == 0) { return false; } else { return true; } } #endregion #region 获取多边形最小外接矩形 /// /// 获取多边形最小外接矩形 /// /// 不规则形状坐标集合 /// public static Rectangle GetPolygonToMinRectangle(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(); } #endregion } }