MeasureSettingDialog.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using System;
  6. using System.Collections.Generic;
  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.Shapes;
  18. namespace Compdfkit_Tools.Measure
  19. {
  20. /// <summary>
  21. /// MeasureSettingDialog.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class MeasureSettingDialog : Window
  24. {
  25. public MeasureSettingDialog()
  26. {
  27. InitializeComponent();
  28. }
  29. public event EventHandler CancelEvent;
  30. public event EventHandler DoneEvent;
  31. public bool ReturnToInfoPanel { get; set; }
  32. public List<AnnotHandlerEventArgs> UpdateArgsList { get; set; } = new List<AnnotHandlerEventArgs>();
  33. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  34. {
  35. if (UpdateArgsList != null && UpdateArgsList.Count > 0)
  36. {
  37. ReturnToInfoPanel = false;
  38. }
  39. UpdateArgsList?.Clear();
  40. CancelEvent?.Invoke(this, e);
  41. ReturnToInfoPanel = false;
  42. Close();
  43. }
  44. private void ButtonDone_Click(object sender, RoutedEventArgs e)
  45. {
  46. SaveMeasureSetting();
  47. if (UpdateArgsList != null && UpdateArgsList.Count > 0)
  48. {
  49. List<AnnotArgsType> allowTypeList = new List<AnnotArgsType>()
  50. {
  51. AnnotArgsType.LineMeasure,
  52. AnnotArgsType.PolyLineMeasure,
  53. AnnotArgsType.PolygonMeasure
  54. };
  55. foreach (AnnotHandlerEventArgs args in UpdateArgsList)
  56. {
  57. if (allowTypeList.Contains(args.EventType))
  58. {
  59. CPDFAnnotation pdfAnnot = args.GetPDFAnnot();
  60. switch (pdfAnnot.Type)
  61. {
  62. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  63. {
  64. CPDFLineAnnotation lineAnnot = (CPDFLineAnnotation)pdfAnnot;
  65. if (lineAnnot.IsMersured())
  66. {
  67. CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
  68. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  69. measureInfo.Precision = MeasureSetting.GetMeasureSavePrecision();
  70. measureInfo.RulerBase = (float)MeasureSetting.RulerBase;
  71. measureInfo.RulerBaseUnit = MeasureSetting.RulerBaseUnit;
  72. measureInfo.RulerTranslate = (float)MeasureSetting.RulerTranslate;
  73. measureInfo.RulerTranslateUnit = MeasureSetting.RulerTranslateUnit;
  74. lineMeasure.SetMeasureInfo(measureInfo);
  75. lineMeasure.SetMeasureScale(
  76. measureInfo.RulerBase,
  77. measureInfo.RulerBaseUnit,
  78. measureInfo.RulerTranslate,
  79. measureInfo.RulerTranslateUnit);
  80. lineMeasure.UpdateAnnotMeasure();
  81. lineAnnot.UpdateAp();
  82. args.Draw();
  83. }
  84. }
  85. break;
  86. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  87. {
  88. CPDFPolylineAnnotation polylineAnnot = (CPDFPolylineAnnotation)pdfAnnot;
  89. if (polylineAnnot.IsMersured())
  90. {
  91. CPDFPerimeterMeasure polylineMeasure = polylineAnnot.GetPerimeterMeasure();
  92. CPDFMeasureInfo measureInfo = polylineMeasure.MeasureInfo;
  93. measureInfo.Precision = MeasureSetting.GetMeasureSavePrecision();
  94. measureInfo.RulerBase = (float)MeasureSetting.RulerBase;
  95. measureInfo.RulerBaseUnit = MeasureSetting.RulerBaseUnit;
  96. measureInfo.RulerTranslate = (float)MeasureSetting.RulerTranslate;
  97. measureInfo.RulerTranslateUnit = MeasureSetting.RulerTranslateUnit;
  98. polylineMeasure.SetMeasureInfo(measureInfo);
  99. polylineMeasure.SetMeasureScale(
  100. measureInfo.RulerBase,
  101. measureInfo.RulerBaseUnit,
  102. measureInfo.RulerTranslate,
  103. measureInfo.RulerTranslateUnit);
  104. polylineMeasure.UpdateAnnotMeasure();
  105. polylineAnnot.UpdateAp();
  106. args.Draw();
  107. }
  108. }
  109. break;
  110. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  111. {
  112. CPDFPolygonAnnotation areaAnnot = (CPDFPolygonAnnotation)pdfAnnot;
  113. if (areaAnnot.IsMersured())
  114. {
  115. CPDFAreaMeasure areaMeasure = areaAnnot.GetAreaMeasure();
  116. CPDFMeasureInfo measureInfo = areaMeasure.MeasureInfo;
  117. measureInfo.Precision = MeasureSetting.GetMeasureSavePrecision();
  118. measureInfo.RulerBase = (float)MeasureSetting.RulerBase;
  119. measureInfo.RulerBaseUnit = MeasureSetting.RulerBaseUnit;
  120. measureInfo.RulerTranslate = (float)MeasureSetting.RulerTranslate;
  121. measureInfo.RulerTranslateUnit = MeasureSetting.RulerTranslateUnit;
  122. measureInfo.CaptionType = CPDFCaptionType.CPDF_CAPTION_NONE;
  123. if (MeasureSetting.IsShowArea)
  124. {
  125. measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_AREA;
  126. }
  127. if (MeasureSetting.IsShowLength)
  128. {
  129. measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_LENGTH;
  130. }
  131. areaMeasure.SetMeasureInfo(measureInfo);
  132. areaMeasure.SetMeasureScale(
  133. measureInfo.RulerBase,
  134. measureInfo.RulerBaseUnit,
  135. measureInfo.RulerTranslate,
  136. measureInfo.RulerTranslateUnit);
  137. areaMeasure.UpdateAnnotMeasure();
  138. areaAnnot.UpdateAp();
  139. args.Draw();
  140. }
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. }
  148. ReturnToInfoPanel = false;
  149. }
  150. UpdateArgsList?.Clear();
  151. DoneEvent?.Invoke(this, e);
  152. ReturnToInfoPanel = false;
  153. Close();
  154. }
  155. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  156. {
  157. List<Key> allowKeys = new List<Key>()
  158. {
  159. Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
  160. Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
  161. Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right,
  162. Key.OemPeriod,Key.Decimal
  163. };
  164. if (allowKeys.Contains(e.Key) == false)
  165. {
  166. e.Handled = true;
  167. }
  168. }
  169. private void TextBox_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  170. {
  171. try
  172. {
  173. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  174. {
  175. string checkText = Clipboard.GetText();
  176. if (int.TryParse(checkText, out int value))
  177. {
  178. e.CanExecute = true;
  179. }
  180. e.Handled = true;
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. }
  186. }
  187. public void BindMeasureSetting()
  188. {
  189. RulerBaseText.Text = MeasureSetting.RulerBase.ToString();
  190. RulerTranslateText.Text = MeasureSetting.RulerTranslate.ToString();
  191. RulerTranslateCombo.SelectedIndex = -1;
  192. RulerBaseUnitCombo.SelectedIndex = -1;
  193. PrecisionBox.SelectedIndex = -1;
  194. if (MeasureSetting.RulerBaseUnit == "in")
  195. {
  196. RulerBaseUnitCombo.SelectedIndex = 0;
  197. }
  198. if (MeasureSetting.RulerBaseUnit == "cm")
  199. {
  200. RulerBaseUnitCombo.SelectedIndex = 1;
  201. }
  202. if (MeasureSetting.RulerBaseUnit == "mm")
  203. {
  204. RulerBaseUnitCombo.SelectedIndex = 2;
  205. }
  206. for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
  207. {
  208. ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
  209. if (checkItem != null && checkItem.Content.ToString() == MeasureSetting.RulerTranslateUnit.ToString())
  210. {
  211. RulerTranslateCombo.SelectedIndex = i;
  212. }
  213. }
  214. for (int i = 0; i < PrecisionBox.Items.Count; i++)
  215. {
  216. ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  217. if (checkItem != null && checkItem.Content.ToString() == ((decimal)MeasureSetting.Precision).ToString())
  218. {
  219. PrecisionBox.SelectedIndex = i;
  220. }
  221. }
  222. }
  223. private void SaveMeasureSetting()
  224. {
  225. if (double.TryParse(RulerBaseText.Text, out double ruleBase))
  226. {
  227. MeasureSetting.RulerBase = ruleBase;
  228. }
  229. if (RulerBaseUnitCombo.SelectedItem != null)
  230. {
  231. ComboBoxItem checkItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  232. MeasureSetting.RulerBaseUnit = checkItem.Content.ToString();
  233. }
  234. if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  235. {
  236. MeasureSetting.RulerTranslate = ruletranBase;
  237. }
  238. if (RulerTranslateCombo.SelectedItem != null)
  239. {
  240. ComboBoxItem checkItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  241. MeasureSetting.RulerTranslateUnit = checkItem.Content.ToString();
  242. }
  243. if (PrecisionBox.SelectedValue != null)
  244. {
  245. ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  246. if (double.TryParse(checkItem.Content.ToString(), out double precision))
  247. {
  248. MeasureSetting.Precision = precision;
  249. }
  250. }
  251. MeasureSetting.IsShowArea = (bool)AreaCheckBox.IsChecked;
  252. MeasureSetting.IsShowLength = (bool)LengthCheckBox.IsChecked;
  253. }
  254. public void ChangedCheckBoxIsChecked(bool Area, bool Lenght)
  255. {
  256. AreaCheckBox.IsChecked = Area;
  257. LengthCheckBox.IsChecked = Lenght;
  258. }
  259. }
  260. }