// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero 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 Deltares.LayerOnSlopeTool.Data; using Deltares.LayerOnSlopeTool.Importer; namespace Deltares.LayerOnSlopeTool.LayerCreator { /// public class LayerCreator { private List locations = new List(); private List surfaceLines = new List(); private List errors = new List(); private bool errorFound; private string fullLogFilePath; public const string LogFile = "LayerOnSlopeTool.log"; /// Initializes a new instance of the class. /// Name of the input folder. /// Name of the geometries folder. /// Name of the output folder. public LayerCreator(string inputFolderName, string geometriesFolderName, string outputFolderName) { InputFolderName = inputFolderName; GeometriesFolderName = geometriesFolderName; OutputFolderName = outputFolderName; fullLogFilePath = Path.Combine(OutputFolderName, LogFile); } /// Gets the name of the output folder. /// The name of the output folder. public string OutputFolderName { get; } /// Gets the name of the geometries folder. /// The name of the geometries folder. public string GeometriesFolderName { get;} /// Gets the name of the input folder. /// The name of the input folder. public string InputFolderName { get; } /// /// Executes this instance. /// public void Execute() { PrepareOutputFolder(); errorFound = false; locations = ReadLocations(InputFolderName); if (locations != null && locations.Count > 0) { surfaceLines = ReadSurfaceLines(InputFolderName); if (surfaceLines != null && surfaceLines.Count > 0) { ProcessLocations(); } } if (errorFound) { throw new Exception(String.Format("Error(s) found, see log file {0}.", fullLogFilePath)); } } private void ProcessLocations() { errors.Clear(); var loggedLocations = new List(); foreach (var location in locations) { loggedLocations.Clear(); loggedLocations.Add(string.Format("Handling location {0} with surface line {1}", location.LocationId, location.SurfacelineId)); WriteToLog(loggedLocations); SurfaceLine surfaceLine = FindSurfaceLine(location.SurfacelineId); var layerSurfaceLine = DetermineSurfaceLineForGivenLayerData(surfaceLine, location.LayerThickness); var layerGeometryCreator = new GeometryCreator(location, surfaceLine, layerSurfaceLine, InputFolderName, OutputFolderName); try { layerGeometryCreator.Execute(true); layerGeometryCreator.Execute(false); } catch (Exception e) { errorFound = true; errors.Add(e.Message); WriteToLog(errors); } } } private void PrepareOutputFolder() { if (!Directory.Exists(OutputFolderName)) { Directory.CreateDirectory(OutputFolderName); } else { if (File.Exists(fullLogFilePath)) { File.Delete(fullLogFilePath); } } } private SurfaceLine FindSurfaceLine(string surfaceLineId) { foreach (var surfaceLine in surfaceLines) { if (surfaceLine.SurfaceLineId == surfaceLineId) { return surfaceLine; } } return null; } private List ReadSurfaceLines(string inputFolderName) { errors.Clear(); if (CsvImporter.ReadSurfaceLines(inputFolderName, out surfaceLines, out errors)) { return surfaceLines; } WriteToLog(errors); errorFound = true; return null; } private List ReadLocations(string inputFolderName) { errors.Clear(); if (CsvImporter.ReadLocations(inputFolderName, out locations, out errors)) { return locations; } WriteToLog(errors); errorFound = true; return null; } private void WriteToLog(List linesToAdd) { File.AppendAllLines(fullLogFilePath, linesToAdd); } private SurfaceLine DetermineSurfaceLineForGivenLayerData(SurfaceLine originalSurfaceLine, double layerThickness) { return originalSurfaceLine.DetermineLoweredSurfaceLineForGivenLayerThickness(layerThickness); } } }