using System; using System.Collections; using System.ComponentModel; using System.Linq; using Deltares.Geographic; using Deltares.Probabilistic; using Deltares.Standard; using Deltares.Standard.Attributes; using Deltares.Standard.EventPublisher; using Deltares.Standard.Reflection; using Deltares.Standard.Units; namespace Deltares.DeltaModel { /// /// A dike line is a ‘backbone’ of a dike and applicable for all failure mechanisms. /// The user can select points along the dike line to view properties such as a name. /// Points have global XYZ-coordinates and local L-coordinates along dike line. /// Locations can have name (labels) or L-coordinate indications (km-pole) /// [TrackChanges] public class DikeLine : GeographicString, IName, IDomain, IDisposable, IComparable { private ProbabilityDefinition allowedFailureProbability; private DikeLinePoint beginPoint; private DikeLinePoint endPoint; private string name; /// /// Initializes a new instance of the class. /// public DikeLine() { name = string.Empty; DataEventPublisher.OnDataListModified += DataEventPublisher_OnDataListModified; DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange; } /// /// Gets or sets the allowed failure probability of the DikeLine. /// /// /// The allowed failure probability. /// [Unit(UnitType.None)] [Format("F2")] [Label("Allowed failure probability")] [Description("AllowedFailureProbability")] [PropertyOrder(1)] public ProbabilityDefinition AllowedFailureProbability { get { if (allowedFailureProbability == null && ProbabilityDefinitionCollection.ProbabilityDefinitions.Count > 0) { allowedFailureProbability = ProbabilityDefinitionCollection.ProbabilityDefinitions[0]; } return allowedFailureProbability; } set { this.SetAndNotify2(out allowedFailureProbability, value, dl => dl.AllowedFailureProbability); } } #region IDisposable public void Dispose() { DataEventPublisher.OnDataListModified -= DataEventPublisher_OnDataListModified; DataEventPublisher.OnAfterChange -= DataEventPublisher_OnAfterChange; } #endregion /// /// Gets or sets the name of the DikeLine. /// /// /// The name. /// [Label("Name")] [Description("Name")] [PropertyOrder(0)] public string Name { get { return name; } set { this.SetAndNotify2(out name, value, dl => dl.Name); } } /// /// Gets the DikeLine as a WKTstring, /// mainly to export to csv /// public string GetWKTstring() { var coordList = "LINESTRING("; foreach (var point in Points) { coordList += point.X + " " + point.Y + ","; } return coordList.Remove(coordList.Length - 1) + ")"; } /// /// Gets the maximum X coordinate of all points /// /// max X public double GetMaxX() { return Points.Select(point => point.X).Concat(new[] { double.MinValue }).Max(); } /// /// Gets the minimum X coordinate of all points /// /// min X public double GetMinX() { return Points.Select(point => point.X).Concat(new[] { double.MaxValue }).Min(); } /// /// Gets the maximum Y coordinate of all points /// /// max Y public double GetMaxY() { return Points.Select(point => point.Y).Concat(new[] { double.MinValue }).Max(); } /// /// Gets the minimum Y coordinate of all points /// /// min Y public double GetMinY() { return Points.Select(point => point.Y).Concat(new[] { double.MaxValue }).Min(); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return Name; } /// /// Gets the domain. /// /// The property. /// Domain public ICollection GetDomain(string property) { if (property == this.GetMemberName(dl => dl.AllowedFailureProbability)) { return ProbabilityDefinitionCollection.ProbabilityDefinitions; } return null; } public int CompareTo(DikeLine dikeLine) { return Name.CompareTo(dikeLine.Name); } public override bool Equals(object obj) { DikeLine dikeLine = obj as DikeLine; if (dikeLine == null) { return false; } if (dikeLine.Points.Count != Points.Count) { return false; } for (int i = 0; i < dikeLine.Points.Count; i++) { if (!Points[i].Equals(dikeLine.Points[i])) { return false; } } return true; } public override int GetHashCode() { return Points.Count; } /// /// Handles the OnAfterChange event of the DataEventPublisher control. /// /// The source of the event. /// The instance containing the event data. private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e) { if (ReferenceEquals(sender, AllowedFailureProbability)) { DataEventPublisher.AfterChange(this, this.GetMemberName(dl => dl.AllowedFailureProbability)); } var geographicPoint = sender as IGeographicPoint; if (geographicPoint != null && Points.Any(p => ReferenceEquals(p, geographicPoint))) { DataEventPublisher.AfterChange(this); } } /// /// Handles the OnDataListModified event of the DataEventPublisher control. /// /// The source of the event. /// The instance containing the event data. private void DataEventPublisher_OnDataListModified(object sender, PublishEventArgs e) { if (ReferenceEquals(sender, Points)) { DataEventPublisher.AfterChange(this); } } } }