using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using Deltares.Geometry;
using Deltares.Stability;
using Deltares.Standard.EventPublisher;
namespace Deltares.MStab.IO.Classic
{
public class Converter
{
public StabilityModel ConvertClassicMStab(string classicFileName)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var mStabSerializerDefinitions = new MStabSerializerDefinitions();
try
{
mStabSerializerDefinitions.ClassicCreateHandle();
var stabilityModel = new StabilityModel();
if (classicFileName.ToLower().EndsWith(".sti") == false)
{
throw new Exception("Incorrect filename format");
}
if (!mStabSerializerDefinitions.ClassicLoadMStabFile(classicFileName))
{
throw new Exception("Could not load classic MStab input file");
}
DataEventPublisher.InvokeWithoutPublishingEvents(() => CreateStabilityModel(mStabSerializerDefinitions.ClassicGetHandle, stabilityModel, classicFileName));
mStabSerializerDefinitions.ClassicDestroyHandle();
return stabilityModel;
}
catch (Exception e)
{
mStabSerializerDefinitions.ClassicDestroyHandle();
Debug.Assert(false, e.ToString());
}
return null;
}
///
/// Import geo data form classic geo file (Doesn't work yet...)
///
/// Target project
/// *.Geo file
public void ImportGeoDataFromClassicGeoFile(StabilityModel stabilityModel, string classicFileName)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var mStabSerializerDefinitions = new MStabSerializerDefinitions();
try
{
mStabSerializerDefinitions.ClassicCreateHandle();
if (classicFileName.EndsWith(".geo") == false)
{
throw new Exception("Incorrect filename format");
}
if (!mStabSerializerDefinitions.ClassicLoadMStabFile(classicFileName))
{
throw new Exception("Could not load classic MStab input file");
}
CreateGeoObjects(mStabSerializerDefinitions.ClassicGetHandle, stabilityModel);
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
finally
{
mStabSerializerDefinitions.ClassicDestroyHandle();
}
}
///
/// Read ClassicMStabFile paramaters and fill each object which resides in faNewMStabProject
/// There is no 1 to 1 relation between the ClassicMStab objects and NewMStab objects,
/// so some conversions need to be applied.
///
private void CreateStabilityModel(int handle, StabilityModel stabilityModel, string aClassicFileName)
{
if (handle == 0)
{
throw new Exception("Invalid ClassicMStab FileHandle");
}
//Create new ProjectIdentification Object
try
{
//Header (finished)
var header = new Header();
header.GetHeaderInfo(handle, aClassicFileName, stabilityModel.ProjectInfo);
var soilData = new SoilData();
soilData.GetSoilData(handle, ref stabilityModel); //1 Do not change the order
//Geometry (finished)
Dictionary aSurfaceDictionary = null; //Conversion old layer new surface
Dictionary> aBoundaryDictionary = null; //Conversion old layer new surface
Dictionary> aCurveDictionary = null;
//Conversion old curve new curve(s)
List waternetGeometryPoints = null; //Conversion old curve new curve(s)\
var geometry = new Geometry();
geometry.GetGeometryData(handle, stabilityModel, ref aSurfaceDictionary, ref aBoundaryDictionary,
ref aCurveDictionary, ref waternetGeometryPoints);
//Waternet model (not finished)
var waternet = new Waternet();
waternet.GetWaterNet(handle, stabilityModel, aSurfaceDictionary, aBoundaryDictionary,
waternetGeometryPoints);
//Soil model (finished)
var soilModelInfo = new SoilModelInfo();
soilModelInfo.GetSoilModel(handle, stabilityModel, aSurfaceDictionary); //3 Do not change the order
//Loads
var loads = new Loads();
loads.GetLoads(handle, stabilityModel, aSurfaceDictionary);
//Definitions
var definitions = new Definitions();
definitions.GetDefinitions(handle, stabilityModel, aSurfaceDictionary);
//GeoTextiles (finished)
var geoTextiles = new GeoTextiles();
geoTextiles.GetGeoTextiles(handle, stabilityModel);
//Calculation not finished
var calculation = new Calculation();
calculation.GetCalculation(handle, stabilityModel);
soilData.GetProbabilisticDefaults(handle, stabilityModel);
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
}
private void CreateGeoObjects(int handle, StabilityModel stabilityModel)
{
if (handle == 0)
{
throw new Exception("Invalid ClassicMStab FileHandle");
}
try
{
//Geometry (finished)
Dictionary aSurfaceDictionary = null; //Conversion old layer new surface
Dictionary> aBoundaryDictionary = null; //Conversion old layer new surface
Dictionary> aCurveDictionary = null;
//Conversion old curve new curve(s)
List waternetGeometryPoints = null; //Conversion old curve new curve(s)
var geometry = new Geometry();
geometry.GetGeometryData(handle, stabilityModel, ref aSurfaceDictionary, ref aBoundaryDictionary,
ref aCurveDictionary, ref waternetGeometryPoints);
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
}
}
}