// Copyright (C) Stichting Deltares 2020. 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 Deltares.LayerOnSlopeTool.Io;
using System;
using System.IO;
using System.Text;
namespace Deltares.LayerOnSlopeTool.StiFileCreator
{
/// Class for creating a *.sti file with DGSMStabDam.dll
public class StiFileCreator
{
/// Create a new .sti file based on the defined input.
/// The sti file creator input.
public static void ProcessFile(StiFileCreatorInput stiFileCreatorInput)
{
ValidateInput(stiFileCreatorInput);
// TODO process file
var mStabDAMInterface = new DGSMStabDAMInterface();
IoMStabDamDoc ioMStabDamDoc = CreateMStabDamDocFromInput(stiFileCreatorInput);
tnsPrefixDamMStabDoc xmlDamMStabDoc = FillXmlDamMStabDocFromIo.FillXmlDamMStabDoc(ioMStabDamDoc);
string xmlString = MStabDamXmlSerializer.SaveDamMStabDocAsXmlString(xmlDamMStabDoc);
var length = xmlString.Length;
File.WriteAllText(stiFileCreatorInput.OutputFilename + ".xml", xmlString, Encoding.ASCII);
int result = mStabDAMInterface.CreateProjectFile(xmlString);
if (result != 0)
{
string errorMessage = mStabDAMInterface.ErrorMessage();
throw new Exception(String.Format("Error {0} in DGSMStabDamDll.CreateProjectFile(): '{1}'", result, errorMessage));
}
}
private static IoMStabDamDoc CreateMStabDamDocFromInput(StiFileCreatorInput stiFileCreatorInput)
{
var ioMStabDamDoc = new IoMStabDamDoc();
ioMStabDamDoc.MStabInput.MStabFileName = stiFileCreatorInput.OutputFilename;
ioMStabDamDoc.MStabInput.Model.CalculationModel = ModelType.Bishop;
ioMStabDamDoc.MStabInput.Model.GridPosition = GridPositionType.Right;
ioMStabDamDoc.MStabInput.Model.Probabilistic = false;
ioMStabDamDoc.MStabInput.Model.SearchMethod = SearchMethodType.Grid;
ioMStabDamDoc.MStabInput.Model.ShearStrength = ShearStrengthType.CPhi;
ioMStabDamDoc.MStabInput.GeometryCreationOptions.SoilGeometryType = SoilGeometryType.SoilGeometry2D;
ioMStabDamDoc.MStabInput.GeometryCreationOptions.MaterialForDike = stiFileCreatorInput.DikeMaterialName;
ioMStabDamDoc.MStabInput.GeometryCreationOptions.XOffsetSoilGeometry2DOrigin = stiFileCreatorInput.XOffset;
ioMStabDamDoc.MStabInput.GeometryCreationOptions.SoilGeometry2DFilename = stiFileCreatorInput.InputFilename;
ioMStabDamDoc.MStabInput.GeometryCreationOptions.PlLineAssignment = PlLineAssignment.NoPlLines;
ioMStabDamDoc.MStabInput.CalculationOptions.ZonesType = ZonesType.NoZones;
ioMStabDamDoc.MStabInput.TrafficLoad.DegreeOfConsolidationSpecified = false;
ioMStabDamDoc.MStabInput.SlipCircleDefinition.BishopTangentLinesDefinition =
TangentLinesDefinitionType.OnBoundaryLines;
ioMStabDamDoc.MStabInput.SlipCircleDefinition.BishopTangentLinesDefinition =
TangentLinesDefinitionType.OnBoundaryLines;
ioMStabDamDoc.MStabInput.SlipCircleDefinition.GridSizeDetermination = GridSizeDeterminationType.Automatic;
double leftSide = stiFileCreatorInput.SurfaceLine.SurfaceLinePoints[0].XCoordinate;
double rightSide = stiFileCreatorInput.SurfaceLine.SurfaceLinePoints[stiFileCreatorInput.SurfaceLine.SurfaceLinePoints.Count - 1].XCoordinate;
double yLevel = -10;
var pl1 = new IoPlLine();
pl1.Points.Add(new IoGeometryPoint()
{
X = leftSide,
Y = yLevel
});
pl1.Points.Add(new IoGeometryPoint()
{
X = rightSide,
Y = yLevel
});
ioMStabDamDoc.MStabInput.ExternalPlLines.Add(pl1);
var pl2 = new IoPlLine();
pl2.Points.Add(new IoGeometryPoint()
{
X = leftSide,
Y = yLevel
});
pl2.Points.Add(new IoGeometryPoint()
{
X = rightSide,
Y = yLevel
});
ioMStabDamDoc.MStabInput.ExternalPlLines.Add(pl2);
var pl3 = new IoPlLine();
pl3.Points.Add(new IoGeometryPoint()
{
X = leftSide,
Y = yLevel
});
pl3.Points.Add(new IoGeometryPoint()
{
X = rightSide,
Y = yLevel
});
ioMStabDamDoc.MStabInput.ExternalPlLines.Add(pl3);
for (int i = 0; i < stiFileCreatorInput.SurfaceLine.SurfaceLinePoints.Count; i++)
{
var surfaceLinePoint = stiFileCreatorInput.SurfaceLine.SurfaceLinePoints[i];
ioMStabDamDoc.MStabInput.SurfaceLine.Add(new IoGeometryPoint()
{
X = surfaceLinePoint.XCoordinate,
Y = surfaceLinePoint.ZCoordinate
});
CharacteristicPointType ioCharacteristicPointType = ConversionHelper.ConvertDomainToIo(surfaceLinePoint.PointType);
if (ioCharacteristicPointType != CharacteristicPointType.Unknown)
{
ioMStabDamDoc.MStabInput.CharacteristicPoints.Add(new IoCharacteristicPoint()
{
X = surfaceLinePoint.XCoordinate,
Y = surfaceLinePoint.ZCoordinate,
CharacteristicPointType = ioCharacteristicPointType
});
}
}
return ioMStabDamDoc;
}
private static void ValidateInput(StiFileCreatorInput stiFileCreatorInput)
{
if (stiFileCreatorInput == null)
{
throw new ArgumentNullException("stiFileCreatorInput");
}
if (String.IsNullOrWhiteSpace(stiFileCreatorInput.InputFilename))
{
throw new ArgumentException("No input filename specified in StiFileCreatorInput");
}
if (!File.Exists(stiFileCreatorInput.InputFilename))
{
throw new ArgumentException(String.Format("File '{0}' not found", stiFileCreatorInput.InputFilename));
}
if (stiFileCreatorInput.SurfaceLine == null)
{
throw new ArgumentException("Invalid surfaceline in StiFileCreatorInput");
}
if (String.IsNullOrWhiteSpace(stiFileCreatorInput.DikeMaterialName))
{
throw new ArgumentException("No dike material name specified in StiFileCreatorInput");
}
}
}
}