// 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); foreach (string soilName in readSoilProfile.Surfaces.Select(surface => surface.Soil.Name)) { if (!IsSoilOfSurfacePresent(availableSoils.Soils, soilName)) { string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorUndefinedMaterials"); throw new SoilProfileImporterException(string.Format(messageFormat, profileName, soilName)); } } 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); readSoilProfile.Geometry.RegenerateGeometry(); 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 bool IsSoilOfSurfacePresent(IEnumerable availableSoils, string soilName) { var soilNames = new HashSet(availableSoils.Select(s => s.Name)); return soilNames.Contains(soilName, StringComparer.OrdinalIgnoreCase); } private static void TransferSoilLayerProperties(IEnumerable soilLayers) { foreach (SoilLayer2D layer in soilLayers) { layer.WaterpressureInterpolationModel = WaterpressureInterpolationModel.Hydrostatic; } } }