MeasureLength.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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 MeasureLength : MeasureDrawObject
  19. {
  20. public PointList pointArray;
  21. private const string entryLength = "Length";
  22. private const string entryPoint = "Point";
  23. /// <summary>
  24. /// Graphic objects for hit test
  25. /// </summary>
  26. private GraphicsPath areaPath = null;
  27. private Pen areaPen = null;
  28. private Region areaRegion = null;
  29. ///<summary>
  30. ///长度
  31. ///</summary>
  32. //private double length = 0.0;
  33. /// <summary>
  34. /// 测量信息矩形定义
  35. /// </summary>
  36. private RectangleF rectangleF1 = new RectangleF();
  37. private RectangleF rectangleF2 = new RectangleF();
  38. private RectangleF rectangleF3 = new RectangleF();
  39. private RectangleF rectangleF4 = new RectangleF();
  40. private RectangleF rectangleF5 = new RectangleF();
  41. private RectangleF rectangleF6 = new RectangleF();
  42. private RectangleF rectangleF7 = new RectangleF();
  43. private RectangleF rectangleF8 = new RectangleF();
  44. private RectangleF rectangleF9 = new RectangleF();
  45. /// <summary>
  46. /// 文本上用于拖动的点
  47. /// </summary>
  48. private Point pointL = new Point();
  49. /// <summary>
  50. /// 绘制限制(第一次绘制时)
  51. /// </summary>
  52. public bool pointChange = true;
  53. /// <summary>
  54. /// 区分移动文本
  55. /// </summary>
  56. private int moveKb;
  57. /// <summary>
  58. /// 绘制限制(从配置文件加载)
  59. /// </summary>
  60. public bool configurationFile = false;
  61. /// <summary>
  62. /// 绘制限制(更改属性时)
  63. /// </summary>
  64. private bool SavePointChange;
  65. /// <summary>
  66. /// 绘制属性
  67. /// </summary>
  68. private string[] drawingPropertiesList;
  69. /// <summary>
  70. /// 绘制属性(克隆)
  71. /// </summary>
  72. private string[] drawingPropertiesListClone;
  73. /// <summary>
  74. /// 旋转角度
  75. /// </summary>
  76. private double angleClass;
  77. /// <summary>
  78. /// 属性绘制点
  79. /// </summary>
  80. private PointF rotationPoint;
  81. /// <summary>
  82. /// 限制绘制(绘制中)
  83. /// </summary>
  84. public bool mouseUpPointChange = false;
  85. /// <summary>
  86. /// 测量样式信息model
  87. /// </summary>
  88. private MeasureStyleModel.MeasureLength measureStyleModel;
  89. public MeasureStyleModel.MeasureLength MeasureStyleModel
  90. {
  91. set
  92. {
  93. this.measureStyleModel = value;
  94. }
  95. }
  96. public MeasureLength(ISurfaceBox surfaceBox, int x1, int y1, bool clone) : base(surfaceBox)
  97. {
  98. this.objectType = DrawClass.Measure;
  99. this.drawToolType = DrawToolType.MeasureLength;
  100. measureStyleModel = surfaceBox.GetMeasureStyleModel().measureLength;
  101. pointArray = new PointList();
  102. startPoint.X = x1;
  103. startPoint.Y = y1;
  104. if(clone)
  105. pointArray.Add(new Point(x1, y1));
  106. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  107. this.unitString = surfaceBox.GetPxPerUnit()[1];
  108. this.unit = surfaceBox.GetPxPerUnit()[2];
  109. surfaceBox.getMeasureInfo().TryGetValue(measurementUnit, out unitLength);
  110. Initialize();
  111. }
  112. public MeasureLength(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  113. {
  114. this.objectType = DrawClass.Measure;
  115. this.drawToolType = DrawToolType.MeasureLength;
  116. this.ISurfaceBox = surfaceBox;
  117. measureStyleModel = (MeasureStyleModel.MeasureLength)parentStyleModel;
  118. pointArray = DrawRulerHelper.DeepCopyListByReflect(points);
  119. startPoint = points[0];
  120. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  121. this.unitString = surfaceBox.GetPxPerUnit()[1];
  122. this.unit = surfaceBox.GetPxPerUnit()[2];
  123. surfaceBox.getMeasureInfo().TryGetValue(measurementUnit, out unitLength);
  124. this.configurationFile = true;
  125. }
  126. /// <summary>
  127. /// Clone this instance
  128. /// </summary>
  129. public override DrawObject Clone()
  130. {
  131. MeasureLength drawPolygon = new MeasureLength(ISurfaceBox, (int)this.pointArray[0].X, (int)this.pointArray[0].Y, false);
  132. drawPolygon.ISurfaceBox = ISurfaceBox;
  133. foreach (PointF p in this.pointArray)
  134. {
  135. drawPolygon.pointArray.Add(p);
  136. }
  137. if (drawingPropertiesList != null)
  138. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  139. FillDrawObjectFields(drawPolygon);
  140. return drawPolygon;
  141. }
  142. public override DrawObject Clone(ISurfaceBox surfaceBox)
  143. {
  144. MeasureLength drawPolygon = new MeasureLength(surfaceBox, this.GetPoints(), this.measureStyleModel, null);
  145. if (drawingPropertiesList != null)
  146. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  147. FillDrawObjectFields(drawPolygon);
  148. return drawPolygon;
  149. }
  150. /// <summary>
  151. /// 测量属性
  152. /// </summary>
  153. /// <returns></returns>
  154. public override Dictionary<System.Enum, object> GetData()
  155. {
  156. if (data.ContainsKey(MeasureAttributes.MeasureMethod))
  157. data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text");
  158. else
  159. data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"));
  160. if (data.ContainsKey(MeasureAttributes.MeasureUnitCN))
  161. data[MeasureAttributes.MeasureUnitCN] = this.unitString;
  162. else
  163. data.Add(MeasureAttributes.MeasureUnitCN, this.unitString);
  164. if (data.ContainsKey(MeasureAttributes.MeasureUnitEN))
  165. data[MeasureAttributes.MeasureUnitEN] = this.unit;
  166. else
  167. data.Add(MeasureAttributes.MeasureUnitEN, this.unit);
  168. if (data.ContainsKey(MeasureAttributes.PixelStartX))
  169. data[MeasureAttributes.PixelStartX] = startPoint.X;
  170. else
  171. data.Add(MeasureAttributes.PixelStartX, startPoint.X);
  172. if (data.ContainsKey(MeasureAttributes.PixelStartY))
  173. data[MeasureAttributes.PixelStartY] = startPoint.Y;
  174. else
  175. data.Add(MeasureAttributes.PixelStartY, startPoint.Y);
  176. if (data.ContainsKey(MeasureAttributes.PhysicalStartX))
  177. data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces);
  178. else
  179. data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces));
  180. if (data.ContainsKey(MeasureAttributes.PhysicalStartY))
  181. data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces);
  182. else
  183. data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces));
  184. if (data.ContainsKey(MeasureAttributes.PixelLength))
  185. data[MeasureAttributes.PixelLength] = Math.Round(length, decimalPlaces);
  186. else
  187. data.Add(MeasureAttributes.PixelLength, Math.Round(length, decimalPlaces));
  188. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  189. if (s.IndexOf(".") == -1)
  190. {
  191. for (int i = 0; i < decimalPlaces; i++)
  192. {
  193. if (i == 0)
  194. s += ".";
  195. s += "0";
  196. }
  197. }
  198. else
  199. {
  200. int a = s.Length - s.IndexOf(".") - 1;
  201. if (a < decimalPlaces)
  202. {
  203. for (int i = 0; i < decimalPlaces - a; i++)
  204. {
  205. s += "0";
  206. }
  207. }
  208. }
  209. if (data.ContainsKey(MeasureAttributes.PhysicalLength))
  210. data[MeasureAttributes.PhysicalLength] = s;
  211. else
  212. data.Add(MeasureAttributes.PhysicalLength, s);
  213. return data;
  214. }
  215. public override void Draw(Graphics g)
  216. {
  217. drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList);
  218. pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange);
  219. Font textfont = new Font(measureStyleModel.font, measureStyleModel.fontSize);
  220. Brush textbrush = new SolidBrush(Color.FromArgb(measureStyleModel.textColor));
  221. g.SmoothingMode = SmoothingMode.AntiAlias;
  222. Color color = Color.FromArgb(this.measureStyleModel.lineColor);
  223. Pen pen = new Pen(color, this.measureStyleModel.lineWidth);
  224. pen.DashStyle = (DashStyle)this.measureStyleModel.lineStyle;
  225. Matrix mtxSave = g.Transform;
  226. Matrix matrix = g.Transform;
  227. if (pointArray.Count == 2)
  228. {
  229. g.DrawLine(pen, pointArray[0], pointArray[1]);
  230. length = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[1], 10);
  231. }
  232. SizeF sizeF = new SizeF();
  233. if (pointArray.Count >= 3)
  234. {
  235. if (drawingPropertiesList == null)
  236. {
  237. drawingPropertiesList = drawingPropertiesListClone;
  238. this.configurationFile = true;
  239. }
  240. // 垂足坐标
  241. PointF footPoint = new PointF();
  242. // 垂足坐标
  243. footPoint = BasicCalculationHelper.GetDropFeet(pointArray[1], pointArray[0], pointArray[2]);
  244. // 点到直线距离
  245. double lengthF = BasicCalculationHelper.GetDistance(footPoint, pointArray[2], 2);
  246. // 第二个点与X轴的夹角
  247. double point2ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  248. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[1]), 2);
  249. // 第三个点与X轴的夹角
  250. double point3ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  251. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[2]), 2);
  252. // 两点直线与X轴夹角
  253. double angle = BasicCalculationHelper.AngleText(pointArray[0], new PointF(pointArray[0].X + 20, pointArray[0].Y), pointArray[1]);
  254. if (point2ToX > 90)
  255. {
  256. point2ToX = 180 - point2ToX;
  257. }
  258. if (point3ToX > 90)
  259. {
  260. point3ToX = 180 - point3ToX;
  261. }
  262. if (angle > 90)
  263. {
  264. angle = 180 - angle;
  265. }
  266. //判断第二个点相对于第一个点的象限
  267. int x2 = (int)(pointArray[1].X - pointArray[0].X);
  268. if (x2 == 0)
  269. x2 = 1;
  270. int y2 = (int)(pointArray[1].Y - pointArray[0].Y);
  271. if (y2 == 0)
  272. y2 = 1;
  273. int i2 = 0;
  274. if (x2 > 0 && y2 > 0) //第4象限
  275. {
  276. i2 = 4;
  277. }
  278. else if (x2 > 0 && y2 < 0) //第1象限
  279. {
  280. i2 = 1;
  281. }
  282. else if (x2 < 0 && y2 < 0) //第2象限
  283. {
  284. i2 = 2;
  285. }
  286. else if (x2 < 0 && y2 > 0) //第3象限
  287. {
  288. i2 = 3;
  289. }
  290. //判断第三个点相对于第一个点的象限
  291. int x3 = (int)(pointArray[2].X - pointArray[0].X);
  292. if (x3 == 0)
  293. x3 = 1;
  294. int y3 = (int)(pointArray[2].Y - pointArray[0].Y);
  295. if (y3 == 0)
  296. y3 = 1;
  297. int i3 = 0;
  298. if (x3 > 0 && y3 > 0) //第4象限
  299. {
  300. i3 = 4;
  301. }
  302. else if (x3 > 0 && y3 < 0) //第1象限
  303. {
  304. i3 = 1;
  305. }
  306. else if (x3 < 0 && y3 < 0) //第2象限
  307. {
  308. i3 = 2;
  309. }
  310. else if (x3 < 0 && y3 > 0) //第3象限
  311. {
  312. i3 = 3;
  313. }
  314. double angle1 = 0.0;
  315. double angle2 = 0.0;
  316. if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 1 || i3 == 2))
  317. {
  318. angle1 = 270;
  319. angle2 = 90;
  320. }
  321. else if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 3 || i3 == 4))
  322. {
  323. angle1 = 90;
  324. angle2 = 270;
  325. }
  326. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 1 || i3 == 4))
  327. {
  328. angle1 = 0;
  329. angle2 = 180;
  330. }
  331. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 2 || i3 == 3))
  332. {
  333. angle1 = 180;
  334. angle2 = 0;
  335. }
  336. else if ((i2 == 1 && i3 == 1) || (i2 == 3 && i3 == 1))
  337. {
  338. if (point3ToX > point2ToX)
  339. {
  340. angle1 = 270 - angle;
  341. angle2 = 90 - angle;
  342. }
  343. else
  344. {
  345. angle1 = 90 - angle;
  346. angle2 = 270 - angle;
  347. }
  348. }
  349. else if ((i2 == 1 && i3 == 2) || (i2 == 3 && i3 == 2))
  350. {
  351. angle1 = 270 - angle;
  352. angle2 = 90 - angle;
  353. }
  354. else if ((i2 == 1 && i3 == 3) || (i2 == 3 && i3 == 3))
  355. {
  356. if (point3ToX > point2ToX)
  357. {
  358. angle1 = 90 - angle;
  359. angle2 = 270 - angle;
  360. }
  361. else
  362. {
  363. angle1 = 270 - angle;
  364. angle2 = 90 - angle;
  365. }
  366. }
  367. else if ((i2 == 1 && i3 == 4) || (i2 == 3 && i3 == 4))
  368. {
  369. angle1 = 90 - angle;
  370. angle2 = 270 - angle;
  371. }
  372. else if ((i2 == 2 && i3 == 1) || (i2 == 4 && i3 == 1))
  373. {
  374. angle1 = 270 + angle;
  375. angle2 = 90 + angle;
  376. }
  377. else if ((i2 == 2 && i3 == 2) || (i2 == 4 && i3 == 2))
  378. {
  379. if (point3ToX > point2ToX)
  380. {
  381. angle1 = 270 + angle;
  382. angle2 = 90 + angle;
  383. }
  384. else
  385. {
  386. angle1 = 90 + angle;
  387. angle2 = 270 + angle;
  388. }
  389. }
  390. else if ((i2 == 2 && i3 == 3) || (i2 == 4 && i3 == 3))
  391. {
  392. angle1 = 90 + angle;
  393. angle2 = 270 + angle;
  394. }
  395. else if ((i2 == 2 && i3 == 4) || (i2 == 4 && i3 == 4))
  396. {
  397. if (point3ToX > point2ToX)
  398. {
  399. angle1 = 90 + angle;
  400. angle2 = 270 + angle;
  401. }
  402. else
  403. {
  404. angle1 = 270 + angle;
  405. angle2 = 90 + angle;
  406. }
  407. }
  408. PointF point = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[0].X + (int)lengthF,
  409. pointArray[0].Y), pointArray[0], angle1);
  410. PointF point1 = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[1].X - (int)lengthF,
  411. pointArray[1].Y), pointArray[1], angle2);
  412. // 求中点坐标
  413. float x = (point.X + point1.X) / 2;
  414. float y = (point.Y + point1.Y) / 2;
  415. // 绘制长度
  416. g.DrawLine(pen, point, point1);
  417. g.DrawLine(pen, point, pointArray[0]);
  418. g.DrawLine(pen, point1, pointArray[1]);
  419. // 长度
  420. length = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[1], 10);
  421. pen.Dispose();
  422. //计算需要旋转的角度
  423. this.angleClass = BasicCalculationHelper.Angle(point, point1, new PointF(point.X, point1.Y));
  424. int offsetValuePoint = measureStyleModel.fontSize + measureStyleModel.fontSize / 2;
  425. int offsetValue1Point = measureStyleModel.lineWidth * 2 / 3;
  426. int offsetPoint = offsetValue1Point;
  427. if (drawingPropertiesList != null)
  428. {
  429. // 像素长度
  430. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  431. {
  432. offsetPoint += offsetValuePoint;
  433. }
  434. // 物理长度
  435. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  436. {
  437. offsetPoint += offsetValuePoint;
  438. }
  439. // 测量方式
  440. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  441. {
  442. offsetPoint += offsetValuePoint;
  443. }
  444. // 测量单位(中文)
  445. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  446. {
  447. offsetPoint += offsetValuePoint;
  448. }
  449. // 测量单位(英文)
  450. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  451. {
  452. offsetPoint += offsetValuePoint;
  453. }
  454. // 像素起始点X
  455. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  456. {
  457. offsetPoint += offsetValuePoint;
  458. }
  459. // 像素起始点Y
  460. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  461. {
  462. offsetPoint += offsetValuePoint;
  463. }
  464. // 物理起始点X
  465. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  466. {
  467. offsetPoint += offsetValuePoint;
  468. }
  469. // 物理起始点Y
  470. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  471. {
  472. offsetPoint += offsetValuePoint;
  473. }
  474. }
  475. PointF pointF1;
  476. PointF pointF2;
  477. if (angleClass < 90)
  478. pointF1 = BasicCalculationHelper.GetAnglePoint(new Point((int)point.X + offsetPoint + offsetValue1Point, (int)point.Y), point, angleClass - 90);
  479. else
  480. pointF1 = BasicCalculationHelper.GetAnglePoint(new Point((int)point.X + offsetPoint + offsetValue1Point, (int)point.Y), point, angleClass + 90);
  481. if (angleClass < 90)
  482. pointF2 = BasicCalculationHelper.GetAnglePoint(new Point((int)point1.X + offsetPoint + offsetValue1Point, (int)point1.Y), point1, angleClass - 90);
  483. else
  484. pointF2 = BasicCalculationHelper.GetAnglePoint(new Point((int)point1.X + offsetPoint + offsetValue1Point, (int)point1.Y), point1, angleClass + 90);
  485. PointF pointF3 = new PointF((pointF1.X + pointF2.X) / 2, (pointF1.Y + pointF2.Y) / 2);
  486. if (measureStyleModel.linePosition == 0)
  487. this.rotationPoint = pointArray[0];
  488. else if (measureStyleModel.linePosition == 1)
  489. this.rotationPoint = pointF3;
  490. else if (measureStyleModel.linePosition == 2)
  491. this.rotationPoint = pointArray[1];
  492. else if (measureStyleModel.linePosition == 3)
  493. this.rotationPoint = point;
  494. else if (measureStyleModel.linePosition == 4)
  495. this.rotationPoint = new PointF(x, y);
  496. else if (measureStyleModel.linePosition == 5)
  497. this.rotationPoint = point1;
  498. // 画布旋转
  499. if (this.angleClass < 90)
  500. {
  501. matrix.RotateAt((float)this.angleClass, this.rotationPoint);
  502. g.Transform = matrix;
  503. }
  504. else
  505. {
  506. this.angleClass = BasicCalculationHelper.Angle(point1, point, new PointF(point1.X, point.Y));
  507. matrix.RotateAt((float)this.angleClass, this.rotationPoint);
  508. g.Transform = matrix;
  509. }
  510. // 是否绘制
  511. if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute)
  512. {
  513. // 属性文本的间隔定义
  514. int offsetValue = measureStyleModel.fontSize + measureStyleModel.fontSize / 2;
  515. int offsetValue1 = measureStyleModel.lineWidth * 2 / 3;
  516. int offset = offsetValue1;
  517. this.pointL = new Point((int)this.rotationPoint.X, (int)this.rotationPoint.Y);
  518. if (drawingPropertiesList != null)
  519. {
  520. // 像素长度
  521. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  522. {
  523. sizeF = g.MeasureString("" + Math.Round(length, decimalPlaces) + "px", textfont);
  524. rectangleF8.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  525. offset += offsetValue;
  526. }
  527. // 物理长度
  528. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  529. {
  530. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  531. if (s.IndexOf(".") == -1)
  532. {
  533. for (int i = 0; i < decimalPlaces; i++)
  534. {
  535. if (i == 0)
  536. s += ".";
  537. s += "0";
  538. }
  539. }
  540. else
  541. {
  542. int a = s.Length - s.IndexOf(".") - 1;
  543. if (a < decimalPlaces)
  544. {
  545. for (int i = 0; i < decimalPlaces - a; i++)
  546. {
  547. s += "0";
  548. }
  549. }
  550. }
  551. sizeF = g.MeasureString(s + this.unit, textfont);
  552. rectangleF9.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  553. offset += offsetValue;
  554. }
  555. // 测量方式
  556. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  557. {
  558. sizeF = g.MeasureString("" + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont);
  559. rectangleF1.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  560. offset += offsetValue;
  561. }
  562. // 测量单位(中文)
  563. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  564. {
  565. sizeF = g.MeasureString("" + this.unitString, textfont);
  566. rectangleF2.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  567. offset += offsetValue;
  568. }
  569. // 测量单位(英文)
  570. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  571. {
  572. sizeF = g.MeasureString("" + this.unit, textfont);
  573. rectangleF3.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  574. offset += offsetValue;
  575. }
  576. // 像素起始点X
  577. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  578. {
  579. sizeF = g.MeasureString("" + pointArray[0].X, textfont);
  580. rectangleF4.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  581. offset += offsetValue;
  582. }
  583. // 像素起始点Y
  584. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  585. {
  586. sizeF = g.MeasureString("" + pointArray[0].Y, textfont);
  587. rectangleF5.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  588. offset += offsetValue;
  589. }
  590. // 物理起始点X
  591. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  592. {
  593. sizeF = g.MeasureString("" + Math.Round(pointArray[0].X * unitLength, decimalPlaces), textfont);
  594. rectangleF6.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  595. offset += offsetValue;
  596. }
  597. // 物理起始点Y
  598. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  599. {
  600. sizeF = g.MeasureString("" + Math.Round(pointArray[0].Y * unitLength, decimalPlaces), textfont);
  601. rectangleF7.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  602. offset += offsetValue;
  603. }
  604. }
  605. }
  606. if (drawingPropertiesList != null)
  607. {
  608. // 测量方式
  609. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  610. {
  611. sizeF = g.MeasureString("" + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont);
  612. rectangleF1.Width = sizeF.Width;
  613. rectangleF1.Height = sizeF.Height;
  614. g.DrawString("" + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont, textbrush, rectangleF1);
  615. }
  616. else
  617. {
  618. rectangleF1 = new RectangleF();
  619. }
  620. // 测量单位(中文)
  621. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  622. {
  623. sizeF = g.MeasureString("" + this.unitString, textfont);
  624. rectangleF2.Width = sizeF.Width;
  625. rectangleF2.Height = sizeF.Height;
  626. g.DrawString("" + this.unitString, textfont, textbrush, rectangleF2);
  627. }
  628. else
  629. {
  630. rectangleF2 = new RectangleF();
  631. }
  632. // 测量单位(英文)
  633. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  634. {
  635. sizeF = g.MeasureString("" + this.unit, textfont);
  636. rectangleF3.Width = sizeF.Width;
  637. rectangleF3.Height = sizeF.Height;
  638. g.DrawString("" + this.unit, textfont, textbrush, rectangleF3);
  639. }
  640. else
  641. {
  642. rectangleF3 = new RectangleF();
  643. }
  644. // 像素起始点X
  645. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  646. {
  647. sizeF = g.MeasureString("" + pointArray[0].X, textfont);
  648. rectangleF4.Width = sizeF.Width;
  649. rectangleF4.Height = sizeF.Height;
  650. g.DrawString("" + pointArray[0].X, textfont, textbrush, rectangleF4);
  651. }
  652. else
  653. {
  654. rectangleF4 = new RectangleF();
  655. }
  656. // 像素起始点Y
  657. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  658. {
  659. sizeF = g.MeasureString("" + pointArray[0].Y, textfont);
  660. rectangleF5.Width = sizeF.Width;
  661. rectangleF5.Height = sizeF.Height;
  662. g.DrawString("" + pointArray[0].Y, textfont, textbrush, rectangleF5);
  663. }
  664. else
  665. {
  666. rectangleF5 = new RectangleF();
  667. }
  668. // 物理起始点X
  669. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  670. {
  671. sizeF = g.MeasureString("" + Math.Round(pointArray[0].X * unitLength, decimalPlaces), textfont);
  672. rectangleF6.Width = sizeF.Width;
  673. rectangleF6.Height = sizeF.Height;
  674. g.DrawString("" + Math.Round(pointArray[0].X * unitLength, decimalPlaces), textfont, textbrush, rectangleF6);
  675. }
  676. else
  677. {
  678. rectangleF6 = new RectangleF();
  679. }
  680. // 物理起始点Y
  681. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  682. {
  683. sizeF = g.MeasureString("" + Math.Round(pointArray[0].Y * unitLength, decimalPlaces), textfont);
  684. rectangleF7.Width = sizeF.Width;
  685. rectangleF7.Height = sizeF.Height;
  686. g.DrawString("" + Math.Round(pointArray[0].Y * unitLength, decimalPlaces), textfont, textbrush, rectangleF7);
  687. }
  688. else
  689. {
  690. rectangleF7 = new RectangleF();
  691. }
  692. // 像素长度
  693. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  694. {
  695. sizeF = g.MeasureString("" + Math.Round(length, decimalPlaces) + "px", textfont);
  696. rectangleF8.Width = sizeF.Width;
  697. rectangleF8.Height = sizeF.Height;
  698. g.DrawString("" + Math.Round(length, decimalPlaces) + "px", textfont, textbrush, rectangleF8);
  699. }
  700. else
  701. {
  702. rectangleF8 = new RectangleF();
  703. }
  704. // 物理长度
  705. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  706. {
  707. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  708. if (s.IndexOf(".") == -1)
  709. {
  710. for (int i = 0; i < decimalPlaces; i++)
  711. {
  712. if (i == 0)
  713. s += ".";
  714. s += "0";
  715. }
  716. }
  717. else
  718. {
  719. int a = s.Length - s.IndexOf(".") - 1;
  720. if (a < decimalPlaces)
  721. {
  722. for (int i = 0; i < decimalPlaces - a; i++)
  723. {
  724. s += "0";
  725. }
  726. }
  727. }
  728. sizeF = g.MeasureString(s + this.unit, textfont);
  729. rectangleF9.Width = sizeF.Width;
  730. rectangleF9.Height = sizeF.Height;
  731. g.DrawString(s + this.unit, textfont, textbrush, rectangleF9);
  732. }
  733. else
  734. {
  735. rectangleF9 = new RectangleF();
  736. }
  737. }
  738. //还原为原始旋转矩阵
  739. g.Transform = mtxSave;
  740. }
  741. matrix.Dispose();
  742. if (this.configurationFile)
  743. this.pointChange = false;
  744. this.mouseUpAttribute = false;
  745. pointChangeObject.Remove(this.drawToolType);
  746. }
  747. /// <summary>
  748. /// 停止绘制时
  749. /// </summary>
  750. /// <param name="up"></param>
  751. public override void MouseUp(bool up)
  752. {
  753. mouseUpPointChange = up;
  754. }
  755. public void AddPoint(Point point)
  756. {
  757. pointArray.Add(point);
  758. }
  759. public override int HandleCount
  760. {
  761. get
  762. {
  763. return 6;
  764. }
  765. }
  766. /// <summary>
  767. /// Get handle pointscroll by 1-based number
  768. /// </summary>
  769. /// <param name="handleNumber"></param>
  770. /// <returns></returns>
  771. public override PointF GetHandle(int handleNumber)
  772. {
  773. if (handleNumber < 1)
  774. handleNumber = 1;
  775. if (handleNumber > pointArray.Count && handleNumber <= 3)
  776. handleNumber = pointArray.Count;
  777. if (handleNumber == 1)
  778. return pointArray[0];
  779. else if (handleNumber == 2)
  780. return pointArray[1];
  781. else if (handleNumber == 6)
  782. return pointL;
  783. else if (handleNumber > 2 && pointArray.Count >= 3)
  784. {
  785. // 垂足坐标
  786. PointF footPoint = new PointF();
  787. // 垂足坐标
  788. footPoint = BasicCalculationHelper.GetDropFeet(pointArray[1], pointArray[0], pointArray[2]);
  789. // 点到直线距离
  790. double length = BasicCalculationHelper.GetDistance(footPoint, pointArray[2], 2);
  791. // 第二个点与X轴的夹角
  792. double point2ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  793. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[1]), 2);
  794. // 第三个点与X轴的夹角
  795. double point3ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  796. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[2]), 2);
  797. // 两点直线与X轴夹角
  798. double angle = BasicCalculationHelper.AngleText(pointArray[0], new PointF(pointArray[0].X + 20, pointArray[0].Y), pointArray[1]);
  799. if (point2ToX > 90)
  800. {
  801. point2ToX = 180 - point2ToX;
  802. }
  803. if (point3ToX > 90)
  804. {
  805. point3ToX = 180 - point3ToX;
  806. }
  807. if (angle > 90)
  808. {
  809. angle = 180 - angle;
  810. }
  811. //判断第二个点相对于第一个点的象限
  812. int x2 = (int)(pointArray[1].X - pointArray[0].X);
  813. if (x2 == 0)
  814. x2 = 1;
  815. int y2 = (int)(pointArray[1].Y - pointArray[0].Y);
  816. if (y2 == 0)
  817. y2 = 1;
  818. int i2 = 0;
  819. if (x2 > 0 && y2 > 0) //第4象限
  820. {
  821. i2 = 4;
  822. }
  823. else if (x2 > 0 && y2 < 0) //第1象限
  824. {
  825. i2 = 1;
  826. }
  827. else if (x2 < 0 && y2 < 0) //第2象限
  828. {
  829. i2 = 2;
  830. }
  831. else if (x2 < 0 && y2 > 0) //第3象限
  832. {
  833. i2 = 3;
  834. }
  835. //判断第三个点相对于第一个点的象限
  836. int x3 = (int)(pointArray[2].X - pointArray[0].X);
  837. if (x3 == 0)
  838. x3 = 1;
  839. int y3 = (int)(pointArray[2].Y - pointArray[0].Y);
  840. if (y3 == 0)
  841. y3 = 1;
  842. int i3 = 0;
  843. if (x3 > 0 && y3 > 0) //第4象限
  844. {
  845. i3 = 4;
  846. }
  847. else if (x3 > 0 && y3 < 0) //第1象限
  848. {
  849. i3 = 1;
  850. }
  851. else if (x3 < 0 && y3 < 0) //第2象限
  852. {
  853. i3 = 2;
  854. }
  855. else if (x3 < 0 && y3 > 0) //第3象限
  856. {
  857. i3 = 3;
  858. }
  859. double angle1 = 0.0;
  860. double angle2 = 0.0;
  861. if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 1 || i3 == 2))
  862. {
  863. angle1 = 270;
  864. angle2 = 90;
  865. }
  866. else if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 3 || i3 == 4))
  867. {
  868. angle1 = 90;
  869. angle2 = 270;
  870. }
  871. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 1 || i3 == 4))
  872. {
  873. angle1 = 0;
  874. angle2 = 180;
  875. }
  876. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 2 || i3 == 3))
  877. {
  878. angle1 = 180;
  879. angle2 = 0;
  880. }
  881. else if ((i2 == 1 && i3 == 1) || (i2 == 3 && i3 == 1))
  882. {
  883. if (point3ToX > point2ToX)
  884. {
  885. angle1 = 270 - angle;
  886. angle2 = 90 - angle;
  887. }
  888. else
  889. {
  890. angle1 = 90 - angle;
  891. angle2 = 270 - angle;
  892. }
  893. }
  894. else if ((i2 == 1 && i3 == 2) || (i2 == 3 && i3 == 2))
  895. {
  896. angle1 = 270 - angle;
  897. angle2 = 90 - angle;
  898. }
  899. else if ((i2 == 1 && i3 == 3) || (i2 == 3 && i3 == 3))
  900. {
  901. if (point3ToX > point2ToX)
  902. {
  903. angle1 = 90 - angle;
  904. angle2 = 270 - angle;
  905. }
  906. else
  907. {
  908. angle1 = 270 - angle;
  909. angle2 = 90 - angle;
  910. }
  911. }
  912. else if ((i2 == 1 && i3 == 4) || (i2 == 3 && i3 == 4))
  913. {
  914. angle1 = 90 - angle;
  915. angle2 = 270 - angle;
  916. }
  917. else if ((i2 == 2 && i3 == 1) || (i2 == 4 && i3 == 1))
  918. {
  919. angle1 = 270 + angle;
  920. angle2 = 90 + angle;
  921. }
  922. else if ((i2 == 2 && i3 == 2) || (i2 == 4 && i3 == 2))
  923. {
  924. if (point3ToX > point2ToX)
  925. {
  926. angle1 = 270 + angle;
  927. angle2 = 90 + angle;
  928. }
  929. else
  930. {
  931. angle1 = 90 + angle;
  932. angle2 = 270 + angle;
  933. }
  934. }
  935. else if ((i2 == 2 && i3 == 3) || (i2 == 4 && i3 == 3))
  936. {
  937. angle1 = 90 + angle;
  938. angle2 = 270 + angle;
  939. }
  940. else if ((i2 == 2 && i3 == 4) || (i2 == 4 && i3 == 4))
  941. {
  942. if (point3ToX > point2ToX)
  943. {
  944. angle1 = 90 + angle;
  945. angle2 = 270 + angle;
  946. }
  947. else
  948. {
  949. angle1 = 270 + angle;
  950. angle2 = 90 + angle;
  951. }
  952. }
  953. PointF point = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[0].X + (int)length,
  954. pointArray[0].Y), pointArray[0], angle1);
  955. PointF point1 = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[1].X - (int)length,
  956. pointArray[1].Y), pointArray[1], angle2);
  957. // 求中点坐标
  958. float x = (point.X + point1.X) / 2;
  959. float y = (point.Y + point1.Y) / 2;
  960. if (handleNumber == 3)
  961. return point;
  962. else if (handleNumber == 4)
  963. return point1;
  964. else
  965. return new PointF(x, y);
  966. }
  967. else
  968. {
  969. return pointArray[0];
  970. }
  971. }
  972. public override Cursor GetHandleCursor(int handleNumber)
  973. {
  974. return handleCursor;
  975. }
  976. public override void MoveHandleTo(Point point, int handleNumber)
  977. {
  978. if (handleNumber < 1)
  979. handleNumber = 1;
  980. if (handleNumber > pointArray.Count && handleNumber != 6)
  981. handleNumber = pointArray.Count;
  982. if (handleNumber == 6)
  983. {
  984. point = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  985. if (this.moveKb == 1)
  986. this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  987. else if (this.moveKb == 2)
  988. this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  989. else if (this.moveKb == 3)
  990. this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  991. else if (this.moveKb == 4)
  992. this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  993. else if (this.moveKb == 5)
  994. this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  995. else if (this.moveKb == 6)
  996. this.rectangleF6.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  997. else if (this.moveKb == 7)
  998. this.rectangleF7.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  999. else if (this.moveKb == 8)
  1000. this.rectangleF8.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1001. else if (this.moveKb == 9)
  1002. this.rectangleF9.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1003. this.pointL = point;
  1004. }
  1005. else
  1006. {
  1007. this.mouseUpPointChange = true;
  1008. if (handleNumber == 1)
  1009. {
  1010. this.rectangleF1.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1011. this.rectangleF2.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1012. this.rectangleF3.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1013. this.rectangleF4.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1014. this.rectangleF5.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1015. this.rectangleF6.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1016. this.rectangleF7.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1017. this.rectangleF8.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1018. this.rectangleF9.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1019. }
  1020. pointArray[handleNumber - 1] = point;
  1021. }
  1022. this.startPoint = pointArray[0];
  1023. Invalidate();
  1024. }
  1025. public override void Move(int deltaX, int deltaY)
  1026. {
  1027. int n = pointArray.Count;
  1028. PointF point;
  1029. for (int i = 0; i < n; i++)
  1030. {
  1031. point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  1032. pointArray[i] = point;
  1033. }
  1034. this.startPoint = pointArray[0];
  1035. int x = ISurfaceBox.UnscaleScalar(deltaX);
  1036. int y = ISurfaceBox.UnscaleScalar(deltaY);
  1037. pointL.X += x;
  1038. pointL.Y += y;
  1039. this.rectangleF1.Offset(x, y);
  1040. this.rectangleF2.Offset(x, y);
  1041. this.rectangleF3.Offset(x, y);
  1042. this.rectangleF4.Offset(x, y);
  1043. this.rectangleF5.Offset(x, y);
  1044. this.rectangleF6.Offset(x, y);
  1045. this.rectangleF7.Offset(x, y);
  1046. this.rectangleF8.Offset(x, y);
  1047. this.rectangleF9.Offset(x, y);
  1048. Invalidate();
  1049. }
  1050. /// <summary>
  1051. /// 用于创建一个路径或者是闭合的范围
  1052. /// 用于响应点击选中
  1053. /// 需要咨询用户是仅点击线还是矩形选择
  1054. /// </summary>
  1055. protected virtual void CreateObjects()
  1056. {
  1057. if (AreaPath != null)
  1058. return;
  1059. AreaPath = new GraphicsPath();
  1060. AreaPath.AddRectangle(GetBoundingBox());
  1061. AreaPath.CloseFigure();
  1062. AreaRegion = new Region(AreaPath);
  1063. }
  1064. /// <summary>
  1065. /// Invalidate object.
  1066. /// When object is invalidated, path used for hit test
  1067. /// is released and should be created again.
  1068. /// </summary>
  1069. protected void Invalidate()
  1070. {
  1071. if (AreaPath != null)
  1072. {
  1073. AreaPath.Dispose();
  1074. AreaPath = null;
  1075. }
  1076. if (AreaPen != null)
  1077. {
  1078. AreaPen.Dispose();
  1079. AreaPen = null;
  1080. }
  1081. if (AreaRegion != null)
  1082. {
  1083. AreaRegion.Dispose();
  1084. AreaRegion = null;
  1085. }
  1086. }
  1087. protected GraphicsPath AreaPath
  1088. {
  1089. get
  1090. {
  1091. return areaPath;
  1092. }
  1093. set
  1094. {
  1095. areaPath = value;
  1096. }
  1097. }
  1098. protected Pen AreaPen
  1099. {
  1100. get
  1101. {
  1102. return areaPen;
  1103. }
  1104. set
  1105. {
  1106. areaPen = value;
  1107. }
  1108. }
  1109. protected Region AreaRegion
  1110. {
  1111. get
  1112. {
  1113. return areaRegion;
  1114. }
  1115. set
  1116. {
  1117. areaRegion = value;
  1118. }
  1119. }
  1120. /// <summary>
  1121. /// Draw tracker for selected object
  1122. /// </summary>
  1123. /// <param name="g"></param>
  1124. public override void DrawTracker(Graphics g)
  1125. {
  1126. if (!Selected)
  1127. return;
  1128. SolidBrush brush = new SolidBrush(Color.Black);
  1129. for (int i = 1; i <= HandleCount; i++)
  1130. {
  1131. if (i == 6)
  1132. brush = new SolidBrush(Color.Transparent);
  1133. g.FillRectangle(brush, GetHandleRectangle(i));
  1134. }
  1135. brush.Dispose();
  1136. RectangleF r = GetBoundingBox();
  1137. //g.DrawRectangle(new Pen(Color.White), r.X, r.Y, r.Width, r.Height);
  1138. }
  1139. /// <summary>
  1140. /// Hit test.
  1141. /// Return value: -1 - no hit
  1142. /// 0 - hit anywhere
  1143. /// > 1 - handle number
  1144. /// </summary>
  1145. /// <param name="pointscroll"></param>
  1146. /// <returns></returns>
  1147. public override int HitTest(Point point)
  1148. {
  1149. if (Selected)
  1150. {
  1151. for (int i = 1; i <= HandleCount; i++)
  1152. {
  1153. if (GetHandleRectangle(i).Contains(point))
  1154. return i;
  1155. }
  1156. if (GetRectanglePosition(this.rectangleF1, point, this.rotationPoint, this.angleClass))
  1157. {
  1158. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1159. moveKb = 1;
  1160. return 6;
  1161. }
  1162. else if (GetRectanglePosition(this.rectangleF2, point, this.rotationPoint, this.angleClass))
  1163. {
  1164. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1165. moveKb = 2;
  1166. return 6;
  1167. }
  1168. else if (GetRectanglePosition(this.rectangleF3, point, this.rotationPoint, this.angleClass))
  1169. {
  1170. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1171. moveKb = 3;
  1172. return 6;
  1173. }
  1174. else if (GetRectanglePosition(this.rectangleF4, point, this.rotationPoint, this.angleClass))
  1175. {
  1176. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1177. moveKb = 4;
  1178. return 6;
  1179. }
  1180. else if (GetRectanglePosition(this.rectangleF5, point, this.rotationPoint, this.angleClass))
  1181. {
  1182. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1183. moveKb = 5;
  1184. return 6;
  1185. }
  1186. else if (GetRectanglePosition(this.rectangleF6, point, this.rotationPoint, this.angleClass))
  1187. {
  1188. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1189. moveKb = 6;
  1190. return 6;
  1191. }
  1192. else if (GetRectanglePosition(this.rectangleF7, point, this.rotationPoint, this.angleClass))
  1193. {
  1194. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1195. moveKb = 7;
  1196. return 6;
  1197. }
  1198. else if (GetRectanglePosition(this.rectangleF8, point, this.rotationPoint, this.angleClass))
  1199. {
  1200. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1201. moveKb = 8;
  1202. return 6;
  1203. }
  1204. else if (GetRectanglePosition(this.rectangleF9, point, this.rotationPoint, this.angleClass))
  1205. {
  1206. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1207. moveKb = 9;
  1208. return 6;
  1209. }
  1210. }
  1211. if (PointInObject(point))
  1212. return 0;
  1213. return -1;
  1214. }
  1215. protected override bool PointInObject(Point point)
  1216. {
  1217. CreateObjects();
  1218. return AreaRegion.IsVisible(point);
  1219. }
  1220. public override bool IntersectsWith(Rectangle rectangle)
  1221. {
  1222. CreateObjects();
  1223. return AreaRegion.IsVisible(rectangle);
  1224. }
  1225. public override RectangleF GetBoundingBox()
  1226. {
  1227. Rectangle rectangle;
  1228. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  1229. for (int i = 0; i < pointArray.Count; i++)
  1230. {
  1231. if (i == 0)
  1232. {
  1233. minx = maxx = (int)pointArray[i].X;
  1234. miny = maxy = (int)pointArray[i].Y;
  1235. }
  1236. else
  1237. {
  1238. if (pointArray[i].X > maxx) maxx = (int)pointArray[i].X;
  1239. if (pointArray[i].X < minx) minx = (int)pointArray[i].X;
  1240. if (pointArray[i].Y > maxy) maxy = (int)pointArray[i].Y;
  1241. if (pointArray[i].Y < miny) miny = (int)pointArray[i].Y;
  1242. }
  1243. }
  1244. rectangle = new Rectangle(minx, miny, maxx - minx, maxy - miny);
  1245. return rectangle;
  1246. }
  1247. internal void setNextPoint(Point p)
  1248. {
  1249. AddPoint(p);
  1250. startPoint = endPoint;
  1251. endPoint = p;
  1252. }
  1253. internal void setEndPoint(Point p)
  1254. {
  1255. endPoint = p;
  1256. }
  1257. public override List<PointF> GetPoints()
  1258. {
  1259. return pointArray;
  1260. }
  1261. public override ParentStyleModel GetStyle()
  1262. {
  1263. return measureStyleModel;
  1264. }
  1265. }
  1266. }