MeasureSettingPanel.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using Compdfkit_Tools.PDFControl;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using ComPDFKit.Tool.SettingParam;
  10. namespace Compdfkit_Tools.Measure
  11. {
  12. /// <summary>
  13. /// MeasureSettingPanel.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class MeasureSettingPanel : UserControl
  16. {
  17. public event EventHandler CancelEvent;
  18. public event EventHandler DoneEvent;
  19. public PDFViewControl PdfViewControl { get; set; }
  20. public bool ReturnToInfoPanel { get; set; }
  21. //public List<AnnotHandlerEventArgs> UpdateArgsList { get; set; } = new List<AnnotHandlerEventArgs>();
  22. public MeasureSettingPanel()
  23. {
  24. InitializeComponent();
  25. }
  26. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  27. {
  28. //if (UpdateArgsList != null && UpdateArgsList.Count > 0)
  29. //{
  30. // ReturnToInfoPanel = false;
  31. //}
  32. //UpdateArgsList?.Clear();
  33. CancelEvent?.Invoke(this, e);
  34. ReturnToInfoPanel = false;
  35. }
  36. public void ShowAreaAndLength(Visibility visibility)
  37. {
  38. if (visibility == Visibility.Visible)
  39. {
  40. SettingPanel.Height = 350;
  41. }
  42. else
  43. {
  44. SettingPanel.Height = 290;
  45. }
  46. AreaAndLength.Visibility = visibility;
  47. }
  48. private void ButtonDone_Click(object sender, RoutedEventArgs e)
  49. {
  50. if (string.IsNullOrEmpty(RulerBaseText.Text) || string.IsNullOrEmpty(RulerTranslateText.Text))
  51. {
  52. MessageBox.Show("Scale is not greater than zero");
  53. return;
  54. }
  55. if (double.TryParse(RulerBaseText.Text, out double ruleBase))
  56. {
  57. if (ruleBase <= 0)
  58. {
  59. MessageBox.Show("Scale is not greater than zero");
  60. return;
  61. }
  62. }
  63. if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  64. {
  65. if (ruletranBase <= 0)
  66. {
  67. MessageBox.Show("Scale is not greater than zero");
  68. return;
  69. }
  70. }
  71. DoneEvent?.Invoke(this, e);
  72. ReturnToInfoPanel = false;
  73. }
  74. private int GetMeasureSavePrecision(double Precision)
  75. {
  76. if (Precision == 1)
  77. {
  78. return CPDFMeasure.PRECISION_VALUE_ZERO;
  79. }
  80. if (Precision == 0.1)
  81. {
  82. return CPDFMeasure.PRECISION_VALUE_ONE;
  83. }
  84. if (Precision == 0.01)
  85. {
  86. return CPDFMeasure.PRECISION_VALUE_TWO;
  87. }
  88. if (Precision == 0.001)
  89. {
  90. return CPDFMeasure.PRECISION_VALUE_THREE;
  91. }
  92. if (Precision == 0.0001)
  93. {
  94. return CPDFMeasure.PRECISION_VALUE_FOUR;
  95. }
  96. return 0;
  97. }
  98. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  99. {
  100. List<Key> allowKeys = new List<Key>()
  101. {
  102. Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
  103. Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
  104. Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right,
  105. Key.OemPeriod,Key.Decimal
  106. };
  107. if (allowKeys.Contains(e.Key) == false)
  108. {
  109. e.Handled = true;
  110. }
  111. }
  112. private void TextBox_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  113. {
  114. try
  115. {
  116. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  117. {
  118. string checkText = Clipboard.GetText();
  119. if (int.TryParse(checkText, out int value))
  120. {
  121. e.CanExecute = true;
  122. }
  123. e.Handled = true;
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. }
  129. }
  130. public void BindMeasureSetting(CPDFMeasureInfo measureInfo)
  131. {
  132. if(measureInfo == null)
  133. {
  134. return;
  135. }
  136. RulerBaseText.Text = measureInfo.RulerBase.ToString();
  137. RulerTranslateText.Text = measureInfo.RulerTranslate.ToString();
  138. RulerTranslateCombo.SelectedIndex = -1;
  139. RulerBaseUnitCombo.SelectedIndex = -1;
  140. PrecisionBox.SelectedIndex = -1;
  141. if (measureInfo.RulerBaseUnit == "in")
  142. {
  143. RulerBaseUnitCombo.SelectedIndex = 0;
  144. }
  145. if (measureInfo.RulerBaseUnit == "cm")
  146. {
  147. RulerBaseUnitCombo.SelectedIndex = 1;
  148. }
  149. if (measureInfo.RulerBaseUnit == "mm")
  150. {
  151. RulerBaseUnitCombo.SelectedIndex = 2;
  152. }
  153. for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
  154. {
  155. ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
  156. if (checkItem != null && checkItem.Tag.ToString() == measureInfo.RulerTranslateUnit.ToString())
  157. {
  158. RulerTranslateCombo.SelectedIndex = i;
  159. }
  160. }
  161. string precision = GetMeasureShowPrecision(measureInfo.Precision).ToString();
  162. for (int i = 0; i < PrecisionBox.Items.Count; i++)
  163. {
  164. ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  165. if (checkItem != null && checkItem.Content.ToString() == precision)
  166. {
  167. PrecisionBox.SelectedIndex = i;
  168. }
  169. }
  170. }
  171. //public void BindMeasureSetting(MeasureEventArgs measureEventArgs)
  172. //{
  173. // if (measureEventArgs != null)
  174. // {
  175. // RulerBaseText.Text = measureEventArgs.RulerBase.ToString();
  176. // RulerTranslateText.Text = measureEventArgs.RulerTranslate.ToString();
  177. // RulerTranslateCombo.SelectedIndex = -1;
  178. // RulerBaseUnitCombo.SelectedIndex = -1;
  179. // PrecisionBox.SelectedIndex = -1;
  180. // if (measureEventArgs.RulerBaseUnit == "in")
  181. // {
  182. // RulerBaseUnitCombo.SelectedIndex = 0;
  183. // }
  184. // if (measureEventArgs.RulerBaseUnit == "cm")
  185. // {
  186. // RulerBaseUnitCombo.SelectedIndex = 1;
  187. // }
  188. // if (measureEventArgs.RulerBaseUnit == "mm")
  189. // {
  190. // RulerBaseUnitCombo.SelectedIndex = 2;
  191. // }
  192. // for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
  193. // {
  194. // ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
  195. // if (checkItem != null && checkItem.Content.ToString() == measureEventArgs.RulerTranslateUnit.ToString())
  196. // {
  197. // RulerTranslateCombo.SelectedIndex = i;
  198. // }
  199. // }
  200. // for (int i = 0; i < PrecisionBox.Items.Count; i++)
  201. // {
  202. // ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  203. // if (checkItem != null && checkItem.Content.ToString() == ((decimal)measureEventArgs.Precision).ToString())
  204. // {
  205. // PrecisionBox.SelectedIndex = i;
  206. // }
  207. // }
  208. // }
  209. //}
  210. public void SaveMeasureSetting(CPDFAnnotation annot)
  211. {
  212. MeasureSetting measureSetting = new MeasureSetting();
  213. if (double.TryParse(RulerBaseText.Text, out double ruleBase))
  214. {
  215. measureSetting.RulerBase = ruleBase;
  216. }
  217. if (RulerBaseUnitCombo.SelectedItem != null)
  218. {
  219. ComboBoxItem checkItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  220. measureSetting.RulerBaseUnit = checkItem.Tag.ToString();
  221. }
  222. if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  223. {
  224. measureSetting.RulerTranslate = ruletranBase;
  225. }
  226. if (RulerTranslateCombo.SelectedItem != null)
  227. {
  228. ComboBoxItem checkItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  229. measureSetting.RulerTranslateUnit = checkItem.Tag.ToString();
  230. }
  231. if (PrecisionBox.SelectedValue != null)
  232. {
  233. ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  234. if (double.TryParse(checkItem.Content.ToString(), out double precision))
  235. {
  236. measureSetting.Precision = precision;
  237. }
  238. }
  239. measureSetting.IsShowArea = (bool)AreaCheckBox?.IsChecked;
  240. measureSetting.IsShowLength = (bool)LengthCheckBox?.IsChecked;
  241. UpdateAnnotWithSetting(annot, measureSetting);
  242. }
  243. private void UpdateAnnotWithSetting(CPDFAnnotation annot, MeasureSetting measureSetting)
  244. {
  245. if(annot is CPDFLineAnnotation lineAnnot)
  246. {
  247. if (lineAnnot.IsMersured())
  248. {
  249. CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
  250. CPDFMeasureInfo info = lineMeasure.MeasureInfo;
  251. info.RulerBaseUnit = measureSetting.RulerBaseUnit;
  252. info.RulerBase = (float)measureSetting.RulerBase;
  253. info.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  254. info.RulerTranslate = (float)measureSetting.RulerTranslate;
  255. info.Precision = GetMeasureSavePrecision(measureSetting.Precision);
  256. lineAnnot.GetDistanceMeasure().SetMeasureInfo(info);
  257. lineAnnot.GetDistanceMeasure().SetMeasureScale(info.RulerBase, info.RulerBaseUnit, info.RulerTranslate, info.RulerTranslateUnit);
  258. lineAnnot.GetDistanceMeasure().UpdateAnnotMeasure();
  259. lineAnnot.UpdateAp();
  260. PdfViewControl.UpdateAnnotFrame();
  261. }
  262. }
  263. else if(annot is CPDFPolylineAnnotation polylineAnnot)
  264. {
  265. if (polylineAnnot.IsMersured())
  266. {
  267. CPDFPerimeterMeasure polylineMeasure = polylineAnnot.GetPerimeterMeasure();
  268. CPDFMeasureInfo info = polylineMeasure.MeasureInfo;
  269. info.RulerBaseUnit = measureSetting.RulerBaseUnit;
  270. info.RulerBase = (float)measureSetting.RulerBase;
  271. info.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  272. info.RulerTranslate = (float)measureSetting.RulerTranslate;
  273. info.Precision = GetMeasureSavePrecision(measureSetting.Precision);
  274. polylineAnnot.GetPerimeterMeasure().SetMeasureInfo(info);
  275. polylineAnnot.GetPerimeterMeasure().SetMeasureScale(info.RulerBase, info.RulerBaseUnit, info.RulerTranslate, info.RulerTranslateUnit);
  276. polylineAnnot.GetPerimeterMeasure().UpdateAnnotMeasure();
  277. polylineAnnot.UpdateAp();
  278. PdfViewControl.UpdateAnnotFrame();
  279. }
  280. }
  281. else if(annot is CPDFPolygonAnnotation areaAnnot)
  282. {
  283. if (areaAnnot.IsMersured())
  284. {
  285. CPDFAreaMeasure areaMeasure = areaAnnot.GetAreaMeasure();
  286. CPDFMeasureInfo info = areaMeasure.MeasureInfo;
  287. info.RulerBaseUnit = measureSetting.RulerBaseUnit;
  288. info.RulerBase = (float)measureSetting.RulerBase;
  289. info.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  290. info.RulerTranslate = (float)measureSetting.RulerTranslate;
  291. info.Precision = GetMeasureSavePrecision(measureSetting.Precision);
  292. info.CaptionType = CPDFCaptionType.CPDF_CAPTION_NONE;
  293. if (measureSetting.IsShowArea)
  294. {
  295. info.CaptionType |= CPDFCaptionType.CPDF_CAPTION_AREA;
  296. }
  297. if (measureSetting.IsShowLength)
  298. {
  299. info.CaptionType |= CPDFCaptionType.CPDF_CAPTION_LENGTH;
  300. }
  301. areaAnnot.GetAreaMeasure().SetMeasureInfo(info);
  302. areaAnnot.GetAreaMeasure().SetMeasureScale(info.RulerBase, info.RulerBaseUnit, info.RulerTranslate, info.RulerTranslateUnit);
  303. areaAnnot.GetAreaMeasure().UpdateAnnotMeasure();
  304. areaAnnot.UpdateAp();
  305. PdfViewControl.UpdateAnnotFrame();
  306. }
  307. }
  308. else
  309. {
  310. MeasureSetting setting = PdfViewControl.PDFViewTool.GetMeasureSetting();
  311. setting.RulerBase = measureSetting.RulerBase;
  312. setting.RulerBaseUnit = measureSetting.RulerBaseUnit;
  313. setting.RulerTranslate = measureSetting.RulerTranslate;
  314. setting.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  315. setting.Precision = measureSetting.Precision;
  316. setting.IsShowArea = measureSetting.IsShowArea;
  317. setting.IsShowLength = measureSetting.IsShowLength;
  318. }
  319. }
  320. private double GetMeasureShowPrecision(int precision)
  321. {
  322. if (precision == CPDFMeasure.PRECISION_VALUE_ZERO)
  323. {
  324. return 1;
  325. }
  326. if (CPDFMeasure.PRECISION_VALUE_ONE == precision)
  327. {
  328. return 0.1;
  329. }
  330. if (CPDFMeasure.PRECISION_VALUE_TWO == precision)
  331. {
  332. return 0.01;
  333. }
  334. if (CPDFMeasure.PRECISION_VALUE_THREE == precision)
  335. {
  336. return 0.001;
  337. }
  338. if (CPDFMeasure.PRECISION_VALUE_FOUR == precision)
  339. {
  340. return 0.0001;
  341. }
  342. return 0;
  343. }
  344. public void ChangedCheckBoxIsChecked(bool Area, bool Lenght)
  345. {
  346. AreaCheckBox.IsChecked = Area;
  347. LengthCheckBox.IsChecked = Lenght;
  348. }
  349. }
  350. }