using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.Serialization; using Deltares.Geometry; using Deltares.Geotechnics.GeotechnicalGeometry; using Deltares.MStab; using Deltares.Stability.Calculation.Inner; using Deltares.Standard.Extensions; using Soil = Deltares.Geotechnics.Soils.Soil; #if DELPHI using InterfaceData; using MStabDataTypes; #else #endif namespace Deltares.Stability.Calculation { [Serializable] public class GeometryException : Exception { // // For guidelines regarding the creation of new exception types, see // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp // and // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp // public GeometryException() {} public GeometryException(string message) : base(message) {} public GeometryException(string message, Exception inner) : base(message, inner) {} protected GeometryException( SerializationInfo info, StreamingContext context) : base(info, context) {} } public static partial class LoadObjects { public static void LoadGeometry(MStabProject aMStabProject, ref TInterfaceData aStabilityObjects) { if (aMStabProject == null) { throw new GeometryException("Empty aMStabProject"); } //Fill Geometry GeometryPoints(aMStabProject, ref aStabilityObjects); GeometryLayers(aMStabProject, ref aStabilityObjects); GeometrySurfacePoints(aMStabProject, ref aStabilityObjects); } private static void GeometryPoints(MStabProject aMStabProject, ref TInterfaceData aStabilityObjects) { try { if (aMStabProject.Geometry == null) { throw new GeometryException("Empty Geometry object in GeometryPoints."); } if (aMStabProject.Geometry.Points == null) { throw new GeometryException("Empty Points object in Geometry in GeometryPoints"); } aStabilityObjects.GeometryPoints = new TPoints[aMStabProject.Geometry.Points.Count]; for (int pointIndex = 0; pointIndex < aStabilityObjects.GeometryPoints.Length; pointIndex++) { aStabilityObjects.GeometryPoints[pointIndex].XCoor = aMStabProject.Geometry.Points[pointIndex].X; aStabilityObjects.GeometryPoints[pointIndex].ZCoor = aMStabProject.Geometry.Points[pointIndex].Z; } } catch (GeometryException e) { Debug.Assert(false, e.ToString()); throw e; } } private static void GeometryLayers(MStabProject aMStabProject, ref TInterfaceData aStabilityObjects) { var generator = new GeometryRegenerator(aMStabProject.Geometry); generator.SetupCurveSurfaceAssociations(); aStabilityObjects.Layers = new TLayerRecord[aMStabProject.Geometry.Surfaces.Count]; for (int layerNumber = 0; layerNumber < aMStabProject.Geometry.Surfaces.Count; layerNumber++) { if (aMStabProject.Geometry == null) { throw new GeometryException("Empty Geometry object in CoordinatesFromLayer"); } if (aMStabProject.Geometry.Surfaces == null) { throw new GeometryException("Empty Surfaces object in Geometry object in CoordinatesFromLayer"); } // Todo: added the multiple source for soilIndex code to be able to work from DAM but this should not be needed int soilIndex; int soilSurfaceIndex = aMStabProject.Stability.SoilProfile.Surfaces.FindIndex(x => x.GeometrySurface == aMStabProject.Geometry.Surfaces[layerNumber]); if (soilSurfaceIndex < 0) { var soilf = aMStabProject.SoilModel.Soils.Where(x => x.Name == aMStabProject.Stability.SoilProfile.Surfaces[layerNumber].Soil.Name); var soilsFound = soilf as IList ?? soilf.ToList(); if (!soilsFound.Any()) { throw new GeometryException("Soil " + aMStabProject.Stability.SoilProfile.Surfaces[layerNumber].Soil.Name + " of layer " + layerNumber + " was not found in the soil list."); } var soil = soilsFound.First(); soilIndex = aMStabProject.SoilModel.Soils.FindIndex(x => x == (soil)); } else { soilIndex = aMStabProject.SoilModel.Soils.FindIndex(x => x.Name == aMStabProject.Stability.SoilProfile.Surfaces[soilSurfaceIndex].Soil.Name); } aStabilityObjects.Layers[layerNumber].MaterialNumber = soilIndex; int curves = aMStabProject.Geometry.Surfaces[layerNumber].OuterLoop.CurveList.Count; GeometryLoop loop = aMStabProject.Geometry.Surfaces[layerNumber].OuterLoop; //generator.CheckAndOrderClockwise(loop); var tempCoords = new List(); List curvesdirection; // (List) generator.GeometryLoopDirections[loop]; if (generator.GeometryLoopDirections.TryGetValue(loop, out curvesdirection)) { tempCoords = CreateTempCoordinates(curves, curvesdirection, loop); } else { foreach (var geometryLoopDirection in generator.GeometryLoopDirections) { if (loop.Equals(geometryLoopDirection.Key)) { tempCoords = CreateTempCoordinates(curves, geometryLoopDirection.Value, loop); break; } } } aStabilityObjects.Layers[layerNumber].NumberOfLayerPoints = tempCoords.Count; #if DELPHI aStabilityObjects.Layers[layerNumber].Layerpoints = new TPoints[tempCoords.Count]; #else aStabilityObjects.Layers[layerNumber].Layerpoints = new List(); #endif for (int layerPoint = 0; layerPoint < tempCoords.Count; layerPoint++) { #if DELPHI aStabilityObjects.Layers[layerNumber].Layerpoints[layerPoint].XCoor = tempCoords[layerPoint].x; aStabilityObjects.Layers[layerNumber].Layerpoints[layerPoint].ZCoor = tempCoords[layerPoint].z; #else aStabilityObjects.Layers[layerNumber].Layerpoints.Add(new TPoints(tempCoords[layerPoint].x, tempCoords[layerPoint].z)); #endif } } } private static List CreateTempCoordinates(int curves, List curvesdirection, GeometryLoop loop) { var tempCoords = new List(); for (int curveIndex = 0; curveIndex < curves; curveIndex++) { //Add begin points var direction = curvesdirection[curveIndex]; double pointx = loop.CurveList[curveIndex].GetHeadPoint(direction).X; double pointz = loop.CurveList[curveIndex].GetHeadPoint(direction).Z; if ((!tempCoords.Exists(x => ((x.x == pointx)) && (x.z == pointz)))) { var point = new Point(); point.x = pointx; point.z = pointz; tempCoords.Add(point); } //Add end points direction = curvesdirection[curveIndex]; pointx = loop.CurveList[curveIndex].GetEndPoint(direction).X; pointz = loop.CurveList[curveIndex].GetEndPoint(direction).Z; if ((!tempCoords.Exists(x => ((x.x == pointx)) && (x.z == pointz)))) { var point = new Point(); point.x = pointx; point.z = pointz; tempCoords.Add(point); } } return tempCoords; } private static void GeometrySurfacePoints(MStabProject aMStabProject, ref TInterfaceData aStabilityObjects) { if (aMStabProject.Geometry == null) { throw new GeometryException("Empty Geometry object in SurfacePoints."); } if (aMStabProject.Geometry.Loops == null) { throw new GeometryException("Empty Loops object in Geometry object in SurfacePoints"); } for (int loopIndex = 0; loopIndex < aMStabProject.Geometry.Loops.Count; loopIndex++) { bool isAssignedToSurface = false; for (int SurfaceIndex = 0; SurfaceIndex < aMStabProject.Geometry.Surfaces.Count; SurfaceIndex++) { if (aMStabProject.Geometry.Loops[loopIndex] == aMStabProject.Geometry.Surfaces[SurfaceIndex].OuterLoop) { isAssignedToSurface = true; } } if (!isAssignedToSurface) { var tempCoords = new List(); List surfaceLine = aMStabProject.Geometry.GetTopOfLoop((GeometryLoop) aMStabProject.Geometry.Loops[loopIndex]); for (int i = 0; i < surfaceLine.Count; i++) { if (!tempCoords.Contains((GeometryPoint) surfaceLine[i].EndPoint)) { tempCoords.Add((GeometryPoint) surfaceLine[i].EndPoint); } if (!tempCoords.Contains((GeometryPoint) surfaceLine[i].HeadPoint)) { tempCoords.Add((GeometryPoint) surfaceLine[i].HeadPoint); } } tempCoords.Reverse(); aStabilityObjects.SurfaceLine = new TPoints[tempCoords.Count]; for (int pointIndex = 0; pointIndex < tempCoords.Count; pointIndex++) { aStabilityObjects.SurfaceLine[pointIndex].XCoor = tempCoords[pointIndex].X; aStabilityObjects.SurfaceLine[pointIndex].ZCoor = tempCoords[pointIndex].Z; } return; } } } #region Nested type: Point private struct Point { public double x; public double z; } #endregion } }