Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvImporters/CsvImporterSoils.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvImporters/CsvImporterSoils.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvImporters/CsvImporterSoils.cs (revision 4144) @@ -0,0 +1,227 @@ +// Copyright (C) Stichting Deltares 2023. All rights reserved. +// +// This file is part of the application DAM - UI. +// +// DAM - UI is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Globalization; +using System.IO; +using System.Threading; +using Deltares.Dam.Data.DamEngineIo; +using Deltares.Dam.Data.Importers; +using Deltares.Geotechnics.Soils; +using Deltares.Standard.IO; +using Deltares.Standard.Language; +using LumenWorks.Framework.IO.Csv; + +namespace Deltares.Dam.Data.CsvImporters; + +public class CsvImporterSoilsException : ApplicationException +{ + public CsvImporterSoilsException(string message) : base(message) {} +} + +public class CsvImporterSoils +{ + public CsvImporterSoils(string fileName) + { + ErrorMessages.Clear(); + ThrowHelper.ThrowIfStringArgumentNullOrEmpty(fileName, StringResourceNames.CsvFileNotValid); + ThrowHelper.ThrowIfFileNotExist(fileName, StringResourceNames.CsvFileNotFound); + + CultureInfo oldCultureInfo = Thread.CurrentThread.CurrentCulture; + try + { + Thread.CurrentThread.CurrentCulture = CsvReaderUtilities.DetermineCultureForFile(fileName); + using (var csv = new CsvReader(new StreamReader(fileName), true, ';')) + { + string[] headers = CsvImporterHelper.GetFieldHeaders(this, csv); + if (headers.Length < 13) + { + string csvHeaderError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderError"); + throw new CsvImporterSoilsException($"{fileName} : {csvHeaderError}"); + } + + int colIndexSoilName = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.SoilNameColumnName); + int colIndexSoilColor = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.SoilColorColumnName); + int colIndexSoilType = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.SoilTypeColumnName); + int colIndexSaturatedUnitWeight = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.SaturatedUnitWeightColumnName); + int colIndexUnsaturatedUnitWeight = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.UnsaturatedUnitWeightColumnName); + int colIndexCohesion = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.CohesionColumnName); + int colIndexFrictionAngle = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.FrictionAngleColumnName); + int colIndexDiameterD70 = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.DiameterD70ColumnName); + int colIndexPermeabilityX = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PermeabilityXColumnName); + int colIndexShearStrengthModel = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.ShearStrengthModelColumnName); + int colIndexStrengthIncreaseExponent = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.StrengthIncreaseExponentColumnName); + int colIndexRatioSuPc = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.RatioSuPcColumnName); + int colIndexUsePop = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.UsePopColumnName); + int colIndexPop = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PopColumnName); + + while (csv.ReadNextRecord()) + { + var soil = new SoilRecord(); + int colIndex = -1; // Keep track of column for error message + try + { + if (colIndexSoilName > -1) + { + colIndex = colIndexSoilName; + soil.SoilName = csv[colIndex]; + } + + if (colIndexSoilColor > -1) + { + colIndex = colIndexSoilColor; + soil.SoilColor = ColorTranslator.FromHtml(csv[colIndex]); + } + + if (colIndexSoilType > -1) + { + colIndex = colIndexSoilType; + soil.SoilType = ConversionHelper.ConvertToSoilType(csv[colIndex]); + } + + if (colIndexSaturatedUnitWeight > -1) + { + colIndex = colIndexSaturatedUnitWeight; + soil.SaturatedUnitWeight = Convert.ToDouble(csv[colIndex]); + } + + if (colIndexSaturatedUnitWeight > -1) + { + colIndex = colIndexSaturatedUnitWeight; + soil.SaturatedUnitWeight = Convert.ToDouble(csv[colIndex]); + } + + if (colIndexUnsaturatedUnitWeight > -1) + { + colIndex = colIndexUnsaturatedUnitWeight; + soil.UnsaturatedUnitWeight = Convert.ToDouble(csv[colIndex]); + } + + if (colIndexCohesion > -1) + { + colIndex = colIndexCohesion; + soil.Cohesion = Convert.ToDouble(csv[colIndex]); + } + + if (colIndexFrictionAngle > -1) + { + colIndex = colIndexFrictionAngle; + soil.FrictionAngle = Convert.ToDouble(csv[colIndex]); + } + + if (colIndexDiameterD70 > -1) + { + colIndex = colIndexDiameterD70; + soil.DiameterD70 = Convert.ToDouble(csv[colIndex]); + } + + if (colIndexPermeabilityX > -1) + { + colIndex = colIndexPermeabilityX; + soil.PermeabilityX = Convert.ToDouble(csv[colIndex]); + } + + if (colIndexShearStrengthModel > -1) + { + colIndex = colIndexShearStrengthModel; + soil.ShearStrengthModel = ConversionHelper.ConvertToShearStrengthModel(csv[colIndex]); + } + + if (colIndexStrengthIncreaseExponent > -1) + { + colIndex = colIndexStrengthIncreaseExponent; + if (csv[colIndex] != "") + { + soil.StrengthIncreaseExponent = Convert.ToDouble(csv[colIndex]); + } + } + + if (colIndexRatioSuPc > -1) + { + colIndex = colIndexRatioSuPc; + if (csv[colIndex] != "") + { + soil.RatioSuPc = Convert.ToDouble(csv[colIndex]); + } + } + + if (colIndexUsePop > -1) + { + colIndex = colIndexUsePop; + soil.UsePop = csv[colIndex] == "1"; + } + + if (colIndexPop > -1) + { + colIndex = colIndexPop; + soil.Pop = Convert.ToDouble(csv[colIndex]); + } + + ImportedItems.Add(soil); + } + catch (Exception e) + { + string csvScenarioError = string.Format(LocalizationManager.GetTranslatedText(GetType(), "csvScenarioError"), 0, colIndex + 1); + ErrorMessages.Add(csvScenarioError + e.Message); + } + } + } + } + finally + { + Thread.CurrentThread.CurrentCulture = oldCultureInfo; + } + } + + public List ImportedItems { get; } = new(); + + public List ErrorMessages { get; } = new(); + + private void CheckColumn(int index, string fileName, string fieldName) + { + if (index < 0) + { + string csvHeaderFieldError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderFieldError"); + throw new CsvImporterSoilsException($"{fileName} : {csvHeaderFieldError} {fieldName}"); + } + } + + public class SoilRecord + { + public string SoilName { get; set; } + public Color SoilColor { get; set; } + + public SoilType SoilType { get; set; } + public double? SaturatedUnitWeight { get; set; } + public double? UnsaturatedUnitWeight { get; set; } + public double? Cohesion { get; set; } + public double? FrictionAngle { get; set; } + public double? DiameterD70 { get; set; } + public double? PermeabilityX { get; set; } + public ShearStrengthModel ShearStrengthModel { get; set; } + public double? StrengthIncreaseExponent { get; set; } + public double? RatioSuPc { get; set; } + public bool UsePop { get; set; } + public double? Pop { get; set; } + } +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvImporters/CsvImporterHelper.cs =================================================================== diff -u -r4070 -r4144 --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvImporters/CsvImporterHelper.cs (.../CsvImporterHelper.cs) (revision 4070) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvImporters/CsvImporterHelper.cs (.../CsvImporterHelper.cs) (revision 4144) @@ -33,7 +33,7 @@ /// the calling object /// the scv reader /// - static public string[] GetFieldHeaders(Object caller, CsvReader csvReader) + public static string[] GetFieldHeaders(Object caller, CsvReader csvReader) { try { @@ -45,4 +45,13 @@ throw new CsvImporterException(message); } } + + public static void CheckColumn(Type type, int index, string fileName, string fieldName) + { + if (index < 0) + { + string csvHeaderFieldError = LocalizationManager.GetTranslatedText(type, "csvHeaderFieldError"); + throw new CsvImporterSoilsException($"{fileName} : {csvHeaderFieldError} {fieldName}"); + } + } } \ No newline at end of file