Skip to content

Commit

Permalink
Merge pull request #21 from rklomp/develop
Browse files Browse the repository at this point in the history
Provide Navaids to GetExportText of FileExport
  • Loading branch information
JetStream96 authored Aug 26, 2019
2 parents 9b80226 + 54fb4b0 commit c6c9448
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/QSP/QSP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
<Compile Include="RouteFinding\FileExport\Providers\FlightFactorA320Provider.cs" />
<Compile Include="RouteFinding\FileExport\Providers\JarDesignAirbusProvider.cs" />
<Compile Include="RouteFinding\FileExport\Providers\Util.cs" />
<Compile Include="RouteFinding\FileExport\Providers\Xplane11Provider.cs" />
<Compile Include="RouteFinding\FileExport\Providers\XplaneProvider.cs" />
<Compile Include="RouteFinding\Navaids\Navaid.cs" />
<Compile Include="RouteFinding\Navaids\NavaidExtension.cs" />
Expand Down
6 changes: 5 additions & 1 deletion src/QSP/RouteFinding/FileExport/FileExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using QSP.Common.Options;
using QSP.LibraryExtension;
using QSP.RouteFinding.Airports;
using QSP.RouteFinding.Navaids;
using QSP.RouteFinding.Routes;
using QSP.Utilities;
using System;
Expand All @@ -17,16 +18,19 @@ public class FileExporter
{
private readonly Route route;
private readonly AirportManager airports;
private readonly MultiMap<string, Navaid> navaids;
private readonly IEnumerable<ExportCommand> commands;
private readonly Func<AppOptions> options;

public FileExporter(
Route route,
MultiMap<string, Navaid> navaids,
AirportManager airports,
IEnumerable<ExportCommand> commands,
Func<AppOptions> options)
{
this.route = route;
this.navaids = navaids;
this.airports = airports;
this.commands = commands;
this.options = options;
Expand Down Expand Up @@ -73,7 +77,7 @@ private Status Export(string nameBase, ExportCommand c, int i)
: fileName;

File.WriteAllText(newName,
Providers.Types.GetExportText(c.ProviderType, route, airports));
Providers.Types.GetExportText(c.ProviderType, route, navaids, airports));
return new Status(newName, true, "", false);
}
catch (Exception ex)
Expand Down
19 changes: 13 additions & 6 deletions src/QSP/RouteFinding/FileExport/Providers/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using QSP.LibraryExtension;
using QSP.LibraryExtension.Sets;
using QSP.RouteFinding.Airports;
using QSP.RouteFinding.Navaids;
using QSP.RouteFinding.Routes;
using QSP.Utilities;
using System;
Expand All @@ -25,11 +26,12 @@ public enum ProviderType
JarDesignAirbus = 8,
PmdgWind = 9,
Xplane = 10,
FsxSteam = 11,
P3Dv1 = 12,
P3Dv2 = 13,
P3Dv3 = 14,
P3Dv4 = 15
Xplane11 = 11,
FsxSteam = 12,
P3Dv1 = 13,
P3Dv2 = 14,
P3Dv3 = 15,
P3Dv4 = 16
}

public static class Types
Expand Down Expand Up @@ -136,6 +138,10 @@ private static IEnumerable<SimTypePath> Xplane(string relativePath) =>
new Match(".fms", "X-plane", XplaneProvider.GetExportText,
Xplane("Output/FMS plans"))),

(ProviderType.Xplane11,
new Match(".fms", "X-plane 11", Xplane11Provider.GetExportText,
Xplane("Output/FMS plans"))),

(ProviderType.FsxSteam,
new Match(".PLN", "Fsx: Steam edition", FsxProvider.GetExportText,
Arr(new SimTypePath(SimulatorType.FSX_Steam, new AbsolutePath(Path.Combine(
Expand Down Expand Up @@ -201,11 +207,12 @@ public static string GetSimulatorPath(SimulatorType t)
public static string GetExtension(ProviderType type) => Lookup[type].FileExtension;

public static string GetExportText(ProviderType type, Route route,
AirportManager airports)
MultiMap<string, Navaid> navaids, AirportManager airports)
{
var input = new ExportInput()
{
Route = route,
Navaids = navaids,
Airports = airports
};

Expand Down
112 changes: 112 additions & 0 deletions src/QSP/RouteFinding/FileExport/Providers/Xplane11Provider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using QSP.AviationTools.Airac;
using QSP.AviationTools.Coordinates;
using QSP.Common.Options;
using QSP.LibraryExtension;
using QSP.NavData;
using QSP.RouteFinding.Data.Interfaces;
using QSP.RouteFinding.Navaids;
using System;
using System.Linq;
using static QSP.LibraryExtension.Types;
using static System.Math;

namespace QSP.RouteFinding.FileExport.Providers
{
/// <summary>
/// Implements the "1100 version" format. Supports x-plane 11 and up.
/// Specs: https://developer.x-plane.com/article/flightplan-files-v11-fms-file-format/
/// </summary>
public static class Xplane11Provider
{
/// <summary>
/// Get string of the flight plan to export.
/// </summary>
/// <exception cref="Exception"></exception>
public static string GetExportText(ExportInput input)
{
var (route, navaids, wptList) = (input.Route, input.Navaids, input.Waypoints);
if (route.Count < 2) throw new ArgumentException();
var from = route.FirstWaypoint;
var navdatapath = OptionManager.ReadFromFile().NavDataLocation;
var cycle = AiracTools.AiracCyclePeriod(navdatapath);
var to = route.LastWaypoint;
var s = @"I
1100 Version
CYCLE " + cycle.Cycle;

s += "\r\nADEP " + from.ID.Substring(0, 4);
s += "\r\nDEPRWY RW" + from.ID.Substring(4);
var sid = route.First.Value.AirwayToNext.Airway;
if (sid != "DCT")
{
var sidtrans = sid.Split('.');

s += "\r\nSID " + sidtrans[0];

if (sidtrans.Length > 1)
{
s += "\r\nSIDTRANS " + sidtrans[1];
}
}

s += "\r\nADES " + to.ID.Substring(0, 4);
s += "\r\nDESRWY RW" + to.ID.Substring(4);
var star = route.Last.Previous.Value.AirwayToNext.Airway;
if (star != "DCT")
{
var startrans = star.Split('.');

s += "\r\nSTAR " + startrans[0];

if (startrans.Length > 1)
{
s += "\r\nSTARTRANS " + startrans[1];
}
}

s += "\r\nNUMENR " + (route.Count);


var firstLine = GetLine(from.ID.Substring(0, 4), "ADEP", from, 1);
var lastLine = GetLine(to.ID.Substring(0, 4), "ADES", to, 1);
var middleLines = route.WithoutFirstAndLast().Select(n =>
{
var w = n.Waypoint;
var a = n.AirwayToNext.Airway;
var id = w.ID;
var navaid = navaids.Find(id, w);
if (navaid != null && navaid.IsVOR) return GetLine(id, a, w, 3);
if (navaid != null && navaid.IsNDB) return GetLine(id, a, w, 2);

var coordinate = id.ParseLatLon();
if (coordinate == null || Format5Letter.Parse(w.ID) != null)
{
return GetLine(id, a, w, 11);
}

return GetLine(coordinate.FormatLatLon(), a, w, 28);
});

var lines = List(s, firstLine)
.Concat(middleLines)
.Concat(lastLine)
.Concat("");

return string.Join("\n", lines);
}

// Types:
// 1 - Airport ICAO
// 2 - NDB
// 3 - VOR
// 11 - Fix
// 28 - Lat/Lon Position
private static string GetLine(string id, string a, ICoordinate c, int type)
{
var lat = c.Lat.ToString("0.000000");
var lon = c.Lon.ToString("0.000000");
var airway = a == "DCT" ? "DRCT" : a;
return $"{type} {id} {airway} 0 {lat} {lon}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using System.Windows.Forms;
using static QSP.UI.Views.Factories.FormFactory;
using System;
using QSP.LibraryExtension;
using QSP.RouteFinding.Navaids;

namespace QSP.UI.Presenters.FuelPlan.Routes
{
Expand All @@ -33,6 +35,7 @@ public static void ExportRouteFiles(
IMessageDisplay view,
RouteGroup Route,
IEnumerable<ExportCommand> cmds,
MultiMap<string, Navaid> Navaids,
AirportManager airportList,
ExportMenu menu)
{
Expand All @@ -46,6 +49,7 @@ public static void ExportRouteFiles(
{
menu.Location = new Point(0, 0);
menu.Route = Route;
menu.Navaids = Navaids;
menu.AirportList = airportList;

frm.AutoSizeMode = AutoSizeMode.GrowAndShrink;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void ExportRouteFiles()
view,
Route,
AppOptions.ExportCommands,
airwayNetwork.Navaids,
AirportList,
exportMenu);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ public void ExportRouteFiles()
Debug.Assert(view.IsAirportToAirport());

var o = model.FuelPlanningModel.AppOption.Instance;
var navaids = model.FuelPlanningModel.AirwayNetwork.Navaids;
var airportList = model.FuelPlanningModel.AirwayNetwork.AirportList;
var cmds = o.ExportCommands;
ActionContextMenuHelper.ExportRouteFiles(view, Route, cmds, airportList, exportMenu);
ActionContextMenuHelper.ExportRouteFiles(view, Route, cmds, navaids, airportList, exportMenu);
}

public void AnalyzeRoute()
Expand Down
6 changes: 4 additions & 2 deletions src/QSP/UI/UserControls/ExportMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using QSP.LibraryExtension;
using QSP.RouteFinding.Airports;
using QSP.RouteFinding.FileExport;
using QSP.RouteFinding.Navaids;
using QSP.RouteFinding.Routes;
using QSP.UI.Util;
using QSP.UI.Views;
Expand All @@ -19,9 +20,10 @@ public partial class ExportMenu : UserControl, IMessageDisplay
private Locator<AppOptions> appOption;
private Action showOptions;

// These two properties are required for exporting the route.
// These three properties are required for exporting the route.
public RouteGroup Route { get; set; }
public AirportManager AirportList { get; set; }
public MultiMap<string, Navaid> Navaids { get; set; }

public ExportMenu()
{
Expand Down Expand Up @@ -57,7 +59,7 @@ private void ExportFiles(object sender, EventArgs e)
var o = UpdatedOption();
UpdateOption(o);

var writer = new FileExporter(Route.Expanded, AirportList, o.ExportCommands,
var writer = new FileExporter(Route.Expanded, Navaids, AirportList, o.ExportCommands,
() => appOption.Instance);
IEnumerable<FileExporter.Status> reports = null;

Expand Down

0 comments on commit c6c9448

Please sign in to comment.