MeasureVLine.cs 46 KB

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