using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using XdCxRhDW.App.Api;
namespace XdCxRhDW.Core.Api
{
public class DrawDtoLineHelper
{
private const string XdtsDll = @"Api\时差线\Positioning.dll";
//三星双时差带参、三星双时差无参、三星双频差带参、双星时频差带参、两星一地无参定位及时差线
private const string xddtodll = @"AddIns\DLL_11J_DW.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 }; //定位区域
private const string locexeName = "locow.exe";
///
/// 星地有参时差线
///
///
///
///
public static IEnumerable<(double lon, double lat)> DtoLineXd(DtoLineXdOption opt)
{
List list = new List();
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\时差线\\DtoLine.dat");
string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\时差线");
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();
arguments.Add(file);
arguments.Add($"{opt.MsEph[0]}");
arguments.Add($"{opt.MsEph[1]}");
arguments.Add($"{opt.MsEph[2]}");
arguments.Add($"{opt.MsAnt[0]}");
arguments.Add($"{opt.MsAnt[1]}");
arguments.Add($"{opt.CDBAnt[0]}");
arguments.Add($"{opt.CDBAnt[1]}");
arguments.Add($"{opt.RefGeod[0]}");
arguments.Add($"{opt.RefGeod[1]}");
arguments.Add($"{opt.xdDto}");
arguments.Add($"{opt.RefDto}");
arguments.Add($"{opt.PosLon}");
arguments.Add($"{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,
zone, 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,
zone,
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,
zone,
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;
}
///
/// 高轨双星频差线
///
///
///
///
public static IEnumerable<(double lon, double lat)> DtoLineTwoStart(DtoLineTwoStartOption opt)
{
List list = new List();
string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "API\\频差线");
if (string.IsNullOrWhiteSpace(exePath))
throw new Exception($"请先调用SetExePath指定{locexeName}进程所在路径,支持相对路径");
if (!Directory.Exists(exePath))
throw new Exception($"路径[{exePath}]不存在");
var exeFile = Path.Combine(exePath, locexeName);
if (!File.Exists(exeFile))
throw new Exception($"文件[{exeFile}]不存在");
List arguments = new List();
arguments.Add("dtoline");
arguments.Add($"{opt.TargetDto}");
arguments.Add($"--rec1 {opt.MsAnt[0]} {opt.MsAnt[1]} 0");
arguments.Add($"--rec2 {opt.NsAnt[0]} {opt.NsAnt[1]} 0");
arguments.Add($"--eph1 {opt.MsEph[0]} {opt.MsEph[1]} {opt.MsEph[2]}");
arguments.Add($"--eph2 {opt.NsEph[0]} {opt.NsEph[1]} {opt.NsEph[2]}");
arguments.Add($"--refdt {opt.RefDto}");
arguments.Add($"--reflla {opt.RefGeod[0]} {opt.RefGeod[1]} 0");
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;
StringBuilder sb = new StringBuilder();
p.OutputDataReceived += (sender, e) => sb.Append(e.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
if (string.IsNullOrWhiteSpace(sb.ToString()))
{
throw new Exception("计算时差线出现未知错误!");
}
var array = sb.ToString().Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var item in array)
{
var strs = item.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
DfoLinePoint point = new DfoLinePoint();
point.Lon = Convert.ToDouble(strs[0]);
point.Lat = Convert.ToDouble(strs[1]);
list.Add(point);
}
var Lines = list.Select(s => (s.Lon, s.Lat));
return Lines;
}
private static IEnumerable<(double lon, double lat)> ParseResult(IntPtr LOP_ValuePtr, int LOP_Len)
{
List list = new List();
double[] LOP_Value = new double[LOP_Len * 3];
if (LOP_Len > 0)
{
Marshal.Copy(LOP_ValuePtr, LOP_Value, 0, LOP_Value.Length);
for (int idx = 0; idx < LOP_Len; ++idx)
{
if (LOP_Value[3 * idx + 1] != -1)
{
list.Add(new DtoLinePoint()
{
Lon = LOP_Value[3 * idx + 1],
Lat = LOP_Value[3 * idx]
});
}
}
for (int idx = 0; idx < LOP_Len; ++idx)
{
if (LOP_Value[3 * idx + 2] != -1)
{
list.Add(new DtoLinePoint()
{
Lon = LOP_Value[3 * idx + 2],
Lat = LOP_Value[3 * idx]
});
}
}
}
var Lines = list.Select(p => (p.Lon, p.Lat));
return Lines;
}
}
}