MeasureTwoLineVLDistance.cs 59 KB

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