MeasureThreePointAngle.cs 49 KB

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