MeasureInfoPanel.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using ComPDFKit.Measure;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing.Drawing2D;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using Compdfkit_Tools.Helper;
  20. using ComPDFKit.PDFAnnotation;
  21. using ComPDFKit.Tool;
  22. namespace Compdfkit_Tools.Measure
  23. {
  24. /// <summary>
  25. /// MeasureInfoPanel.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class MeasureInfoPanel : UserControl
  28. {
  29. public event EventHandler SettingClick;
  30. public CPDFMeasureType MeasureType { get; private set; }
  31. internal Dictionary<string, string> IconsDict { get; private set; } = new Dictionary<string, string>()
  32. {
  33. {
  34. LanguageHelper.ToolBarManager.GetString("Button_Line") ?? string.Empty,
  35. "M17.7782 5.75646L14.2427 2.22093L13.0052 3.45837L15.4802 5.93333L14.4195 6.99399L11.9446 4.51903L10.1768 " +
  36. "6.28679L12.6518 8.76175L11.5911 9.82241L9.11615 7.34745L7.34838 9.11522L9.82334 11.5902L8.76268 " +
  37. "12.6508L6.28772 10.1759L4.51996 11.9436L6.99492 14.4186L5.93426 15.4793L3.4593 13.0043L2.22186 " +
  38. "14.2417L5.75739 17.7773L17.7782 5.75646ZM15.3033 1.16027L14.2427 0.0996094L13.182 1.16027L1.1612 " +
  39. "13.1811L0.100539 14.2417L1.1612 15.3024L4.69673 18.8379L5.75739 19.8986L6.81805 18.8379L18.8389 " +
  40. "6.81712L19.8995 5.75646L18.8389 4.6958L15.3033 1.16027Z"
  41. },
  42. {
  43. LanguageHelper.ToolBarManager.GetString("Button_Multiline") ?? string.Empty,
  44. "M0.5 3C0.5 1.89543 1.39543 1 2.5 1C3.33934 1 4.05793 1.51704 4.35462 2.25H15.6454C15.9421 1.51704 16.6607 1 " +
  45. "17.5 1C18.6046 1 19.5 1.89543 19.5 3C19.5 3.83934 18.983 4.55793 18.25 4.85462V17V17.75H17.5H13.7011L14.5167 " +
  46. "18.5765L13.5138 19.5928L11.4986 17.5505L10.9972 17.0423L11.4986 16.5342L13.5138 14.4918L14.5167 15.5082L13.7847 " +
  47. "16.25H16.75V4.85462C16.2487 4.65168 15.8483 4.25135 15.6454 3.75H4.35462C4.15168 4.25135 3.75135 4.65168 3.25 " +
  48. "4.85462V16.25H6.25V15H7.75V19H6.25V17.75H2.5H1.75V17V4.85462C1.01704 4.55793 0.5 3.83934 0.5 3Z"
  49. },
  50. {
  51. LanguageHelper.ToolBarManager.GetString("Button_Polygonal") ?? string.Empty,
  52. "M1.37111 7.6963L10 1.42705L18.6289 7.6963L15.3329 17.8402H4.66705L1.37111 7.6963Z"
  53. },
  54. {
  55. LanguageHelper.ToolBarManager.GetString("Button_Rectangle") ?? string.Empty,
  56. "M20.25 2.25H0.75V17.75H20.25V2.25ZM18.75 10.75V16.25H2.25V10.75H18.75ZM18.75 9.25V3.75H2.25V9.25H18.75ZM6.5 5.5H3.5V7.5H6.5V5.5ZM3.5 12.5H6.5V14.5H3.5V12.5Z"
  57. }
  58. };
  59. public MeasureInfoPanel()
  60. {
  61. InitializeComponent();
  62. }
  63. internal void SetMeasureScale(CPDFMeasureType measureType, string scale)
  64. {
  65. switch (measureType)
  66. {
  67. case CPDFMeasureType.CPDF_DISTANCE_MEASURE:
  68. ScaleText.Text = scale;
  69. break;
  70. case CPDFMeasureType.CPDF_PERIMETER_MEASURE:
  71. ScalePolyLineText.Text = scale;
  72. break;
  73. case CPDFMeasureType.CPDF_AREA_MEASURE:
  74. ScalePolygonText.Text = scale;
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. public void SetMeasureType(CPDFMeasureType newType)
  81. {
  82. MeasureType=newType;
  83. int iconIndex = (int)newType;
  84. LinePanel.Visibility = Visibility.Collapsed;
  85. PolyLinePanel.Visibility = Visibility.Collapsed;
  86. PolygonPanel.Visibility = Visibility.Collapsed;
  87. if (iconIndex>=0 && iconIndex< IconsDict.Count)
  88. {
  89. TypeConverter typeCovert = TypeDescriptor.GetConverter(typeof(Geometry));
  90. MeasureIcon.Data = PathGeometry.CreateFromGeometry((Geometry)typeCovert.ConvertFrom(IconsDict.ElementAt(iconIndex).Value));
  91. MeasureTitelText.Text = IconsDict.ElementAt(iconIndex).Key;
  92. switch(newType)
  93. {
  94. case CPDFMeasureType.CPDF_DISTANCE_MEASURE:
  95. LinePanel.Visibility = Visibility.Visible;
  96. MeasureIcon.Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D));
  97. MeasureIcon.Stroke = null;
  98. break;
  99. case CPDFMeasureType.CPDF_PERIMETER_MEASURE:
  100. PolyLinePanel.Visibility = Visibility.Visible;
  101. MeasureIcon.Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D));
  102. MeasureIcon.Stroke = null;
  103. break;
  104. case CPDFMeasureType.CPDF_AREA_MEASURE:
  105. PolygonPanel.Visibility = Visibility.Visible;
  106. MeasureIcon.Fill = null;
  107. MeasureIcon.Stroke=new SolidColorBrush(Color.FromRgb(0x43,0x47,0x4D));
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. }
  114. public void ClearMeasureInfo()
  115. {
  116. DistanceText.Text = string.Empty;
  117. PrecisionText.Text = string.Empty;
  118. AngleText.Text = string.Empty;
  119. XText.Text = string.Empty;
  120. YText.Text = string.Empty;
  121. ScaleText.Text= string.Empty;
  122. DistancePolyLineText.Text = string.Empty;
  123. PrecisionPolyLineText.Text = string.Empty;
  124. AnglePolyLineText.Text = string.Empty;
  125. ScalePolyLineText.Text = string.Empty;
  126. ScalePolygonText.Text=string.Empty;
  127. RoundPolygonText.Text = string.Empty;
  128. PrecisionPolygonText.Text = string.Empty;
  129. AnglePolygonText.Text = string.Empty;
  130. }
  131. public void SetMeasureInfo(CPDFAnnotation annot)
  132. {
  133. if (annot is CPDFLineAnnotation lineAnnot)
  134. {
  135. SetLineMeasureInfo(lineAnnot);
  136. }
  137. else if (annot is CPDFPolylineAnnotation polyLineAnnot)
  138. {
  139. SetPolylineMeasureInfo(polyLineAnnot);
  140. }
  141. else if (annot is CPDFPolygonAnnotation polygonAnnot)
  142. {
  143. SetPolygonMeasureInfo(polygonAnnot);
  144. }
  145. }
  146. public void SetLineMeasureInfo(CPDFLineAnnotation lineAnnot)
  147. {
  148. var lineMeasure = lineAnnot.GetDistanceMeasure();
  149. Vector standVector = new Vector(1, 0);
  150. Point startPoint = new Point(lineAnnot.Points[0].x, lineAnnot.Points[0].y);
  151. Point endPoint = new Point(lineAnnot.Points[1].x, lineAnnot.Points[1].y);
  152. Vector moveVector = endPoint - startPoint;
  153. AngleText.Text = ((int)Math.Abs(Vector.AngleBetween(moveVector, standVector))) + "°";
  154. double showLength = lineMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  155. XText.Text = ((int)Math.Abs(moveVector.X)).ToString();
  156. YText.Text = ((int)Math.Abs(moveVector.Y)).ToString();
  157. NumberFormatInfo formatInfo = new NumberFormatInfo();
  158. formatInfo.NumberDecimalDigits = Math.Abs(lineMeasure.MeasureInfo.Precision).ToString().Length - 1;
  159. DistanceText.Text = showLength.ToString("N", formatInfo) + " " + lineMeasure.MeasureInfo.RulerTranslateUnit;
  160. ScaleText.Text = string.Format("{0} {1} = {2} {3}",
  161. lineMeasure.MeasureInfo.RulerBase,
  162. lineMeasure.MeasureInfo.RulerBaseUnit,
  163. lineMeasure.MeasureInfo.RulerTranslate,
  164. lineMeasure.MeasureInfo.RulerTranslateUnit);
  165. PrecisionText.Text = GetMeasureShowPrecision(lineMeasure.MeasureInfo.Precision).ToString();
  166. }
  167. public void SetPolylineMeasureInfo(CPDFPolylineAnnotation polyLineAnnot)
  168. {
  169. var polyLineMeasure = polyLineAnnot.GetPerimeterMeasure();
  170. double showLength = polyLineMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  171. NumberFormatInfo formatInfo = new NumberFormatInfo();
  172. formatInfo.NumberDecimalDigits = Math.Abs(polyLineMeasure.MeasureInfo.Precision).ToString().Length - 1;
  173. DistancePolyLineText.Text = showLength.ToString("N", formatInfo) + " " + polyLineMeasure.MeasureInfo.RulerTranslateUnit;
  174. ScalePolyLineText.Text = string.Format("{0} {1} = {2} {3}",
  175. polyLineMeasure.MeasureInfo.RulerBase,
  176. polyLineMeasure.MeasureInfo.RulerBaseUnit,
  177. polyLineMeasure.MeasureInfo.RulerTranslate,
  178. polyLineMeasure.MeasureInfo.RulerTranslateUnit);
  179. PrecisionPolyLineText.Text = GetMeasureShowPrecision(polyLineMeasure.MeasureInfo.Precision).ToString();
  180. AnglePolyLineText.Text = 0 + "°";
  181. }
  182. public void SetPolygonMeasureInfo(CPDFPolygonAnnotation polygonAnnot)
  183. {
  184. var polygonMeasure = polygonAnnot.GetAreaMeasure();
  185. double showArea = polygonMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_AREA);
  186. NumberFormatInfo formatInfo = new NumberFormatInfo();
  187. formatInfo.NumberDecimalDigits = Math.Abs(polygonMeasure.MeasureInfo.Precision).ToString().Length - 1;
  188. RoundPolygonText.Text = showArea.ToString("N", formatInfo) + " " + polygonMeasure.MeasureInfo.RulerTranslateUnit;
  189. ScalePolygonText.Text = string.Format("{0} {1} = {2} {3}",
  190. polygonMeasure.MeasureInfo.RulerBase,
  191. polygonMeasure.MeasureInfo.RulerBaseUnit,
  192. polygonMeasure.MeasureInfo.RulerTranslate,
  193. polygonMeasure.MeasureInfo.RulerTranslateUnit);
  194. PrecisionPolygonText.Text = GetMeasureShowPrecision(polygonMeasure.MeasureInfo.Precision).ToString();
  195. double inch = polygonMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_AREA);
  196. double currentInch = polygonMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  197. RoundPolygonText.Text = inch.ToString("N", formatInfo) + " sq " + polygonMeasure.MeasureInfo.RulerTranslateUnit;
  198. AnglePolygonText.Text = 0 + "°";
  199. }
  200. private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  201. {
  202. SettingClick?.Invoke(this, e);
  203. }
  204. private double GetMeasureShowPrecision(int precision)
  205. {
  206. if (precision == CPDFMeasure.PRECISION_VALUE_ZERO)
  207. {
  208. return 1;
  209. }
  210. if (CPDFMeasure.PRECISION_VALUE_ONE == precision)
  211. {
  212. return 0.1;
  213. }
  214. if (CPDFMeasure.PRECISION_VALUE_TWO == precision)
  215. {
  216. return 0.01;
  217. }
  218. if (CPDFMeasure.PRECISION_VALUE_THREE == precision)
  219. {
  220. return 0.001;
  221. }
  222. if (CPDFMeasure.PRECISION_VALUE_FOUR == precision)
  223. {
  224. return 0.0001;
  225. }
  226. return 0;
  227. }
  228. public void SetMeasureInfo(MeasureEventArgs args)
  229. {
  230. if (args.Type == CPDFMeasureType.CPDF_DISTANCE_MEASURE)
  231. {
  232. DistanceText.Text = args.Distance;
  233. PrecisionText.Text = args.Precision.ToString();
  234. AngleText.Text = args.Angle.ToString();
  235. XText.Text = args.MousePos.X.ToString();
  236. YText.Text = args.MousePos.Y.ToString();
  237. }
  238. else if (args.Type == CPDFMeasureType.CPDF_PERIMETER_MEASURE)
  239. {
  240. DistancePolyLineText.Text = args.Distance;
  241. PrecisionPolyLineText.Text = args.Precision.ToString();
  242. AnglePolyLineText.Text = args.Angle.ToString();
  243. }
  244. else if (args.Type == CPDFMeasureType.CPDF_AREA_MEASURE)
  245. {
  246. RoundPolygonText.Text = args.Distance;
  247. PrecisionPolygonText.Text = args.Precision.ToString();
  248. AnglePolygonText.Text = args.Angle.ToString();
  249. }
  250. }
  251. }
  252. }