MeasureHLine.cs 38 KB

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