MeasureVLine.cs 42 KB

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