MeasureRandRectangle.cs 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. using PaintDotNet.Base.SettingModel;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. using PaintDotNet.Annotation.Enum;
  7. using System.Collections.Generic;
  8. using PaintDotNet.Base.CommTool;
  9. using System.Linq;
  10. namespace PaintDotNet.Annotation.Measure
  11. {
  12. /// <summary>
  13. /// 测量->多边形测量->任意矩形
  14. /// </summary>
  15. public class MeasureRandRectangle : MeasureDrawObject
  16. {
  17. /// <summary>
  18. /// 标注样式信息model
  19. /// </summary>
  20. public MeasureStyleModel.MeasureRandRectangle measureRandRectangle;
  21. /// <summary>
  22. /// 坐标点集合
  23. /// </summary>
  24. public List<PointF> pointArray;
  25. /// <summary>
  26. /// 第三个点
  27. /// </summary>
  28. PointF linePoint;
  29. /// <summary>
  30. /// 第四个点
  31. /// </summary>
  32. PointF linePoint1;
  33. /// <summary>
  34. /// 中心点
  35. /// </summary>
  36. PointF centerPoint;
  37. /// <summary>
  38. /// 旋转时的最后一个点记录,用于计算角度
  39. /// </summary>
  40. PointF lastPoint;
  41. /// <summary>
  42. /// 旋转时的标记
  43. /// </summary>
  44. public bool isPress = false;
  45. /// <summary>
  46. /// 初始绘制时第一个点和第二个点的角度
  47. /// </summary>
  48. public double angleF = 0;
  49. /// <summary>
  50. /// 初始绘制的标记
  51. /// </summary>
  52. public bool first = false;
  53. /// <summary>
  54. /// 绘制完成的标记
  55. /// </summary>
  56. public bool finish = false;
  57. /// <summary>
  58. /// 第一个点和第二个点的组成的直线的斜率
  59. /// </summary>
  60. public double k1;
  61. /// <summary>
  62. /// 重新计算过的外接矩形
  63. /// </summary>
  64. RectangleF largerRect;
  65. /// <summary>
  66. /// 周长
  67. /// </summary>
  68. //private double length = 0.0;
  69. /// <summary>
  70. /// 面积
  71. /// </summary>
  72. private double area = 0.0;
  73. /// <summary>
  74. /// 测量信息矩形定义
  75. /// </summary>
  76. private RectangleF rectangleF1 = new RectangleF();
  77. private RectangleF rectangleF2 = new RectangleF();
  78. private RectangleF rectangleF3 = new RectangleF();
  79. private RectangleF rectangleF4 = new RectangleF();
  80. private RectangleF rectangleF5 = new RectangleF();
  81. private RectangleF rectangleF6 = new RectangleF();
  82. private RectangleF rectangleF7 = new RectangleF();
  83. private RectangleF rectangleF8 = new RectangleF();
  84. private RectangleF rectangleF9 = new RectangleF();
  85. private RectangleF rectangleF10 = new RectangleF();
  86. private RectangleF rectangleF11 = new RectangleF();
  87. /// <summary>
  88. /// 文本上用于拖动的点
  89. /// </summary>
  90. private Point pointL = new Point();
  91. /// <summary>
  92. /// 绘制限制(第一次绘制时)
  93. /// </summary>
  94. public bool pointChange = true;
  95. /// <summary>
  96. /// 区分移动文本
  97. /// </summary>
  98. private int moveKb;
  99. /// <summary>
  100. /// 绘制限制(从配置文件加载)
  101. /// </summary>
  102. public bool configurationFile = false;
  103. /// <summary>
  104. /// 绘制限制(更改属性时)
  105. /// </summary>
  106. private bool SavePointChange;
  107. /// <summary>
  108. /// 绘制属性
  109. /// </summary>
  110. private string[] drawingPropertiesList;
  111. /// <summary>
  112. /// 绘制属性(克隆)
  113. /// </summary>
  114. private string[] drawingPropertiesListClone;
  115. private bool drawing;
  116. /// <summary>
  117. /// 旋转角度
  118. /// </summary>
  119. private double angle;
  120. /// <summary>
  121. /// 限制绘制(绘制中)
  122. /// </summary>
  123. public bool mouseUpPointChange = false;
  124. /// <summary>
  125. /// 绘制点
  126. /// </summary>
  127. private PointF PointF = new PointF();
  128. public MeasureRandRectangle(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2, bool clone) : base()
  129. {
  130. this.objectType = DrawClass.Measure;
  131. this.drawToolType = DrawToolType.MeasureRandRectangle;
  132. this.pointArray = new List<PointF>();
  133. this.measureRandRectangle = surfaceBox.GetMeasureStyleModel().measureRandRectangle;
  134. this.startPoint.X = x1;
  135. this.startPoint.Y = y1;
  136. this.endPoint.X = x2;
  137. this.endPoint.Y = y2;
  138. if (clone)
  139. {
  140. drawing = true;
  141. this.pointArray.Add(this.startPoint);
  142. this.pointArray.Add(this.endPoint);
  143. }
  144. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  145. this.unitString = surfaceBox.GetPxPerUnit()[1];
  146. this.unit = surfaceBox.GetPxPerUnit()[2];
  147. }
  148. /// <summary>
  149. /// 测量属性
  150. /// </summary>
  151. /// <returns></returns>
  152. public override Dictionary<System.Enum, object> GetData()
  153. {
  154. if (data.ContainsKey(MeasureAttributes.MeasureMethod))
  155. data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text");
  156. else
  157. data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"));
  158. if (data.ContainsKey(MeasureAttributes.MeasureUnitCN))
  159. data[MeasureAttributes.MeasureUnitCN] = this.unitString;
  160. else
  161. data.Add(MeasureAttributes.MeasureUnitCN, this.unitString);
  162. if (data.ContainsKey(MeasureAttributes.MeasureUnitEN))
  163. data[MeasureAttributes.MeasureUnitEN] = this.unit;
  164. else
  165. data.Add(MeasureAttributes.MeasureUnitEN, this.unit);
  166. if (data.ContainsKey(MeasureAttributes.PixelStartX))
  167. data[MeasureAttributes.PixelStartX] = startPoint.X;
  168. else
  169. data.Add(MeasureAttributes.PixelStartX, startPoint.X);
  170. if (data.ContainsKey(MeasureAttributes.PixelStartY))
  171. data[MeasureAttributes.PixelStartY] = startPoint.Y;
  172. else
  173. data.Add(MeasureAttributes.PixelStartY, startPoint.Y);
  174. if (data.ContainsKey(MeasureAttributes.PhysicalStartX))
  175. data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces);
  176. else
  177. data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces));
  178. if (data.ContainsKey(MeasureAttributes.PhysicalStartY))
  179. data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces);
  180. else
  181. data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces));
  182. if (data.ContainsKey(MeasureAttributes.PixelArea))
  183. data[MeasureAttributes.PixelArea] = Math.Round(area, decimalPlaces);
  184. else
  185. data.Add(MeasureAttributes.PixelArea, Math.Round(area, decimalPlaces));
  186. string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
  187. if (s.IndexOf(".") == -1)
  188. {
  189. for (int i = 0; i < decimalPlaces; i++)
  190. {
  191. if (i == 0)
  192. s += ".";
  193. s += "0";
  194. }
  195. }
  196. else
  197. {
  198. int a = s.Length - s.IndexOf(".") - 1;
  199. if (a < decimalPlaces)
  200. {
  201. for (int i = 0; i < decimalPlaces - a; i++)
  202. {
  203. s += "0";
  204. }
  205. }
  206. }
  207. if (data.ContainsKey(MeasureAttributes.PhysicalArea))
  208. data[MeasureAttributes.PhysicalArea] = s;
  209. else
  210. data.Add(MeasureAttributes.PhysicalArea, s);
  211. if (data.ContainsKey(MeasureAttributes.PixelCircumference))
  212. data[MeasureAttributes.PixelCircumference] = Math.Round(length, decimalPlaces);
  213. else
  214. data.Add(MeasureAttributes.PixelCircumference, Math.Round(length, decimalPlaces));
  215. s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
  216. if (s.IndexOf(".") == -1)
  217. {
  218. for (int i = 0; i < decimalPlaces; i++)
  219. {
  220. if (i == 0)
  221. s += ".";
  222. s += "0";
  223. }
  224. }
  225. else
  226. {
  227. int a = s.Length - s.IndexOf(".") - 1;
  228. if (a < decimalPlaces)
  229. {
  230. for (int i = 0; i < decimalPlaces - a; i++)
  231. {
  232. s += "0";
  233. }
  234. }
  235. }
  236. if (data.ContainsKey(MeasureAttributes.PhysicalCircumference))
  237. data[MeasureAttributes.PhysicalCircumference] = s;
  238. else
  239. data.Add(MeasureAttributes.PhysicalCircumference, s);
  240. return data;
  241. }
  242. public MeasureRandRectangle(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  243. {
  244. this.objectType = DrawClass.Measure;
  245. this.drawToolType = DrawToolType.MeasureRandRectangle;
  246. this.ISurfaceBox = surfaceBox;
  247. measureRandRectangle = (MeasureStyleModel.MeasureRandRectangle)parentStyleModel;
  248. pointArray = DrawRulerHelper.DeepCopyListByReflect(points);
  249. startPoint.X = points[0].X;
  250. startPoint.Y = points[0].Y;
  251. endPoint.X = points[1].X;
  252. endPoint.Y = points[1].Y;
  253. linePoint.X = points[2].X;
  254. linePoint.Y = points[2].Y;
  255. linePoint1.X = points[3].X;
  256. linePoint1.Y = points[3].Y;
  257. finish = true;
  258. CalcSlope();
  259. CalcOtherRectangle();
  260. CalcCenter();
  261. largerRect = GetBoundingBox();
  262. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  263. this.unitString = surfaceBox.GetPxPerUnit()[1];
  264. this.unit = surfaceBox.GetPxPerUnit()[2];
  265. this.configurationFile = true;
  266. }
  267. /// <summary>
  268. /// Clone this instance
  269. /// </summary>
  270. public override DrawObject Clone()
  271. {
  272. MeasureRandRectangle measureRandRectangle = new MeasureRandRectangle(ISurfaceBox, 0, 0, 0, 1, false);
  273. measureRandRectangle.objectType = DrawClass.Measure;
  274. measureRandRectangle.drawToolType = DrawToolType.MeasureRandRectangle;
  275. measureRandRectangle.ISurfaceBox = this.ISurfaceBox;
  276. foreach (PointF p in this.pointArray)
  277. {
  278. measureRandRectangle.pointArray.Add(p);
  279. }
  280. measureRandRectangle.startPoint = this.startPoint;
  281. measureRandRectangle.endPoint = this.endPoint;
  282. measureRandRectangle.linePoint = this.linePoint;
  283. measureRandRectangle.linePoint1 = this.linePoint1;
  284. measureRandRectangle.finish = true;
  285. measureRandRectangle.first = true;
  286. if (drawingPropertiesList != null)
  287. measureRandRectangle.drawingPropertiesListClone = drawingPropertiesList;
  288. FillDrawObjectFields(measureRandRectangle);
  289. return measureRandRectangle;
  290. }
  291. public override DrawObject Clone(ISurfaceBox surfaceBox)
  292. {
  293. MeasureRandRectangle measureRandRectangle = new MeasureRandRectangle(surfaceBox, this.GetPoints(), this.measureRandRectangle, null);
  294. measureRandRectangle.finish = true;
  295. measureRandRectangle.first = true;
  296. if (drawingPropertiesList != null)
  297. measureRandRectangle.drawingPropertiesListClone = drawingPropertiesList;
  298. FillDrawObjectFields(measureRandRectangle);
  299. return measureRandRectangle;
  300. }
  301. public override void Draw(Graphics g)
  302. {
  303. this.measureRandRectangle = this.ISurfaceBox.GetMeasureStyleModel().measureRandRectangle;
  304. drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList);
  305. if (drawingPropertiesList == null)
  306. {
  307. drawingPropertiesList = drawingPropertiesListClone;
  308. if (!this.drawing)
  309. this.configurationFile = true;
  310. }
  311. pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange);
  312. g.SmoothingMode = SmoothingMode.AntiAlias;
  313. Font textfont = new Font(this.measureRandRectangle.font, this.measureRandRectangle.fontSize);
  314. Brush textbrush = new SolidBrush(Color.FromArgb(this.measureRandRectangle.textColor));
  315. Color color = Color.FromArgb(this.measureRandRectangle.lineColor);
  316. Pen pen = new Pen(color, this.measureRandRectangle.lineWidth);
  317. //Pen pen = new Pen(color, 1);
  318. pen.DashStyle = (DashStyle)this.measureRandRectangle.lineStyle;
  319. if (this.Selected)
  320. {
  321. if (measureRandRectangle.chooseStyle != null)
  322. {
  323. textfont = new Font(measureRandRectangle.chooseStyle.font, measureRandRectangle.chooseStyle.fontSize);
  324. textbrush = new SolidBrush(Color.FromArgb(measureRandRectangle.chooseStyle.textColor));
  325. pen = new Pen(Color.FromArgb(measureRandRectangle.chooseStyle.lineColor), measureRandRectangle.chooseStyle.lineWidth);
  326. pen.DashStyle = (DashStyle)measureRandRectangle.chooseStyle.lineStyle;
  327. }
  328. }
  329. if (!this.Selected)
  330. {
  331. this.pointChange = true;
  332. rectangleF1 = new RectangleF();
  333. rectangleF2 = new RectangleF();
  334. rectangleF3 = new RectangleF();
  335. rectangleF4 = new RectangleF();
  336. rectangleF5 = new RectangleF();
  337. rectangleF6 = new RectangleF();
  338. rectangleF7 = new RectangleF();
  339. rectangleF8 = new RectangleF();
  340. rectangleF9 = new RectangleF();
  341. rectangleF10 = new RectangleF();
  342. rectangleF11 = new RectangleF();
  343. }
  344. Matrix mtxSave = g.Transform;
  345. Matrix matrix = g.Transform;
  346. string frontStr = string.Empty;
  347. if (this.measureRandRectangle.showSuffix)
  348. {
  349. frontStr = this.measureRandRectangle.suffixName;
  350. }
  351. if (this.measureRandRectangle.showSerial)
  352. {
  353. #region [获取序号]
  354. List<DrawObject> newGraphicsList = new List<DrawObject>();
  355. int n = ISurfaceBox.GraphicsList.Count;
  356. int num = 0;
  357. for (int i = n - 1; i >= 0; i--)
  358. {
  359. newGraphicsList.Add(ISurfaceBox.GraphicsList[i].Clone());
  360. }
  361. var newGraphics = newGraphicsList.Where(m => m.ID.Equals(this.ID)).FirstOrDefault();
  362. if (newGraphics != null)
  363. {
  364. num = newGraphicsList.IndexOf(newGraphics) + 1;
  365. }
  366. #endregion
  367. frontStr = $"[{ num }]{frontStr}";
  368. }
  369. if (this.measureRandRectangle.showAlias)
  370. {
  371. frontStr = this.measureRandRectangle.aliasName + frontStr;
  372. }
  373. if (pointArray.Count == 2)
  374. {
  375. g.DrawLine(pen, pointArray[0].X, pointArray[0].Y, pointArray[1].X, pointArray[1].Y);
  376. }
  377. else if (pointArray.Count >= 4)
  378. {
  379. SizeF sizeF = new SizeF();
  380. g.DrawLine(pen, pointArray[0].X, pointArray[0].Y, pointArray[1].X, pointArray[1].Y);
  381. g.DrawLine(pen, pointArray[1].X, pointArray[1].Y, pointArray[2].X, pointArray[2].Y);
  382. g.DrawLine(pen, pointArray[0].X, pointArray[0].Y, pointArray[3].X, pointArray[3].Y);
  383. g.DrawLine(pen, pointArray[2].X, pointArray[2].Y, pointArray[3].X, pointArray[3].Y);
  384. // 计算矩形长和宽
  385. double h = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[1], 10);
  386. double w = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[3], 10);
  387. length = (h + w) * 2;
  388. area = h * w;
  389. if (pointArray[0].X < pointArray[1].X && pointArray[0].X < pointArray[2].X && pointArray[0].X < pointArray[3].X)
  390. {
  391. this.PointF = pointArray[0];
  392. if (pointArray[1].Y > pointArray[3].Y)
  393. {
  394. //计算需要旋转的角度
  395. angle = BasicCalculationHelper.Angle(pointArray[0], pointArray[3], new PointF(pointArray[0].X, pointArray[3].Y));
  396. // 画布旋转
  397. if (angle < 90)
  398. {
  399. matrix.RotateAt((float)angle, new PointF(pointArray[0].X, pointArray[0].Y));
  400. g.Transform = matrix;
  401. }
  402. else
  403. {
  404. angle = BasicCalculationHelper.Angle(pointArray[3], pointArray[0], new PointF(pointArray[3].X, pointArray[0].Y));
  405. matrix.RotateAt((float)angle, new PointF(pointArray[0].X, pointArray[0].Y));
  406. g.Transform = matrix;
  407. }
  408. }
  409. else
  410. {
  411. //计算需要旋转的角度
  412. angle = BasicCalculationHelper.Angle(pointArray[0], pointArray[1], new PointF(pointArray[0].X, pointArray[1].Y));
  413. // 画布旋转
  414. if (angle < 90)
  415. {
  416. matrix.RotateAt((float)angle, new PointF(pointArray[0].X, pointArray[0].Y));
  417. g.Transform = matrix;
  418. }
  419. else
  420. {
  421. angle = BasicCalculationHelper.Angle(pointArray[1], pointArray[0], new PointF(pointArray[1].X, pointArray[0].Y));
  422. matrix.RotateAt((float)angle, new PointF(pointArray[0].X, pointArray[0].Y));
  423. g.Transform = matrix;
  424. }
  425. }
  426. }
  427. else if (pointArray[1].X < pointArray[0].X && pointArray[1].X < pointArray[2].X && pointArray[1].X < pointArray[3].X)
  428. {
  429. this.PointF = pointArray[1];
  430. if(pointArray[0].Y > pointArray[2].Y)
  431. {
  432. //计算需要旋转的角度
  433. angle = BasicCalculationHelper.Angle(pointArray[1], pointArray[2], new PointF(pointArray[1].X, pointArray[2].Y));
  434. // 画布旋转
  435. if (angle < 90)
  436. {
  437. matrix.RotateAt((float)angle, new PointF(pointArray[1].X, pointArray[1].Y));
  438. g.Transform = matrix;
  439. }
  440. else
  441. {
  442. angle = BasicCalculationHelper.Angle(pointArray[2], pointArray[1], new PointF(pointArray[2].X, pointArray[1].Y));
  443. matrix.RotateAt((float)angle, new PointF(pointArray[1].X, pointArray[1].Y));
  444. g.Transform = matrix;
  445. }
  446. }
  447. else
  448. {
  449. //计算需要旋转的角度
  450. angle = BasicCalculationHelper.Angle(pointArray[1], pointArray[0], new PointF(pointArray[1].X, pointArray[0].Y));
  451. // 画布旋转
  452. if (angle < 90)
  453. {
  454. matrix.RotateAt((float)angle, new PointF(pointArray[1].X, pointArray[1].Y));
  455. g.Transform = matrix;
  456. }
  457. else
  458. {
  459. angle = BasicCalculationHelper.Angle(pointArray[0], pointArray[1], new PointF(pointArray[0].X, pointArray[1].Y));
  460. matrix.RotateAt((float)angle, new PointF(pointArray[1].X, pointArray[1].Y));
  461. g.Transform = matrix;
  462. }
  463. }
  464. }
  465. else if (pointArray[2].X < pointArray[0].X && pointArray[2].X < pointArray[1].X && pointArray[2].X < pointArray[3].X)
  466. {
  467. this.PointF = pointArray[2];
  468. if(pointArray[1].Y > pointArray[3].Y)
  469. {
  470. //计算需要旋转的角度
  471. angle = BasicCalculationHelper.Angle(pointArray[2], pointArray[3], new PointF(pointArray[2].X, pointArray[3].Y));
  472. // 画布旋转
  473. if (angle < 90)
  474. {
  475. matrix.RotateAt((float)angle, new PointF(pointArray[2].X, pointArray[2].Y));
  476. g.Transform = matrix;
  477. }
  478. else
  479. {
  480. angle = BasicCalculationHelper.Angle(pointArray[3], pointArray[2], new PointF(pointArray[3].X, pointArray[2].Y));
  481. matrix.RotateAt((float)angle, new PointF(pointArray[2].X, pointArray[2].Y));
  482. g.Transform = matrix;
  483. }
  484. }
  485. else
  486. {
  487. //计算需要旋转的角度
  488. angle = BasicCalculationHelper.Angle(pointArray[2], pointArray[1], new PointF(pointArray[2].X, pointArray[1].Y));
  489. // 画布旋转
  490. if (angle < 90)
  491. {
  492. matrix.RotateAt((float)angle, new PointF(pointArray[2].X, pointArray[2].Y));
  493. g.Transform = matrix;
  494. }
  495. else
  496. {
  497. angle = BasicCalculationHelper.Angle(pointArray[1], pointArray[2], new PointF(pointArray[1].X, pointArray[2].Y));
  498. matrix.RotateAt((float)angle, new PointF(pointArray[2].X, pointArray[2].Y));
  499. g.Transform = matrix;
  500. }
  501. }
  502. }
  503. else if (pointArray[3].X < pointArray[0].X && pointArray[3].X < pointArray[1].X && pointArray[3].X < pointArray[2].X)
  504. {
  505. this.PointF = pointArray[3];
  506. if(pointArray[0].Y > pointArray[2].Y)
  507. {
  508. //计算需要旋转的角度
  509. angle = BasicCalculationHelper.Angle(pointArray[3], pointArray[2], new PointF(pointArray[3].X, pointArray[2].Y));
  510. // 画布旋转
  511. if (angle < 90)
  512. {
  513. matrix.RotateAt((float)angle, new PointF(pointArray[3].X, pointArray[3].Y));
  514. g.Transform = matrix;
  515. }
  516. else
  517. {
  518. angle = BasicCalculationHelper.Angle(pointArray[2], pointArray[3], new PointF(pointArray[2].X, pointArray[3].Y));
  519. matrix.RotateAt((float)angle, new PointF(pointArray[3].X, pointArray[3].Y));
  520. g.Transform = matrix;
  521. }
  522. }
  523. else
  524. {
  525. //计算需要旋转的角度
  526. angle = BasicCalculationHelper.Angle(pointArray[3], pointArray[0], new PointF(pointArray[3].X, pointArray[0].Y));
  527. // 画布旋转
  528. if (angle < 90)
  529. {
  530. matrix.RotateAt((float)angle, new PointF(pointArray[3].X, pointArray[3].Y));
  531. g.Transform = matrix;
  532. }
  533. else
  534. {
  535. angle = BasicCalculationHelper.Angle(pointArray[0], pointArray[3], new PointF(pointArray[0].X, pointArray[3].Y));
  536. matrix.RotateAt((float)angle, new PointF(pointArray[3].X, pointArray[3].Y));
  537. g.Transform = matrix;
  538. }
  539. }
  540. }
  541. // 是否绘制
  542. if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute)
  543. {
  544. // 属性文本的间隔定义
  545. int offsetValue = measureRandRectangle.fontSize + measureRandRectangle.fontSize / 2;
  546. int offsetValue1 = measureRandRectangle.lineWidth * 2 / 3;
  547. int offset = offsetValue1;
  548. this.pointL = new Point((int)pointArray[0].X, (int)pointArray[0].Y);
  549. if (drawingPropertiesList != null)
  550. {
  551. // 像素周长
  552. if (drawingPropertiesList.Contains(MeasureAttributes.PixelCircumference.ToString()))
  553. {
  554. sizeF = g.MeasureString("" + Math.Round(this.length, decimalPlaces) + "px", textfont);
  555. rectangleF10.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  556. offset += offsetValue;
  557. }
  558. // 物理周长
  559. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalCircumference.ToString()))
  560. {
  561. string s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
  562. if (s.IndexOf(".") == -1)
  563. {
  564. for (int i = 0; i < decimalPlaces; i++)
  565. {
  566. if (i == 0)
  567. s += ".";
  568. s += "0";
  569. }
  570. }
  571. else
  572. {
  573. int a = s.Length - s.IndexOf(".") - 1;
  574. if (a < decimalPlaces)
  575. {
  576. for (int i = 0; i < decimalPlaces - a; i++)
  577. {
  578. s += "0";
  579. }
  580. }
  581. }
  582. sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
  583. rectangleF11.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  584. offset += offsetValue;
  585. }
  586. // 像素面积
  587. if (drawingPropertiesList.Contains(MeasureAttributes.PixelArea.ToString()))
  588. {
  589. sizeF = g.MeasureString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont);
  590. rectangleF8.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  591. offset += offsetValue;
  592. }
  593. // 物理面积
  594. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalArea.ToString()))
  595. {
  596. string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
  597. if (s.IndexOf(".") == -1)
  598. {
  599. for (int i = 0; i < decimalPlaces; i++)
  600. {
  601. if (i == 0)
  602. s += ".";
  603. s += "0";
  604. }
  605. }
  606. else
  607. {
  608. int a = s.Length - s.IndexOf(".") - 1;
  609. if (a < decimalPlaces)
  610. {
  611. for (int i = 0; i < decimalPlaces - a; i++)
  612. {
  613. s += "0";
  614. }
  615. }
  616. }
  617. sizeF = g.MeasureString(frontStr + s + this.unit + "²", textfont);
  618. rectangleF9.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  619. offset += offsetValue;
  620. }
  621. // 测量方式
  622. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  623. {
  624. sizeF = g.MeasureString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont);
  625. rectangleF1.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  626. offset += offsetValue;
  627. }
  628. // 测量单位(中文)
  629. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  630. {
  631. sizeF = g.MeasureString(frontStr + "" + this.unitString, textfont);
  632. rectangleF2.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  633. offset += offsetValue;
  634. }
  635. // 测量单位(英文)
  636. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  637. {
  638. sizeF = g.MeasureString(frontStr + "" + this.unit, textfont);
  639. rectangleF3.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  640. offset += offsetValue;
  641. }
  642. // 像素起始点X
  643. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  644. {
  645. sizeF = g.MeasureString(frontStr + "" + startPoint.X, textfont);
  646. rectangleF4.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  647. offset += offsetValue;
  648. }
  649. // 像素起始点Y
  650. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  651. {
  652. sizeF = g.MeasureString(frontStr + "" + startPoint.Y, textfont);
  653. rectangleF5.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  654. offset += offsetValue;
  655. }
  656. // 物理起始点X
  657. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  658. {
  659. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  660. rectangleF6.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  661. offset += offsetValue;
  662. }
  663. // 物理起始点Y
  664. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  665. {
  666. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  667. rectangleF7.Location = new Point((int) this.PointF .X + offsetValue1 - (int)sizeF.Width / 2, (int) this.PointF .Y + offset);
  668. offset += offsetValue;
  669. }
  670. }
  671. }
  672. if (drawingPropertiesList != null)
  673. {
  674. // 测量方式
  675. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  676. {
  677. sizeF = g.MeasureString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont);
  678. rectangleF1.Width = sizeF.Width;
  679. rectangleF1.Height = sizeF.Height;
  680. g.DrawString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont, textbrush, rectangleF1);
  681. }
  682. else
  683. {
  684. rectangleF1 = new RectangleF();
  685. }
  686. // 测量单位(中文)
  687. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  688. {
  689. sizeF = g.MeasureString(frontStr + "" + this.unitString, textfont);
  690. rectangleF2.Width = sizeF.Width;
  691. rectangleF2.Height = sizeF.Height;
  692. g.DrawString(frontStr + "" + this.unitString, textfont, textbrush, rectangleF2);
  693. }
  694. else
  695. {
  696. rectangleF2 = new RectangleF();
  697. }
  698. // 测量单位(英文)
  699. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  700. {
  701. sizeF = g.MeasureString(frontStr + "" + this.unit, textfont);
  702. rectangleF3.Width = sizeF.Width;
  703. rectangleF3.Height = sizeF.Height;
  704. g.DrawString(frontStr + "" + this.unit, textfont, textbrush, rectangleF3);
  705. }
  706. else
  707. {
  708. rectangleF3 = new RectangleF();
  709. }
  710. // 像素起始点X
  711. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  712. {
  713. sizeF = g.MeasureString(frontStr + "" + startPoint.X, textfont);
  714. rectangleF4.Width = sizeF.Width;
  715. rectangleF4.Height = sizeF.Height;
  716. g.DrawString(frontStr + "" + startPoint.X, textfont, textbrush, rectangleF4);
  717. }
  718. else
  719. {
  720. rectangleF4 = new RectangleF();
  721. }
  722. // 像素起始点Y
  723. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  724. {
  725. sizeF = g.MeasureString(frontStr + "" + startPoint.Y, textfont);
  726. rectangleF5.Width = sizeF.Width;
  727. rectangleF5.Height = sizeF.Height;
  728. g.DrawString(frontStr + "" + startPoint.Y, textfont, textbrush, rectangleF5);
  729. }
  730. else
  731. {
  732. rectangleF5 = new RectangleF();
  733. }
  734. // 物理起始点X
  735. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  736. {
  737. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  738. rectangleF6.Width = sizeF.Width;
  739. rectangleF6.Height = sizeF.Height;
  740. g.DrawString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont, textbrush, rectangleF6);
  741. }
  742. else
  743. {
  744. rectangleF6 = new RectangleF();
  745. }
  746. // 物理起始点Y
  747. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  748. {
  749. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  750. rectangleF7.Width = sizeF.Width;
  751. rectangleF7.Height = sizeF.Height;
  752. g.DrawString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont, textbrush, rectangleF7);
  753. }
  754. else
  755. {
  756. rectangleF7 = new RectangleF();
  757. }
  758. // 像素面积
  759. if (drawingPropertiesList.Contains(MeasureAttributes.PixelArea.ToString()))
  760. {
  761. sizeF = g.MeasureString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont);
  762. rectangleF8.Width = sizeF.Width;
  763. rectangleF8.Height = sizeF.Height;
  764. g.DrawString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont, textbrush, rectangleF8);
  765. }
  766. else
  767. {
  768. rectangleF8 = new RectangleF();
  769. }
  770. // 物理面积
  771. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalArea.ToString()))
  772. {
  773. string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
  774. if (s.IndexOf(".") == -1)
  775. {
  776. for (int i = 0; i < decimalPlaces; i++)
  777. {
  778. if (i == 0)
  779. s += ".";
  780. s += "0";
  781. }
  782. }
  783. else
  784. {
  785. int a = s.Length - s.IndexOf(".") - 1;
  786. if (a < decimalPlaces)
  787. {
  788. for (int i = 0; i < decimalPlaces - a; i++)
  789. {
  790. s += "0";
  791. }
  792. }
  793. }
  794. sizeF = g.MeasureString(frontStr + s + this.unit + "²", textfont);
  795. rectangleF9.Width = sizeF.Width;
  796. rectangleF9.Height = sizeF.Height;
  797. g.DrawString(frontStr + s + this.unit + "²", textfont, textbrush, rectangleF9);
  798. }
  799. else
  800. {
  801. rectangleF9 = new RectangleF();
  802. }
  803. // 像素周长
  804. if (drawingPropertiesList.Contains(MeasureAttributes.PixelCircumference.ToString()))
  805. {
  806. sizeF = g.MeasureString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont);
  807. rectangleF10.Width = sizeF.Width;
  808. rectangleF10.Height = sizeF.Height;
  809. g.DrawString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont, textbrush, rectangleF10);
  810. }
  811. else
  812. {
  813. rectangleF10 = new RectangleF();
  814. }
  815. // 物理周长
  816. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalCircumference.ToString()))
  817. {
  818. string s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
  819. if (s.IndexOf(".") == -1)
  820. {
  821. for (int i = 0; i < decimalPlaces; i++)
  822. {
  823. if (i == 0)
  824. s += ".";
  825. s += "0";
  826. }
  827. }
  828. else
  829. {
  830. int a = s.Length - s.IndexOf(".") - 1;
  831. if (a < decimalPlaces)
  832. {
  833. for (int i = 0; i < decimalPlaces - a; i++)
  834. {
  835. s += "0";
  836. }
  837. }
  838. }
  839. sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
  840. rectangleF11.Width = sizeF.Width;
  841. rectangleF11.Height = sizeF.Height;
  842. g.DrawString(frontStr + s + this.unit, textfont, textbrush, rectangleF11);
  843. }
  844. else
  845. {
  846. rectangleF11 = new RectangleF();
  847. }
  848. }
  849. //还原为原始旋转矩阵
  850. g.Transform = mtxSave;
  851. }
  852. matrix.Dispose();
  853. if (this.configurationFile)
  854. this.pointChange = false;
  855. this.mouseUpAttribute = false;
  856. pointChangeObject.Remove(this.drawToolType);
  857. }
  858. /// <summary>
  859. /// 停止绘制时
  860. /// </summary>
  861. /// <param name="up"></param>
  862. public override void MouseUp(bool up)
  863. {
  864. mouseUpPointChange = up;
  865. }
  866. public void CalcCenter()
  867. {
  868. centerPoint = new PointF(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);//中心点
  869. first = true;
  870. }
  871. public void CalcSlope()
  872. {
  873. //计算斜率
  874. k1 = (double)(pointArray[1].Y - pointArray[0].Y) / (pointArray[1].X - pointArray[0].X);
  875. }
  876. public override int HandleCount
  877. {
  878. get
  879. {
  880. return 13;
  881. }
  882. }
  883. /// <summary>
  884. /// Get handle pointscroll by 1-based number
  885. /// </summary>
  886. /// <param name="handleNumber"></param>
  887. /// <returns></returns>
  888. public override PointF GetHandle(int handleNumber)
  889. {
  890. float x = 0;
  891. float y = 0;
  892. switch (handleNumber)
  893. {
  894. case 1:
  895. x = (int)pointArray[0].X;
  896. y = (int)pointArray[0].Y;
  897. break;
  898. case 2:
  899. x = (int)pointArray[1].X;
  900. y = (int)pointArray[1].Y;
  901. break;
  902. case 3:
  903. if (pointArray.Count >= 3)
  904. {
  905. x = (int)pointArray[2].X;
  906. y = (int)pointArray[2].Y;
  907. }
  908. break;
  909. case 4:
  910. if (pointArray.Count >= 4)
  911. {
  912. x = (int)pointArray[3].X;
  913. y = (int)pointArray[3].Y;
  914. }
  915. break;
  916. case 5:
  917. x = ((int)pointArray[0].X + (int)pointArray[1].X) / 2;
  918. y = ((int)pointArray[0].Y + (int)pointArray[1].Y) / 2;
  919. break;
  920. case 6:
  921. if (pointArray.Count >= 3)
  922. {
  923. x = ((int)pointArray[1].X + (int)pointArray[2].X) / 2;
  924. y = ((int)pointArray[1].Y + (int)pointArray[2].Y) / 2;
  925. }
  926. break;
  927. case 7:
  928. if (pointArray.Count >= 4)
  929. {
  930. x = ((int)pointArray[2].X + (int)pointArray[3].X) / 2;
  931. y = ((int)pointArray[2].Y + (int)pointArray[3].Y) / 2;
  932. }
  933. break;
  934. case 8:
  935. if (pointArray.Count >= 4)
  936. {
  937. x = ((int)pointArray[3].X + (int)pointArray[0].X) / 2;
  938. y = ((int)pointArray[3].Y + (int)pointArray[0].Y) / 2;
  939. }
  940. break;
  941. case 9:
  942. x = largerRect.X;
  943. y = largerRect.Y;
  944. break;
  945. case 10:
  946. x = largerRect.Right;
  947. y = largerRect.Y;
  948. break;
  949. case 11:
  950. x = largerRect.Right;
  951. y = largerRect.Bottom;
  952. break;
  953. case 12:
  954. x = largerRect.X;
  955. y = largerRect.Bottom;
  956. break;
  957. case 13:
  958. x = this.pointL.X;
  959. y = this.pointL.Y;
  960. break;
  961. }
  962. return new PointF(x, y);
  963. }
  964. /// <summary>
  965. /// Hit test.
  966. /// Return value: -1 - no hit
  967. /// 0 - hit anywhere
  968. /// > 1 - handle number
  969. /// </summary>
  970. /// <param name="pointscroll"></param>
  971. /// <returns></returns>
  972. public override int HitTest(Point point)
  973. {
  974. System.Console.WriteLine(point.X + "|" + point.Y);
  975. if (Selected)
  976. {
  977. for (int i = 1; i <= HandleCount; i++)
  978. {
  979. if (GetHandleRectangle(i).Contains(point))
  980. {
  981. System.Console.WriteLine(i);
  982. return i;
  983. }
  984. }
  985. if (GetRectanglePosition(this.rectangleF1, point, this.PointF , angle))
  986. {
  987. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  988. moveKb = 1;
  989. return 13;
  990. }
  991. else if (GetRectanglePosition(this.rectangleF2, point, this.PointF , angle))
  992. {
  993. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  994. moveKb = 2;
  995. return 13;
  996. }
  997. else if (GetRectanglePosition(this.rectangleF3, point, this.PointF , angle))
  998. {
  999. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1000. moveKb = 3;
  1001. return 13;
  1002. }
  1003. else if (GetRectanglePosition(this.rectangleF4, point, this.PointF , angle))
  1004. {
  1005. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1006. moveKb = 4;
  1007. return 13;
  1008. }
  1009. else if (GetRectanglePosition(this.rectangleF5, point, this.PointF , angle))
  1010. {
  1011. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1012. moveKb = 5;
  1013. return 13;
  1014. }
  1015. else if (GetRectanglePosition(this.rectangleF6, point, this.PointF , angle))
  1016. {
  1017. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1018. moveKb = 6;
  1019. return 13;
  1020. }
  1021. else if (GetRectanglePosition(this.rectangleF7, point, this.PointF , angle))
  1022. {
  1023. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1024. moveKb = 7;
  1025. return 13;
  1026. }
  1027. else if (GetRectanglePosition(this.rectangleF8, point, this.PointF , angle))
  1028. {
  1029. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1030. moveKb = 8;
  1031. return 13;
  1032. }
  1033. else if (GetRectanglePosition(this.rectangleF9, point, this.PointF , angle))
  1034. {
  1035. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1036. moveKb = 9;
  1037. return 13;
  1038. }
  1039. else if (GetRectanglePosition(this.rectangleF10, point, this.PointF , angle))
  1040. {
  1041. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1042. moveKb = 10;
  1043. return 13;
  1044. }
  1045. else if (GetRectanglePosition(this.rectangleF11, point, this.PointF , angle))
  1046. {
  1047. this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
  1048. moveKb = 11;
  1049. return 13;
  1050. }
  1051. }
  1052. if (PointInObject(point))
  1053. return 0;
  1054. return -1;
  1055. }
  1056. protected override bool PointInObject(Point point)
  1057. {
  1058. return largerRect.Contains(point);
  1059. }
  1060. public override bool IntersectsWith(Rectangle rectangle)
  1061. {
  1062. return largerRect.IntersectsWith(rectangle);
  1063. }
  1064. public override Cursor GetHandleCursor(int handleNumber)
  1065. {
  1066. return handleCursor;
  1067. }
  1068. public override void MoveHandleTo(Point point, int handleNumber)
  1069. {
  1070. if (handleNumber < 9)
  1071. {
  1072. this.mouseUpPointChange = true;
  1073. switch (handleNumber)
  1074. {
  1075. case 1:
  1076. this.rectangleF1.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1077. this.rectangleF2.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1078. this.rectangleF3.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1079. this.rectangleF4.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1080. this.rectangleF5.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1081. this.rectangleF6.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1082. this.rectangleF7.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1083. this.rectangleF8.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1084. this.rectangleF9.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1085. this.rectangleF10.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1086. this.rectangleF11.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1087. //更新第一个点
  1088. startPoint = point;
  1089. this.pointArray[0] = startPoint;
  1090. //计算第二个点
  1091. this.pointArray[1] = BasicCalculationHelper.GetDropFeet(startPoint, k1, linePoint);
  1092. endPoint.X = (int)this.pointArray[1].X;
  1093. endPoint.Y = (int)this.pointArray[1].Y;
  1094. //计算第四个点
  1095. this.pointArray[3] = BasicCalculationHelper.GetDropFeet(startPoint, -1/k1, linePoint);
  1096. linePoint1.X = (int)this.pointArray[3].X;
  1097. linePoint1.Y = (int)this.pointArray[3].Y;
  1098. break;
  1099. case 2:
  1100. endPoint = point;
  1101. this.pointArray[1] = endPoint;
  1102. if(this.pointArray.Count>3)
  1103. {
  1104. //计算第一个点
  1105. this.pointArray[0] = BasicCalculationHelper.GetDropFeet(endPoint, k1, linePoint1);
  1106. startPoint.X = (int)this.pointArray[0].X;
  1107. startPoint.Y = (int)this.pointArray[0].Y;
  1108. //计算第三个点
  1109. this.pointArray[2] = BasicCalculationHelper.GetDropFeet(endPoint, -1 / k1, linePoint1);
  1110. linePoint.X = (int)this.pointArray[2].X;
  1111. linePoint.Y = (int)this.pointArray[2].Y;
  1112. }
  1113. break;
  1114. case 3:
  1115. linePoint = point;
  1116. CalcSlope();
  1117. this.pointArray[1] = BasicCalculationHelper.GetDropFeet(point, -1 / k1, this.pointArray[0]);
  1118. endPoint.X = (int)this.pointArray[1].X;
  1119. endPoint.Y = (int)this.pointArray[1].Y;
  1120. if (this.pointArray.Count == 2)
  1121. {
  1122. this.pointArray.Add(point);
  1123. this.CalcOtherPoint(point);
  1124. }
  1125. else
  1126. {
  1127. this.pointArray[2] = linePoint;
  1128. if(!finish) this.CalcOtherPoint(point);
  1129. if (this.pointArray.Count == 4 && finish)
  1130. {
  1131. //计算第二个点
  1132. //this.pointArray[1] = BasicCalculationHelper.GetDropFeet(linePoint, -1/k1, this.pointArray[0]);
  1133. //endPoint.X = (int)this.pointArray[1].X;
  1134. //endPoint.Y = (int)this.pointArray[1].Y;
  1135. //计算第四个点
  1136. this.pointArray[3] = BasicCalculationHelper.GetDropFeet(linePoint, k1, this.pointArray[0]);
  1137. linePoint1.X = (int)this.pointArray[3].X;
  1138. linePoint1.Y = (int)this.pointArray[3].Y;
  1139. }
  1140. }
  1141. break;
  1142. case 4:
  1143. this.linePoint1 = point;
  1144. if (this.pointArray.Count == 3)
  1145. {
  1146. this.pointArray.Add(point);
  1147. }
  1148. else
  1149. {
  1150. this.pointArray[3] = point;
  1151. //if(finish)
  1152. {
  1153. //计算第一个点
  1154. this.pointArray[0] = BasicCalculationHelper.GetDropFeet(linePoint1, -1 / k1, endPoint);
  1155. startPoint.X = (int)this.pointArray[0].X;
  1156. startPoint.Y = (int)this.pointArray[0].Y;
  1157. //计算第三个点
  1158. this.pointArray[2] = BasicCalculationHelper.GetDropFeet(linePoint1, k1, endPoint);
  1159. linePoint.X = (int)this.pointArray[2].X;
  1160. linePoint.Y = (int)this.pointArray[2].Y;
  1161. }
  1162. }
  1163. break;
  1164. case 5:
  1165. this.pointArray[0] = BasicCalculationHelper.GetDropFeet(point, k1, this.pointArray[0]);
  1166. startPoint.X = (int)this.pointArray[0].X;
  1167. startPoint.Y = (int)this.pointArray[0].Y;
  1168. this.pointArray[1] = BasicCalculationHelper.GetDropFeet(point, k1, this.pointArray[1]);
  1169. endPoint.X = (int)this.pointArray[1].X;
  1170. endPoint.Y = (int)this.pointArray[1].Y;
  1171. break;
  1172. case 6:
  1173. this.pointArray[1] = BasicCalculationHelper.GetDropFeet(point, -1 / k1, this.pointArray[1]);
  1174. endPoint.X = (int)this.pointArray[1].X;
  1175. endPoint.Y = (int)this.pointArray[1].Y;
  1176. this.pointArray[2] = BasicCalculationHelper.GetDropFeet(point, -1 / k1, this.pointArray[2]);
  1177. linePoint.X = (int)this.pointArray[2].X;
  1178. linePoint.Y = (int)this.pointArray[2].Y;
  1179. break;
  1180. case 7:
  1181. this.pointArray[2] = BasicCalculationHelper.GetDropFeet(point, k1, this.pointArray[2]);
  1182. linePoint.X = (int)this.pointArray[2].X;
  1183. linePoint.Y = (int)this.pointArray[2].Y;
  1184. this.pointArray[3] = BasicCalculationHelper.GetDropFeet(point, k1, this.pointArray[3]);
  1185. linePoint1.X = (int)this.pointArray[3].X;
  1186. linePoint1.Y = (int)this.pointArray[3].Y;
  1187. break;
  1188. case 8:
  1189. this.pointArray[3] = BasicCalculationHelper.GetDropFeet(point, -1 / k1, this.pointArray[3]);
  1190. linePoint1.X = (int)this.pointArray[3].X;
  1191. linePoint1.Y = (int)this.pointArray[3].Y;
  1192. this.pointArray[0] = BasicCalculationHelper.GetDropFeet(point, -1 / k1, this.pointArray[0]);
  1193. startPoint.X = (int)this.pointArray[0].X;
  1194. startPoint.Y = (int)this.pointArray[0].Y;
  1195. break;
  1196. }
  1197. if(this.pointArray.Count == 4)
  1198. {
  1199. CalcOtherRectangle();
  1200. CalcCenter();
  1201. }
  1202. }
  1203. else
  1204. {
  1205. float x = 0, y = 0;
  1206. switch (handleNumber)
  1207. {
  1208. case 9:
  1209. this.mouseUpPointChange = true;
  1210. x = largerRect.X;
  1211. y = largerRect.Y;
  1212. break;
  1213. case 10:
  1214. this.mouseUpPointChange = true;
  1215. x = largerRect.Right;
  1216. y = largerRect.Y;
  1217. break;
  1218. case 11:
  1219. this.mouseUpPointChange = true;
  1220. x = largerRect.Right;
  1221. y = largerRect.Bottom;
  1222. break;
  1223. case 12:
  1224. this.mouseUpPointChange = true;
  1225. x = largerRect.X;
  1226. y = largerRect.Bottom;
  1227. break;
  1228. case 13:
  1229. point = SetOffsetAfterRotation(point, this.PointF , angle);
  1230. if (this.moveKb == 1)
  1231. this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1232. else if (this.moveKb == 2)
  1233. this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1234. else if (this.moveKb == 3)
  1235. this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1236. else if (this.moveKb == 4)
  1237. this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1238. else if (this.moveKb == 5)
  1239. this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1240. else if (this.moveKb == 6)
  1241. this.rectangleF6.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1242. else if (this.moveKb == 7)
  1243. this.rectangleF7.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1244. else if (this.moveKb == 8)
  1245. this.rectangleF8.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1246. else if (this.moveKb == 9)
  1247. this.rectangleF9.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1248. else if (this.moveKb == 10)
  1249. this.rectangleF10.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1250. else if (this.moveKb == 11)
  1251. this.rectangleF11.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1252. this.pointL = point;
  1253. break;
  1254. }
  1255. if(handleNumber != 13)
  1256. {
  1257. this.mouseUpPointChange = true;
  1258. if (!this.isPress)
  1259. {
  1260. this.isPress = true;
  1261. lastPoint = new PointF(x, y);
  1262. }
  1263. double angle = BasicCalculationHelper.CalculateAngle(centerPoint.X, centerPoint.Y, lastPoint.X, lastPoint.Y, point.X, point.Y);
  1264. lastPoint = point;
  1265. for (int l = 0; l < pointArray.Count; l++)
  1266. {
  1267. pointArray[l] = BasicCalculationHelper.GetAnglePoint(pointArray[l], centerPoint, angle);
  1268. }
  1269. startPoint = BasicCalculationHelper.GetAnglePoint(startPoint, centerPoint, angle);
  1270. endPoint = BasicCalculationHelper.GetAnglePoint(endPoint, centerPoint, angle);
  1271. linePoint = BasicCalculationHelper.GetAnglePoint(linePoint, centerPoint, angle);
  1272. linePoint1 = BasicCalculationHelper.GetAnglePoint(linePoint1, centerPoint, angle);
  1273. CalcSlope();
  1274. }
  1275. }
  1276. }
  1277. /// <summary>
  1278. /// 计算点
  1279. /// </summary>
  1280. private void CalcOtherPoint(PointF temp)
  1281. {
  1282. if (!first)
  1283. {
  1284. angleF = BasicCalculationHelper.Angle(startPoint, endPoint, new PointF(startPoint.X, endPoint.Y));
  1285. if (startPoint.X > endPoint.X)
  1286. {
  1287. angleF += 180;
  1288. }
  1289. }
  1290. float height = temp.Y - endPoint.Y;
  1291. linePoint = new PointF(endPoint.X, endPoint.Y + height);
  1292. linePoint = BasicCalculationHelper.GetAnglePoint(linePoint, new PointF(endPoint.X, endPoint.Y), angleF);
  1293. if (this.pointArray.Count == 2)
  1294. {
  1295. this.pointArray.Add(linePoint);
  1296. }
  1297. else
  1298. {
  1299. this.pointArray[2] = linePoint;
  1300. }
  1301. linePoint1 = new PointF(startPoint.X, startPoint.Y + height);
  1302. linePoint1 = BasicCalculationHelper.GetAnglePoint(linePoint1, new PointF(startPoint.X, startPoint.Y), angleF);
  1303. if (this.pointArray.Count == 3)
  1304. {
  1305. this.pointArray.Add(linePoint1);
  1306. }
  1307. else
  1308. {
  1309. this.pointArray[3] = linePoint1;
  1310. }
  1311. CalcOtherRectangle();
  1312. }
  1313. /// <summary>
  1314. /// 计算外接矩形
  1315. /// </summary>
  1316. private void CalcOtherRectangle()
  1317. {
  1318. float[] pointX = new float[] { pointArray[0].X, pointArray[1].X, pointArray[2].X, pointArray[3].X };
  1319. rectangle.X = (int)pointX.Min();
  1320. float[] pointY = new float[] { pointArray[0].Y, pointArray[1].Y, pointArray[2].Y, pointArray[3].Y };
  1321. rectangle.Y = (int)pointY.Min();
  1322. rectangle.Width = (int)Math.Abs(pointX.Max() - pointX.Min());
  1323. rectangle.Height = (int)Math.Abs(pointY.Max() - pointY.Min());
  1324. }
  1325. /// <summary>
  1326. /// 对象移动
  1327. /// </summary>
  1328. /// <param name="deltaX"></param>
  1329. /// <param name="deltaY"></param>
  1330. public override void Move(int deltaX, int deltaY)
  1331. {
  1332. int x = ISurfaceBox.UnscaleScalar(deltaX);
  1333. int y = ISurfaceBox.UnscaleScalar(deltaY);
  1334. startPoint.X += x;
  1335. startPoint.Y += y;
  1336. endPoint.X += x;
  1337. endPoint.Y += y;
  1338. linePoint.X += x;
  1339. linePoint.Y += y;
  1340. linePoint1.X += x;
  1341. linePoint1.Y += y;
  1342. for (int i = 0; i < pointArray.Count; i++)
  1343. {
  1344. pointArray[i] = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  1345. }
  1346. this.startPoint = pointArray[0];
  1347. pointL.X += x;
  1348. pointL.Y += y;
  1349. this.rectangleF1.Offset(x, y);
  1350. this.rectangleF2.Offset(x, y);
  1351. this.rectangleF3.Offset(x, y);
  1352. this.rectangleF4.Offset(x, y);
  1353. this.rectangleF5.Offset(x, y);
  1354. this.rectangleF6.Offset(x, y);
  1355. this.rectangleF7.Offset(x, y);
  1356. this.rectangleF8.Offset(x, y);
  1357. this.rectangleF9.Offset(x, y);
  1358. this.rectangleF10.Offset(x, y);
  1359. this.rectangleF11.Offset(x, y);
  1360. CalcOtherRectangle();
  1361. CalcCenter();
  1362. }
  1363. public override RectangleF GetBoundingBox()
  1364. {
  1365. RectangleF temp = new RectangleF();
  1366. temp.X = rectangle.X - 10;
  1367. temp.Y = rectangle.Y - 10;
  1368. temp.Width = rectangle.Width + 20;
  1369. temp.Height = rectangle.Height + 20;
  1370. return temp;
  1371. }
  1372. /// <summary>
  1373. /// Draw tracker for selected object
  1374. /// </summary>
  1375. /// <param name="g"></param>
  1376. public override void DrawTracker(Graphics g)
  1377. {
  1378. if (!Selected)
  1379. return;
  1380. if (pointArray.Count >= 3)
  1381. {
  1382. SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
  1383. Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth);
  1384. for (int i = 1; i <= HandleCount; i++)
  1385. {
  1386. if (i == HandleCount)
  1387. {
  1388. brush = new SolidBrush(Color.Transparent);
  1389. pen = new Pen(Color.Transparent);
  1390. }
  1391. switch (MarkpointStyle)
  1392. {
  1393. case 0:
  1394. g.FillRectangle(brush, GetHandleRectangle(i));
  1395. g.DrawRectangle(pen, GetHandleRectangle(i));
  1396. break;
  1397. case 1:
  1398. g.FillEllipse(brush, GetHandleRectangle(i));
  1399. g.DrawEllipse(pen, GetHandleRectangle(i));
  1400. break;
  1401. case 2:
  1402. g.FillPolygon(brush, GetHandlePoint(i));
  1403. g.DrawPolygon(pen, GetHandlePoint(i));
  1404. break;
  1405. }
  1406. }
  1407. brush.Dispose();
  1408. pen.Dispose();
  1409. //g.DrawRectangle(new Pen(Color.Black), largerRect.X, largerRect.Y, largerRect.Width, largerRect.Height);
  1410. }
  1411. }
  1412. /// <summary>
  1413. /// 标准化外接矩形
  1414. /// </summary>
  1415. public override void Normalize()
  1416. {
  1417. this.isPress = false;
  1418. CalcOtherRectangle();
  1419. }
  1420. /// <summary>
  1421. /// 获取点集合
  1422. /// </summary>
  1423. /// <returns></returns>
  1424. public override List<PointF> GetPoints()
  1425. {
  1426. return pointArray;
  1427. }
  1428. /// <summary>
  1429. /// 获取样式
  1430. /// </summary>
  1431. /// <returns></returns>
  1432. public override ParentStyleModel GetStyle()
  1433. {
  1434. return measureRandRectangle;
  1435. }
  1436. }
  1437. }