MeasureFourPointAngle.cs 41 KB

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