using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace XdCxRhDW.Core.Api { /// /// 星历推算帮助类.该类调用了Tle2XYZ.exe进程 /// public static class EphHelper2 { private static readonly DateTime dtZero = new DateTime(1970, 1, 1, 0, 0, 0, 0); private const string dll = @"AddIns\Tle2XYZ.dll";//此dll已加密 #region cpp dll Interop [DllImport(dll, EntryPoint = "GenerateXYZByPeriod", CallingConvention = CallingConvention.Cdecl)] private extern static void GenerateXYZByPeriod(); [DllImport(dll, EntryPoint = "GenerateXYZByPeriodWithOrbit", CallingConvention = CallingConvention.Cdecl)] private extern static void GenerateXYZByPeriodWithOrbit(); [DllImport(dll, EntryPoint = "GenerateXYZByTimes", CallingConvention = CallingConvention.Cdecl)] private extern static void GenerateXYZByTimes(); #endregion /// /// 推算双行星历 /// /// 双行根数 /// 指定时刻(北京时间) /// public static SatEphDto2 Calc(string tleStr, DateTime dt) { if (string.IsNullOrWhiteSpace(dll)) throw new Exception($"找不到{dll}"); return null; } /// /// 批量推算双行星历 /// /// 双行根数 /// 起始时刻(北京) /// 结束(北京) /// 推算间隔(秒) /// public static List CalcMult(string tleStr, DateTime start, DateTime end, int spanSeconds) { if (string.IsNullOrWhiteSpace(dll)) throw new Exception($"找不到{dll}"); return null; } static List ParseResult(string result) { if (string.IsNullOrWhiteSpace(result)) { throw new Exception("星历推算出现未知错误!"); } var array = result.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); List list = new List(); foreach (var item in array) { var arrayItem = item.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arrayItem.Length; i++) { var dto = new SatEphDto2 { SatId = Convert.ToInt32(arrayItem[i++]), TleTime = dtZero.AddSeconds(Convert.ToInt64(arrayItem[i++])).AddHours(16),//内部有BUG需要多加8h SatTime = dtZero.AddSeconds(Convert.ToInt64(arrayItem[i++])).AddHours(8),//utc to beijing X = Math.Round(Convert.ToDouble(arrayItem[i++]), 3), Y = Math.Round(Convert.ToDouble(arrayItem[i++]), 3), Z = Math.Round(Convert.ToDouble(arrayItem[i++]), 3), VX = Math.Round(Convert.ToDouble(arrayItem[i++]), 3), VY = Math.Round(Convert.ToDouble(arrayItem[i++]), 3), VZ = Math.Round(Convert.ToDouble(arrayItem[i]), 3) }; list.Add(dto); } } return list; } static SatEphDto2 ParseOneResult(string result) { if (string.IsNullOrWhiteSpace(result)) { throw new Exception("星历推算出现未知错误!"); } result = result.Replace(";", ""); var array = result.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); int i = 0; var dto = new SatEphDto2 { SatId = Convert.ToInt32(array[i++]), TleTime = dtZero.AddSeconds(Convert.ToInt64(array[i++])).AddHours(16),//内部有BUG需要多加8h SatTime = dtZero.AddSeconds(Convert.ToInt64(array[i++])).AddHours(8),//utc to beijing X = Math.Round(Convert.ToDouble(array[i++]), 3), Y = Math.Round(Convert.ToDouble(array[i++]), 3), Z = Math.Round(Convert.ToDouble(array[i++]), 3), VX = Math.Round(Convert.ToDouble(array[i++]), 3), VY = Math.Round(Convert.ToDouble(array[i++]), 3), VZ = Math.Round(Convert.ToDouble(array[i++]), 3) }; return dto; } } public class SatEphDto2 { /// /// 卫星编号 /// [Display(Name = "卫星编号")] public int SatId { get; set; } /// /// 卫星时刻(北京时间) /// [Display(Name = "卫星时刻")] public DateTime SatTime { get; set; } /// /// 星历时刻(北京时间) /// [Display(Name = "星历时刻")] public DateTime TleTime { get; set; } /// /// X坐标 /// [Display(Name = "X")] public double X { get; set; } /// /// Y坐标 /// [Display(Name = "Y")] public double Y { get; set; } /// /// Z坐标 /// [Display(Name = "Z")] public double Z { get; set; } /// /// X方向速率 /// [Display(Name = "VX")] public double VX { get; set; } /// /// Y方向速率 /// [Display(Name = "VY")] public double VY { get; set; } /// /// Z方向速率 /// [Display(Name = "VZ")] public double VZ { get; set; } public override string ToString() { return $"{X},{Y},{Z},{VX},{VY},{VZ}"; } } }