CMeasureArea.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. 
  2. using NLog.Fluent;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. using Point = System.Drawing.Point;
  9. using Rectangle = System.Drawing.Rectangle;
  10. namespace OTSMeasureApp._4_OTSSamplespaceGraphicsPanel.VisualGDIObjects
  11. {
  12. public class CMeasureArea : CDisplayGDIObject
  13. {
  14. private ShapeType myshape;
  15. private List<PointF> m_PolygonPoints = new List<PointF>();
  16. //绘制时与移动缩放时记录的位置
  17. private List<PointF> m_originalPolygonPoints = new List<PointF>();
  18. private PointF startPoint;
  19. private PointF endPoint;
  20. protected List<CVisualFieldGDIObject> subItems = new List<CVisualFieldGDIObject>();
  21. public CMeasureArea()
  22. {
  23. }
  24. public CMeasureArea(RectangleF rectMeasure, ShapeType shape, string name, string sampleName, Color selColor)
  25. {
  26. m_OrigineRegionF = rectMeasure;
  27. SetInitRegionF(m_OrigineRegionF);
  28. NameOrHoleName = name;
  29. myshape = shape;
  30. SelColor = selColor;
  31. SampleName = sampleName;
  32. ID = System.Guid.NewGuid().ToString();
  33. OTSX = -1;
  34. OTSY = -1;
  35. }
  36. public CMeasureArea(List<PointF> mPoint, ShapeType shape, string name, string sampleName, Color selColor)
  37. {
  38. ID = System.Guid.NewGuid().ToString();
  39. this.SetOriginalPolygonPointFList(mPoint);
  40. myshape = shape;
  41. SelColor = selColor;
  42. }
  43. public List<CVisualFieldGDIObject> SubItems()
  44. {
  45. if (subItems == null) subItems = new List<CVisualFieldGDIObject>();
  46. return subItems;
  47. }
  48. virtual public void AddSubItems(CVisualFieldGDIObject item)
  49. {
  50. if (subItems == null) subItems = new List<CVisualFieldGDIObject>();
  51. item.SequenceNum = subItems.Count ;
  52. subItems.Add(item);
  53. }
  54. public void ClearSubItems()
  55. {
  56. subItems.Clear();
  57. }
  58. public override void OnPaint(PaintEventArgs e)
  59. {
  60. Color myColor = selColor;
  61. System.Drawing.SolidBrush sampleBrush = new System.Drawing.SolidBrush(myColor);
  62. Pen p = new Pen(myColor, 1);
  63. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //图片柔顺模式选择
  64. e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
  65. e.Graphics.CompositingQuality = CompositingQuality.HighQuality;//再加一点
  66. switch (myshape)
  67. {
  68. case ShapeType.CIRCLE:
  69. e.Graphics.DrawEllipse(p, m_RegionF);
  70. break;
  71. case ShapeType.RECTANGLE:
  72. var m_Region = new System.Drawing.Rectangle();
  73. m_Region.X = (int)m_RegionF.X;
  74. m_Region.Y = (int)m_RegionF.Y;
  75. m_Region.Width = (int)m_RegionF.Width;
  76. m_Region.Height = (int)m_RegionF.Height;
  77. e.Graphics.DrawRectangle(p, m_Region);
  78. break;
  79. case ShapeType.POLYGON:
  80. myColor = selColor;
  81. Pen pen = new Pen(myColor, 1);
  82. if (GetPolygonPointFList().Count > 0)
  83. {
  84. List<PointF> PolygonPointF = new List<PointF>();
  85. foreach (var item in m_PolygonPoints)
  86. {
  87. PolygonPointF.Add(item);
  88. }
  89. if (PolygonDrawingEndPoint.X != 0 && PolygonDrawingEndPoint.Y != 0)
  90. {
  91. PolygonPointF.Add(PolygonDrawingEndPoint);
  92. }
  93. if (PolygonPointF.Count > 1)
  94. {
  95. e.Graphics.DrawLines(pen, PolygonPointF.ToArray());
  96. }
  97. }
  98. break;
  99. }
  100. if (subItems.Count != 0)
  101. {
  102. foreach (var item in subItems)
  103. {
  104. item.OnPaint(e);
  105. }
  106. }
  107. }
  108. override public PointF DraggingPoint
  109. {
  110. get { return m_DraggingPoint; }
  111. set
  112. {
  113. m_DraggingPoint = value;
  114. if (subItems.Count != 0)
  115. {
  116. foreach (var item in subItems)
  117. {
  118. item.DraggingPoint = m_DraggingPoint;
  119. }
  120. }
  121. }
  122. }
  123. override public void PositionAltering(PointF location, bool ifZoomCoord)
  124. {
  125. if (myshape == ShapeType.POLYGON)
  126. {
  127. PointF offset = new PointF(location.X - DraggingPoint.X, location.Y - DraggingPoint.Y);
  128. PointF realShift;
  129. if (ifZoomCoord)
  130. {
  131. realShift = new PointF(offset.X / m_zoomNum, offset.Y / m_zoomNum);
  132. }
  133. else
  134. {
  135. realShift = new PointF(offset.X, offset.Y);
  136. }
  137. m_OrigineRegionF = new RectangleF(m_OrigineRegionF.X + realShift.X, m_OrigineRegionF.Y + realShift.Y, m_OrigineRegionF.Width, m_OrigineRegionF.Height);
  138. SetInitRegionF(m_OrigineRegionF);
  139. var scalePs = new List<PointF>();
  140. var OriginalPs = new List<PointF>();
  141. foreach (var p in m_PolygonPoints)
  142. {
  143. float x, y;
  144. x = (p.X + offset.X);
  145. y = (p.Y + offset.Y);
  146. scalePs.Add(new PointF(x, y));
  147. }
  148. m_PolygonPoints = scalePs;
  149. foreach (var p in m_originalPolygonPoints)
  150. {
  151. OriginalPs.Add(new PointF(p.X + realShift.X, p.Y + realShift.Y));
  152. }
  153. m_originalPolygonPoints = OriginalPs;
  154. m_DraggingPoint = new Point((int)location.X, (int)location.Y);
  155. }
  156. else
  157. {
  158. base.PositionAltering(location, ifZoomCoord);
  159. }
  160. if (subItems.Count != 0)
  161. {
  162. foreach (var g in subItems)
  163. {
  164. g.PositionAltering(location, ifZoomCoord);
  165. }
  166. }
  167. }
  168. override public void Move(PointF location)
  169. {
  170. if (myshape == ShapeType.POLYGON)
  171. {
  172. PointF offset = new PointF(location.X - DraggingPoint.X, location.Y - DraggingPoint.Y);
  173. var scalePs = new List<PointF>();
  174. foreach (var p in m_PolygonPoints)
  175. {
  176. float x, y;
  177. x = (p.X + offset.X);
  178. y = (p.Y + offset.Y);
  179. scalePs.Add(new PointF(x, y));
  180. }
  181. m_PolygonPoints = scalePs;
  182. m_DraggingPoint = new Point((int)location.X, (int)location.Y);
  183. }
  184. else
  185. {
  186. base.Move(location);
  187. }
  188. if (this.subItems.Count != 0)
  189. {
  190. foreach (var g in subItems)
  191. {
  192. g.Move(location);
  193. }
  194. }
  195. }
  196. override public void Zoom(PointF mousePoint, float zoomNum)
  197. {
  198. if (myshape == ShapeType.POLYGON)
  199. {
  200. float curZoom = m_zoomNum;
  201. float deltaZoom = zoomNum - curZoom;
  202. if (zoomNum == 1)
  203. {
  204. m_zoomNum = 1;
  205. m_RegionF = m_OrigineRegionF;
  206. m_refPoint = new PointF(0, 0);
  207. m_PolygonPoints = m_originalPolygonPoints;
  208. }
  209. else
  210. {
  211. m_refPoint.X = (m_refPoint.X - mousePoint.X) / curZoom * deltaZoom + m_refPoint.X;
  212. m_refPoint.Y = (m_refPoint.Y - mousePoint.Y) / curZoom * deltaZoom + m_refPoint.Y;
  213. var scalePs = new List<PointF>();
  214. foreach (var p in m_PolygonPoints)
  215. {
  216. float x, y;
  217. x = (p.X - mousePoint.X) / curZoom * deltaZoom + p.X;
  218. y = (p.Y - mousePoint.Y) / curZoom * deltaZoom + p.Y;
  219. scalePs.Add(new PointF(x, y));
  220. }
  221. m_PolygonPoints = scalePs;
  222. m_zoomNum = zoomNum;
  223. }
  224. }
  225. else
  226. {
  227. base.Zoom(mousePoint, zoomNum);
  228. }
  229. if (this.subItems.Count != 0)
  230. {
  231. foreach (var g in subItems)
  232. {
  233. g.Zoom(mousePoint, zoomNum);
  234. }
  235. }
  236. }
  237. override public void SetZoomNumber(float value)
  238. {
  239. base.SetZoomNumber(value);
  240. if (m_originalPolygonPoints.Count != 0)
  241. {
  242. this.SetPolygonPointFList(m_originalPolygonPoints);
  243. }
  244. }
  245. public override GraphicsPath GetGPath()
  246. {
  247. //重新绘制测量区域路径
  248. GraphicsPath GPath ;
  249. if (myshape == ShapeType.POLYGON)
  250. {
  251. GraphicsPath PolygonMeasurePath = new GraphicsPath();
  252. PolygonMeasurePath.AddPolygon(this.GetPolygonPointFList().ToArray());
  253. GPath = PolygonMeasurePath;
  254. }
  255. else
  256. {
  257. GPath= base.GetGPath();
  258. }
  259. return GPath;
  260. }
  261. public List<PointF> GetOriginalPolygonPointFList()
  262. { return m_originalPolygonPoints; }
  263. public void SetOriginalPolygonPointFList(List<PointF> value)
  264. {
  265. if (m_zoomNum != 1)
  266. {
  267. var ps = new List<PointF>();
  268. foreach (var p in value)
  269. {
  270. var p1 = new PointF();
  271. p1.X = p.X * m_zoomNum + m_refPoint.X;
  272. p1.Y = p.Y * m_zoomNum + m_refPoint.Y;
  273. ps.Add(p1);
  274. }
  275. m_PolygonPoints = ps;
  276. m_originalPolygonPoints = value;
  277. }
  278. else
  279. {
  280. m_originalPolygonPoints = value;
  281. m_PolygonPoints = value;
  282. }
  283. }
  284. public List<PointF> GetPolygonPointFList()
  285. { return m_PolygonPoints; }
  286. public void SetPolygonPointFList(List<PointF> value)
  287. {
  288. if (m_zoomNum != 1)
  289. {
  290. var ps = new List<PointF>();
  291. foreach (var p in value)
  292. {
  293. var p1 = new PointF();
  294. p1.X = (p.X - m_refPoint.X) / m_zoomNum;
  295. p1.Y = (p.Y - m_refPoint.Y) / m_zoomNum;
  296. ps.Add(p1);
  297. }
  298. m_originalPolygonPoints = ps;
  299. m_PolygonPoints = value;
  300. }
  301. else
  302. {
  303. m_originalPolygonPoints = value;
  304. m_PolygonPoints = value;
  305. }
  306. var region = GetMinRectangleOfPolygon(m_PolygonPoints);
  307. SetZoomedRegionF(region);
  308. }
  309. private Rectangle GetMinRectangleOfPolygon(List<PointF> polygonPointList)
  310. {
  311. if (polygonPointList != null)
  312. {
  313. if (polygonPointList.Count > 0)
  314. {
  315. int pCount = polygonPointList.Count;
  316. float minX = polygonPointList[0].X;
  317. float minY = polygonPointList[0].Y;
  318. float maxX = polygonPointList[0].X;
  319. float maxY = polygonPointList[0].Y;
  320. //获取最小X,Y 最大X,Y
  321. for (int i = 0; i < pCount; i++)
  322. {
  323. minX = Math.Min(minX, polygonPointList[i].X);
  324. minY = Math.Min(minY, polygonPointList[i].Y);
  325. maxX = Math.Max(maxX, polygonPointList[i].X);
  326. maxY = Math.Max(maxY, polygonPointList[i].Y);
  327. }
  328. //创建外接矩形
  329. Rectangle rect = new Rectangle();
  330. rect.Location = new Point((int)minX, (int)minY);
  331. rect.Size = new Size((int)maxX - (int)minX, (int)maxY - (int)minY);
  332. return rect;
  333. }
  334. }
  335. return new Rectangle();
  336. }
  337. public PointF PolygonDrawingEndPoint
  338. {
  339. get { return endPoint; }
  340. set { endPoint = value; }
  341. }
  342. public ShapeType Myshape { get => myshape; set => myshape = value; }
  343. }
  344. }