MeasureSettingPanel.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. namespace Compdfkit_Tools.Measure
  10. {
  11. /// <summary>
  12. /// MeasureSettingPanel.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class MeasureSettingPanel : UserControl
  15. {
  16. public event EventHandler CancelEvent;
  17. public event EventHandler DoneEvent;
  18. public PDFViewControl PdfViewControl { get; set; }
  19. public bool ReturnToInfoPanel { get; set; }
  20. //public List<AnnotHandlerEventArgs> UpdateArgsList { get; set; } = new List<AnnotHandlerEventArgs>();
  21. public MeasureSettingPanel()
  22. {
  23. InitializeComponent();
  24. }
  25. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  26. {
  27. //if (UpdateArgsList != null && UpdateArgsList.Count > 0)
  28. //{
  29. // ReturnToInfoPanel = false;
  30. //}
  31. //UpdateArgsList?.Clear();
  32. CancelEvent?.Invoke(this, e);
  33. ReturnToInfoPanel = false;
  34. }
  35. public void ShowAreaAndLength(Visibility visibility)
  36. {
  37. if (visibility == Visibility.Visible)
  38. {
  39. SettingPanel.Height = 350;
  40. }
  41. else
  42. {
  43. SettingPanel.Height = 290;
  44. }
  45. AreaAndLength.Visibility = visibility;
  46. }
  47. private void ButtonDone_Click(object sender, RoutedEventArgs e)
  48. {
  49. if (string.IsNullOrEmpty(RulerBaseText.Text) || string.IsNullOrEmpty(RulerTranslateText.Text))
  50. {
  51. MessageBox.Show("Scale is not greater than zero");
  52. return;
  53. }
  54. if (double.TryParse(RulerBaseText.Text, out double ruleBase))
  55. {
  56. if (ruleBase <= 0)
  57. {
  58. MessageBox.Show("Scale is not greater than zero");
  59. return;
  60. }
  61. }
  62. if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  63. {
  64. if (ruletranBase <= 0)
  65. {
  66. MessageBox.Show("Scale is not greater than zero");
  67. return;
  68. }
  69. }
  70. //if (UpdateArgsList != null && UpdateArgsList.Count == 0)
  71. //{
  72. // SaveMeasureSetting();
  73. //}
  74. //if (UpdateArgsList != null && UpdateArgsList.Count > 0)
  75. //{
  76. // List<AnnotArgsType> allowTypeList = new List<AnnotArgsType>()
  77. // {
  78. // AnnotArgsType.LineMeasure,
  79. // AnnotArgsType.PolyLineMeasure,
  80. // AnnotArgsType.PolygonMeasure
  81. // };
  82. // foreach (AnnotHandlerEventArgs args in UpdateArgsList)
  83. // {
  84. // if (allowTypeList.Contains(args.EventType))
  85. // {
  86. // CPDFAnnotation pdfAnnot = args.GetPDFAnnot();
  87. // switch (pdfAnnot.Type)
  88. // {
  89. // case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  90. // {
  91. // CPDFLineAnnotation lineAnnot = (CPDFLineAnnotation)pdfAnnot;
  92. // if (lineAnnot.IsMersured())
  93. // {
  94. // CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
  95. // CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  96. // if (PrecisionBox.SelectedValue != null)
  97. // {
  98. // ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  99. // if (double.TryParse(checkItem.Content.ToString(), out double precision))
  100. // {
  101. // measureInfo.Precision = GetMeasureSavePrecision(precision);
  102. // }
  103. // }
  104. // if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
  105. // {
  106. // measureInfo.RulerBase = (float)ruleBasedata;
  107. // }
  108. // if (RulerBaseUnitCombo.SelectedItem != null)
  109. // {
  110. // ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  111. // measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
  112. // }
  113. // if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
  114. // {
  115. // measureInfo.RulerTranslate = (float)ruletranBasedata;
  116. // }
  117. // if (RulerTranslateCombo.SelectedItem != null)
  118. // {
  119. // ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  120. // measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
  121. // }
  122. // lineMeasure.SetMeasureInfo(measureInfo);
  123. // lineMeasure.SetMeasureScale(
  124. // measureInfo.RulerBase,
  125. // measureInfo.RulerBaseUnit,
  126. // measureInfo.RulerTranslate,
  127. // measureInfo.RulerTranslateUnit);
  128. // lineMeasure.UpdateAnnotMeasure();
  129. // lineAnnot.UpdateAp();
  130. // args.Draw();
  131. // if (PdfViewControl != null && PdfViewControl.PDFView != null)
  132. // {
  133. // CPDFViewer viewer = PdfViewControl.PDFView;
  134. // if (viewer != null && viewer.UndoManager != null)
  135. // {
  136. // viewer.UndoManager.CanSave = true;
  137. // }
  138. // }
  139. // }
  140. // }
  141. // break;
  142. // case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  143. // {
  144. // CPDFPolylineAnnotation polylineAnnot = (CPDFPolylineAnnotation)pdfAnnot;
  145. // if (polylineAnnot.IsMersured())
  146. // {
  147. // CPDFPerimeterMeasure polylineMeasure = polylineAnnot.GetPerimeterMeasure();
  148. // CPDFMeasureInfo measureInfo = polylineMeasure.MeasureInfo;
  149. // if (PrecisionBox.SelectedValue != null)
  150. // {
  151. // ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  152. // if (double.TryParse(checkItem.Content.ToString(), out double precision))
  153. // {
  154. // measureInfo.Precision = GetMeasureSavePrecision(precision);
  155. // }
  156. // }
  157. // if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
  158. // {
  159. // measureInfo.RulerBase = (float)ruleBasedata;
  160. // }
  161. // if (RulerBaseUnitCombo.SelectedItem != null)
  162. // {
  163. // ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  164. // measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
  165. // }
  166. // if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
  167. // {
  168. // measureInfo.RulerTranslate = (float)ruletranBasedata;
  169. // }
  170. // if (RulerTranslateCombo.SelectedItem != null)
  171. // {
  172. // ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  173. // measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
  174. // }
  175. // polylineMeasure.SetMeasureInfo(measureInfo);
  176. // polylineMeasure.SetMeasureScale(
  177. // measureInfo.RulerBase,
  178. // measureInfo.RulerBaseUnit,
  179. // measureInfo.RulerTranslate,
  180. // measureInfo.RulerTranslateUnit);
  181. // polylineMeasure.UpdateAnnotMeasure();
  182. // polylineAnnot.UpdateAp();
  183. // args.Draw();
  184. // if (PdfViewControl != null && PdfViewControl.PDFView != null)
  185. // {
  186. // CPDFViewer viewer = PdfViewControl.PDFView;
  187. // if (viewer != null && viewer.UndoManager != null)
  188. // {
  189. // viewer.UndoManager.CanSave = true;
  190. // }
  191. // }
  192. // }
  193. // }
  194. // break;
  195. // case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  196. // {
  197. // CPDFPolygonAnnotation areaAnnot = (CPDFPolygonAnnotation)pdfAnnot;
  198. // if (areaAnnot.IsMersured())
  199. // {
  200. // CPDFAreaMeasure areaMeasure = areaAnnot.GetAreaMeasure();
  201. // CPDFMeasureInfo measureInfo = areaMeasure.MeasureInfo;
  202. // if (PrecisionBox.SelectedValue != null)
  203. // {
  204. // ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  205. // if (double.TryParse(checkItem.Content.ToString(), out double precision))
  206. // {
  207. // measureInfo.Precision = GetMeasureSavePrecision(precision);
  208. // }
  209. // }
  210. // if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
  211. // {
  212. // measureInfo.RulerBase = (float)ruleBasedata;
  213. // }
  214. // if (RulerBaseUnitCombo.SelectedItem != null)
  215. // {
  216. // ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  217. // measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
  218. // }
  219. // if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
  220. // {
  221. // measureInfo.RulerTranslate = (float)ruletranBasedata;
  222. // }
  223. // if (RulerTranslateCombo.SelectedItem != null)
  224. // {
  225. // ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  226. // measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
  227. // }
  228. // measureInfo.CaptionType = CPDFCaptionType.CPDF_CAPTION_NONE;
  229. // if ((bool)AreaCheckBox.IsChecked)
  230. // {
  231. // measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_AREA;
  232. // }
  233. // if ((bool)LengthCheckBox.IsChecked)
  234. // {
  235. // measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_LENGTH;
  236. // }
  237. // areaMeasure.SetMeasureInfo(measureInfo);
  238. // areaMeasure.SetMeasureScale(
  239. // measureInfo.RulerBase,
  240. // measureInfo.RulerBaseUnit,
  241. // measureInfo.RulerTranslate,
  242. // measureInfo.RulerTranslateUnit);
  243. // areaMeasure.UpdateAnnotMeasure();
  244. // areaAnnot.UpdateAp();
  245. // args.Draw();
  246. // if(PdfViewControl!=null && PdfViewControl.PDFView!=null)
  247. // {
  248. // CPDFViewer viewer = PdfViewControl.PDFView;
  249. // if(viewer!=null && viewer.UndoManager!=null)
  250. // {
  251. // viewer.UndoManager.CanSave = true;
  252. // }
  253. // }
  254. // }
  255. // }
  256. // break;
  257. // default:
  258. // break;
  259. // }
  260. // }
  261. // }
  262. // ReturnToInfoPanel = false;
  263. //}
  264. //UpdateArgsList?.Clear();
  265. DoneEvent?.Invoke(this, e);
  266. ReturnToInfoPanel = false;
  267. }
  268. private int GetMeasureSavePrecision(double Precision)
  269. {
  270. if (Precision == 1)
  271. {
  272. return CPDFMeasure.PRECISION_VALUE_ZERO;
  273. }
  274. if (Precision == 0.1)
  275. {
  276. return CPDFMeasure.PRECISION_VALUE_ONE;
  277. }
  278. if (Precision == 0.01)
  279. {
  280. return CPDFMeasure.PRECISION_VALUE_TWO;
  281. }
  282. if (Precision == 0.001)
  283. {
  284. return CPDFMeasure.PRECISION_VALUE_THREE;
  285. }
  286. if (Precision == 0.0001)
  287. {
  288. return CPDFMeasure.PRECISION_VALUE_FOUR;
  289. }
  290. return 0;
  291. }
  292. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  293. {
  294. List<Key> allowKeys = new List<Key>()
  295. {
  296. Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
  297. Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
  298. Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right,
  299. Key.OemPeriod,Key.Decimal
  300. };
  301. if (allowKeys.Contains(e.Key) == false)
  302. {
  303. e.Handled = true;
  304. }
  305. }
  306. private void TextBox_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  307. {
  308. try
  309. {
  310. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  311. {
  312. string checkText = Clipboard.GetText();
  313. if (int.TryParse(checkText, out int value))
  314. {
  315. e.CanExecute = true;
  316. }
  317. e.Handled = true;
  318. }
  319. }
  320. catch (Exception ex)
  321. {
  322. }
  323. }
  324. public void BindMeasureSetting()
  325. {
  326. //if (UpdateArgsList != null && UpdateArgsList.Count>0)
  327. //{
  328. // return;
  329. //}
  330. //RulerBaseText.Text = MeasureSetting.RulerBase.ToString();
  331. //RulerTranslateText.Text = MeasureSetting.RulerTranslate.ToString();
  332. //RulerTranslateCombo.SelectedIndex = -1;
  333. //RulerBaseUnitCombo.SelectedIndex = -1;
  334. //PrecisionBox.SelectedIndex = -1;
  335. //if (MeasureSetting.RulerBaseUnit == "in")
  336. //{
  337. // RulerBaseUnitCombo.SelectedIndex = 0;
  338. //}
  339. //if (MeasureSetting.RulerBaseUnit == "cm")
  340. //{
  341. // RulerBaseUnitCombo.SelectedIndex = 1;
  342. //}
  343. //if (MeasureSetting.RulerBaseUnit == "mm")
  344. //{
  345. // RulerBaseUnitCombo.SelectedIndex = 2;
  346. //}
  347. //for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
  348. //{
  349. // ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
  350. // if (checkItem != null && checkItem.Content.ToString() == MeasureSetting.RulerTranslateUnit.ToString())
  351. // {
  352. // RulerTranslateCombo.SelectedIndex = i;
  353. // }
  354. //}
  355. //for (int i = 0; i < PrecisionBox.Items.Count; i++)
  356. //{
  357. // ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  358. // if (checkItem != null && checkItem.Content.ToString() == ((decimal)MeasureSetting.Precision).ToString())
  359. // {
  360. // PrecisionBox.SelectedIndex = i;
  361. // }
  362. //}
  363. }
  364. //public void BindMeasureSetting(MeasureEventArgs measureEventArgs)
  365. //{
  366. // if (measureEventArgs != null)
  367. // {
  368. // RulerBaseText.Text = measureEventArgs.RulerBase.ToString();
  369. // RulerTranslateText.Text = measureEventArgs.RulerTranslate.ToString();
  370. // RulerTranslateCombo.SelectedIndex = -1;
  371. // RulerBaseUnitCombo.SelectedIndex = -1;
  372. // PrecisionBox.SelectedIndex = -1;
  373. // if (measureEventArgs.RulerBaseUnit == "in")
  374. // {
  375. // RulerBaseUnitCombo.SelectedIndex = 0;
  376. // }
  377. // if (measureEventArgs.RulerBaseUnit == "cm")
  378. // {
  379. // RulerBaseUnitCombo.SelectedIndex = 1;
  380. // }
  381. // if (measureEventArgs.RulerBaseUnit == "mm")
  382. // {
  383. // RulerBaseUnitCombo.SelectedIndex = 2;
  384. // }
  385. // for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
  386. // {
  387. // ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
  388. // if (checkItem != null && checkItem.Content.ToString() == measureEventArgs.RulerTranslateUnit.ToString())
  389. // {
  390. // RulerTranslateCombo.SelectedIndex = i;
  391. // }
  392. // }
  393. // for (int i = 0; i < PrecisionBox.Items.Count; i++)
  394. // {
  395. // ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  396. // if (checkItem != null && checkItem.Content.ToString() == ((decimal)measureEventArgs.Precision).ToString())
  397. // {
  398. // PrecisionBox.SelectedIndex = i;
  399. // }
  400. // }
  401. // }
  402. //}
  403. //private void SaveMeasureSetting()
  404. //{
  405. // if (double.TryParse(RulerBaseText.Text, out double ruleBase))
  406. // {
  407. // MeasureSetting.RulerBase = ruleBase;
  408. // }
  409. // if (RulerBaseUnitCombo.SelectedItem != null)
  410. // {
  411. // ComboBoxItem checkItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  412. // MeasureSetting.RulerBaseUnit = checkItem.Content.ToString();
  413. // }
  414. // if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  415. // {
  416. // MeasureSetting.RulerTranslate = ruletranBase;
  417. // }
  418. // if (RulerTranslateCombo.SelectedItem != null)
  419. // {
  420. // ComboBoxItem checkItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  421. // MeasureSetting.RulerTranslateUnit = checkItem.Content.ToString();
  422. // }
  423. // if (PrecisionBox.SelectedValue != null)
  424. // {
  425. // ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  426. // if (double.TryParse(checkItem.Content.ToString(), out double precision))
  427. // {
  428. // MeasureSetting.Precision = precision;
  429. // }
  430. // }
  431. // MeasureSetting.IsShowArea = (bool)AreaCheckBox.IsChecked;
  432. // MeasureSetting.IsShowLength = (bool)LengthCheckBox.IsChecked;
  433. //}
  434. public void ChangedCheckBoxIsChecked(bool Area, bool Lenght)
  435. {
  436. AreaCheckBox.IsChecked = Area;
  437. LengthCheckBox.IsChecked = Lenght;
  438. }
  439. }
  440. }