MeasureCurveLine.cs 37 KB

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