MeasureDistanceLine.cs 40 KB

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