From acaedf118ef54c56b08ef90ff64e0ebcd3ed162e Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Thu, 13 Oct 2016 12:32:45 +0200
Subject: [PATCH] minor fixes user interface, input validation torque converter

---
 VECTO/GUI/VectoJobForm.vb                     | 18 ++++++++++-------
 VECTO/GUI/VehicleForm.vb                      |  1 +
 VECTO/Input Files/Gearbox.vb                  |  3 ++-
 VECTO/OutputData/JSONFileWriter.vb            |  6 +++---
 .../VectoCommon/Models/AuxiliaryModel.cs      | 11 ++++++++++
 .../EngineeringDataAdapter.cs                 |  6 +++---
 .../Data/Gearbox/GearData.cs                  | 20 +++++++++++++++++++
 7 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/VECTO/GUI/VectoJobForm.vb b/VECTO/GUI/VectoJobForm.vb
index bb1d0152ec..3311df7fd4 100644
--- a/VECTO/GUI/VectoJobForm.vb
+++ b/VECTO/GUI/VectoJobForm.vb
@@ -454,7 +454,7 @@ Public Class VectoJobForm
 			Dim auxInput As IAuxiliariesDeclarationInputData = declarationInput.AuxiliaryInputData()
 
 			cboAdvancedAuxiliaries.SelectedIndex = 0
-
+			cboAdvancedAuxiliaries.Enabled = False
 			LvAux.Items.Clear()
 			Dim entry As IAuxiliaryDeclarationInputData
 			For Each entry In auxInput.Auxiliaries
@@ -470,7 +470,7 @@ Public Class VectoJobForm
 			TbDesMaxFile.Text =
 				If(driver.AccelerationCurve Is Nothing, "", GetRelativePath(driver.AccelerationCurve.Source, _basePath))
 
-
+			cboAdvancedAuxiliaries.Enabled = True
 			Dim auxInput As IAuxiliariesEngineeringInputData = inputData.AuxiliaryInputData()
 			For Each item As AdvancedAuxiliary In cboAdvancedAuxiliaries.Items
 				If _
@@ -716,6 +716,7 @@ Public Class VectoJobForm
 		tbLacDfTargetSpeedFile.Text = ""
 		tbLacDfVelocityDropFile.Text = ""
 
+		cboAdvancedAuxiliaries.Enabled = Not Cfg.DeclMode
 		'---------------------------------------------------
 
 		DeclInit()
@@ -872,15 +873,15 @@ lbDlog:
 		End If
 
 		Dim selItem As ListViewItem = LvAux.SelectedItems(0)
-
 		_auxDialog.VehPath = GetPath(VectoFile)
 		_auxDialog.CbType.SelectedIndex = -1
-		_auxDialog.CbType.Text = selItem.SubItems(AuxViewColumns.AuxType).Text
-		_auxDialog.NumAxles = AxleConfigurationHelper.Parse(TbAxleConf.Text).NumAxles()
-		_auxDialog.TbID.Text = selItem.SubItems(AuxViewColumns.AuxID).Text	' last call, updates GUI
+
+		_auxDialog.NumAxles =
+			If(String.IsNullOrWhiteSpace(TbAxleConf.Text), 1, AxleConfigurationHelper.Parse(TbAxleConf.Text).NumAxles())
+
 
 		If Cfg.DeclMode Then
-			If _auxDialog.TbID.Text = AuxiliaryTypeHelper.GetAuxKey(AuxiliaryType.SteeringPump) Then
+			If selItem.SubItems(AuxViewColumns.AuxID).Text = AuxiliaryTypeHelper.GetAuxKey(AuxiliaryType.SteeringPump) Then
 				Dim parts As String() = selItem.SubItems(AuxViewColumns.AuxInputOrTech).Text.Split(";"c)
 				_auxDialog.CbTech2.SelectedItem = VehicleAuxiliariesDialog.AxleNotSteered
 				_auxDialog.CbTech3.SelectedItem = VehicleAuxiliariesDialog.AxleNotSteered
@@ -892,12 +893,15 @@ lbDlog:
 			Else
 				_auxDialog.CbTech.SelectedItem = selItem.SubItems(AuxViewColumns.AuxInputOrTech).Text
 				_auxDialog.TbPath.Text = ""
+				_auxDialog.NumAxles = 0
 			End If
 		Else
 			_auxDialog.CbTech.SelectedIndex = -1
 			_auxDialog.TbPath.Text = selItem.SubItems(AuxViewColumns.AuxInputOrTech).Text
 		End If
 
+		'_auxDialog.TbID.Text = selItem.SubItems(AuxViewColumns.AuxID).Text	
+		_auxDialog.CbType.Text = selItem.SubItems(AuxViewColumns.AuxType).Text	' last call, updates GUI
 		If _auxDialog.ShowDialog = DialogResult.OK Then
 			selItem.SubItems(AuxViewColumns.AuxID).Text = UCase(Trim(_auxDialog.TbID.Text))
 			selItem.SubItems(AuxViewColumns.AuxType).Text = Trim(_auxDialog.CbType.Text)
diff --git a/VECTO/GUI/VehicleForm.vb b/VECTO/GUI/VehicleForm.vb
index ac872c4b3f..5d455b6516 100644
--- a/VECTO/GUI/VehicleForm.vb
+++ b/VECTO/GUI/VehicleForm.vb
@@ -173,6 +173,7 @@ Public Class VehicleForm
 		TbMassExtra.Text = "-"
 		TbLoad.Text = "-"
 		CbCdMode.SelectedValue = CrossWindCorrectionMode.DeclarationModeCorrection
+
 		TbCdFile.Text = ""
 
 		Dim rdyn As Double
diff --git a/VECTO/Input Files/Gearbox.vb b/VECTO/Input Files/Gearbox.vb
index 128d21118e..189bae2878 100644
--- a/VECTO/Input Files/Gearbox.vb	
+++ b/VECTO/Input Files/Gearbox.vb	
@@ -314,7 +314,7 @@ Public Class Gearbox
 
 	Public ReadOnly Property IComponentInputData_ModelName As String Implements IComponentInputData.ModelName
 		Get
-			Return "N.A." ' todo MQ 20160915
+			Return ModelName
 		End Get
 	End Property
 
@@ -371,6 +371,7 @@ Public Class Gearbox
 
 	Public ReadOnly Property ShiftPolygon As TableData Implements ITorqueConverterEngineeringInputData.ShiftPolygon
 		Get
+			If Not File.Exists(Path.Combine(_myPath, TorqueConverterShiftPolygonFile)) Then Return Nothing
 			Return VectoCSVFile.Read(Path.Combine(_myPath, TorqueConverterShiftPolygonFile))
 		End Get
 	End Property
diff --git a/VECTO/OutputData/JSONFileWriter.vb b/VECTO/OutputData/JSONFileWriter.vb
index 020149a33a..460195c08a 100644
--- a/VECTO/OutputData/JSONFileWriter.vb
+++ b/VECTO/OutputData/JSONFileWriter.vb
@@ -102,7 +102,7 @@ Public Class JSONFileWriter
 				gearDict.Add("LossMap", GetRelativePath(gear.LossMap.Source, Path.GetDirectoryName(filename)))
 			End If
 			gearDict.Add("ShiftPolygon", If _
-							(gbx.SavedInDeclarationMode AndAlso Not gear.ShiftPolygon Is Nothing,
+							(Not gbx.SavedInDeclarationMode AndAlso Not gear.ShiftPolygon Is Nothing,
 							GetRelativePath(gear.ShiftPolygon.Source, Path.GetDirectoryName(filename)), ""))
 			gearDict.Add("MaxTorque", If(gear.MaxTorque Is Nothing, "", gear.MaxTorque.Value().ToString()))
 
@@ -130,7 +130,7 @@ Public Class JSONFileWriter
 			torqueConverterDict.Add("Inertia", torqueConverter.Inertia.Value())
 			torqueConverterDict.Add("ShiftPolygon",
 									If _
-										(gbx.SavedInDeclarationMode AndAlso Not torqueConverter.ShiftPolygon Is Nothing,
+										(Not gbx.SavedInDeclarationMode AndAlso Not torqueConverter.ShiftPolygon Is Nothing,
 										GetRelativePath(torqueConverter.ShiftPolygon.Source, Path.GetDirectoryName(filename)), ""))
 		End If
 		body.Add("TorqueConverter", torqueConverterDict)
@@ -261,7 +261,7 @@ Public Class JSONFileWriter
 
 		'AA-TB
 		'ADVANCED AUXILIARIES 
-		body.Add("AuxiliaryAssembly", aux.AuxiliaryAssembly.ToString())
+		body.Add("AuxiliaryAssembly", aux.AuxiliaryAssembly.GetName())
 		body.Add("AuxiliaryVersion", aux.AuxiliaryVersion)
 		body.Add("AdvancedAuxiliaryFilePath", aux.AdvancedAuxiliaryFilePath)
 
diff --git a/VectoCommon/VectoCommon/Models/AuxiliaryModel.cs b/VectoCommon/VectoCommon/Models/AuxiliaryModel.cs
index f86b29c2a2..3e4615fe40 100644
--- a/VectoCommon/VectoCommon/Models/AuxiliaryModel.cs
+++ b/VectoCommon/VectoCommon/Models/AuxiliaryModel.cs
@@ -29,6 +29,8 @@
 *   Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
 */
 
+using System;
+
 namespace TUGraz.VectoCommon.Models
 {
 	public enum AuxiliaryModel
@@ -50,5 +52,14 @@ namespace TUGraz.VectoCommon.Models
 			}
 			return AuxiliaryModel.Classic;
 		}
+
+		public static string GetName(this AuxiliaryModel model)
+		{
+			switch (model) {
+				case AuxiliaryModel.Advanced:
+					return "BusAuxiliaries";
+			}
+			return model.ToString();
+		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs
index e6cf649a77..730bae0a4c 100644
--- a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs
+++ b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs
@@ -178,7 +178,7 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter
 						// powersplit transmission: torque converter already contains ratio and losses
 						gearData.TorqueConverterRatio = 1;
 						gearData.TorqueConverterGearLossMap = TransmissionLossMapReader.Create(1, 1, string.Format("TCGear {0}", i + 1));
-						gearData.TorqueConverterShiftPolygon = ShiftPolygonReader.Create(gearbox.TorqueConverter.ShiftPolygon);
+						gearData.TorqueConverterShiftPolygon = gearbox.TorqueConverter.ShiftPolygon == null ? null : ShiftPolygonReader.Create(gearbox.TorqueConverter.ShiftPolygon);
 					}
 				}
 				if (gearbox.Type == GearboxType.ATSerial) {
@@ -186,14 +186,14 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter
 						// torqueconverter is active in first gear - duplicate ratio and lossmap for torque converter mode
 						gearData.TorqueConverterRatio = gearData.Ratio;
 						gearData.TorqueConverterGearLossMap = gearData.LossMap;
-						gearData.TorqueConverterShiftPolygon = ShiftPolygonReader.Create(gearbox.TorqueConverter.ShiftPolygon);
+						gearData.TorqueConverterShiftPolygon = gearbox.TorqueConverter.ShiftPolygon == null ? null : ShiftPolygonReader.Create(gearbox.TorqueConverter.ShiftPolygon);
 					}
 					if (i == 1 && gearDifferenceRatio >= DeclarationData.Gearbox.TorqueConverterSecondGearThreshold) {
 						// ratio between first and second gear is above threshold, torqueconverter is active in second gear as well
 						// -> duplicate ratio and lossmap for torque converter mode, remove locked transmission for previous gear
 						gearData.TorqueConverterRatio = gearData.Ratio;
 						gearData.TorqueConverterGearLossMap = gearData.LossMap;
-						gearData.TorqueConverterShiftPolygon = ShiftPolygonReader.Create(gearbox.TorqueConverter.ShiftPolygon);
+						gearData.TorqueConverterShiftPolygon = gearbox.TorqueConverter.ShiftPolygon == null ? null : ShiftPolygonReader.Create(gearbox.TorqueConverter.ShiftPolygon);
 						// NOTE: the lower gear in 'gears' dictionary has index i !!
 						gears[i].Ratio = double.NaN;
 						gears[i].LossMap = null;
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/GearData.cs b/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/GearData.cs
index 74f32811f0..2fa8a90ade 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/GearData.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/GearData.cs
@@ -30,6 +30,8 @@
 */
 
 using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Utils;
 
 namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox
@@ -43,6 +45,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox
 		public double Ratio { get; internal set; }
 	}
 
+	[CustomValidation(typeof(GearData), "ValidateGearData")]
 	public class GearData : TransmissionData
 	{
 		public GearData()
@@ -70,5 +73,22 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox
 		public NewtonMeter MaxTorque { get; internal set; }
 
 		public ShiftPolygon TorqueConverterShiftPolygon { get; set; }
+
+		public static ValidationResult ValidateGearData(GearData gearData, ValidationContext context)
+		{
+			var modeService = context.GetService(typeof(ExecutionMode)) as ExecutionModeServiceContainer;
+			var mode = modeService == null ? ExecutionMode.Declaration : modeService.Mode;
+
+			if (gearData.HasTorqueConverter) {
+				if (gearData.TorqueConverterShiftPolygon == null) {
+					return new ValidationResult("Shift Polygon for Torque Converter Gear required!");
+				}
+				var result = gearData.TorqueConverterShiftPolygon.Validate(mode);
+				if (result.Any()) {
+					return new ValidationResult(string.Format("Validation of GearData failed"), result.Select(x => x.ErrorMessage));
+				}
+			}
+			return ValidationResult.Success;
+		}
 	}
 }
\ No newline at end of file
-- 
GitLab