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