MeasureCurveLine.cs 40 KB

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