// Copyright (C) Stichting Deltares 2019. 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.Linq; using System.Xml.Linq; using Deltares.DamEngine.Calculators.General; using Deltares.DamEngine.Data.Geotechnics; namespace Deltares.DamEngine.Calculators.KernelWrappers.Assemblers { /// /// /// public class DamMstabGeometry2DSectionAssembler : DtoDocumentAssembler { /// /// Holds the xml element name /// public const string XmlElementNameGeometry2DSectionInput = "Geometry2DSectionInput"; /// /// Holds the Geometry2DSectionParameters xml element and attribute names /// public const string XmlElementGeometry2DSectionInput = "Geometry2DSectionInput"; public const string XmlAttributeSoilGeometry2DFilename = "SoilGeometry2DFilename"; public const string XmlAttributeXCoordinateSection = "XCoordinateSection"; /// /// /// public DamMstabGeometry2DSectionAssembler() : base(DamMStabAssembler.XmlElementName, DamMStabAssembler.XmlElementNamespace, DamMStabAssembler.XsdEmbeddedResourcePath) { } public override XDocument CreateDataTransferObject(Geometry2DSectionParameters geometry2DSectionParameters) { var doc = base.CreateDataTransferObject(geometry2DSectionParameters); XNamespace tns = ElementNamespace; // Geometry2DSectionParameters XElement geometry2DSectionElement = GetGeometry2DSectionParametersElement(geometry2DSectionParameters, tns); if (doc.Root != null) { doc.Root.Add(geometry2DSectionElement); // Profile var profile = geometry2DSectionParameters.SoilProfile; if (profile != null) { var soilProfileAssembler = new SoilProfileAssembler(); XNamespace tnsa = soilProfileAssembler.ElementNamespace; doc.Root.Add(new XAttribute(XNamespace.Xmlns + "tnsa", tnsa.NamespaceName)); //TODO: Remove duplicate code. The method GetProfileElement should call the SoilProfileAssembler already defined in another namespace XElement profileElement = GetProfileElement(profile, tns, tnsa); // XElement profileElement = soilProfileAssembler.CreateDataTransferObject(profile); //GetProfileElement(profile, tns, tnsa); // profileElement.Name = tns.GetName(profileElement.Name.LocalName);// .SetNamespace(DamMStabAssembler.XmlElementProfile, tns.NamespaceName); doc.Root.Add(profileElement); } } return doc; } /// /// Assembles the GeometryCreationOptions element /// /// /// /// private static XElement GetGeometry2DSectionParametersElement(Geometry2DSectionParameters geometry2DSectionParameters, XNamespace tnsb) { return new XElement(tnsb + XmlElementGeometry2DSectionInput, new XAttribute(XmlAttributeSoilGeometry2DFilename, geometry2DSectionParameters.SoilGeometry2DName), new XAttribute(XmlAttributeXCoordinateSection, geometry2DSectionParameters.XCoordinateSection) ); } /// /// /// /// /// /// /// private static XElement GetProfileElement(SoilProfile1D profile, XNamespace tns, XNamespace tnsa) { var profileElement = new XElement(tns + DamMStabAssembler.XmlElementProfile); profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeName, profile.Name)); // Next four attributes are fake but never used in DAM anyway so ok. profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeXCoordinate, 0)); profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeYCoordinate, 0)); profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributePhreaticLevel, 0)); profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeHasPhreaticLevel, false)); profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeBottomLevel, profile.BottomLevel)); if (profile.Layers.Count > 0) { var layersElement = new XElement(tnsa + DamMStabAssembler.XmlElementLayers); GetProfileLayersElement(profile, tnsa, layersElement); profileElement.Add(layersElement); } // Bottom sand layer if (profile.BottomAquiferLayer != null) profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeBottomSandLayerID, profile.BottomAquiferLayer.Name)); // In-between sand layer if (profile.InBetweenAquiferLayer != null) profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeInBetweenSandLayerID, profile.InBetweenAquiferLayer.Name)); // Infiltration layer if (profile.InfiltrationLayer != null) profileElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeInfiltrationLayerID, profile.InfiltrationLayer.Name)); return profileElement; } // Assembles the layer elements for the profile private static void GetProfileLayersElement(SoilProfile1D profile, XNamespace tnsa, XElement layersElement) { foreach (var layer in profile.Layers) { if (layer.Name == null) layer.Name = profile.GetNewUniqueLayerName(); var layerElement = new XElement(tnsa + DamMStabAssembler.XmlElementLayer); layersElement.Add(layerElement); layerElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeID, String.Format("{0}", layer.Name))); layerElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeSoilID, layer.Soil.Name)); layerElement.Add(new XAttribute(DamMStabAssembler.XmlAttributeTopLevel, layer.TopLevel)); } } public override Geometry2DSectionParameters CreateOutputObject(XDocument dtoDocument) { Geometry2DSectionParameters geometry2DSectionParameters = new Geometry2DSectionParameters(); // Get the dto XElement dto = (from x in dtoDocument.Descendants() where x.Name.LocalName == XmlElementGeometry2DSectionInput select x).FirstOrDefault(); if (dto != null) { geometry2DSectionParameters.SoilGeometry2DName = dto.AttributeAs(XmlAttributeSoilGeometry2DFilename); geometry2DSectionParameters.XCoordinateSection = dto.AttributeAs(XmlAttributeXCoordinateSection); } // Get the dto XElement profileDto = (from x in dtoDocument.Descendants() where x.Name.LocalName == DamMStabAssembler.XmlElementProfile select x).FirstOrDefault(); if (profileDto != null) { var soilProfile = new SoilProfile1D { Name = profileDto.AttributeAs(DamMStabAssembler.XmlAttributeName), BottomLevel = profileDto.AttributeAs(DamMStabAssembler.XmlAttributeBottomLevel) }; // Layers XElement layerCollectionElement = (from element in profileDto.Descendants() where element.Name.LocalName == DamMStabAssembler.XmlElementLayers select element).Single(); IEnumerable layerElementCollection = from element in layerCollectionElement.Descendants() where element.Name.LocalName == DamMStabAssembler.XmlElementLayer select element; foreach (XElement layerElement in layerElementCollection) { var soilLayer = new SoilLayer1D { Name = layerElement.AttributeAs("ID"), SoilName = layerElement.AttributeAs(DamMStabAssembler.XmlAttributeSoilID), TopLevel = layerElement.AttributeAs(DamMStabAssembler.XmlAttributeTopLevel) }; soilProfile.Layers.Add(soilLayer); } geometry2DSectionParameters.SoilProfile = soilProfile; } return geometry2DSectionParameters; } } }