FlightEditor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using DataSimulation.Repostory;
  2. using DataSimulation.Repostory.EFContext;
  3. using DataSimulation.Repostory.Model;
  4. using DevExpress.Utils.About;
  5. using DevExpress.XtraEditors.Controls;
  6. using DevExpress.XtraEditors.DXErrorProvider;
  7. using DxHelper;
  8. using ExtensionsDev;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Data.Entity;
  14. using System.Data.Entity.Migrations;
  15. using System.Drawing;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows.Forms;
  20. namespace DataSimulation.Forms.EditForms
  21. {
  22. public partial class FlightEditor : DevExpress.XtraEditors.XtraForm
  23. {
  24. public SimulationInfo info;
  25. private List<SimulationInfo> simulationInfos;
  26. public FlightEditor(List<SimulationInfo> simulationInfos)
  27. {
  28. InitializeComponent();
  29. this.Text = "添加航迹";
  30. this.simulationInfos = simulationInfos;
  31. this.StartPosition = FormStartPosition.CenterParent;
  32. }
  33. public FlightEditor(List<SimulationInfo> simulationInfos, SimulationInfo info)
  34. : this(simulationInfos)
  35. {
  36. this.Text = "编辑航迹";
  37. this.info = info;
  38. }
  39. private async void Editor_Load(object sender, EventArgs e)
  40. {
  41. mapControl.UseDefalutOptions()
  42. .UseClearAll()
  43. .UseDistanceLine()
  44. .UseMarkDot()
  45. .UseExportImg()
  46. .UseExportXlsx()
  47. .UseExportCsv()
  48. .UseExportFlightLine()
  49. .SetMapLayerType(null)
  50. .UseDrawRect(rect =>
  51. {
  52. (double startLon, double startLat, double centerLon, double centerLat, double endLon, double endLat, double lonRange, double latRange) = rect;
  53. });
  54. txtFlightName.EditValueChanged += TxtFlightName_EditValueChanged;
  55. txtSpeed.EditValueChanged += TxtSpeed_EditValueChanged;
  56. if (this.Text == "编辑航迹" && info != null)
  57. {
  58. this.txtFlightName.Text = info.SimulationName;
  59. this.txtSpeed.Text = $"{info.SimulationSpeed}";
  60. var points = await SimulationCache.GetAllByIDAsync(info.ID, info.CreateTime);
  61. var flinfo = new FlightInfo(info.SimulationName, info.SimulationSpeed);
  62. points.ForEach(m => flinfo.flights.Add(new FlightData(m.SimulationLon, m.SimulationLat)));
  63. mapControl.SetFlightLine(new FlightInfo[1] { flinfo });
  64. layoutControlItem12.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  65. layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  66. }
  67. }
  68. private void TxtSpeed_EditValueChanged(object sender, EventArgs e)
  69. {
  70. double speed;
  71. if (!double.TryParse(txtSpeed.Text, out speed))
  72. {
  73. dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(m/s)");
  74. return;
  75. }
  76. if (speed <= 0)
  77. {
  78. dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0(m/s)");
  79. return;
  80. }
  81. dxErrorProvider.SetError(txtSpeed, string.Empty);
  82. }
  83. private void TxtFlightName_EditValueChanged(object sender, EventArgs e)
  84. {
  85. string Name = txtFlightName.Text.Trim();
  86. if (string.IsNullOrEmpty(Name))
  87. {
  88. dxErrorProvider.SetError(txtFlightName, "请设置航迹名称");
  89. return;
  90. }
  91. if (simulationInfos.Any(m => m.SimulationName == Name))
  92. {
  93. dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在");
  94. return;
  95. }
  96. dxErrorProvider.SetError(txtFlightName, string.Empty);
  97. }
  98. private bool Validators()
  99. {
  100. dxErrorProvider.ClearErrors();
  101. string Name = txtFlightName.Text.Trim();
  102. if (string.IsNullOrEmpty(Name))
  103. {
  104. dxErrorProvider.SetError(txtFlightName, "请设置航迹名称");
  105. return false;
  106. }
  107. if (simulationInfos.Any(m => m.SimulationName == Name))
  108. {
  109. dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在");
  110. return false;
  111. }
  112. double speed;
  113. if (!double.TryParse(txtSpeed.Text, out speed))
  114. {
  115. dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(m/s)");
  116. return false;
  117. }
  118. if (speed <= 0)
  119. {
  120. dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0(m/s)");
  121. return false;
  122. }
  123. return true;
  124. }
  125. private async void btnSave_Click(object sender, EventArgs e)
  126. {
  127. if (!Validators())
  128. {
  129. return;
  130. }
  131. try
  132. {
  133. var fligths = mapControl.GetFlightLine<FlightInfo>();
  134. var fligth = fligths.First();
  135. info = new SimulationInfo();
  136. info.SimulationName = fligth.FlightName;
  137. info.SimulationSpeed = fligth.Speed;
  138. using (SimulationPartContext db = SimulationPartContext.GetContext(info.CreateTime))
  139. {
  140. var sinfo = db.SimulationInfos.Add(info);
  141. await db.SaveChangesAsync();
  142. var points = fligth.flights.Select(f => new SimulationPonit()
  143. {
  144. SimulationId = sinfo.ID,
  145. SimulationLon = f.FlightLon,
  146. SimulationLat = f.FlightLat,
  147. });
  148. db.SimulationPonits.AddRange(points);
  149. await db.SaveChangesAsync();
  150. }
  151. this.DialogResult = DialogResult.OK;
  152. }
  153. catch (Exception ex)
  154. {
  155. Serilog.Log.Error(ex, $"{this.Text}异常");
  156. DxHelper.MsgBoxHelper.ShowError($"{this.Text}异常");
  157. }
  158. }
  159. private void btnFlight_Click(object sender, EventArgs e)
  160. {
  161. if (!Validators())
  162. {
  163. return;
  164. }
  165. try
  166. {
  167. string Name = txtFlightName.Text.Trim();
  168. double speed = Convert.ToDouble(txtSpeed.Text);
  169. mapControl.DrawFlightLine(Name, speed);
  170. }
  171. catch (Exception ex)
  172. {
  173. Serilog.Log.Error(ex, $"绘制航迹异常");
  174. DxHelper.MsgBoxHelper.ShowError($"绘制航迹异常");
  175. }
  176. }
  177. }
  178. }