Index: DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/StiFileReaderTest.cs =================================================================== diff -u -r4625 -r4817 --- DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/StiFileReaderTest.cs (.../StiFileReaderTest.cs) (revision 4625) +++ DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/StiFileReaderTest.cs (.../StiFileReaderTest.cs) (revision 4817) @@ -23,7 +23,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using Deltares.Dam.Data.StiImporter; +using Deltares.Dam.Data.Geometry2DImporter; using Deltares.Geometry; using Deltares.Geotechnics.Soils; using Deltares.Standard.Logging; Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileValidator.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileValidator.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileValidator.cs (revision 4817) @@ -0,0 +1,130 @@ +// Copyright (C) Stichting Deltares 2024. 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.IO; +using System.Linq; +using Deltares.Geotechnics.IO.Importers; +using Deltares.Standard.Language; + +namespace Deltares.Dam.Data.Geometry2DImporter; + +/// +/// Class which contains methods to determine whether a file is valid to import. +/// +public class StiFileValidator +{ + // First implementation of code started with SoilVersion = 1001 + // SoilVersion 1002 added: + // Parameter "SoilStrengthIncreaseExponent". This has been correctly implemented in DslGeoIo since SVN rev. 1176 + // SoilVersion 1003 added: + // MatStrengthType option mstCalculatedSuDOV = 9 (is not implemented in Soil, but will be translated to + // soil.ShearStrengthModel = ShearStrengthModel.CuCalculated and soil.UsePop = true) + // Parameter "SoilPOPTop" (ignored, not present in Soil) + // Parameter "SoilPOPBottom" (ignored, not present in Soil) + // Parameter "SoilAdditionalFactorLEM" (ignored, not present in Soil) + private const int HighestSupportedSoilVersion = 1003; + private const int HighestSupportedGeometryVersion = 1000; + + private readonly DSerieFileInfo fileInfo; + private readonly string filePath; + + /// + /// Creates a new instance of . + /// + /// The file path of the file to validate. + /// Thrown when + /// is null or consists of whitespaces. + /// Thrown when the validator could not be created. + public StiFileValidator(string filePath) + { + if (string.IsNullOrWhiteSpace(filePath)) + { + throw new ArgumentException($"'{nameof(filePath)}' cannot be null or consist of whitespaces only."); + } + + List fileContent = ReadFileContent(filePath); + + var importerHelper = new DSerieImporterHelper(); + fileInfo = importerHelper.ReadVersionInfo(string.Empty, fileContent); // Filename is not used for this operation and can be kept empty + + this.filePath = filePath; + } + + /// + /// Validates the sti file. + /// + /// A collection of validation messages, empty if the file was valid. + public IEnumerable Validate() + { + if (fileInfo.DSerieFileType != DSerieFileType.DGeoStability) + { + string messageFormat = LocalizationManager.GetTranslatedText(this, "ValidateStiFileNotAStiFile"); + return new[] + { + string.Format(messageFormat, filePath) + }; + } + + var messages = new List(); + if (!IsVersionValid(fileInfo.SoilVersionNumber, HighestSupportedSoilVersion)) + { + messages.Add(GenerateUnsupportedVersionMessage(LocalizationManager.GetTranslatedText(this, "ValidateStiFileUnsupportedSoilVersion"))); + } + + if (!IsVersionValid(fileInfo.GeometryVersionNumber, HighestSupportedGeometryVersion)) + { + messages.Add(GenerateUnsupportedVersionMessage(LocalizationManager.GetTranslatedText(this, "ValidateStiFileUnsupportedGeometryVersion"))); + } + + return messages; + } + + /// + /// Reads the content from . + /// + /// The file path to read the file contents from. + /// A collection of strings of the file content. + /// Thrown when the content could not be read successfully. + private static List ReadFileContent(string filePath) + { + try + { + List fileContent = File.ReadAllLines(filePath).ToList(); + return fileContent; + } + catch (IOException e) + { + throw new StiFileReadException(e.Message, e); + } + } + + private string GenerateUnsupportedVersionMessage(string messageFormat) + { + return string.Format(messageFormat, filePath); + } + + private static bool IsVersionValid(int versionNumber, int highestSupportedVersionNumber) + { + return (versionNumber <= highestSupportedVersionNumber); + } +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileReadException.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileReadException.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileReadException.cs (revision 4817) @@ -0,0 +1,67 @@ +// Copyright (C) Stichting Deltares 2024. 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.Runtime.Serialization; + +namespace Deltares.Dam.Data.Geometry2DImporter; + +/// +/// Exception thrown when reading a sti file went wrong. +/// +[Serializable] +public class StiFileReadException : Exception +{ + /// + /// Initializes a new instance of the class. + /// + public StiFileReadException() {} + + /// + /// Initializes a new instance of the class + /// with a specified error message. + /// + /// The message that describes the error. + public StiFileReadException(string message) + : base(message) {} + + /// + /// Initializes a new instance of the class with a specified error message + /// and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, + /// or null if no inner exception is specified. + public StiFileReadException(string message, Exception innerException) : base(message, innerException) {} + + /// + /// Initializes a new instance of with + /// serialized data. + /// The that holds the serialized + /// object data about the exception being thrown. + /// The that contains contextual + /// information about the source or destination. + /// The parameter is + /// null. + /// The class name is null or + /// is zero (0). + protected StiFileReadException(SerializationInfo info, StreamingContext context) : base(info, context) {} +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/DamEngineIo/FillXmlInputFromDamUi.cs =================================================================== diff -u -r4673 -r4817 --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/DamEngineIo/FillXmlInputFromDamUi.cs (.../FillXmlInputFromDamUi.cs) (revision 4673) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/DamEngineIo/FillXmlInputFromDamUi.cs (.../FillXmlInputFromDamUi.cs) (revision 4817) @@ -25,7 +25,7 @@ using System.Linq; using Deltares.Dam.Data.Properties; using Deltares.Dam.Data.Sensors; -using Deltares.Dam.Data.StiImporter; +using Deltares.Dam.Data.Geometry2DImporter; using Deltares.DamEngine.Data.Standard; using Deltares.DamEngine.Io; using Deltares.DamEngine.Io.XmlInput; Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/SoilProfile2DImporter.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/SoilProfile2DImporter.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/SoilProfile2DImporter.cs (revision 4817) @@ -0,0 +1,169 @@ +// Copyright (C) Stichting Deltares 2024. 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.IO; +using System.Linq; +using Deltares.Dam.StixFileReader; +using Deltares.Geotechnics.Soils; +using Deltares.Standard.Language; + +namespace Deltares.Dam.Data.Geometry2DImporter; + +/// +/// Importer to import . +/// +public static class SoilProfile2DImporter +{ + /// + /// Imports the that are contained within . + /// + /// The directory to retrieve the soil profiles from. + /// The to import the soil profiles for. + /// The that contains all the valid soil materials. + /// A collection of . + /// Thrown when or is null. + /// + /// Thrown when + /// is null, empty or consist of only whitespaces. + /// + /// Thrown when the soil profiles could not be successfully imported. + public static IEnumerable Import(string soilProfileDirectory, Segment segment, SoilList availableSoils) + { + if (segment == null) + { + throw new ArgumentNullException(nameof(segment)); + } + + if (availableSoils == null) + { + throw new ArgumentNullException(nameof(availableSoils)); + } + + if (string.IsNullOrWhiteSpace(soilProfileDirectory)) + { + throw new ArgumentException($"'{nameof(soilProfileDirectory)}' cannot be null or consist of whitespaces only."); + } + + // Extract all soil profile 2D from a segment + IEnumerable soilProfiles = segment.SoilProfileProbabilities.Where(sp => sp.SoilProfileType == SoilProfileType.SoilGeometryStiFile); + + var importedSoilProfiles = new List(); + foreach (SoilGeometryProbability profile in soilProfiles) + { + string profileName = profile.SoilGeometry2DName; + SoilProfile2D readSoilProfile = ReadSoilProfile(soilProfileDirectory, profileName); + string missingSoil = IsValidSoilProfile(availableSoils.Soils, readSoilProfile.Surfaces); + if (missingSoil != string.Empty) + { + string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorUndefinedMaterials"); + throw new SoilProfileImporterException(string.Format(messageFormat, profileName, missingSoil)); + } + + TransferSoilLayerProperties(readSoilProfile.Surfaces); + importedSoilProfiles.Add(readSoilProfile); + } + + return importedSoilProfiles; + } + + /// + /// Reads a soil profile. + /// + /// The directory to read the from. + /// The name of the soil profile. + /// A . + /// Thrown when the soil profile could not be read. + private static SoilProfile2D ReadSoilProfile(string soilProfileDirectory, string soilProfileName) + { + string soilProfileFileName = HasStiFileExtension(soilProfileName) ? soilProfileName : $"{soilProfileName}.sti"; + string filePath = Path.Combine(soilProfileDirectory, soilProfileFileName); + if (File.Exists(filePath)) + { + soilProfileFileName = HasStiFileExtension(soilProfileName) ? soilProfileName : $"{soilProfileName}.sti"; + filePath = Path.Combine(soilProfileDirectory, soilProfileFileName); + try + { + var reader = new StiFileReader(); + SoilProfile2D readSoilProfile = reader.ReadSoilProfile(filePath); + readSoilProfile.Name = soilProfileName; + + return readSoilProfile; + } + catch (StiFileReadException e) + { + string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorReadFailed"); + throw new SoilProfileImporterException(string.Format(messageFormat, soilProfileName, e.Message), e); + } + } + + soilProfileFileName = StixFileReaderHelper.FetchFileNameWithStixExtension(soilProfileName); + filePath = Path.Combine(soilProfileDirectory, soilProfileFileName); + if (File.Exists(filePath)) + { + filePath = Path.Combine(soilProfileDirectory, soilProfileFileName); + try + { + var reader = new StixFileReader.StixFileReader(); + SoilProfile2D readSoilProfile = reader.ReadSoilProfile(filePath); + + return readSoilProfile; + } + catch (StixFileReadException e) + { + string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorReadFailed"); + throw new SoilProfileImporterException(string.Format(messageFormat, soilProfileName, e.Message), e); + } + } + + string message = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorReadFailed"); + throw new SoilProfileImporterException(string.Format(message, soilProfileName, string.Empty)); + } + + private static bool HasStiFileExtension(string profileName) + { + return string.Equals(Path.GetExtension(profileName), ".sti", StringComparison.OrdinalIgnoreCase); + } + + private static string IsValidSoilProfile(IEnumerable availableSoils, IEnumerable surfaces) + { + var missingSoil = string.Empty; + var soilNames = new HashSet(availableSoils.Select(s => s.Name)); + foreach (string soilName in surfaces.Select(s => s.Soil.Name)) + { + if (!soilNames.Contains(soilName, StringComparer.OrdinalIgnoreCase)) + { + missingSoil = soilName; + } + } + + return missingSoil; + } + + private static void TransferSoilLayerProperties(IEnumerable soilLayers) + { + foreach (SoilLayer2D layer in soilLayers) + { + layer.WaterpressureInterpolationModel = WaterpressureInterpolationModel.Hydrostatic; + } + } +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/SoilProfileImporterException.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/SoilProfileImporterException.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/SoilProfileImporterException.cs (revision 4817) @@ -0,0 +1,67 @@ +// Copyright (C) Stichting Deltares 2024. 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.Runtime.Serialization; + +namespace Deltares.Dam.Data.Geometry2DImporter; + +/// +/// Exception thrown when the soil profiles could not be successfully imported. +/// +[Serializable] +public class SoilProfileImporterException : Exception +{ + /// + /// Initializes a new instance of the class. + /// + public SoilProfileImporterException() {} + + /// + /// Initializes a new instance of the class + /// with a specified error message. + /// + /// The message that describes the error. + public SoilProfileImporterException(string message) + : base(message) {} + + /// + /// Initializes a new instance of the class with a specified error message + /// and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, + /// or null if no inner exception is specified. + public SoilProfileImporterException(string message, Exception innerException) : base(message, innerException) {} + + /// + /// Initializes a new instance of with + /// serialized data. + /// The that holds the serialized + /// object data about the exception being thrown. + /// The that contains contextual + /// information about the source or destination. + /// The parameter is + /// null. + /// The class name is null or + /// is zero (0). + protected SoilProfileImporterException(SerializationInfo info, StreamingContext context) : base(info, context) {} +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/SoilProfile2DImporterTest.cs =================================================================== diff -u -r4625 -r4817 --- DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/SoilProfile2DImporterTest.cs (.../SoilProfile2DImporterTest.cs) (revision 4625) +++ DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/SoilProfile2DImporterTest.cs (.../SoilProfile2DImporterTest.cs) (revision 4817) @@ -23,7 +23,7 @@ using System.Collections.Generic; using System.Linq; using Deltares.Dam.Data; -using Deltares.Dam.Data.StiImporter; +using Deltares.Dam.Data.Geometry2DImporter; using Deltares.Geotechnics.Soils; using NUnit.Framework; using GeotechnicsWaterPressureInterpolationModel = Deltares.Geotechnics.Soils.WaterpressureInterpolationModel; Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileReader.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileReader.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Geometry2DImporter/StiFileReader.cs (revision 4817) @@ -0,0 +1,74 @@ +// Copyright (C) Stichting Deltares 2024. 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.Linq; +using Deltares.Geotechnics.IO.Importers; +using Deltares.Geotechnics.Soils; +using Deltares.Standard.Language; +using Deltares.Standard.Logging; + +namespace Deltares.Dam.Data.Geometry2DImporter; + +/// +/// Reader to read the contents of a sti file. +/// +public class StiFileReader +{ + /// + /// Reads the from a sti file. + /// + /// The file path to read from. + /// A . + /// Thrown when + /// could not be read successfully. + /// This only reads the names and the geometry of the soil layers. + public SoilProfile2D ReadSoilProfile(string filePath) + { + var validator = new StiFileValidator(filePath); + IEnumerable validationMessages = validator.Validate(); + if (validationMessages.Any()) + { + foreach (string message in validationMessages) + { + LogManager.Add(new LogMessage(LogMessageType.Error, this, message)); + } + + string errorMessageFormat = LocalizationManager.GetTranslatedText(this, "ReadUnsupportedStiFile"); + string errorMessage = string.Format(errorMessageFormat, filePath); + throw new StiFileReadException(errorMessage); + } + + try + { + var soilProfileImporter = new SoilProfile2DFromDSerieFileImporter(); + SoilProfile2D soilProfile = soilProfileImporter.ConvertToSoilProfile2D(filePath); + return soilProfile; + } + catch (Exception e) + { + string errorMessageFormat = LocalizationManager.GetTranslatedText(this, "ReadStiFileFailed"); + string errorMessage = string.Format(errorMessageFormat, filePath, e.Message); + throw new StiFileReadException(errorMessage, e); + } + } +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/StiFileValidatorTest.cs =================================================================== diff -u -r4625 -r4817 --- DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/StiFileValidatorTest.cs (.../StiFileValidatorTest.cs) (revision 4625) +++ DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/StiImporter/StiFileValidatorTest.cs (.../StiFileValidatorTest.cs) (revision 4817) @@ -22,7 +22,7 @@ using System; using System.Collections.Generic; using System.IO; -using Deltares.Dam.Data.StiImporter; +using Deltares.Dam.Data.Geometry2DImporter; using NUnit.Framework; namespace Deltares.Dam.Tests.StiImporter Fisheye: Tag 4817 refers to a dead (removed) revision in file `DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/StiFileValidator.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 4817 refers to a dead (removed) revision in file `DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/StiFileReader.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 4817 refers to a dead (removed) revision in file `DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/StiFileReadException.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 4817 refers to a dead (removed) revision in file `DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfile2DImporter.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 4817 refers to a dead (removed) revision in file `DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfileImporterException.cs'. Fisheye: No comparison available. Pass `N' to diff?