MeasureBrokenLine.cs 43 KB

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