OTSSamplespaceGraphicsPanelFun.cs 10 KB

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