DrawMulLineA.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Globalization;
  8. using System.Windows.Forms;
  9. using PaintDotNet.Base.CommTool;
  10. using System.Net.NetworkInformation;
  11. using System.Collections;
  12. using System.Linq;
  13. using PaintDotNet.Annotation.Measure;
  14. namespace PaintDotNet.Annotation.PhysicalPhaseAction
  15. {
  16. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  17. using PointList = List<PointF>;
  18. public class DrawMulLineA : DrawObject
  19. {
  20. public PointList pointArray;
  21. private const string entryLength = "Length";
  22. private const string entryPoint = "Point";
  23. /// <summary>
  24. /// Graphic objects for hit test
  25. /// </summary>
  26. private GraphicsPath areaPath = null;
  27. private Pen areaPen = null;
  28. private Region areaRegion = null;
  29. /// <summary>
  30. /// 长度
  31. /// </summary>
  32. private double allLength = 0.0;
  33. /// <summary>
  34. /// 测量信息矩形定义
  35. /// </summary>
  36. private Point pointL = new Point();
  37. public bool pointChange = true;
  38. private int moveKb;
  39. public bool configurationFile = false;
  40. public List<RectangleF> RectangleFList = new List<RectangleF>();
  41. public DrawAnalysisModel labelCircleStyleModel;
  42. public DrawMulLineA(ISurfaceBox surfaceBox, int x1, int y1) : base()
  43. {
  44. this.objectType = DrawClass.Label;
  45. this.drawToolType = DrawToolType.DrawMulLineA;
  46. labelCircleStyleModel = surfaceBox.AnalysisStyleModel;
  47. pointArray = new PointList();
  48. startPoint.X = x1;
  49. startPoint.Y = y1;
  50. pointArray.Add(new Point(x1, y1));
  51. Initialize();
  52. }
  53. public DrawMulLineA(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  54. {
  55. this.objectType = DrawClass.Label;
  56. this.drawToolType = DrawToolType.DrawMulLineA;
  57. this.ISurfaceBox = surfaceBox;
  58. labelCircleStyleModel = (DrawAnalysisModel)parentStyleModel;
  59. pointArray = points;
  60. this.configurationFile = true;
  61. }
  62. /// <summary>
  63. /// Clone this instance
  64. /// </summary>
  65. public override DrawObject Clone()
  66. {
  67. DrawMulLineA drawPolygon = new DrawMulLineA(ISurfaceBox, 0, 0);
  68. drawPolygon.ISurfaceBox = ISurfaceBox;
  69. foreach (PointF p in this.pointArray)
  70. {
  71. drawPolygon.pointArray.Add(p);
  72. }
  73. FillDrawObjectFields(drawPolygon);
  74. return drawPolygon;
  75. }
  76. public override void Draw(Graphics g)
  77. {
  78. Matrix mtxSave1 = g.Transform;
  79. Matrix matrix1 = g.Transform;
  80. double alllength = 0.0;
  81. double length = 0.0;
  82. int pxPerUnit = 1;
  83. if (this.pointChange)
  84. {
  85. this.pointL = new Point((int)pointArray[0].X, (int)pointArray[0].Y);
  86. }
  87. for (int i = 1; i <= pointArray.Count - 1; i++)
  88. {
  89. Pen linePen = new Pen(Color.FromArgb(labelCircleStyleModel.lineColor), labelCircleStyleModel.lineWidth);
  90. linePen.DashStyle = (DashStyle)labelCircleStyleModel.lineStyle;
  91. g.SmoothingMode = SmoothingMode.AntiAlias;
  92. length = BasicCalculationHelper.GetDistance(pointArray[i - 1], pointArray[i], 10) / pxPerUnit;
  93. g.DrawLine(linePen, pointArray[i - 1], pointArray[i]);
  94. alllength = alllength + length;
  95. linePen.Dispose();
  96. }
  97. this.allLength = alllength;
  98. if (this.configurationFile)
  99. this.pointChange = false;
  100. }
  101. public void AddPoint(Point point)
  102. {
  103. pointArray.Add(point);
  104. }
  105. public override int HandleCount
  106. {
  107. get
  108. {
  109. return pointArray.Count + 1;
  110. }
  111. }
  112. /// <summary>
  113. /// Get handle pointscroll by 1-based number
  114. /// </summary>
  115. /// <param name="handleNumber"></param>
  116. /// <returns></returns>
  117. public override PointF GetHandle(int handleNumber)
  118. {
  119. if (handleNumber < 1)
  120. handleNumber = 1;
  121. if (handleNumber > pointArray.Count && handleNumber != pointArray.Count + 1)
  122. handleNumber = pointArray.Count;
  123. if (handleNumber == pointArray.Count + 1)
  124. return this.pointL;
  125. else
  126. return pointArray[handleNumber - 1];
  127. }
  128. public override Cursor GetHandleCursor(int handleNumber)
  129. {
  130. switch (handleNumber)
  131. {
  132. case 1:
  133. return Cursors.SizeNWSE;
  134. case 2:
  135. return Cursors.SizeNS;
  136. case 3:
  137. return Cursors.SizeNESW;
  138. case 4:
  139. return Cursors.SizeWE;
  140. case 5:
  141. return Cursors.SizeNWSE;
  142. case 6:
  143. return Cursors.SizeNS;
  144. case 7:
  145. return Cursors.SizeNESW;
  146. case 8:
  147. return Cursors.SizeWE;
  148. default:
  149. return Cursors.Default;
  150. }
  151. }
  152. public override void MoveHandleTo(Point point, int handleNumber)
  153. {
  154. double k = 0.0;
  155. if (handleNumber < 1)
  156. handleNumber = 1;
  157. if (handleNumber > pointArray.Count && handleNumber != pointArray.Count + 1)
  158. handleNumber = pointArray.Count;
  159. if (handleNumber > 2)
  160. {
  161. if (handleNumber == pointArray.Count + 1)
  162. {
  163. //if (this.moveKb == 1)
  164. // this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  165. //else if (this.moveKb == 2)
  166. // this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  167. //else if (this.moveKb == 3)
  168. // this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  169. //else if (this.moveKb == 4)
  170. // this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  171. //else if (this.moveKb == 5)
  172. // this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  173. //else
  174. //{
  175. // for (int i = 0; i < this.RectangleFList.Count; i++)
  176. // {
  177. // RectangleF rectangleF = this.RectangleFList[i];
  178. // if (this.moveKb == -i)
  179. // rectangleF.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  180. // this.RectangleFList[i] = rectangleF;
  181. // }
  182. //}
  183. //this.pointL = point;
  184. }
  185. else
  186. {
  187. // 求直线斜率
  188. if (pointArray[0].X != pointArray[1].X && pointArray[0].Y != pointArray[1].Y)
  189. {
  190. k = (double)(pointArray[1].Y - pointArray[0].Y) / (pointArray[1].X - pointArray[0].X);
  191. point.Y = (int)(k * (point.X - pointArray[handleNumber - 2].X) + pointArray[handleNumber - 2].Y);
  192. for (int i = 1; i <= this.RectangleFList.Count; i++)
  193. {
  194. if (i == handleNumber)
  195. {
  196. RectangleF rectangleF = this.RectangleFList[i * 6 - 12];
  197. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  198. this.RectangleFList[i * 6 - 12] = rectangleF;
  199. rectangleF = this.RectangleFList[i * 6 - 11];
  200. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  201. this.RectangleFList[i * 6 - 11] = rectangleF;
  202. rectangleF = this.RectangleFList[i * 6 - 10];
  203. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  204. this.RectangleFList[i * 6 - 10] = rectangleF;
  205. rectangleF = this.RectangleFList[i * 6 - 9];
  206. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  207. this.RectangleFList[i * 6 - 9] = rectangleF;
  208. rectangleF = this.RectangleFList[i * 6 - 8];
  209. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  210. this.RectangleFList[i * 6 - 8] = rectangleF;
  211. rectangleF = this.RectangleFList[i * 6 - 7];
  212. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  213. this.RectangleFList[i * 6 - 7] = rectangleF;
  214. }
  215. }
  216. pointArray[handleNumber - 1] = point;
  217. }
  218. if (pointArray[0].X == pointArray[1].X)
  219. {
  220. point.X = (int)pointArray[0].X;
  221. for (int i = 1; i <= this.RectangleFList.Count; i++)
  222. {
  223. if (i == handleNumber)
  224. {
  225. RectangleF rectangleF = this.RectangleFList[i * 6 - 12];
  226. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  227. this.RectangleFList[i * 6 - 12] = rectangleF;
  228. rectangleF = this.RectangleFList[i * 6 - 11];
  229. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  230. this.RectangleFList[i * 6 - 11] = rectangleF;
  231. rectangleF = this.RectangleFList[i * 6 - 10];
  232. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  233. this.RectangleFList[i * 6 - 10] = rectangleF;
  234. rectangleF = this.RectangleFList[i * 6 - 9];
  235. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  236. this.RectangleFList[i * 6 - 9] = rectangleF;
  237. rectangleF = this.RectangleFList[i * 6 - 8];
  238. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  239. this.RectangleFList[i * 6 - 8] = rectangleF;
  240. rectangleF = this.RectangleFList[i * 6 - 7];
  241. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  242. this.RectangleFList[i * 6 - 7] = rectangleF;
  243. }
  244. }
  245. pointArray[handleNumber - 1] = point;
  246. }
  247. if (pointArray[0].Y == pointArray[1].Y)
  248. {
  249. point.Y = (int)pointArray[0].Y;
  250. for (int i = 1; i <= this.RectangleFList.Count; i++)
  251. {
  252. if (i == handleNumber)
  253. {
  254. RectangleF rectangleF = this.RectangleFList[i * 6 - 12];
  255. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  256. this.RectangleFList[i * 6 - 12] = rectangleF;
  257. rectangleF = this.RectangleFList[i * 6 - 11];
  258. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  259. this.RectangleFList[i * 6 - 11] = rectangleF;
  260. rectangleF = this.RectangleFList[i * 6 - 10];
  261. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  262. this.RectangleFList[i * 6 - 10] = rectangleF;
  263. rectangleF = this.RectangleFList[i * 6 - 9];
  264. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  265. this.RectangleFList[i * 6 - 9] = rectangleF;
  266. rectangleF = this.RectangleFList[i * 6 - 8];
  267. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  268. this.RectangleFList[i * 6 - 8] = rectangleF;
  269. rectangleF = this.RectangleFList[i * 6 - 7];
  270. rectangleF.Offset(point.X - this.pointArray[i - 1].X, point.Y - this.pointArray[i - 1].Y);
  271. this.RectangleFList[i * 6 - 7] = rectangleF;
  272. }
  273. }
  274. pointArray[handleNumber - 1] = point;
  275. }
  276. }
  277. }
  278. else
  279. {
  280. if (handleNumber == 1)
  281. {
  282. //this.rectangleF1.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  283. //this.rectangleF2.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  284. //this.rectangleF3.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  285. //this.rectangleF4.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  286. //this.rectangleF5.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  287. }
  288. else if (handleNumber == 2)
  289. {
  290. RectangleF rectangleF = this.RectangleFList[0];
  291. rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y);
  292. this.RectangleFList[0] = rectangleF;
  293. rectangleF = this.RectangleFList[1];
  294. rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y);
  295. this.RectangleFList[1] = rectangleF;
  296. rectangleF = this.RectangleFList[2];
  297. rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y);
  298. this.RectangleFList[2] = rectangleF;
  299. rectangleF = this.RectangleFList[3];
  300. rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y);
  301. this.RectangleFList[3] = rectangleF;
  302. rectangleF = this.RectangleFList[4];
  303. rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y);
  304. this.RectangleFList[4] = rectangleF;
  305. rectangleF = this.RectangleFList[5];
  306. rectangleF.Offset(point.X - this.pointArray[1].X, point.Y - this.pointArray[1].Y);
  307. this.RectangleFList[5] = rectangleF;
  308. }
  309. pointArray[handleNumber - 1] = point;
  310. }
  311. this.startPoint = pointArray[0];
  312. Invalidate();
  313. }
  314. public override void Move(int deltaX, int deltaY)
  315. {
  316. //for (int i = 0; i < pointArray.Count; i++)
  317. //{
  318. // PointF point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  319. // pointArray[i] = point;
  320. //}
  321. //this.startPoint = pointArray[0];
  322. //int x = ISurfaceBox.UnscaleScalar(deltaX);
  323. //int y = ISurfaceBox.UnscaleScalar(deltaY);
  324. //pointL.X += x;
  325. //pointL.Y += y;
  326. ////this.rectangleF1.Offset(x, y);
  327. ////this.rectangleF2.Offset(x, y);
  328. ////this.rectangleF3.Offset(x, y);
  329. ////this.rectangleF4.Offset(x, y);
  330. ////this.rectangleF5.Offset(x, y);
  331. //for (int i = 0; i < this.RectangleFList.Count; i++)
  332. //{
  333. // RectangleF rectangleF = this.RectangleFList[i];
  334. // rectangleF.Offset(x, y);
  335. // this.RectangleFList[i] = rectangleF;
  336. //}
  337. //Invalidate();
  338. }
  339. public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  340. {
  341. info.AddValue(
  342. String.Format(CultureInfo.InvariantCulture,
  343. "{0}{1}",
  344. entryLength, orderNumber),
  345. pointArray.Count);
  346. int i = 0;
  347. foreach (PointF p in pointArray)
  348. {
  349. info.AddValue(
  350. String.Format(CultureInfo.InvariantCulture,
  351. "{0}{1}-{2}",
  352. entryPoint, orderNumber, i++),
  353. p);
  354. }
  355. base.SaveToStream(info, orderNumber); // ??
  356. }
  357. public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  358. {
  359. Point point;
  360. int n = info.GetInt32(
  361. String.Format(CultureInfo.InvariantCulture,
  362. "{0}{1}",
  363. entryLength, orderNumber));
  364. for (int i = 0; i < n; i++)
  365. {
  366. point = (Point)info.GetValue(
  367. String.Format(CultureInfo.InvariantCulture,
  368. "{0}{1}-{2}",
  369. entryPoint, orderNumber, i),
  370. typeof(Point));
  371. pointArray.Add(point);
  372. }
  373. base.LoadFromStream(info, orderNumber);
  374. }
  375. /// <summary>
  376. /// 用于创建一个路径或者是闭合的范围
  377. /// 用于响应点击选中
  378. /// 需要咨询用户是仅点击线还是矩形选择
  379. /// </summary>
  380. protected virtual void CreateObjects()
  381. {
  382. if (AreaPath != null)
  383. return;
  384. AreaPath = new GraphicsPath();
  385. AreaPath.AddRectangle(GetBoundingBox());
  386. AreaPath.CloseFigure();
  387. AreaRegion = new Region(AreaPath);
  388. }
  389. /// <summary>
  390. /// Invalidate object.
  391. /// When object is invalidated, path used for hit test
  392. /// is released and should be created again.
  393. /// </summary>
  394. protected void Invalidate()
  395. {
  396. if (AreaPath != null)
  397. {
  398. AreaPath.Dispose();
  399. AreaPath = null;
  400. }
  401. if (AreaPen != null)
  402. {
  403. AreaPen.Dispose();
  404. AreaPen = null;
  405. }
  406. if (AreaRegion != null)
  407. {
  408. AreaRegion.Dispose();
  409. AreaRegion = null;
  410. }
  411. }
  412. protected GraphicsPath AreaPath
  413. {
  414. get
  415. {
  416. return areaPath;
  417. }
  418. set
  419. {
  420. areaPath = value;
  421. }
  422. }
  423. protected Pen AreaPen
  424. {
  425. get
  426. {
  427. return areaPen;
  428. }
  429. set
  430. {
  431. areaPen = value;
  432. }
  433. }
  434. protected Region AreaRegion
  435. {
  436. get
  437. {
  438. return areaRegion;
  439. }
  440. set
  441. {
  442. areaRegion = value;
  443. }
  444. }
  445. /// <summary>
  446. /// Draw tracker for selected object
  447. /// </summary>
  448. /// <param name="g"></param>
  449. public override void DrawTracker(Graphics g)
  450. {
  451. if (!Selected)
  452. return;
  453. SolidBrush brush = new SolidBrush(Color.Black);
  454. for (int i = 1; i <= HandleCount; i++)
  455. {
  456. if (i == HandleCount)
  457. brush = new SolidBrush(Color.Transparent);
  458. g.FillRectangle(brush, GetHandleRectangle(i));
  459. }
  460. //g.DrawRectangle(new Pen(Color.White), GetBoundingBox());
  461. brush.Dispose();
  462. RectangleF r = GetBoundingBox();
  463. g.DrawRectangle(new Pen(Color.White), r.X, r.Y, r.Width, r.Height);
  464. }
  465. /// <summary>
  466. /// Hit test.
  467. /// Return value: -1 - no hit
  468. /// 0 - hit anywhere
  469. /// > 1 - handle number
  470. /// </summary>
  471. /// <param name="pointscroll"></param>
  472. /// <returns></returns>
  473. public override int HitTest(Point point)
  474. {
  475. if (Selected)
  476. {
  477. for (int i = 1; i <= HandleCount; i++)
  478. {
  479. if (GetHandleRectangle(i).Contains(point))
  480. return i;
  481. }
  482. for (int i = 0; i < this.RectangleFList.Count; i++)
  483. {
  484. RectangleF rectangleF = this.RectangleFList[i];
  485. if (rectangleF.Contains(point))
  486. {
  487. this.pointL = point;
  488. moveKb = -i;
  489. return this.pointArray.Count;
  490. }
  491. }
  492. }
  493. if (PointInObject(point))
  494. return 0;
  495. return -1;
  496. }
  497. protected override bool PointInObject(Point point)
  498. {
  499. CreateObjects();
  500. return AreaRegion.IsVisible(point);
  501. }
  502. public override bool IntersectsWith(Rectangle rectangle)
  503. {
  504. CreateObjects();
  505. return AreaRegion.IsVisible(rectangle);
  506. }
  507. public override RectangleF GetBoundingBox()
  508. {
  509. Rectangle rectangle;
  510. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  511. for (int i = 0; i < pointArray.Count; i++)
  512. {
  513. if (i == 0)
  514. {
  515. minx = maxx = (int)pointArray[i].X;
  516. miny = maxy = (int)pointArray[i].Y;
  517. }
  518. else
  519. {
  520. if (pointArray[i].X > maxx) maxx = (int)pointArray[i].X;
  521. if (pointArray[i].X < minx) minx = (int)pointArray[i].X;
  522. if (pointArray[i].Y > maxy) maxy = (int)pointArray[i].Y;
  523. if (pointArray[i].Y < miny) miny = (int)pointArray[i].Y;
  524. }
  525. }
  526. rectangle = new Rectangle(minx, miny, maxx - minx, maxy - miny);
  527. return rectangle;
  528. }
  529. internal void setNextPoint(Point p)
  530. {
  531. AddPoint(p);
  532. //startPoint = endPoint;
  533. //endPoint = p;
  534. }
  535. internal void setEndPoint(Point p)
  536. {
  537. endPoint = p;
  538. }
  539. public override List<PointF> GetPoints()
  540. {
  541. return pointArray;
  542. }
  543. public override ParentStyleModel GetStyle()
  544. {
  545. return labelCircleStyleModel;
  546. }
  547. }
  548. }