using DataSimulation.Repostory; using DataSimulation.Repostory.EFContext; using DataSimulation.Repostory.Model; using DevExpress.Utils.About; using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.DXErrorProvider; using DxHelper; using ExtensionsDev; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DataSimulation.Forms.EditForms { public partial class FlightEditor : DevExpress.XtraEditors.XtraForm { public SimulationInfo info; private List simulationInfos; public FlightEditor(List simulationInfos) { InitializeComponent(); this.Text = "添加航迹"; info = new SimulationInfo(); this.simulationInfos = simulationInfos; this.StartPosition = FormStartPosition.CenterParent; } public FlightEditor(List simulationInfos,SimulationInfo info) : this(simulationInfos) { this.Text = "编辑航迹"; this.info = info; } private void Editor_Load(object sender, EventArgs e) { mapControl.UseDefalutOptions() .UseClearAll() .UseDistanceLine() .UseMarkDot() .UseExportImg() .UseExportXlsx() .UseExportCsv() .SetMapLayerType(null); txtFlightName.EditValueChanged += TxtFlightName_EditValueChanged; txtSpeed.EditValueChanged += TxtSpeed_EditValueChanged; } private void TxtSpeed_EditValueChanged(object sender, EventArgs e) { double speed; if (!double.TryParse(txtSpeed.Text, out speed)) { dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(Km/s)"); return; } if (speed <= 0) { dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0Km/s"); return; } dxErrorProvider.SetError(txtSpeed, string.Empty); } private void TxtFlightName_EditValueChanged(object sender, EventArgs e) { string Name = txtFlightName.Text.Trim(); if (string.IsNullOrEmpty(Name)) { dxErrorProvider.SetError(txtFlightName, "请设置航迹名称"); return; } if (simulationInfos.Any(m => m.SimulationName == Name)) { dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在"); return; } dxErrorProvider.SetError(txtFlightName, string.Empty); } private bool Validators() { dxErrorProvider.ClearErrors(); string Name = txtFlightName.Text.Trim(); if (string.IsNullOrEmpty(Name)) { dxErrorProvider.SetError(txtFlightName, "请设置航迹名称"); return false; } if (simulationInfos.Any(m => m.SimulationName == Name)) { dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在"); return false; } double speed; if (!double.TryParse(txtSpeed.Text, out speed)) { dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(Km/s)"); return false; } if (speed <= 0) { dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0Km/s"); return false; } return true; } private async void btnSave_Click(object sender, EventArgs e) { if (!Validators()) { return; } try { var fligths = mapControl.GetFlightLine(); var fligth = fligths.First(); SimulationInfo simulationInfo = new SimulationInfo(); simulationInfo.SimulationName = fligth.FlightName; simulationInfo.SimulationSpeed = fligth.Speed; using (SimulationPartContext db = SimulationPartContext.GetContext(simulationInfo.CreateTime)) { var sinfo = db.SimulationInfos.Add(simulationInfo); await db.SaveChangesAsync(); var points = fligth.flights.Select(f => new SimulationPonit() { SimulationId = sinfo.ID, SimulationLon = f.FlightLon, SimulationLat = f.FlightLat, }); db.SimulationPonits.AddRange(points); await db.SaveChangesAsync(); } this.DialogResult = DialogResult.OK; } catch (Exception ex) { Serilog.Log.Error(ex, $"{this.Text}异常"); DxHelper.MsgBoxHelper.ShowError($"{this.Text}异常"); } } private void btnFlight_Click(object sender, EventArgs e) { if (!Validators()) { return; } try { string Name = txtFlightName.Text.Trim(); double speed = Convert.ToDouble(txtSpeed.Text); mapControl.DrawFlightLine(Name, speed); } catch (Exception ex) { Serilog.Log.Error(ex, $"绘制航迹异常"); DxHelper.MsgBoxHelper.ShowError($"绘制航迹异常"); } } } }