OTSSamplespaceGraphicsPanelFun.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using OTSCLRINTERFACE;
  2. using OTSDataType;
  3. using OTSCommon;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Windows.Forms;
  10. using OTSModelSharp.ServiceInterface;
  11. using OTSModelSharp.ServiceCenter;
  12. using OTSMeasureApp._4_OTSSamplespaceGraphicsPanel;
  13. namespace OTSMeasureApp
  14. {
  15. public class OTSSamplespaceGraphicsPanelFun
  16. {
  17. #region 鼠标移动位置
  18. public static CRectangleGDIObject GetMouseMoveLocation(CRectangleGDIObject item, MouseEventArgs e)
  19. {
  20. item.Region = new Rectangle(
  21. item.Region.Left + e.X - item.DraggingPoint.X,
  22. item.Region.Top + e.Y - item.DraggingPoint.Y,
  23. item.Region.Width,
  24. item.Region.Height);
  25. item.DraggingPoint = e.Location;
  26. return item;
  27. }
  28. #endregion
  29. #region 鼠标方法操作
  30. public static RectangleF MouseWheelFunctionF(CRectangleGDIObject RectangleGDIObject, RectangleF itemRect, float globalZoomNum)
  31. {
  32. //缩放比例
  33. float X = itemRect.X * globalZoomNum;
  34. float Y = itemRect.Y * globalZoomNum;
  35. float Width = itemRect.Width * globalZoomNum;
  36. float Height = itemRect.Height * globalZoomNum;
  37. return new RectangleF(X, Y, Width, Height);
  38. }
  39. const int Band = 5;
  40. #endregion
  41. #region 根据颜色枚举 获取颜色值
  42. public static string GetColorValue(ColorType ColorEnum)
  43. {
  44. string ColorStr = string.Empty;
  45. switch (ColorEnum)
  46. {
  47. case ColorType.SingleColor:
  48. ColorStr = "#fd8f8f";//帧图fd8f8f
  49. break;
  50. case ColorType.RoundRectangleColor:
  51. ColorStr = "#c8c8c8";//c8c8c8圆角矩形
  52. break;
  53. case ColorType.FontColor:
  54. ColorStr = "#90ee90";//90ee90文字颜色
  55. break;
  56. case ColorType.SampleColor:
  57. ColorStr = "#f4f4f4";//f4f4f4 样品未选择
  58. break;
  59. case ColorType.SampleSelColor:
  60. ColorStr = "#505050";//505050 样品选择后
  61. break;
  62. }
  63. return ColorStr;
  64. }
  65. #endregion
  66. #region 根据list中的帧图信息 获取中心位置
  67. public static PointF GetSingleCenterLocation(List<CRectangleGDIObject> m_SingleGDIObjects)
  68. {
  69. float MinX = 0f;
  70. float MinY = 0f;
  71. float MaxX = 0f;
  72. float MaxY = 0f;
  73. float width = 0f;
  74. float height = 0f;
  75. //获取最小XY 与最大XY
  76. MinX = m_SingleGDIObjects[0].RegionF.X;
  77. MinY = m_SingleGDIObjects[0].RegionF.Y;
  78. MaxX = m_SingleGDIObjects[0].RegionF.X;
  79. MaxY = m_SingleGDIObjects[0].RegionF.Y;
  80. width = m_SingleGDIObjects[0].RegionF.Width;
  81. height = m_SingleGDIObjects[0].RegionF.Height;
  82. for (int i = 0; i < m_SingleGDIObjects.Count; i++)
  83. {
  84. MinX = Math.Min(m_SingleGDIObjects[i].RegionF.X, MinX);
  85. MinY = Math.Min(m_SingleGDIObjects[i].RegionF.Y, MinY);
  86. MaxX = Math.Max(m_SingleGDIObjects[i].RegionF.X, MaxX);
  87. MaxY = Math.Max(m_SingleGDIObjects[i].RegionF.Y, MaxY);
  88. }
  89. //获取帧图的外框矩形
  90. RectangleF SingleRect = new RectangleF(MinX, MinY, MaxX- MinX+ width, MaxY- MinY+ height);
  91. //计算外框矩形的中心点
  92. float PointFX = SingleRect.X + SingleRect.Width / 2;
  93. float PointFY = SingleRect.Y + SingleRect.Height / 2;
  94. PointF SingleCenterPoint = new PointF(PointFX, PointFY);
  95. return SingleCenterPoint;
  96. }
  97. public static PointF GetSingleDrawCenterLocation(List<CRectangleGDIObject> m_SingleGDIObjects)
  98. {
  99. float MinX = 0f;
  100. float MinY = 0f;
  101. float MaxX = 0f;
  102. float MaxY = 0f;
  103. float width = 0f;
  104. float height = 0f;
  105. //获取最小XY 与最大XY
  106. MinX = m_SingleGDIObjects[0].DrawRegionF.X;
  107. MinY = m_SingleGDIObjects[0].DrawRegionF.Y;
  108. MaxX = m_SingleGDIObjects[0].DrawRegionF.X;
  109. MaxY = m_SingleGDIObjects[0].DrawRegionF.Y;
  110. width = m_SingleGDIObjects[0].DrawRegionF.Width;
  111. height = m_SingleGDIObjects[0].DrawRegionF.Height;
  112. for (int i = 0; i < m_SingleGDIObjects.Count; i++)
  113. {
  114. MinX = Math.Min(m_SingleGDIObjects[i].DrawRegionF.X, MinX);
  115. MinY = Math.Min(m_SingleGDIObjects[i].DrawRegionF.Y, MinY);
  116. MaxX = Math.Max(m_SingleGDIObjects[i].DrawRegionF.X, MaxX);
  117. MaxY = Math.Max(m_SingleGDIObjects[i].DrawRegionF.Y, MaxY);
  118. }
  119. //获取帧图的外框矩形
  120. RectangleF SingleRect = new RectangleF(MinX, MinY, MaxX - MinX + width, MaxY - MinY + height);
  121. //计算外框矩形的中心点
  122. float PointFX = SingleRect.X + SingleRect.Width / 2;
  123. float PointFY = SingleRect.Y + SingleRect.Height / 2;
  124. PointF SingleCenterPoint = new PointF(PointFX, PointFY);
  125. return SingleCenterPoint;
  126. }
  127. #endregion
  128. #region 将所有帧图移植测量区域位置
  129. public static void SetSingleLocationToMeasureLocation(List<CRectangleGDIObject> m_ObjectListGDIObjects, CRectangleGDIObject m_ObjectGDIObjects)
  130. {
  131. //获取帧图中心与测量区域中心
  132. PointF singleCenterPoinF= GetSingleCenterLocation(m_ObjectListGDIObjects);
  133. PointF singleDrawCenterPoinF = GetSingleDrawCenterLocation(m_ObjectListGDIObjects);
  134. //测量区域 已改变为倍数的尺寸 中心点
  135. PointF measureCenterPoin = m_ObjectGDIObjects. GetCenterPoint();
  136. //测量区域 正常尺寸 中心点
  137. PointF measureCenterPoinF = m_ObjectGDIObjects. GetCenterPoint();
  138. //计算相差的XY距离
  139. float moveX = measureCenterPoin.X - singleCenterPoinF.X;
  140. float moveY = measureCenterPoin.Y - singleCenterPoinF.Y;
  141. float moveFX = measureCenterPoinF.X - singleDrawCenterPoinF.X;
  142. float moveFY = measureCenterPoinF.Y - singleDrawCenterPoinF.Y;
  143. //需要移动的位置
  144. PointF movePoint = new PointF(moveX, moveY);
  145. PointF movePointF = new PointF(moveFX, moveFY);
  146. foreach (var ObjectItem in m_ObjectListGDIObjects)
  147. {
  148. //计算移动后的位置
  149. float X = ObjectItem.RegionF.X + movePoint.X;
  150. float Y = ObjectItem.RegionF.Y + movePoint.Y;
  151. float Width = ObjectItem.RegionF.Width;
  152. float Height = ObjectItem.RegionF.Height;
  153. ObjectItem.Region = new Rectangle((int)X, (int)Y, (int)Width, (int)Height);
  154. ObjectItem.RegionF = new RectangleF(X, Y, Width, Height);
  155. ObjectItem.DrawRegionF = new RectangleF(ObjectItem.DrawRegionF.X + movePointF.X, ObjectItem.DrawRegionF.Y + movePointF.Y, ObjectItem.DrawRegionF.Width, ObjectItem.DrawRegionF.Height);
  156. }
  157. }
  158. #endregion
  159. #region 判断当前位置是否在不规则形状里面
  160. /// <summary>
  161. /// 判断当前位置是否在不规则形状里面
  162. /// </summary>
  163. /// <param name="PointList">不规则形状坐标集合</param>
  164. /// <param name="currentMouseLocation">当前鼠标坐标</param>
  165. /// <returns></returns>
  166. public static bool PositionPnpoly(List<Point> polygonPointList, Point currentMouseLocation)
  167. {
  168. double testx = currentMouseLocation.X;
  169. double testy = currentMouseLocation.Y;
  170. int nvert = polygonPointList.Count;
  171. List<double> vertx = new List<double>();
  172. List<double> verty = new List<double>();
  173. for (int pointIndex = 0; pointIndex < polygonPointList.Count; pointIndex++)
  174. {
  175. vertx.Add(polygonPointList[pointIndex].X);
  176. verty.Add(polygonPointList[pointIndex].Y);
  177. }
  178. int i, j, c = 0;
  179. for (i = 0, j = nvert - 1; i < nvert; j = i++)
  180. {
  181. if (((verty[i] > testy) != (verty[j] > testy)) && (testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))
  182. {
  183. c = 1 + c; ;
  184. }
  185. }
  186. if (c % 2 == 0)
  187. {
  188. return false;
  189. }
  190. else
  191. {
  192. return true;
  193. }
  194. }
  195. #endregion
  196. #region 获取多边形最小外接矩形
  197. /// <summary>
  198. /// 获取多边形最小外接矩形
  199. /// </summary>
  200. /// <param name="polygonPointList">不规则形状坐标集合</param>
  201. /// <returns></returns>
  202. public static Rectangle GetPolygonToMinRectangle(List<PointF> polygonPointList)
  203. {
  204. if (polygonPointList != null)
  205. {
  206. if (polygonPointList.Count > 0)
  207. {
  208. int pCount = polygonPointList.Count;
  209. float minX = polygonPointList[0].X;
  210. float minY = polygonPointList[0].Y;
  211. float maxX = polygonPointList[0].X;
  212. float maxY = polygonPointList[0].Y;
  213. //获取最小X,Y 最大X,Y
  214. for (int i = 0; i < pCount; i++)
  215. {
  216. minX = Math.Min(minX, polygonPointList[i].X);
  217. minY = Math.Min(minY, polygonPointList[i].Y);
  218. maxX = Math.Max(maxX, polygonPointList[i].X);
  219. maxY = Math.Max(maxY, polygonPointList[i].Y);
  220. }
  221. //创建外接矩形
  222. Rectangle rect = new Rectangle();
  223. rect.Location = new Point((int)minX, (int)minY);
  224. rect.Size = new Size((int)maxX - (int)minX, (int)maxY - (int)minY);
  225. return rect;
  226. }
  227. }
  228. return new Rectangle();
  229. }
  230. #endregion
  231. }
  232. }