public bool Export(Timetable tt, string filename, IInfo info) { if (tt.Type == TimetableType.Network) { throw new Exception("Der Fahrplan ist bereits ein Netzwerk-Fahrplan"); } var clone = tt.Clone(); var trainPaths = new Dictionary <Train, TrainPathData>(); foreach (var orig in clone.Trains) { trainPaths[orig] = new TrainPathData(clone, orig); } var rt = Timetable.LINEAR_ROUTE_ID.ToString(); var id = 0; var y = 0; foreach (var sta in clone.Stations) { ConvertStationLinToNet(sta); sta.SetAttribute("fpl-rt", rt); sta.SetAttribute("fpl-pos", (y += 40).ToString() + ";0"); sta.SetAttribute("fpl-id", id++.ToString()); } var actions = info.GetRegistered <ITimetableTypeChangeAction>(); foreach (var action in actions) { action.ToNetwork(clone); } clone.SetAttribute("version", TimetableVersion.Extended_FPL.ToNumberString()); foreach (var train in clone.Trains) { var data = trainPaths[train]; train.Children.Clear(); train.AddAllArrDeps(data.GetRawPath()); train.XMLEntity.XName = "tr"; foreach (var sta in data.PathEntries) { if (sta.ArrDep != null) { train.GetArrDep(sta.Station).ApplyCopy(sta.ArrDep); } } } ColorTimetableConverter.ConvertAll(clone); return(new XMLExport().Export(clone, filename, info)); }
public bool Export(Timetable tt, string filename, IInfo info) { if (tt.Version.Compare(TimetableVersion.JTG3_1) >= 0) { throw new Exception("Nur mit jTrainGraph 2.x oder 3.0x erstellte Fahrpläne können aktualisiert werden."); } var origVersion = tt.Version; var clone = tt.Clone(); clone.SetAttribute("version", "010"); if (origVersion == TimetableVersion.JTG2_x) { var shv = clone.GetAttribute <bool>("shV"); clone.SetAttribute("shV", shv ? "1" : "0"); // km -> kml/kmr foreach (var sta in clone.Stations) { var km_old = sta.GetAttribute("km", ""); sta.SetAttribute("kml", km_old); sta.SetAttribute("kmr", km_old); sta.RemoveAttribute("km"); } // Allocate train ids var nextId = clone.Trains.DefaultIfEmpty().Max(t => t?.Id) ?? 0; foreach (var orig in clone.Trains) { if (orig.Id == -1) { orig.Id = ++nextId; } } } ColorTimetableConverter.ConvertAll(clone); return(new XMLExport().Export(clone, filename, info)); }
public void SyncBack(Timetable singleRoute) { ColorTimetableConverter.ConvertAll(singleRoute, TimetableVersion.Extended_FPL); // Zu Hex-Farben zurück orig.Attributes = AttrDiff(orig, singleRoute); orig.SetAttribute("version", TimetableVersion.Extended_FPL.ToNumberString()); // Wieder in Netzwerk-Modus wechseln foreach (var sta in orig.Stations) { var srSta = singleRoute.Stations.FirstOrDefault(s => s.GetAttribute <int>("fpl-id") == sta.Id); if (srSta == null) { continue; } //TODO: Better method to remove all known attributes? foreach (var a in new[] { "km", "kml", "kmr", "fpl-rt", "fpl-id", "fpl-wl", "fpl-vmax", "tr", "dTi", "dTa" }) { srSta.RemoveAttribute(a); // Alte Angaben entfernen } sta.Attributes = AttrDiff(sta, srSta); } foreach (var tra in orig.Trains) { var syncId = trainMap.IndexOf(tra); if (syncId == -1) { continue; } var srTra = singleRoute.Trains.FirstOrDefault(t => t.GetAttribute <int>("fpl-sync-tra-id") == syncId); srTra.RemoveAttribute("fpl-sync-id"); tra.Attributes = AttrDiff(tra, srTra); } }
public string Init(Timetable tt) { // Farbangaben zwischen jTG- und FPLedit-Versionen vereinheitlichen ColorTimetableConverter.ConvertAll(tt); return(null); }
public Timetable GetRouteTimetable(TimetableVersion targetVersion) { var copy = orig.Clone(); var route = copy.GetRoute(routeIndex); for (int si = 0; si < copy.Stations.Count; si++) { var sta = copy.Stations[si]; if (!route.Stations.Contains(sta)) { copy.RemoveStation(sta); si--; continue; } } // XML-Elemente wirklich sortieren. In copy.Stations wird nicht zurückgesynct, // daher eine eigene sortierte Liste für später var sortedStations = copy.Stations.OrderBy(s => s.Positions.GetPosition(routeIndex)).ToList(); var stasElm = copy.Children.First(x => x.XName == "stations"); stasElm.Children = stasElm.Children.OrderBy(c => { if (c.XName == "sta") { return(copy.Stations.FirstOrDefault(s => s.XMLEntity == c)?.Positions.GetPosition(routeIndex)); } return(null); }).ToList(); int syncId = 0; for (int ti = 0; ti < copy.Trains.Count; ti++) { var tra = copy.Trains[ti]; var ardps = tra.GetArrDeps(); var path = tra.GetPath(); var pf = path.FirstOrDefault(); var pl = path.LastOrDefault(); var isEmpty = pf != null && pl != null && ((ardps[pf].Arrival != default && ardps[pf].Departure == default) || (ardps[pl].Departure != default && ardps[pl].Arrival == default)); if (ardps.Count == 0 || ardps.All(a => !a.Value.HasMinOneTimeSet) || isEmpty) // Dieser Zug berührt diese Route nicht { copy.RemoveTrain(tra); ti--; continue; } tra.SetAttribute("fpl-sync-id", syncId++.ToString()); trainMap.Add(tra); // Der Index wird immer um 1 hochegzählt, daher brauchts hier kein Dictionary // Fahrtzeiteneinträge setzen tra.Children.Clear(); tra.AddAllArrDeps(sortedStations); foreach (var ardp in ardps) { if (sortedStations.Contains(ardp.Key)) { tra.GetArrDep(ardp.Key).ApplyCopy(ardp.Value); } } // Lineare Fahrtrichtung bestimmen var sta1 = ardps.FirstOrDefault().Key; var sta2 = ardps.LastOrDefault().Key; var dir = TrainDirection.ti; if (sta1 != sta2) { if (sortedStations.IndexOf(sta1) > sortedStations.IndexOf(sta2)) { dir = TrainDirection.ta; } } else if (sortedStations.IndexOf(sta1) == sortedStations.Count - 1) { dir = TrainDirection.ta; } tra.XMLEntity.XName = dir.ToString(); } // Am Ende die Kilometer & anderen Attribute auf den linearen Stil setzen foreach (var sta in copy.Stations) { ConvertStationNetToLin(sta, routeIndex, targetVersion); } copy.SetAttribute("version", targetVersion.ToNumberString()); // Wir gehen aus dem Extended-Modus raus ColorTimetableConverter.ConvertAll(copy); // Zum Ziel-Farbformat konvertieren return(copy); }
public bool Export(Timetable tt, string filename, IInfo info) { if (tt.Type == TimetableType.Linear) { throw new Exception("Der Fahrplan ist bereits ein Linear-Fahrplan"); } if (tt.GetRoutes().Count() != 1) { throw new Exception("Der Fahrplan hat mehr als eine oder keine Strecke"); } var clone = tt.Clone(); var trainPaths = new Dictionary <Train, TrainPathData>(); foreach (var orig in clone.Trains) { trainPaths[orig] = new TrainPathData(clone, orig); } var route = clone.GetRoutes().FirstOrDefault().Index; foreach (var sta in clone.Stations) { ConvertStationNetToLin(sta, route, Timetable.DefaultLinearVersion); sta.RemoveAttribute("fpl-rt"); sta.RemoveAttribute("fpl-pos"); sta.RemoveAttribute("fpl-id"); } var actions = info.GetRegistered <ITimetableTypeChangeAction>(); foreach (var action in actions) { action.ToLinear(clone); } clone.SetAttribute("version", Timetable.DefaultLinearVersion.ToNumberString()); var sortedStations = clone.GetRoutes()[Timetable.LINEAR_ROUTE_ID].GetOrderedStations(); foreach (var t in clone.Trains) { var data = trainPaths[t]; var sta1 = data.PathEntries.FirstOrDefault()?.Station; var sta2 = data.PathEntries.LastOrDefault()?.Station; var dir = TrainDirection.ti; if (sta1 != sta2) { if (sortedStations.IndexOf(sta1) > sortedStations.IndexOf(sta2)) { dir = TrainDirection.ta; } } else if (sortedStations.IndexOf(sta1) == sortedStations.Count - 1) { dir = TrainDirection.ta; } t.XMLEntity.XName = dir.ToString(); t.Children.Clear(); t.AddLinearArrDeps(); foreach (var sta in data.PathEntries) { if (sta.ArrDep != null) { t.GetArrDep(sta.Station).ApplyCopy(sta.ArrDep); } } } ColorTimetableConverter.ConvertAll(clone); return(new XMLExport().Export(clone, filename, info)); }