MeasureDistanceLine.cs 40 KB

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