MeasureTraceCurve.cs 41 KB

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