Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.30 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows.Input;
  4. using Passport.GUI.DB;
  5. using System.Data.Entity;
  6. using System.Windows;
  7. using Passport.GUI.ViewModels.Commands;
  8. using Passport.GUI.Models;
  9. using System.Data;
  10. using OxyPlot;
  11. using System.Collections.ObjectModel;
  12. using Passport.GUI.ViewModels.Plots;
  13. using OxyPlot.Axes;
  14. using System;
  15. using OxyPlot.Series;
  16. using OxyPlot.Annotations;
  17. using System.Threading;
  18.  
  19. namespace Passport.GUI.ViewModels
  20. {
  21. class PassportPageViewModel : AbstractTab
  22. {
  23. private bool isReadOnly;
  24. public bool IsReadOnly
  25. {
  26. get
  27. {
  28. return isReadOnly;
  29. }
  30.  
  31. set
  32. {
  33. isReadOnly = value;
  34. if (isReadOnly == false)
  35. {
  36. Name = Name + "..";
  37. }
  38. OnPropertyChanged("IsReadOnly");
  39. }
  40. }
  41.  
  42. public DBPassport Passport { get; set; }
  43.  
  44. private List<DBUklon> uklons;
  45. public List<DBUklon> Uklons
  46. {
  47. get
  48. {
  49. return uklons;
  50. }
  51. set
  52. {
  53. uklons = value;
  54. OnPropertyChanged("Uklons");
  55. }
  56. }
  57.  
  58. private List<DBVSM> vsms;
  59. public List<DBVSM> VSMs
  60. {
  61. get
  62. {
  63. return vsms;
  64. }
  65. set
  66. {
  67. vsms = value;
  68. OnPropertyChanged("VSMs");
  69. }
  70. }
  71.  
  72.  
  73. public ICommand ChangeReadOnlyModeCommand { get; set; }
  74.  
  75. public ICommand SaveToDB { get; set; }
  76.  
  77.  
  78. private void saveToDB(PassportPageViewModel context)
  79. {
  80. using (var db = new PassportDBContext())
  81. {
  82. Passport = context.Passport;
  83. Uklons = context.Uklons;
  84. VSMs = context.VSMs;
  85.  
  86. if (Passport.Id == 0)
  87. {
  88. db.DBPassports.Add(Passport);
  89. }
  90. else
  91. {
  92. db.Entry(Passport).State = EntityState.Modified;
  93. }
  94.  
  95. int Id = Passport.Id;
  96.  
  97. foreach (var u in Uklons)
  98. {
  99. if (u.Id == 0)
  100. {
  101. u.PassportId = Id;
  102. db.DBUklons.Add(u);
  103. }
  104. else
  105. {
  106. db.Entry(u).State = EntityState.Modified;
  107. }
  108. }
  109.  
  110. foreach (var v in VSMs)
  111. {
  112. if (v.Id == 0)
  113. {
  114. v.PassportId = Id;
  115. db.DBVSMs.Add(v);
  116. }
  117. else
  118. {
  119. db.Entry(v).State = EntityState.Modified;
  120. }
  121. }
  122. IsReadOnly = true;
  123. Name = "Участок № " + Passport.AreaNum.ToString();
  124. db.SaveChanges();
  125. context.CleanDirty();
  126. context.CurvSpec = new CurvSpecModel(VSMs);
  127. }
  128. }
  129.  
  130. public bool IsDirty()
  131. {
  132. if (Passport.IsDirty() || !(VSMs.Where(p => p.IsDirty() == true).Count() == 0) || !(Uklons.Where(p => p.IsDirty() == true).Count() == 0))
  133. { return true; }
  134. return false;
  135. }
  136.  
  137. public void CleanDirty()
  138. {
  139. Passport.CleanDirty();
  140.  
  141. foreach (DBVSM vsm in VSMs)
  142. {
  143. vsm.CleanDirty();
  144. }
  145.  
  146. foreach (DBUklon ukl in Uklons)
  147. {
  148. ukl.CleanDirty();
  149. }
  150. }
  151.  
  152. private CurvSpecModel curvSpec;
  153. public CurvSpecModel CurvSpec {
  154. get
  155. {
  156. return curvSpec;
  157. }
  158. set
  159. {
  160. curvSpec = value;
  161. OnPropertyChanged("CurvSpec");
  162. }
  163. }
  164.  
  165. public PlotModel UklonPlotModelZ { get; set; }
  166.  
  167. public PlotModel VsmPlotModel { get; set; }
  168.  
  169. public PlotModel UklonHeightModel { get; set; }
  170.  
  171. public PlotController StaticControl { get; set; }
  172.  
  173. public void testSer(Object o, OxyMouseEventArgs e)
  174. {
  175. //MessageBox.Show("WORKED");
  176. }
  177.  
  178. public PassportPageViewModel()
  179. {
  180. ChangeReadOnlyModeCommand = new RelayCommand(p => { IsReadOnly = true; });
  181.  
  182. Passport = new DBPassport();
  183. Name = "Новый участок";
  184.  
  185. Uklons = new List<DBUklon>();
  186. VSMs = new List<DBVSM>();
  187.  
  188. ///Удалить если работает с ptableviewmodel
  189. SaveToDB = new RelayCommand(p => saveToDB((PassportPageViewModel)p));
  190. }
  191.  
  192.  
  193. //Действующий конструктор
  194. public PassportPageViewModel(DBPassport passport)
  195. {
  196. ChangeReadOnlyModeCommand = new RelayCommand(p => { IsReadOnly = false; });
  197. Passport = passport;
  198. Name = "Участок № " + Passport.AreaNum;
  199.  
  200. using (var db = new PassportDBContext())
  201. {
  202. Uklons = (from u in db.DBUklons where u.PassportId.Equals(Passport.Id) select u).ToList<DBUklon>();
  203. VSMs = (from v in db.DBVSMs where v.PassportId.Equals(Passport.Id) select v).ToList<DBVSM>();
  204. }
  205.  
  206. StaticControl = new CustomUnbindedPlotControl();
  207.  
  208. UklonPlotModelZ = PlotFun.UklonPlotModelL(Uklons, passport.BgnPK);
  209. VsmPlotModel = PlotFun.VSMPlotModelAnn(VSMs, passport.BgnPK);
  210. UklonHeightModel = PlotFun.UklonHeightModelz(Uklons, passport.BgnPK, passport.BgnHeight);
  211. UklonPlotModelZ.Axes[0].AxisChanged += UklonPlotModelZ_XAxisChanged;
  212. UklonHeightModel.Axes[0].AxisChanged += UklonHeightModel_XAxisChanged;
  213.  
  214.  
  215. LineAnnotation pointer1 = new LineAnnotation { Type = LineAnnotationType.Vertical, X = 0, Color = OxyColors.Green };
  216. UklonHeightModel.Annotations.Add(pointer1);
  217.  
  218. LineSeries prevSelectedUklonLine = null;
  219. int selectedUklonSeriesInd = -1;
  220.  
  221. UklonHeightModel.MouseMove += (s, e) =>
  222. {
  223.  
  224. ScreenPoint pos = new ScreenPoint(e.Position.X, e.Position.Y);
  225. LineSeries selectedL = UklonHeightModel.GetSeriesFromPoint(pos) as LineSeries;
  226.  
  227.  
  228. var smth = VsmPlotModel.Axes[0].Transform(0, 0, UklonHeightModel.Axes[1]).Y;
  229. var ind1 = UklonHeightModel.Annotations.IndexOf(pointer1);
  230. var a = (LineAnnotation)UklonHeightModel.Annotations[ind1];
  231. var newx = a.InverseTransform(new ScreenPoint(e.Position.X, e.Position.Y));
  232. if (Math.Abs(a.X - newx.X) > 1)
  233. {
  234. a.Color = OxyColors.Green;
  235. a.X = Math.Round(newx.X);
  236.  
  237. if (UklonHeightModel.Series.IndexOf(selectedL) != -1)
  238. {
  239. selectedUklonSeriesInd = UklonHeightModel.Series.IndexOf(selectedL);
  240. DBUklon v = Uklons[selectedUklonSeriesInd];
  241.  
  242. TrackerHitResult nearestP = UklonHeightModel.Series[0].GetNearestPoint(e.Position, true);
  243. a.Text = "М: " + a.X.ToString() + Environment.NewLine +
  244. "В: " + Math.Round(nearestP.DataPoint.Y, 2).ToString();
  245. a.TextOrientation = AnnotationTextOrientation.Horizontal;
  246. a.TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left;
  247. a.TextPadding = 5;
  248.  
  249.  
  250. if (selectedL != null)
  251. {
  252. selectedL.Color = OxyColors.Red;
  253. selectedL.StrokeThickness = 3;
  254. }
  255.  
  256. if (prevSelectedUklonLine != null && selectedL != prevSelectedUklonLine)
  257. {
  258. prevSelectedUklonLine.Color = OxyColors.Black;
  259. prevSelectedUklonLine.StrokeThickness = 2;
  260. }
  261. prevSelectedUklonLine = selectedL;
  262.  
  263.  
  264. }
  265. UklonHeightModel.InvalidatePlot(false);
  266. }
  267.  
  268. };
  269.  
  270.  
  271. RectangleAnnotation range = null;
  272. double startx = double.NaN;
  273.  
  274. VsmPlotModel.MouseDown += (s, e) =>
  275. {
  276. if (e.ChangedButton == OxyMouseButton.Left && e.IsShiftDown == true)
  277. {
  278. if (range == null)
  279. {
  280. // Create and add the annotation to the plot
  281. range = new RectangleAnnotation { Fill = OxyColors.SkyBlue };
  282. VsmPlotModel.Annotations.Add(range);
  283. VsmPlotModel.InvalidatePlot(true);
  284. }
  285.  
  286. startx = range.InverseTransform(e.Position).X;
  287. range.MinimumX = startx;
  288. range.MaximumX = startx;
  289. VsmPlotModel.InvalidatePlot(true);
  290. e.Handled = true;
  291. }
  292. };
  293.  
  294. VsmPlotModel.MouseMove += (s, e) =>
  295. {
  296. if (e.IsShiftDown == true && !double.IsNaN(startx))
  297. {
  298. var x = range.InverseTransform(e.Position).X;
  299. range.MinimumX = Math.Min(x, startx);
  300. range.MaximumX = Math.Max(x, startx);
  301. range.Text = string.Format("? cos(x) dx = {0:0.00}", Math.Sin(range.MaximumX) - Math.Sin(range.MinimumX));
  302. VsmPlotModel.Subtitle = string.Format("Integrating from {0:0.00} to {1:0.00}", range.MinimumX, range.MaximumX);
  303. VsmPlotModel.InvalidatePlot(true);
  304. e.Handled = true;
  305. }
  306. };
  307.  
  308. VsmPlotModel.MouseUp += (s, e) =>
  309. {
  310. startx = double.NaN;
  311. };
  312.  
  313.  
  314.  
  315. LineAnnotation pointer2 = new LineAnnotation { Type = LineAnnotationType.Vertical, X = 0, Color = OxyColors.Green};
  316. VsmPlotModel.Annotations.Add(pointer2);
  317.  
  318. LineSeries prevSelectedVsmLine = null;
  319. int selectedVSMSeriesInd = -1;
  320.  
  321.  
  322. VsmPlotModel.MouseDown += (s, e) =>
  323. {
  324. if (e.IsShiftDown == true)
  325. {
  326. var re = Axis.InverseTransform(e.Position, VsmPlotModel.Axes[0], VsmPlotModel.Axes[1]);
  327. VsmPlotModel.Annotations.Add(new LineAnnotation()
  328. {
  329. Type = LineAnnotationType.Vertical,
  330. X = re.X
  331. });
  332. }
  333. };
  334.  
  335. VsmPlotModel.MouseMove += testSer;
  336.  
  337.  
  338. VsmPlotModel.MouseMove += (s, e) =>
  339. {
  340. ScreenPoint pos = new ScreenPoint(e.Position.X, VsmPlotModel.Axes[0].Transform(0, 0, VsmPlotModel.Axes[1]).Y);
  341. LineSeries selectedL = (LineSeries)VsmPlotModel.GetSeriesFromPoint(pos);
  342.  
  343. var smth = VsmPlotModel.Axes[0].Transform(0, 0, VsmPlotModel.Axes[1]).Y;
  344. var ind2 = VsmPlotModel.Annotations.IndexOf(pointer2);
  345. var a = (LineAnnotation)VsmPlotModel.Annotations[ind2];
  346. var newx = a.InverseTransform(new ScreenPoint(e.Position.X, e.Position.Y));
  347. if (Math.Abs(a.X - newx.X) > 1)
  348. {
  349. a.Color = OxyColors.Green;
  350. a.X = Math.Round(newx.X);
  351. if (VsmPlotModel.Series.IndexOf(selectedL) != -1)
  352. {
  353. selectedVSMSeriesInd = VsmPlotModel.Series.IndexOf(selectedL);
  354. DBVSM v = VSMs[selectedVSMSeriesInd];
  355. if (v.CurvRad == 0)
  356. {
  357. a.Text = "М: " + a.X.ToString() + System.Environment.NewLine +
  358. "Длина: " + v.Length.ToString() + "м.";
  359. }
  360. else
  361. {
  362. double decimal_degrees = (v.Length * 180.0) / (Math.PI * v.CurvRad);
  363. double rads = Math.PI * decimal_degrees / 180.0;
  364. int sec = (int)Math.Round(decimal_degrees * 3600);
  365. int deg = sec / 3600;
  366. sec = Math.Abs(sec % 3600);
  367. int min = sec / 60;
  368. sec %= 60;
  369. a.Text = "М: " + a.X.ToString() + System.Environment.NewLine +
  370. "Уг: " + deg + '°' + min + '\'' + sec + "\'\'" + System.Environment.NewLine +
  371. "Р: " + v.CurvRad + "м." + System.Environment.NewLine +
  372. "Т: " + Math.Round(v.CurvRad * Math.Tan(rads / 2), 2) + "м." + System.Environment.NewLine +
  373. "L: " + v.Length.ToString() + "м." + System.Environment.NewLine +
  374. "l1: " + v.BgnClothLen.ToString() + "м." + System.Environment.NewLine +
  375. "l2: " + v.EndClothLen.ToString() + "м.";
  376. }
  377.  
  378. if (selectedL != null)
  379. {
  380. selectedL.Color = OxyColors.Red;
  381. selectedL.StrokeThickness = 3;
  382. }
  383.  
  384. if (prevSelectedVsmLine != null && selectedL != prevSelectedVsmLine)
  385. {
  386. prevSelectedVsmLine.Color = OxyColors.Black;
  387. prevSelectedVsmLine.StrokeThickness = 2;
  388. }
  389. prevSelectedVsmLine = selectedL;
  390.  
  391.  
  392.  
  393. if (a.X < selectedL.MinX || a.X > selectedL.MaxX)
  394. { //a.Color = OxyColors.Transparent;
  395. a.Text = "";
  396. selectedL.Color = OxyColors.Black;
  397. selectedL.StrokeThickness = 1;
  398. }
  399.  
  400. };
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408. a.TextOrientation = AnnotationTextOrientation.Horizontal;
  409. a.TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left;
  410. a.TextPadding = 5;
  411. VsmPlotModel.InvalidatePlot(false);
  412. }
  413.  
  414. };
  415.  
  416.  
  417. CurvSpec = new CurvSpecModel(VSMs);
  418.  
  419. SaveToDB = new RelayCommand(p => saveToDB((PassportPageViewModel)p));
  420. }
  421.  
  422.  
  423. private void UklonHeightModel_XAxisChanged(object sender, AxisChangedEventArgs e)
  424. {
  425. if ((UklonHeightModel.Axes[0].ActualMinimum != UklonPlotModelZ.Axes[0].ActualMinimum)
  426. && UklonHeightModel.Axes[0].ActualMaximum != UklonPlotModelZ.Axes[0].ActualMaximum)
  427. {
  428. UklonPlotModelZ.Axes[0].Zoom(UklonHeightModel.Axes[0].ActualMinimum, UklonHeightModel.Axes[0].ActualMaximum);
  429. UklonPlotModelZ.InvalidatePlot(true);
  430. }
  431. }
  432.  
  433. private void UklonPlotModelZ_XAxisChanged(object sender, AxisChangedEventArgs e)
  434. {
  435. if ((UklonPlotModelZ.Axes[0].ActualMinimum != UklonHeightModel.Axes[0].ActualMinimum)
  436. && UklonPlotModelZ.Axes[0].ActualMaximum != UklonHeightModel.Axes[0].ActualMaximum)
  437. {
  438. UklonHeightModel.Axes[0].Zoom(UklonPlotModelZ.Axes[0].ActualMinimum, UklonPlotModelZ.Axes[0].ActualMaximum);
  439. UklonHeightModel.InvalidatePlot(true);
  440. }
  441. }
  442.  
  443.  
  444. public PassportPageViewModel(int passportId)
  445. {
  446. ChangeReadOnlyModeCommand = new RelayCommand(p => { IsReadOnly = false; });
  447.  
  448. using (var db = new PassportDBContext())
  449. {
  450. Passport = (from p in db.DBPassports where p.Id == passportId select p).First<DBPassport>();
  451. Uklons = (from u in db.DBUklons where u.PassportId.Equals(Passport.Id) select u).ToList<DBUklon>();
  452. VSMs = (from v in db.DBVSMs where v.PassportId.Equals(Passport.Id) select v).ToList<DBVSM>();
  453. }
  454. CurvSpec = new CurvSpecModel(VSMs);
  455. Name = "Участок № " + Passport.AreaNum;
  456.  
  457.  
  458. SaveToDB = new RelayCommand(p => saveToDB((PassportPageViewModel)p));
  459. }
  460. }
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement