// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU 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 General Public License for more details. // // You should have received a copy of the GNU 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.Linq; using Deltares.Geometry; using Deltares.Geotechnics; using Deltares.Geotechnics.SurfaceLines; namespace Deltares.Dam.Data { using System; using System.Collections.Generic; /// /// Exception class for DikeCoordinateSystemConverter /// public class DikeCoordinateSystemConverterException : Exception { public DikeCoordinateSystemConverterException(string message) : base(message) { } } /// /// class for converting dike object coordinates from global to local system /// public class DikeCoordinateSystemConverter { public void CreateLocalXZObjects(Dike dike) { ThrowIfInvalidDike(dike); foreach (Location location in dike.Locations.Where(x => null != x.SurfaceLine2)) { // This can obviously only be done when SurfaceLine is available // In unit test situations sometimes the surfaceline is not defined ThrowIfAlreadyConvertedToLocalCoordinates(location.AreLocalXZObjectsCreated, String.Format("Location {0}", location.Name)); var coordinateSystemConverter = new CoordinateSystemConverter(); coordinateSystemConverter.DefineGlobalXYZBasedOnLine(location.SurfaceLine2.Geometry); PL1Line localPL1Line = (location.PL1Line != null) ? location.PL1Line.Clone() : null; SurfaceLine2 localSurfaceLine = location.SurfaceLine2.FullDeepClone(); GeometryPoint localSheetPilePoint = (GeometryPoint)location.SheetPilePoint.Clone(); coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(localSurfaceLine.Geometry); if (localPL1Line != null) coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(localPL1Line); if (location.SheetPileLength > 0) { coordinateSystemConverter.ConvertGlobalXYZToLocalXZ(localSheetPilePoint); } location.LocalXZPL1Line = localPL1Line; if (location.LocalXZSurfaceLine2 != null) { location.LocalXZSurfaceLine2.Dispose(); } location.LocalXZSurfaceLine2 = localSurfaceLine; location.LocalXZSheetPilePoint = localSheetPilePoint; location.AreLocalXZObjectsCreated = true; } } /// /// Validate dike and throw exception if not valid /// /// private static void ThrowIfInvalidDike(Dike dike) { try { dike.Validate(); } catch (Exception exception) { throw new DikeCoordinateSystemConverterException(exception.Message); } } /// /// Throws exception if surfaceline already has been converted /// /// private static void ThrowIfAlreadyConvertedToLocalCoordinates(bool isConvertedToLocalCoordinates, string objectDescription) { if (isConvertedToLocalCoordinates) { throw new DikeCoordinateSystemConverterException(String.Format("{0} has already been converted from global to local coordinates", objectDescription)); } } } }