MeasureDistanceLine.cs 45 KB

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