MeasurePointHLine.cs 44 KB

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