MeasureControl.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using Compdfkit_Tools.Helper;
  4. using Compdfkit_Tools.PDFControl;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.AnnotEvent;
  7. using ComPDFKitViewer.PdfViewer;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace Compdfkit_Tools.Measure
  25. {
  26. /// <summary>
  27. /// MeasureControl.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class MeasureControl : UserControl
  30. {
  31. public MeasurePropertyControl measurePropertyControl = new MeasurePropertyControl();
  32. public MeasureSettingDialog SettingPanel = new MeasureSettingDialog();
  33. private CPDFDisplaySettingsControl displaySettingsControl;
  34. private PDFViewControl PdfViewControl = new PDFViewControl();
  35. private PanelState panelState = PanelState.GetInstance();
  36. public event EventHandler ExpandEvent;
  37. public MeasureControl()
  38. {
  39. InitializeComponent();
  40. MeasureSetting.MeasureChanged += MeasureSetting_MeasureChanged;
  41. SettingPanel.CancelEvent -= SettingPanel_CancelEvent;
  42. SettingPanel.DoneEvent -= SettingPanel_DoneEvent;
  43. SettingPanel.CancelEvent += SettingPanel_CancelEvent;
  44. SettingPanel.DoneEvent += SettingPanel_DoneEvent;
  45. }
  46. private void MeasureSetting_MeasureChanged(object sender, MeasureEventArgs e)
  47. {
  48. InfoPanel.SetMeasureType(e.Type);
  49. InfoPanel.SetMeasureInfo(e);
  50. }
  51. #region Init PDFViewer
  52. public void InitWithPDFViewer(PDFViewControl pdfViewControl, CPDFViewer pdfViewer)
  53. {
  54. PdfViewControl = pdfViewControl;
  55. PdfViewControl.PDFView = pdfViewer;
  56. PDFMeasureTool.InitWithPDFViewer(pdfViewer, measurePropertyControl, this);
  57. FloatPageTool.InitWithPDFViewer(pdfViewer);
  58. PDFGrid.Child = PdfViewControl;
  59. panelState.PropertyChanged -= PanelState_PropertyChanged;
  60. panelState.PropertyChanged += PanelState_PropertyChanged;
  61. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  62. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  63. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  64. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  65. SetInfoPanelVisble(false, false);
  66. }
  67. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  68. {
  69. switch (e.CommandType)
  70. {
  71. case CommandType.Context:
  72. if (e.CommandTarget == TargetType.Annot && e.PressOnAnnot)
  73. {
  74. e.Handle = true;
  75. e.PopupMenu = new ContextMenu();
  76. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  77. MenuItem menuItem = new MenuItem();
  78. menuItem.Header = "Measurement Settings";
  79. menuItem.Click += (item, param) =>
  80. {
  81. SettingPanel.UpdateArgsList = e.AnnotEventArgsList;
  82. SettingPanel.BindMeasureSetting();
  83. SetInfoPanelVisble(false, true);
  84. };
  85. MenuItem propertyItem = new MenuItem();
  86. propertyItem.Header = "Properties";
  87. propertyItem.Click += (item, param) =>
  88. {
  89. ExpandEvent?.Invoke(this, new EventArgs());
  90. };
  91. e.PopupMenu.Items.Add(menuItem);
  92. e.PopupMenu.Items.Add(propertyItem);
  93. }
  94. break;
  95. case CommandType.Delete:
  96. e.DoCommand();
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  103. {
  104. if (e == null || e.IsAnnotCreateReset)
  105. {
  106. if (e == null)
  107. {
  108. measurePropertyControl?.ClearMeasurePanel();
  109. if(PDFMeasureTool.ToolChecked()==false)
  110. {
  111. SetInfoPanelVisble(false, false);
  112. }
  113. }
  114. return;
  115. }
  116. else
  117. {
  118. switch (e.GetAnnotTypes())
  119. {
  120. case AnnotArgsType.LineMeasure:
  121. LineMeasureArgs LineArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.LineMeasure).First() as LineMeasureArgs;
  122. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  123. measurePropertyControl.SetPropertyForMeasureCreate(LineArgs, e);
  124. SetInfoPanelVisble(true, false);
  125. SetMeasureInfoPanel(LineArgs.GetPDFAnnot(), LineArgs);
  126. break;
  127. case AnnotArgsType.PolygonMeasure:
  128. PolygonMeasureArgs polygonArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.PolygonMeasure).First() as PolygonMeasureArgs;
  129. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  130. measurePropertyControl.SetPropertyForMeasureCreate(polygonArgs, e);
  131. SetInfoPanelVisble(true, false);
  132. SetMeasureInfoPanel(polygonArgs.GetPDFAnnot(),polygonArgs);
  133. break;
  134. case AnnotArgsType.PolyLineMeasure:
  135. PolyLineMeasureArgs polyLineArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.PolyLineMeasure).First() as PolyLineMeasureArgs;
  136. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  137. measurePropertyControl.SetPropertyForMeasureCreate(polyLineArgs, e);
  138. SetInfoPanelVisble(true, false);
  139. SetMeasureInfoPanel(polyLineArgs.GetPDFAnnot(),polyLineArgs);
  140. break;
  141. }
  142. }
  143. }
  144. private void SetMeasureInfoPanel(CPDFAnnotation rawAnnot,AnnotHandlerEventArgs annotArgs=null)
  145. {
  146. if (rawAnnot == null)
  147. {
  148. return;
  149. }
  150. try
  151. {
  152. if (rawAnnot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  153. {
  154. CPDFLineAnnotation lineAnnot = rawAnnot as CPDFLineAnnotation;
  155. if (lineAnnot.IsMersured() && lineAnnot.Points != null && lineAnnot.Points.Count() == 2)
  156. {
  157. CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
  158. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  159. Vector standVector = new Vector(1, 0);
  160. Point startPoint = new Point(lineAnnot.Points[0].x, lineAnnot.Points[0].y);
  161. Point endPoint = new Point(lineAnnot.Points[1].x, lineAnnot.Points[1].y);
  162. Vector movevector = endPoint - startPoint;
  163. double inch = movevector.Length / 72D;
  164. double rate = measureInfo.RulerTranslate / measureInfo.RulerBase;
  165. double ratio = GetMeasureRatio(measureInfo.RulerBaseUnit);
  166. double showLenght = inch * rate / ratio;
  167. MeasureEventArgs measureEvent = new MeasureEventArgs();
  168. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(movevector, standVector));
  169. measureEvent.RulerTranslateUnit = measureInfo.RulerTranslateUnit;
  170. measureEvent.RulerTranslate = measureInfo.RulerTranslate;
  171. measureEvent.RulerBase = measureInfo.RulerBase;
  172. measureEvent.RulerBaseUnit = measureInfo.RulerBaseUnit;
  173. measureEvent.MousePos = new Point(
  174. (int)Math.Abs(movevector.X),
  175. (int)Math.Abs(movevector.Y));
  176. measureEvent.Type = CPDFMeasureType.CPDF_DISTANCE_MEASURE;
  177. NumberFormatInfo formatInfo = new NumberFormatInfo();
  178. formatInfo.NumberDecimalDigits = Math.Abs(measureInfo.Precision).ToString().Length - 1;
  179. measureEvent.Distance = showLenght.ToString("N", formatInfo) + measureInfo.RulerTranslateUnit;
  180. measureEvent.Precision = GetMeasureShowPrecision(measureInfo.Precision);
  181. MeasureSetting.InvokeMeasureChangeEvent(this, measureEvent);
  182. if(annotArgs!=null)
  183. {
  184. SettingPanel.UpdateArgsList =new List<AnnotHandlerEventArgs> { annotArgs};
  185. }
  186. }
  187. }
  188. if (rawAnnot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE)
  189. {
  190. CPDFPolylineAnnotation polylineAnnot = rawAnnot as CPDFPolylineAnnotation;
  191. if (polylineAnnot.IsMersured() && polylineAnnot.Points != null && polylineAnnot.Points.Count() >= 2)
  192. {
  193. double totalInch = 0;
  194. for (int i = 0; i < polylineAnnot.Points.Count - 1; i++)
  195. {
  196. Point endLinePoint = new Point(
  197. polylineAnnot.Points[i + 1].x,
  198. polylineAnnot.Points[i + 1].y
  199. );
  200. Point startLinePoint = new Point(
  201. polylineAnnot.Points[i].x,
  202. polylineAnnot.Points[i].y
  203. );
  204. Vector subVector = endLinePoint - startLinePoint;
  205. totalInch += subVector.Length;
  206. }
  207. totalInch = totalInch / 72D;
  208. CPDFPerimeterMeasure lineMeasure = polylineAnnot.GetPerimeterMeasure();
  209. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  210. double rate = measureInfo.RulerTranslate / measureInfo.RulerBase;
  211. double ratio = GetMeasureRatio(measureInfo.RulerBaseUnit);
  212. double showLenght = totalInch * rate / ratio;
  213. MeasureEventArgs measureEvent = new MeasureEventArgs();
  214. measureEvent.Angle = 0;
  215. //if (polylineAnnot.Points.Count() >= 3)
  216. //{
  217. // int count = polylineAnnot.Points.Count();
  218. // Point endLinePoint = new Point(
  219. // polylineAnnot.Points[count - 1].x,
  220. // polylineAnnot.Points[count - 1].y
  221. // );
  222. // Point cornerLinePoint = new Point(
  223. // polylineAnnot.Points[count - 2].x,
  224. // polylineAnnot.Points[count - 2].y
  225. // );
  226. // Point startLinePoint = new Point(
  227. // polylineAnnot.Points[count - 3].x,
  228. // polylineAnnot.Points[count - 3].y
  229. // );
  230. // Vector startVector = startLinePoint - cornerLinePoint;
  231. // Vector endvector = endLinePoint - cornerLinePoint;
  232. // measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, startVector));
  233. //}
  234. measureEvent.RulerTranslateUnit = measureInfo.RulerTranslateUnit;
  235. measureEvent.RulerTranslate = measureInfo.RulerTranslate;
  236. measureEvent.RulerBase = measureInfo.RulerBase;
  237. measureEvent.RulerBaseUnit = measureInfo.RulerBaseUnit;
  238. measureEvent.Precision = GetMeasureShowPrecision(measureInfo.Precision);
  239. measureEvent.Type = CPDFMeasureType.CPDF_PERIMETER_MEASURE;
  240. NumberFormatInfo formatInfo = new NumberFormatInfo();
  241. formatInfo.NumberDecimalDigits = Math.Abs(measureInfo.Precision).ToString().Length - 1;
  242. measureEvent.Distance = showLenght.ToString("N", formatInfo) + measureInfo.RulerTranslateUnit;
  243. MeasureSetting.InvokeMeasureChangeEvent(this, measureEvent);
  244. if (annotArgs != null)
  245. {
  246. SettingPanel.UpdateArgsList = new List<AnnotHandlerEventArgs> { annotArgs };
  247. }
  248. }
  249. }
  250. if(rawAnnot.Type== C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON)
  251. {
  252. CPDFPolygonAnnotation Annot = rawAnnot as CPDFPolygonAnnotation;
  253. CPDFCaptionType CaptionType = Annot.GetAreaMeasure().MeasureInfo.CaptionType;
  254. bool IsArea = false;
  255. bool IsLength = false;
  256. if ((CaptionType& CPDFCaptionType.CPDF_CAPTION_AREA)== CPDFCaptionType.CPDF_CAPTION_AREA)
  257. {
  258. IsArea = true;
  259. }
  260. if ((CaptionType & CPDFCaptionType.CPDF_CAPTION_LENGTH) == CPDFCaptionType.CPDF_CAPTION_LENGTH)
  261. {
  262. IsLength = true;
  263. }
  264. SettingPanel.ChangedCheckBoxIsChecked(IsArea, IsLength);
  265. double Area = Annot.GetMeasureValue(CPDFCaptionType.CPDF_CAPTION_AREA);
  266. double inch = MeasureSetting.GetMeasurePDFArea(Area);
  267. double Length = Annot.GetMeasureValue(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  268. double currentInch = MeasureSetting.GetMeasurePDFLength(Length);
  269. MeasureEventArgs measureEvent = new MeasureEventArgs();
  270. measureEvent.RulerTranslateUnit = MeasureSetting.RulerTranslateUnit;
  271. measureEvent.RulerTranslate = MeasureSetting.RulerTranslate;
  272. measureEvent.RulerBase = MeasureSetting.RulerBase;
  273. measureEvent.RulerBaseUnit = MeasureSetting.RulerBaseUnit;
  274. measureEvent.Precision = MeasureSetting.Precision;
  275. measureEvent.Type = CPDFMeasureType.CPDF_AREA_MEASURE;
  276. measureEvent.Distance = string.Format(
  277. "{0}{1}",
  278. MeasureSetting.GetPrecisionData(currentInch),
  279. MeasureSetting.RulerTranslateUnit);
  280. measureEvent.Area = string.Format(
  281. "{0} sq {1}",
  282. MeasureSetting.GetPrecisionData(inch),
  283. MeasureSetting.RulerTranslateUnit);
  284. MeasureSetting.InvokeMeasureChangeEvent(this, measureEvent);
  285. if (annotArgs != null)
  286. {
  287. SettingPanel.UpdateArgsList = new List<AnnotHandlerEventArgs> { annotArgs };
  288. }
  289. }
  290. }
  291. catch (Exception e)
  292. {
  293. }
  294. }
  295. private double GetMeasureRatio(string baseUnit)
  296. {
  297. if (baseUnit == CPDFMeasure.CPDF_PT)
  298. {
  299. return 1 / 72;
  300. }
  301. if (baseUnit == CPDFMeasure.CPDF_IN)
  302. {
  303. return 1;
  304. }
  305. if (baseUnit == CPDFMeasure.CPDF_MM)
  306. {
  307. return 1 / 25.4;
  308. }
  309. if (baseUnit == CPDFMeasure.CPDF_CM)
  310. {
  311. return 1 / 2.54;
  312. }
  313. if (baseUnit == CPDFMeasure.CPDF_M)
  314. {
  315. return 1 / 0.0254;
  316. }
  317. if (baseUnit == CPDFMeasure.CPDFO_KM)
  318. {
  319. return 1 / 0.0254 / 1000;
  320. }
  321. if (baseUnit == CPDFMeasure.CPDF_FT)
  322. {
  323. return 12;
  324. }
  325. if (baseUnit == CPDFMeasure.CPDF_YD)
  326. {
  327. return 36;
  328. }
  329. if (baseUnit == CPDFMeasure.CPDF_MI)
  330. {
  331. return 63360;
  332. }
  333. return 0;
  334. }
  335. private double GetMeasureShowPrecision(int precision)
  336. {
  337. if (precision == CPDFMeasure.PRECISION_VALUE_ZERO)
  338. {
  339. return 1;
  340. }
  341. if (CPDFMeasure.PRECISION_VALUE_ONE == precision)
  342. {
  343. return 0.1;
  344. }
  345. if (CPDFMeasure.PRECISION_VALUE_TWO == precision)
  346. {
  347. return 0.01;
  348. }
  349. if (CPDFMeasure.PRECISION_VALUE_THREE == precision)
  350. {
  351. return 0.001;
  352. }
  353. if (CPDFMeasure.PRECISION_VALUE_FOUR == precision)
  354. {
  355. return 0.0001;
  356. }
  357. return 0;
  358. }
  359. public void SetSettingsControl(CPDFDisplaySettingsControl cPDFDisplaySettingsControl)
  360. {
  361. displaySettingsControl = cPDFDisplaySettingsControl;
  362. }
  363. public void ClearAllToolState()
  364. {
  365. PDFMeasureTool.ClearAllToolState();
  366. }
  367. public void ClearViewerControl()
  368. {
  369. PDFGrid.Child = null;
  370. BotaContainer.Child = null;
  371. PropertyContainer.Child = null;
  372. displaySettingsControl = null;
  373. }
  374. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  375. {
  376. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  377. {
  378. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  379. }
  380. else if (e.PropertyName == nameof(PanelState.RightPanel))
  381. {
  382. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  383. {
  384. ExpandRightPropertyPanel(measurePropertyControl, Visibility.Visible);
  385. }
  386. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  387. {
  388. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  389. }
  390. else
  391. {
  392. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  393. }
  394. }
  395. }
  396. #endregion
  397. #region Expand and collapse Panel
  398. public void ExpandRightPropertyPanel(Visibility visible)
  399. {
  400. ExpandRightPropertyPanel(measurePropertyControl, visible);
  401. }
  402. public void ExpandNullRightPropertyPanel(Visibility visible)
  403. {
  404. ExpandRightPropertyPanel(null, visible);
  405. }
  406. public void ExpandViewSettings(Visibility visible)
  407. {
  408. SetViewSettings(displaySettingsControl, visible);
  409. }
  410. private void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  411. {
  412. PropertyContainer.Width = 260;
  413. PropertyContainer.Child = propertytPanel;
  414. PropertyContainer.Visibility = visible;
  415. }
  416. private void SetViewSettings(CPDFDisplaySettingsControl displaySettingsControl, Visibility visibility)
  417. {
  418. PropertyContainer.Child = displaySettingsControl;
  419. PropertyContainer.Visibility = visibility;
  420. }
  421. public void ExpandLeftPanel(bool isExpand)
  422. {
  423. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  424. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  425. if (isExpand)
  426. {
  427. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  428. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  429. }
  430. else
  431. {
  432. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  433. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  434. }
  435. }
  436. #endregion
  437. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  438. {
  439. }
  440. private void MeasureInfoPanel_SettingClick(object sender, EventArgs e)
  441. {
  442. SettingPanel.ReturnToInfoPanel = true;
  443. SetInfoPanelVisble(false, true);
  444. SettingPanel.BindMeasureSetting();
  445. }
  446. private void SettingPanel_CancelEvent(object sender, EventArgs e)
  447. {
  448. SetInfoPanelVisble(SettingPanel.ReturnToInfoPanel, false);
  449. }
  450. private void SettingPanel_DoneEvent(object sender, EventArgs e)
  451. {
  452. SetInfoPanelVisble(SettingPanel.ReturnToInfoPanel, false);
  453. }
  454. public void SetInfoPanelVisble(bool measureInfo, bool measureSetting)
  455. {
  456. if (measureInfo)
  457. {
  458. InfoPanel.ClearMeasureInfo();
  459. }
  460. InfoPanel.Visibility = measureInfo ? Visibility.Visible : Visibility.Collapsed;
  461. SettingPanel.Visibility = measureSetting ? Visibility.Visible : Visibility.Collapsed;
  462. if(SettingPanel.Visibility == Visibility.Visible)
  463. {
  464. SettingPanel.Owner=Window.GetWindow(this);
  465. SettingPanel.ShowDialog();
  466. }
  467. }
  468. public void SetMeasureInfoType(CPDFMeasureType measureType)
  469. {
  470. InfoPanel?.SetMeasureType(measureType);
  471. }
  472. public void SetMeasureScale(CPDFMeasureType measureType, string scale)
  473. {
  474. InfoPanel?.SetMeasureScale(measureType,scale);
  475. }
  476. }
  477. }