MeasureFourPointAngle.cs 38 KB

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