MeasureTraceCurve.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using PaintDotNet.Base.CommTool;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.Annotation.Measure
  12. {
  13. using PointEnumerator = IEnumerator<PointF>;
  14. using PointList = List<PointF>;
  15. /// <summary>
  16. /// 测量->曲线->轨迹曲线
  17. /// </summary>
  18. public class MeasureTraceCurve : MeasureDrawObject
  19. {
  20. private PointList pointArray;
  21. private GraphicsPath areaPath = null;
  22. private Pen areaPen = null;
  23. private Region areaRegion = null;
  24. /// <summary>
  25. /// 曲线长度
  26. /// </summary>
  27. private double Length = 0.0;
  28. /// <summary>
  29. /// 属性绘制点
  30. /// </summary>
  31. private PointF rotationPoint;
  32. /// <summary>
  33. /// 测量信息矩形定义
  34. /// </summary>
  35. private RectangleF rectangleF1 = new RectangleF();
  36. private RectangleF rectangleF2 = new RectangleF();
  37. private RectangleF rectangleF3 = new RectangleF();
  38. private RectangleF rectangleF4 = new RectangleF();
  39. private RectangleF rectangleF5 = new RectangleF();
  40. private RectangleF rectangleF6 = new RectangleF();
  41. private RectangleF rectangleF7 = new RectangleF();
  42. private RectangleF rectangleF8 = new RectangleF();
  43. private RectangleF rectangleF9 = new RectangleF();
  44. /// <summary>
  45. /// 文本上用于拖动的点
  46. /// </summary>
  47. private Point pointL = new Point();
  48. /// <summary>
  49. /// 绘制限制(第一次绘制时)
  50. /// </summary>
  51. public bool pointChange = true;
  52. /// <summary>
  53. /// 区分移动文本
  54. /// </summary>
  55. private int moveKb;
  56. /// <summary>
  57. /// 绘制限制(从配置文件加载)
  58. /// </summary>
  59. public bool configurationFile = false;
  60. /// <summary>
  61. /// 绘制限制(更改属性时)
  62. /// </summary>
  63. private bool SavePointChange;
  64. /// <summary>
  65. /// 绘制属性
  66. /// </summary>
  67. private string[] drawingPropertiesList;
  68. /// <summary>
  69. /// 绘制属性(克隆)
  70. /// </summary>
  71. private string[] drawingPropertiesListClone;
  72. /// <summary>
  73. /// 旋转角度
  74. /// </summary>
  75. private double angle;
  76. /// <summary>
  77. /// 限制绘制(绘制中)
  78. /// </summary>
  79. public bool mouseUpPointChange = false;
  80. /// <summary>
  81. /// 是否移动了鼠标
  82. /// </summary>
  83. public bool moved = false;
  84. private MeasureStyleModel.MeasureTraceCurve measureTraceCurve;
  85. public MeasureTraceCurve() : base()
  86. {
  87. this.objectType = DrawClass.Measure;
  88. this.drawToolType = DrawToolType.MeasureTraceCurve;
  89. pointArray = new PointList();
  90. Initialize();
  91. }
  92. public MeasureTraceCurve(ISurfaceBox surfaceBox, float x1, float y1, float x2, float y2) : base(surfaceBox)
  93. {
  94. this.objectType = DrawClass.Measure;
  95. this.drawToolType = DrawToolType.MeasureTraceCurve;
  96. this.pointArray = new PointList();
  97. pointArray.Add(new PointF(x1, y1));
  98. pointArray.Add(new PointF(x2, y2));
  99. startPoint = new PointF(x1, y1);
  100. measureTraceCurve = this.ISurfaceBox.GetMeasureStyleModel().measureTraceCurve;
  101. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  102. this.unitString = surfaceBox.GetPxPerUnit()[1];
  103. this.unit = surfaceBox.GetPxPerUnit()[2];
  104. surfaceBox.getMeasureInfo().TryGetValue(measurementUnit, out unitLength);
  105. Initialize();
  106. }
  107. public MeasureTraceCurve(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base(surfaceBox)
  108. {
  109. this.objectType = DrawClass.Measure;
  110. this.drawToolType = DrawToolType.MeasureTraceCurve;
  111. this.moved = true;
  112. this.pointArray = DrawRulerHelper.DeepCopyListByReflect(points);
  113. this.startPoint = points[0];
  114. this.measureTraceCurve = this.ISurfaceBox.GetMeasureStyleModel().measureTraceCurve;
  115. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  116. this.unitString = surfaceBox.GetPxPerUnit()[1];
  117. this.unit = surfaceBox.GetPxPerUnit()[2];
  118. surfaceBox.getMeasureInfo().TryGetValue(measurementUnit, out unitLength);
  119. this.configurationFile = true;
  120. Initialize();
  121. }
  122. /// <summary>
  123. /// Clone this instance
  124. /// </summary>
  125. public override DrawObject Clone()
  126. {
  127. MeasureTraceCurve drawPolygon = new MeasureTraceCurve(ISurfaceBox, this.startPoint.X, this.startPoint.Y, this.endPoint.X, this.endPoint.Y);
  128. drawPolygon.pointArray.Clear();
  129. foreach (PointF p in this.pointArray)
  130. {
  131. drawPolygon.pointArray.Add(p);
  132. }
  133. if (drawingPropertiesList != null)
  134. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  135. drawPolygon.moved = true;
  136. FillDrawObjectFields(drawPolygon);
  137. return drawPolygon;
  138. }
  139. /// <summary>
  140. /// Clone this instance
  141. /// </summary>
  142. public override DrawObject Clone(ISurfaceBox surfaceBox)
  143. {
  144. MeasureTraceCurve drawPolygon = new MeasureTraceCurve(surfaceBox, this.GetPoints(), this.measureTraceCurve, null);
  145. if (drawingPropertiesList != null)
  146. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  147. drawPolygon.moved = true;
  148. FillDrawObjectFields(drawPolygon);
  149. return drawPolygon;
  150. }
  151. /// <summary>
  152. /// 测量属性
  153. /// </summary>
  154. /// <returns></returns>
  155. public override Dictionary<System.Enum, object> GetData()
  156. {
  157. if (data.ContainsKey(MeasureAttributes.MeasureMethod))
  158. data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.CurveLength.Text");
  159. else
  160. data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.CurveLength.Text"));
  161. if (data.ContainsKey(MeasureAttributes.MeasureUnitCN))
  162. data[MeasureAttributes.MeasureUnitCN] = this.unitString;
  163. else
  164. data.Add(MeasureAttributes.MeasureUnitCN, this.unitString);
  165. if (data.ContainsKey(MeasureAttributes.MeasureUnitEN))
  166. data[MeasureAttributes.MeasureUnitEN] = this.unit;
  167. else
  168. data.Add(MeasureAttributes.MeasureUnitEN, this.unit);
  169. if (data.ContainsKey(MeasureAttributes.PixelStartX))
  170. data[MeasureAttributes.PixelStartX] = startPoint.X;
  171. else
  172. data.Add(MeasureAttributes.PixelStartX, startPoint.X);
  173. if (data.ContainsKey(MeasureAttributes.PixelStartY))
  174. data[MeasureAttributes.PixelStartY] = startPoint.Y;
  175. else
  176. data.Add(MeasureAttributes.PixelStartY, startPoint.Y);
  177. if (data.ContainsKey(MeasureAttributes.PhysicalStartX))
  178. data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces);
  179. else
  180. data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces));
  181. if (data.ContainsKey(MeasureAttributes.PhysicalStartY))
  182. data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces);
  183. else
  184. data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces));
  185. if (data.ContainsKey(MeasureAttributes.PixelLength))
  186. data[MeasureAttributes.PixelLength] = Math.Round(Length, decimalPlaces);
  187. else
  188. data.Add(MeasureAttributes.PixelLength, Math.Round(Length, decimalPlaces));
  189. string s = Math.Round(Length * unitLength, decimalPlaces).ToString();
  190. if (s.IndexOf(".") == -1)
  191. {
  192. for (int i = 0; i < decimalPlaces; i++)
  193. {
  194. if (i == 0)
  195. s += ".";
  196. s += "0";
  197. }
  198. }
  199. else
  200. {
  201. int a = s.Length - s.IndexOf(".") - 1;
  202. if (a < decimalPlaces)
  203. {
  204. for (int i = 0; i < decimalPlaces - a; i++)
  205. {
  206. s += "0";
  207. }
  208. }
  209. }
  210. if (data.ContainsKey(MeasureAttributes.PhysicalLength))
  211. data[MeasureAttributes.PhysicalLength] = s;
  212. else
  213. data.Add(MeasureAttributes.PhysicalLength, s);
  214. return data;
  215. }
  216. protected GraphicsPath AreaPath
  217. {
  218. get
  219. {
  220. return areaPath;
  221. }
  222. set
  223. {
  224. areaPath = value;
  225. }
  226. }
  227. protected Pen AreaPen
  228. {
  229. get
  230. {
  231. return areaPen;
  232. }
  233. set
  234. {
  235. areaPen = value;
  236. }
  237. }
  238. protected Region AreaRegion
  239. {
  240. get
  241. {
  242. return areaRegion;
  243. }
  244. set
  245. {
  246. areaRegion = value;
  247. }
  248. }
  249. public override void Draw(Graphics g)
  250. {
  251. if(this.moved)
  252. {
  253. drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList);
  254. pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange);
  255. SizeF sizeF = new SizeF();
  256. Font textfont = new Font(measureTraceCurve.font, measureTraceCurve.fontSize);
  257. Brush textbrush = new SolidBrush(Color.FromArgb(measureTraceCurve.textColor));
  258. Pen linePen = new Pen(Color.FromArgb(measureTraceCurve.lineColor), measureTraceCurve.lineWidth);
  259. linePen.DashStyle = (DashStyle)measureTraceCurve.lineStyle;
  260. Matrix mtxSave = g.Transform;
  261. Matrix matrix = g.Transform;
  262. float x1 = 0, y1 = 0; // previous pointscroll
  263. float x2, y2; // current pointscroll
  264. g.SmoothingMode = SmoothingMode.AntiAlias;
  265. PointF firstPoint = new PointF();
  266. PointF secondPoint = new PointF();
  267. PointEnumerator enumerator = pointArray.GetEnumerator();
  268. if (pointArray.Count > 2)
  269. {
  270. if (drawingPropertiesList == null)
  271. {
  272. drawingPropertiesList = drawingPropertiesListClone;
  273. this.configurationFile = true;
  274. }
  275. firstPoint = pointArray[0];
  276. secondPoint = pointArray[1];
  277. }
  278. if (enumerator.MoveNext())
  279. {
  280. x1 = ((PointF)enumerator.Current).X;
  281. y1 = ((PointF)enumerator.Current).Y;
  282. }
  283. double length = 0f;
  284. while (enumerator.MoveNext())
  285. {
  286. x2 = ((PointF)enumerator.Current).X;
  287. y2 = ((PointF)enumerator.Current).Y;
  288. g.DrawLine(linePen, x1, y1, x2, y2);
  289. length += BasicCalculationHelper.GetDistance(new PointF(x1, y1), new PointF(x2, y2), 4);
  290. x1 = x2;
  291. y1 = y2;
  292. }
  293. this.Length = length;
  294. //计算需要旋转的角度
  295. if (measureTraceCurve.linePosition == 0)
  296. {
  297. angle = BasicCalculationHelper.Angle(pointArray[0], pointArray[1], new PointF(pointArray[0].X, pointArray[1].Y));
  298. rotationPoint = pointArray[0];
  299. }
  300. else if (measureTraceCurve.linePosition == 1)
  301. {
  302. angle = BasicCalculationHelper.Angle(pointArray[pointArray.Count - 2], pointArray[pointArray.Count - 1], new PointF(pointArray[pointArray.Count - 2].X, pointArray[pointArray.Count - 1].Y));
  303. rotationPoint = pointArray[pointArray.Count - 1];
  304. }
  305. // 画布旋转
  306. if (angle < 90)
  307. {
  308. matrix.RotateAt((float)angle, new PointF(rotationPoint.X, rotationPoint.Y));
  309. g.Transform = matrix;
  310. }
  311. else
  312. {
  313. if (measureTraceCurve.linePosition == 0)
  314. {
  315. angle = BasicCalculationHelper.Angle(pointArray[1], pointArray[0], new PointF(pointArray[1].X, pointArray[0].Y));
  316. }
  317. else if (measureTraceCurve.linePosition == 1)
  318. {
  319. angle = BasicCalculationHelper.Angle(pointArray[pointArray.Count - 1], pointArray[pointArray.Count - 2], new PointF(pointArray[pointArray.Count - 1].X, pointArray[pointArray.Count - 2].Y));
  320. }
  321. matrix.RotateAt((float)angle, new PointF(rotationPoint.X, rotationPoint.Y));
  322. g.Transform = matrix;
  323. }
  324. // 是否绘制
  325. if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute)
  326. {
  327. // 属性文本的间隔定义
  328. int offsetValue = measureTraceCurve.fontSize + measureTraceCurve.fontSize / 2;
  329. int offsetValue1 = measureTraceCurve.lineWidth * 2 / 3;
  330. int offset = offsetValue1;
  331. this.pointL = new Point((int)rotationPoint.X, (int)rotationPoint.Y);
  332. if (drawingPropertiesList != null)
  333. {
  334. // 像素长度
  335. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  336. {
  337. sizeF = g.MeasureString("" + Math.Round(length, decimalPlaces) + "px", textfont);
  338. rectangleF8.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  339. offset += offsetValue;
  340. }
  341. // 物理长度
  342. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  343. {
  344. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  345. if (s.IndexOf(".") == -1)
  346. {
  347. for (int i = 0; i < decimalPlaces; i++)
  348. {
  349. if (i == 0)
  350. s += ".";
  351. s += "0";
  352. }
  353. }
  354. else
  355. {
  356. int a = s.Length - s.IndexOf(".") - 1;
  357. if (a < decimalPlaces)
  358. {
  359. for (int i = 0; i < decimalPlaces - a; i++)
  360. {
  361. s += "0";
  362. }
  363. }
  364. }
  365. sizeF = g.MeasureString(s + this.unit, textfont);
  366. rectangleF9.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  367. offset += offsetValue;
  368. }
  369. // 测量方式
  370. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  371. {
  372. sizeF = g.MeasureString("" + PdnResources.GetString("Menu.MeasureAction.CurveLength.Text"), textfont);
  373. rectangleF1.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  374. offset += offsetValue;
  375. }
  376. // 测量单位(中文)
  377. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  378. {
  379. sizeF = g.MeasureString("" + this.unitString, textfont);
  380. rectangleF2.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  381. offset += offsetValue;
  382. }
  383. // 测量单位(英文)
  384. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  385. {
  386. sizeF = g.MeasureString("" + this.unit, textfont);
  387. rectangleF3.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  388. offset += offsetValue;
  389. }
  390. // 像素起始点X
  391. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  392. {
  393. sizeF = g.MeasureString("" + startPoint.X, textfont);
  394. rectangleF4.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  395. offset += offsetValue;
  396. }
  397. // 像素起始点Y
  398. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  399. {
  400. sizeF = g.MeasureString("" + startPoint.Y, textfont);
  401. rectangleF5.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  402. offset += offsetValue;
  403. }
  404. // 物理起始点X
  405. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  406. {
  407. sizeF = g.MeasureString("" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  408. rectangleF6.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  409. offset += offsetValue;
  410. }
  411. // 物理起始点Y
  412. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  413. {
  414. sizeF = g.MeasureString("" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  415. rectangleF7.Location = new Point((int)rotationPoint.X - (int)sizeF.Width / 2, (int)rotationPoint.Y + offset);
  416. offset += offsetValue;
  417. }
  418. }
  419. }
  420. if (drawingPropertiesList != null)
  421. {
  422. // 测量方式
  423. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  424. {
  425. sizeF = g.MeasureString("" + PdnResources.GetString("Menu.MeasureAction.CurveLength.Text"), textfont);
  426. rectangleF1.Width = sizeF.Width;
  427. rectangleF1.Height = sizeF.Height;
  428. g.DrawString("" + PdnResources.GetString("Menu.MeasureAction.CurveLength.Text"), textfont, textbrush, rectangleF1);
  429. }
  430. else
  431. {
  432. rectangleF1 = new RectangleF();
  433. }
  434. // 测量单位(中文)
  435. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  436. {
  437. sizeF = g.MeasureString("" + this.unitString, textfont);
  438. rectangleF2.Width = sizeF.Width;
  439. rectangleF2.Height = sizeF.Height;
  440. g.DrawString("" + this.unitString, textfont, textbrush, rectangleF2);
  441. }
  442. else
  443. {
  444. rectangleF2 = new RectangleF();
  445. }
  446. // 测量单位(英文)
  447. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  448. {
  449. sizeF = g.MeasureString("" + this.unit, textfont);
  450. rectangleF3.Width = sizeF.Width;
  451. rectangleF3.Height = sizeF.Height;
  452. g.DrawString("" + this.unit, textfont, textbrush, rectangleF3);
  453. }
  454. else
  455. {
  456. rectangleF3 = new RectangleF();
  457. }
  458. // 像素起始点X
  459. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  460. {
  461. sizeF = g.MeasureString("" + startPoint.X, textfont);
  462. rectangleF4.Width = sizeF.Width;
  463. rectangleF4.Height = sizeF.Height;
  464. g.DrawString("" + startPoint.X, textfont, textbrush, rectangleF4);
  465. }
  466. else
  467. {
  468. rectangleF4 = new RectangleF();
  469. }
  470. // 像素起始点Y
  471. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  472. {
  473. sizeF = g.MeasureString("" + startPoint.Y, textfont);
  474. rectangleF5.Width = sizeF.Width;
  475. rectangleF5.Height = sizeF.Height;
  476. g.DrawString("" + startPoint.Y, textfont, textbrush, rectangleF5);
  477. }
  478. else
  479. {
  480. rectangleF5 = new RectangleF();
  481. }
  482. // 物理起始点X
  483. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  484. {
  485. sizeF = g.MeasureString("" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  486. rectangleF6.Width = sizeF.Width;
  487. rectangleF6.Height = sizeF.Height;
  488. g.DrawString("" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont, textbrush, rectangleF6);
  489. }
  490. else
  491. {
  492. rectangleF6 = new RectangleF();
  493. }
  494. // 物理起始点Y
  495. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  496. {
  497. sizeF = g.MeasureString("" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  498. rectangleF7.Width = sizeF.Width;
  499. rectangleF7.Height = sizeF.Height;
  500. g.DrawString("" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont, textbrush, rectangleF7);
  501. }
  502. else
  503. {
  504. rectangleF7 = new RectangleF();
  505. }
  506. // 像素长度
  507. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  508. {
  509. sizeF = g.MeasureString("" + Math.Round(length, decimalPlaces) + "px", textfont);
  510. rectangleF8.Width = sizeF.Width;
  511. rectangleF8.Height = sizeF.Height;
  512. g.DrawString("" + Math.Round(length, decimalPlaces) + "px", textfont, textbrush, rectangleF8);
  513. }
  514. else
  515. {
  516. rectangleF8 = new RectangleF();
  517. }
  518. // 物理长度
  519. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  520. {
  521. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  522. if (s.IndexOf(".") == -1)
  523. {
  524. for (int i = 0; i < decimalPlaces; i++)
  525. {
  526. if (i == 0)
  527. s += ".";
  528. s += "0";
  529. }
  530. }
  531. else
  532. {
  533. int a = s.Length - s.IndexOf(".") - 1;
  534. if (a < decimalPlaces)
  535. {
  536. for (int i = 0; i < decimalPlaces - a; i++)
  537. {
  538. s += "0";
  539. }
  540. }
  541. }
  542. sizeF = g.MeasureString(s + this.unit, textfont);
  543. rectangleF9.Width = sizeF.Width;
  544. rectangleF9.Height = sizeF.Height;
  545. g.DrawString(s + this.unit, textfont, textbrush, rectangleF9);
  546. }
  547. else
  548. {
  549. rectangleF9 = new RectangleF();
  550. }
  551. }
  552. //还原为原始旋转矩阵
  553. g.Transform = mtxSave;
  554. linePen.Dispose();
  555. matrix.Dispose();
  556. if (this.configurationFile)
  557. this.pointChange = false;
  558. this.mouseUpAttribute = false;
  559. pointChangeObject.Remove(this.drawToolType);
  560. }
  561. }
  562. /// <summary>
  563. /// 停止绘制时
  564. /// </summary>
  565. /// <param name="up"></param>
  566. public override void MouseUp(bool up)
  567. {
  568. mouseUpPointChange = up;
  569. }
  570. public void AddPoint(Point point)
  571. {
  572. pointArray.Add(point);
  573. }
  574. public override int HandleCount
  575. {
  576. get
  577. {
  578. return pointArray.Count + 1;
  579. }
  580. }
  581. /// <summary>
  582. /// Get handle pointscroll by 1-based number
  583. /// </summary>
  584. /// <param name="handleNumber"></param>
  585. /// <returns></returns>
  586. public override PointF GetHandle(int handleNumber)
  587. {
  588. if (handleNumber < 1)
  589. handleNumber = 1;
  590. if (handleNumber > pointArray.Count && handleNumber != pointArray.Count + 1)
  591. handleNumber = pointArray.Count;
  592. if (handleNumber == pointArray.Count + 1)
  593. return this.pointL;
  594. else
  595. return pointArray[handleNumber - 1];
  596. }
  597. public override Cursor GetHandleCursor(int handleNumber)
  598. {
  599. return handleCursor;
  600. }
  601. public override void MoveHandleTo(Point point, int handleNumber)
  602. {
  603. if (handleNumber < 1)
  604. handleNumber = 1;
  605. if (handleNumber > pointArray.Count && handleNumber != pointArray.Count + 1)
  606. handleNumber = pointArray.Count;
  607. if (handleNumber == pointArray.Count + 1)
  608. {
  609. point = SetOffsetAfterRotation(point, rotationPoint, angle);
  610. if (this.moveKb == 1)
  611. this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  612. else if (this.moveKb == 2)
  613. this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  614. else if (this.moveKb == 3)
  615. this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  616. else if (this.moveKb == 4)
  617. this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  618. else if (this.moveKb == 5)
  619. this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  620. else if (this.moveKb == 6)
  621. this.rectangleF6.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  622. else if (this.moveKb == 7)
  623. this.rectangleF7.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  624. else if (this.moveKb == 8)
  625. this.rectangleF8.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  626. else if (this.moveKb == 9)
  627. this.rectangleF9.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  628. this.pointL = point;
  629. }
  630. else
  631. {
  632. this.mouseUpPointChange = true;
  633. if (handleNumber == 1)
  634. {
  635. this.rectangleF1.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  636. this.rectangleF2.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  637. this.rectangleF3.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  638. this.rectangleF4.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  639. this.rectangleF5.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  640. this.rectangleF6.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  641. this.rectangleF7.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  642. this.rectangleF8.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  643. this.rectangleF9.Offset(point.X - this.rotationPoint.X, point.Y - this.rotationPoint.Y);
  644. }
  645. pointArray[handleNumber - 1] = point;
  646. }
  647. this.startPoint = pointArray[0];
  648. Invalidate();
  649. }
  650. public override void Move(int deltaX, int deltaY)
  651. {
  652. int n = pointArray.Count;
  653. PointF point;
  654. for (int i = 0; i < n; i++)
  655. {
  656. point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  657. pointArray[i] = point;
  658. }
  659. this.startPoint = pointArray[0];
  660. int x = ISurfaceBox.UnscaleScalar(deltaX);
  661. int y = ISurfaceBox.UnscaleScalar(deltaY);
  662. pointL.X += x;
  663. pointL.Y += y;
  664. this.rectangleF1.Offset(x, y);
  665. this.rectangleF2.Offset(x, y);
  666. this.rectangleF3.Offset(x, y);
  667. this.rectangleF4.Offset(x, y);
  668. this.rectangleF5.Offset(x, y);
  669. this.rectangleF6.Offset(x, y);
  670. this.rectangleF7.Offset(x, y);
  671. this.rectangleF8.Offset(x, y);
  672. this.rectangleF9.Offset(x, y);
  673. Invalidate();
  674. }
  675. /// <summary>
  676. /// Hit test.
  677. /// Return value: -1 - no hit
  678. /// 0 - hit anywhere
  679. /// > 1 - handle number
  680. /// </summary>
  681. /// <param name="pointscroll"></param>
  682. /// <returns></returns>
  683. public override int HitTest(Point point)
  684. {
  685. if (Selected)
  686. {
  687. for (int i = 1; i <= HandleCount; i++)
  688. {
  689. if (GetHandleRectangle(i).Contains(point))
  690. return i;
  691. }
  692. if (GetRectanglePosition(this.rectangleF1, point, rotationPoint, angle))
  693. {
  694. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  695. moveKb = 1;
  696. return this.pointArray.Count + 1;
  697. }
  698. else if (GetRectanglePosition(this.rectangleF2, point, rotationPoint, angle))
  699. {
  700. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  701. moveKb = 2;
  702. return this.pointArray.Count + 1;
  703. }
  704. else if (GetRectanglePosition(this.rectangleF3, point, rotationPoint, angle))
  705. {
  706. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  707. moveKb = 3;
  708. return this.pointArray.Count + 1;
  709. }
  710. else if (GetRectanglePosition(this.rectangleF4, point, rotationPoint, angle))
  711. {
  712. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  713. moveKb = 4;
  714. return this.pointArray.Count + 1;
  715. }
  716. else if (GetRectanglePosition(this.rectangleF5, point, rotationPoint, angle))
  717. {
  718. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  719. moveKb = 5;
  720. return this.pointArray.Count + 1;
  721. }
  722. else if (GetRectanglePosition(this.rectangleF6, point, rotationPoint, angle))
  723. {
  724. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  725. moveKb = 6;
  726. return this.pointArray.Count + 1;
  727. }
  728. else if (GetRectanglePosition(this.rectangleF7, point, rotationPoint, angle))
  729. {
  730. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  731. moveKb = 7;
  732. return this.pointArray.Count + 1;
  733. }
  734. else if (GetRectanglePosition(this.rectangleF8, point, rotationPoint, angle))
  735. {
  736. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  737. moveKb = 8;
  738. return this.pointArray.Count + 1;
  739. }
  740. else if (GetRectanglePosition(this.rectangleF9, point, rotationPoint, angle))
  741. {
  742. this.pointL = SetOffsetAfterRotation(point, rotationPoint, angle);
  743. moveKb = 9;
  744. return this.pointArray.Count + 1;
  745. }
  746. }
  747. if (PointInObject(point))
  748. return 0;
  749. return -1;
  750. }
  751. protected override bool PointInObject(Point point)
  752. {
  753. CreateObjects();
  754. return AreaRegion.IsVisible(point);
  755. }
  756. public override bool IntersectsWith(Rectangle rectangle)
  757. {
  758. CreateObjects();
  759. return AreaRegion.IsVisible(rectangle);
  760. }
  761. protected virtual void CreateObjects()
  762. {
  763. if (AreaPath != null)
  764. return;
  765. AreaPath = new GraphicsPath();
  766. AreaPath.AddRectangle(GetBoundingBox());
  767. AreaPath.CloseFigure();
  768. AreaRegion = new Region(AreaPath);
  769. }
  770. /// <summary>
  771. /// Invalidate object.
  772. /// When object is invalidated, path used for hit test
  773. /// is released and should be created again.
  774. /// </summary>
  775. protected void Invalidate()
  776. {
  777. if (AreaPath != null)
  778. {
  779. AreaPath.Dispose();
  780. AreaPath = null;
  781. }
  782. if (AreaPen != null)
  783. {
  784. AreaPen.Dispose();
  785. AreaPen = null;
  786. }
  787. if (AreaRegion != null)
  788. {
  789. AreaRegion.Dispose();
  790. AreaRegion = null;
  791. }
  792. }
  793. /// <summary>
  794. /// Draw tracker for selected object
  795. /// </summary>
  796. /// <param name="g"></param>
  797. public override void DrawTracker(Graphics g)
  798. {
  799. if (!Selected)
  800. return;
  801. SolidBrush brush = new SolidBrush(Color.Black);
  802. for (int i = 1; i <= HandleCount; i++)
  803. {
  804. if (i == HandleCount)
  805. brush = new SolidBrush(Color.Transparent);
  806. g.FillRectangle(brush, GetHandleRectangle(i));
  807. }
  808. brush.Dispose();
  809. RectangleF r = GetBoundingBox();
  810. //g.DrawRectangle(new Pen(Color.White), r.X, r.Y, r.Width, r.Height);
  811. }
  812. public override RectangleF GetBoundingBox()
  813. {
  814. RectangleF rectangle;
  815. float minx = 0, maxx = 0, miny = 0, maxy = 0;
  816. for (int i = 0; i < pointArray.Count; i++)
  817. {
  818. if (i == 0)
  819. {
  820. minx = maxx = pointArray[i].X;
  821. miny = maxy = pointArray[i].Y;
  822. }
  823. else
  824. {
  825. if (pointArray[i].X > maxx) maxx = pointArray[i].X;
  826. if (pointArray[i].X < minx) minx = pointArray[i].X;
  827. if (pointArray[i].Y > maxy) maxy = pointArray[i].Y;
  828. if (pointArray[i].Y < miny) miny = pointArray[i].Y;
  829. }
  830. }
  831. rectangle = new RectangleF(minx, miny, maxx - minx, maxy - miny);
  832. return rectangle;
  833. }
  834. public override List<PointF> GetPoints()
  835. {
  836. return pointArray;
  837. }
  838. public override ParentStyleModel GetStyle()
  839. {
  840. return measureTraceCurve;
  841. }
  842. }
  843. }