// Copyright (C) Stichting Deltares 2025. 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.DamEngine.Calculators.KernelWrappers.Common; using Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityInwards; using Deltares.DamEngine.Data.General; using Deltares.DamEngine.Data.Geometry; using Deltares.DamEngine.Data.Standard.Calculation; using Deltares.DamEngine.Data.Standard.Language; using Deltares.DamEngine.Data.Standard.Logging; using Deltares.MacroStability.CSharpWrapper.Input; using Deltares.MacroStability.CSharpWrapper.Output; using Deltares.MacroStability.Io.XmlInput; using Deltares.StixFileWriter; using MacroStabilityOutput = Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityInwards.MacroStabilityOutput; using Waternet = Deltares.DamEngine.Data.Geometry.Waternet; namespace Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; public static class StixFileWriterConnector { /// /// Writes the stix file based on only the input /// /// /// /// /// /// /// public static void WriteStixFileWithInput(DamKernelInput damKernelInput, MStabParameters mStabParameters, Waternet waterNet, double xCoordinateLowestUpliftFactorPoint, MacroStabilityOutput macroStabilityOutput, string fileName, List messages) { try { var filler = new FillMacroStabilityWrapperInputFromEngine(); MacroStabilityInput macroStabilityInput = filler.CreateMacroStabilityInput(damKernelInput, mStabParameters, waterNet, xCoordinateLowestUpliftFactorPoint); MacroStabilityOutputItem stabilityOutputItem = macroStabilityOutput.StabilityOutputItems[^1]; CreateCorrectGridAsInputBasedOnOutputItem(stabilityOutputItem, macroStabilityInput); //ToDo #Bka: writing the input stix will only work AFTER Tom has done its task with reading the output for the grids (Bishop and UpliftVan). // These grids are required by the stixwriter in order to write the file (for output stix it works because then the resultcircle(s) // is(are) available. WriteStixFileBasedOnInputOnly(fileName, macroStabilityInput); } catch (Exception exceptionDuringStixFileWriter) { string message = LocalizationManager.GetTranslatedText(typeof(MacroStabilityCommonHelper), "UnexpectedErrorStixFileCreation") + exceptionDuringStixFileWriter.Message; messages.Add(new LogMessage(LogMessageType.Warning, null, message)); } } /// /// Writes the stix file based the input and the results. /// /// /// /// /// /// /// /// public static void WriteStixFileWithInputAndResults(DamKernelInput damKernelInput, MStabParameters mStabParameters, Waternet waterNet, double xCoordinateLowestUpliftFactorPoint, MacroStabilityOutput macroStabilityOutput, string fileName, List messages) { try { var filler = new FillMacroStabilityWrapperInputFromEngine(); MacroStabilityInput macroStabilityInput = filler.CreateMacroStabilityInput(damKernelInput, mStabParameters, waterNet, xCoordinateLowestUpliftFactorPoint); MacroStabilityOutputItem stabilityOutputItem = macroStabilityOutput.StabilityOutputItems[^1]; CreateCorrectGridAsInputBasedOnOutputItem(stabilityOutputItem, macroStabilityInput); if (stabilityOutputItem.CalculationResult == CalculationResult.Succeeded) { WriteStixFileBasedOnInputAndResultsSlipPlane(fileName, macroStabilityInput, stabilityOutputItem); } } catch (Exception exceptionDuringStixFileWriter) { string message = LocalizationManager.GetTranslatedText(typeof(MacroStabilityCommonHelper), "UnexpectedErrorStixFileCreation") + exceptionDuringStixFileWriter.Message; messages.Add(new LogMessage(LogMessageType.Warning, null, message)); } } private static void CreateCorrectGridAsInputBasedOnOutputItem(MacroStabilityOutputItem stabilityOutputItem, MacroStabilityInput macroStabilityInput) { switch (stabilityOutputItem.StabilityModelType) { case StabilityModelType.Bishop: macroStabilityInput.StabilityModel.BishopCalculationCircle = CSharpWrapperConverters.CreateWrapperBishopCalculationCircle(stabilityOutputItem.InputBishopCalculationGrid); break; case StabilityModelType.UpliftVan: macroStabilityInput.StabilityModel.UpliftVanCalculationGrid = CSharpWrapperConverters.CreateWrapperUpliftVanCalculationGrid(stabilityOutputItem.InputUpliftVanCalculationGrid); break; } } /// /// Writes the stix file based on input and results search grid. /// For Uplift-Van with Grid, the stix file is not written because this combination is not possible in D-Stability. /// /// Name of the file. /// The input. private static void WriteStixFileBasedOnInputOnly(string fileName, MacroStabilityInput input) { if (input.StabilityModel.ModelOption == StabilityModelOptionType.UpliftVan && input.StabilityModel.SearchAlgorithm == SearchAlgorithm.Grid) { return; } string fileNameForCalculationAsStix = DetermineStixFilename(fileName, "input"); var inputStixFile = new StixWriter(); inputStixFile.FillInfo("DAM Engine", Path.GetDirectoryName(fileName), Path.GetFileName(fileName), true); StixFileWrite(inputStixFile, fileNameForCalculationAsStix, input); } /// /// Writes the stix file based on input and results slip plane. /// /// Name of the file. /// The input. /// The macro stability output. private static void WriteStixFileBasedOnInputAndResultsSlipPlane(string fileName, MacroStabilityInput input, MacroStabilityOutputItem macroStabilityOutputItem) { macroStabilityOutputItem.CalculationPath = Path.GetDirectoryName(fileName); macroStabilityOutputItem.ProjectName = Path.GetFileName(fileName); string fileNameForCalculationAsStix = DetermineStixFilename(fileName, "result"); var resultStixWrite = new StixWriter(); resultStixWrite.FillInfo("DAM Engine", macroStabilityOutputItem.CalculationPath, macroStabilityOutputItem.ProjectName, true); switch (macroStabilityOutputItem.StabilityModelType) { case StabilityModelType.UpliftVan: resultStixWrite.FillCalculatedDualCircle(macroStabilityOutputItem.ActiveCenterPoint.X, macroStabilityOutputItem.ActiveCenterPoint.Z, macroStabilityOutputItem.ActiveCenterPointRadius, macroStabilityOutputItem.PassiveCenterPoint.X, macroStabilityOutputItem.PassiveCenterPoint.Z); break; case StabilityModelType.Bishop: // #Bka temp code if (macroStabilityOutputItem.ActiveCenterPoint == null) { macroStabilityOutputItem.ActiveCenterPoint = new Point2D(); macroStabilityOutputItem.SafetyFactor = 9999; } resultStixWrite.FillCalculatedCircle(macroStabilityOutputItem.ActiveCenterPoint.X, macroStabilityOutputItem.ActiveCenterPoint.Z, macroStabilityOutputItem.ActiveCenterPointRadius); break; } StixFileWrite(resultStixWrite, fileNameForCalculationAsStix, input); } private static string DetermineStixFilename(string fileNameForCalc, string suffix, string extension = ".stix") { return Path.Combine(Path.GetDirectoryName(fileNameForCalc) ?? string.Empty, $"{Path.GetFileNameWithoutExtension(fileNameForCalc)}_{suffix}{extension}"); } private static void StixFileWrite(StixWriter stixWriter, string fileName, MacroStabilityInput input) { if (File.Exists(fileName)) { File.Delete(fileName); } stixWriter.WriteStixFile(fileName, input); } }