MeasureSettingPanel.xaml.cs 12 KB

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