MeasureVLine.cs 42 KB

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