using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace XdCxRhDW.Api { public class DrawDtoLineHelper { private const string XdtsDll = @"AddIns\时差线\Positioning.dll"; //三星双时差带参、三星双时差无参、三星双频差带参、双星时频差带参、两星一地无参定位及时差线 private const string xddtodll = @"AddIns\时差线\Position-New.dll"; [DllImport(xddtodll, EntryPoint = "XingDi_SCX_NoRef", CallingConvention = CallingConvention.Cdecl)]//高轨双星有参时差线 public extern static void XingDi_SCX_NoRef(double[] main_sat_pos, double[] mbwx_rec_pos, double[] cdb_rec_pos, double[] Zone, double target_dto, out IntPtr LOP_Value, ref int LOP_Len); [DllImport(XdtsDll, EntryPoint = "CurveByTwoTDOA", CallingConvention = CallingConvention.Cdecl)]//高轨双星有参时差线 public extern static void CurveByTwoTDOA(double[] main_sat_pos, double[] neigh_sat_pos, double[] rec1_pos, double[] rec2_pos, double[] ref_pos, double[] Zone, double target_dto, double ref_dto, out IntPtr LOP_Value, ref int LOP_Len); /* 双星时差线-无参 LOP_Value 返回值 结构参考DTO_Plot LOP_Len*3为LOP_Value的长度 */ [DllImport(XdtsDll, EntryPoint = "CurveByTwoTDOAWithNoRef", CallingConvention = CallingConvention.Cdecl)]//高轨双星无参时差线 public extern static void CurveByTwoTDOAWithNoRef(double[] main_sat_pos, double[] neigh_sat_pos, double[] rec1_pos, double[] rec2_pos, double[] Zone, double target_dto, out IntPtr LOP_Value, ref int LOP_Len); [DllImport(XdtsDll, CallingConvention = CallingConvention.Cdecl)] public static extern void Destory(IntPtr val); private const string exeName = "XingDiSCX.exe";//星地时差线 // private static double[] zone = new double[] { -85, 85, -180, 180 }; //定位区域 /// /// 星地有参时差线 /// /// /// /// public static IEnumerable<(double lon, double lat)> DtoLineXd(DtoLineXdOption opt) { List list = new List(); string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns\\时差线\\DtoLine.dat"); string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns\\时差线"); if (string.IsNullOrWhiteSpace(exePath)) throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径"); if (!Directory.Exists(exePath)) throw new Exception($"路径[{exePath}]不存在"); var exeFile = Path.Combine(exePath, exeName); if (!File.Exists(exeFile)) throw new Exception($"文件[{exeFile}]不存在"); List arguments = new List { file, $"{opt.MsEph[0]}", $"{opt.MsEph[1]}", $"{opt.MsEph[2]}", $"{opt.MsAnt[0]}", $"{opt.MsAnt[1]}", $"{opt.CDBAnt[0]}", $"{opt.CDBAnt[1]}", $"{opt.RefGeod[0]}", $"{opt.RefGeod[1]}", $"{opt.xdDto}", $"{opt.RefDto}", $"{opt.PosLon}", $"{opt.PosLat}" }; Process p = new Process(); p.StartInfo.WorkingDirectory = exePath; p.StartInfo.FileName = exeFile; p.StartInfo.Arguments = string.Join(" ", arguments); p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.Start(); var succeed = p.WaitForExit(10000); if (!succeed) { throw new Exception($"进程[{exeName}]超时未完成!"); } string result = p.StandardOutput.ReadToEnd(); if (string.IsNullOrWhiteSpace(result)) { throw new Exception("计算时差线出现未知错误!"); } var array = result.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (array[0] == "1") { throw new Exception(array[1]); } if (File.Exists(file)) { var dtostr = File.ReadAllText(file); list = Newtonsoft.Json.JsonConvert.DeserializeObject>(dtostr); } var Lines = list.Select(s => (s.Lon, s.Lat)); return Lines; } /// /// 星地无参时差线 /// /// /// /// public static IEnumerable<(double lon, double lat)> DtoLineXdNoRef(DtoLineXdOption opt) { List list = new List(); IntPtr LOP_ValuePtr; int LOP_Len = 0; XingDi_SCX_NoRef(opt.MsEph, opt.MsAnt, opt.CDBAnt, new double[] { -85, 85, -180, 180 }, opt.xdDto * 1e-6, out LOP_ValuePtr, ref LOP_Len); double[] LOP_Value = new double[LOP_Len * 3]; if (LOP_Len > 0) { Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length); list = OutputHelper.WriteDtoLine(LOP_Value, LOP_Len); } var Lines = list.Select(p => (p.Lon, p.Lat)); return Lines; } /// /// 高轨双星时差线 /// /// /// public static IEnumerable<(double lon, double lat)> DtoLine2XStart(DtoLineTwoStartOption opt) { List list = new List(); IntPtr LOP_ValuePtr; int LOP_Len = 0; CurveByTwoTDOA( opt.MsEph, opt.NsEph, opt.MsAnt, opt.NsAnt, opt.RefGeod, new double[] { -85, 85, -180, 180 }, opt.TargetDto * 1e-6, opt.RefDto * 1e-6, out LOP_ValuePtr, ref LOP_Len); double[] LOP_Value = new double[LOP_Len * 3]; if (LOP_Len > 0) { Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length); list = OutputHelper.WriteDtoLine(LOP_Value, LOP_Len); } Destory(LOP_ValuePtr); var Lines = list.Select(p => (p.Lon, p.Lat)); return Lines; } public static IEnumerable<(double lon, double lat)> DtoLine2XNoRefStart(DtoLineTwoStartOption opt) { List list = new List(); IntPtr LOP_ValuePtr; int LOP_Len = 0; CurveByTwoTDOAWithNoRef( opt.MsEph, opt.NsEph, opt.MsAnt, opt.NsAnt, new double[] { -85, 85, -180, 180 }, opt.TargetDto * 1e-6, out LOP_ValuePtr, ref LOP_Len); double[] LOP_Value = new double[LOP_Len * 3]; if (LOP_Len > 0) { Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length); list = OutputHelper.WriteDtoLine(LOP_Value, LOP_Len); } Destory(LOP_ValuePtr); var Lines = list.Select(p => (p.Lon, p.Lat)); return Lines; } } }