// Copyright (C) Stichting Deltares 2023. 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 Geometry2DSectionAssembler : 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 Geometry2DSectionAssembler() : base(XMLAttributes.XmlElementName, XMLAttributes.XmlElementNamespace, XMLAttributes.XsdEmbeddedResourcePath) {} public override XDocument CreateDataTransferObject(Geometry2DSectionParameters dtoObject) { XDocument doc = base.CreateDataTransferObject(dtoObject); XNamespace tns = ElementNamespace; // Geometry2DSectionParameters XElement geometry2DSectionElement = GetGeometry2DSectionParametersElement(dtoObject, tns); if (doc.Root != null) { doc.Root.Add(geometry2DSectionElement); // Profile SoilProfile1D profile = dtoObject.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; } public override Geometry2DSectionParameters CreateOutputObject(XDocument dtoObject) { var geometry2DSectionParameters = new Geometry2DSectionParameters(); // Get the dto XElement dto = (from x in dtoObject.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 dtoObject.Descendants() where x.Name.LocalName == XMLAttributes.XmlElementProfile select x).FirstOrDefault(); if (profileDto != null) { var soilProfile = new SoilProfile1D { Name = profileDto.AttributeAs(XMLAttributes.XmlAttributeName), BottomLevel = profileDto.AttributeAs(XMLAttributes.XmlAttributeBottomLevel) }; // Layers XElement layerCollectionElement = (from element in profileDto.Descendants() where element.Name.LocalName == XMLAttributes.XmlElementLayers select element).Single(); IEnumerable layerElementCollection = from element in layerCollectionElement.Descendants() where element.Name.LocalName == XMLAttributes.XmlElementLayer select element; foreach (XElement layerElement in layerElementCollection) { var soilLayer = new SoilLayer1D { Name = layerElement.AttributeAs("ID"), SoilName = layerElement.AttributeAs(XMLAttributes.XmlAttributeSoilID), TopLevel = layerElement.AttributeAs(XMLAttributes.XmlAttributeTopLevel) }; soilProfile.Layers.Add(soilLayer); } geometry2DSectionParameters.SoilProfile = soilProfile; } return geometry2DSectionParameters; } /// /// 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 + XMLAttributes.XmlElementProfile); profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeName, profile.Name)); // Next four attributes are fake but never used in DAM anyway so ok. profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeXCoordinate, 0)); profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeYCoordinate, 0)); profileElement.Add(new XAttribute(XMLAttributes.XmlAttributePhreaticLevel, 0)); profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeHasPhreaticLevel, false)); profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeBottomLevel, profile.BottomLevel)); if (profile.Layers.Count > 0) { var layersElement = new XElement(tnsa + XMLAttributes.XmlElementLayers); GetProfileLayersElement(profile, tnsa, layersElement); profileElement.Add(layersElement); } // Bottom sand layer if (profile.BottomAquiferLayer != null) { profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeBottomSandLayerID, profile.BottomAquiferLayer.Name)); } // In-between sand layer if (profile.InBetweenAquiferLayer != null) { profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeInBetweenSandLayerID, profile.InBetweenAquiferLayer.Name)); } // Infiltration layer if (profile.InfiltrationLayer != null) { profileElement.Add(new XAttribute(XMLAttributes.XmlAttributeInfiltrationLayerID, profile.InfiltrationLayer.Name)); } return profileElement; } // Assembles the layer elements for the profile private static void GetProfileLayersElement(SoilProfile1D profile, XNamespace tnsa, XElement layersElement) { foreach (SoilLayer1D layer in profile.Layers) { if (layer.Name == null) { layer.Name = profile.GetNewUniqueLayerName(); } var layerElement = new XElement(tnsa + XMLAttributes.XmlElementLayer); layersElement.Add(layerElement); layerElement.Add(new XAttribute(XMLAttributes.XmlAttributeID, String.Format("{0}", layer.Name))); layerElement.Add(new XAttribute(XMLAttributes.XmlAttributeSoilID, layer.Soil.Name)); layerElement.Add(new XAttribute(XMLAttributes.XmlAttributeTopLevel, layer.TopLevel)); } } } }