using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Xml.Serialization; using Deltares.Geometry; using Deltares.Geotechnics; using Deltares.Geotechnics.GeotechnicalGeometry; using Deltares.Geotechnics.Soils; using Deltares.Stability; using Deltares.Standard; using Deltares.Standard.Attributes; using Deltares.Standard.EventPublisher; using Deltares.Standard.Extensions; using Deltares.Standard.IO.Xml; using Deltares.Standard.Logging; using Deltares.Standard.Project; using Deltares.Standard.Reflection; using Deltares.Standard.Units; using Deltares.Standard.Validation; using XmlSerializer = Deltares.Standard.IO.Xml.XmlSerializer; namespace Deltares.MStab { public class MStabProject : Project, ICloneable, IDisposable { public bool IsApplicationCreated; private List messages = new List(); private string originalStiFileName; private CalculationResult result = CalculationResult.NoRun; private StabilityModel stabilityModel; //Object of DefinitionModel public Func GetGeometryImage; //public MStabProject(ICoordinateSystem coordinateSystem) public MStabProject() { originalStiFileName = ""; Stability = new StabilityModel(); DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange; } [Label("GeometryPicture")] [Description("GeometryPicture")] public Image GeometryPicture { get { return GetGeometryImage(); } } [XmlCategory("Identification")] public string OriginalStiFileName { get { return originalStiFileName; } set { originalStiFileName = value; } } /// /// //Object of SoilModel /// [XmlCategory("Input")] [XmlElement("Soils")] [Validate] public SoilModel SoilModel { get { return stabilityModel.SoilModel; } } /// /// Object of GeometryData /// [XmlCategory("Input")] public GeometryData Geometry { get { return stabilityModel != null && stabilityModel.SoilProfile != null ? stabilityModel.SoilProfile.Geometry : null; } } /// /// Object of WaternetData /// [XmlCategory("Input")] [XmlElement("WaternetData")] public GeotechnicsModel Geotechnics { get { return stabilityModel.GeotechnicsData; } } /// /// Object of DefinitionModel /// [XmlCategory("Input")] [XmlElement("Definitions")] [Validate] public StabilityModel Stability { get { return stabilityModel; } set { if (stabilityModel != null && !ReferenceEquals(stabilityModel, value) && value != null) // Hack: StabilityCalculation sets this property to null so it can claim owner ship of StabilityModel { Stability.Dispose(); } stabilityModel = value; Soil.SoilPropertyManager = stabilityModel; } } public void SetStabilityModel(StabilityModel newStabilityModel) { stabilityModel = newStabilityModel; Soil.SoilPropertyManager = stabilityModel; } /// /// Units manager object /// [XmlCategory("Input")] public UnitsManager Units { get { return UnitsManager.Units; } } [XmlCategory("Output")] public CalculationResult Result { get { return result; } set { if (result != value) { DataEventPublisher.BeforeChange(this, "Result"); result = value; DataEventPublisher.AfterChange(this, "Result"); } } } [XmlCategory("Output")] [Impact(Impact.Descriptive)] [XmlIgnore] public SlidingModel SlidingData { get { return stabilityModel != null ? stabilityModel.SlidingModel : null; } set { if (stabilityModel != null) { if (value != stabilityModel.SlidingModel) { DataEventPublisher.BeforeChange(this, "SlidingData"); stabilityModel.SlidingModel = value; if (stabilityModel.SlidingModel == null) { Result = CalculationResult.NoRun; } DataEventPublisher.AfterChange(this, "SlidingCurve"); // to force update in UI DataEventPublisher.AfterChange(this, "SlidingData"); DataEventPublisher.AfterChange(this, "RestProfile"); DataEventPublisher.AfterChange(this, "SafeProfile"); } } } } [XmlIgnore] [Browsable(false)] [Impact(Impact.None)] public SlidingCurve SlidingCurve { get { return SlidingData != null ? MinimumSafetyCurve : null; } } [Impact(Impact.None)] public RestProfile RestProfile { get { return SlidingData != null ? SlidingData.RestProfile : null; } } [Impact(Impact.None)] public SafeProfile SafeProfile { get { return SlidingData != null ? SlidingData.SafeProfile : null; } } [XmlIgnore] [ReadOnly(true)] [Translation("Slices")] public IList MinimumSafetySlices { get { if (SlidingData != null && SlidingData.CurrentZone != null && MinimumSafetyCurve != null) { return MinimumSafetyCurve.Slices; } else { return new List(); } } } [Validate] public IGeometryModel GeometryDataModel { get { return Geometry; } } [Validate] public IGeometryModel WaternetDataModel { get { return stabilityModel.GeotechnicsData; } } public List Messages { get { return messages; } } public List GetMStabModels() { var models = new List() { this, Geometry, SoilModel, WaternetDataModel, stabilityModel, stabilityModel.ConsolidationMatrix, SlidingData }; return models; } public void ProcessResults(string inputString) { var deserializer = new XmlDeserializer(); try { MStabProject newProject = null; DataEventPublisher.InvokeWithoutPublishingEvents(() => { newProject = (MStabProject)deserializer.XmlDeserializeFromString(inputString, typeof(MStabProject)); }); SlidingData = newProject.SlidingData; newProject.Dispose(); } catch { } } public IEnumerable[] GetModelComponents() { var lists = new List(); lists.Add(Geometry.Points); lists.Add(Geometry.Curves); lists.AddRange(stabilityModel.GetModelLists()); return lists.ToArray(); } /// /// Update object counters /// public void UpdateCounters() { Geometry.UpdateCounters(); stabilityModel.UpdateCounters(); stabilityModel.GeotechnicsData.UpdateCounters(); SoilModel.UpdateCounters(); } /// /// Create Default Library Data /// public void CreateDefaultLibraryData() { SoilModel.CreateDefaultSoilLibrary(); } /// /// Renumbers the geometry items /// public void RenumberGeometryObjects() { UndoRedoManager.Instance.BeginAction(); // Renumbers all Geometry Points as well GeometryDataModel.RenumberGeometryObjects(); stabilityModel.RenumberGeometryObjects(); DataEventPublisher.Changed(this, "Renumbering"); DataEventPublisher.DoRefreshView(null); UndoRedoManager.Instance.EndAction(); } #region IVisibleEnabled Members public override bool IsVisible(string property) { switch (property) { case "RestProfile": case "SafeProfile": case "SlidingCurve": return SlidingData != null; case "MinimumSafetySlices": return SlidingData != null && SlidingData.CurrentZone != null && MinimumSafetyCurve != null; default: return base.IsVisible(property); } } #endregion public MStabProject Clone() { MStabProject clone = default(MStabProject); DataEventPublisher.InvokeWithoutPublishingEvents(() => { string xml = new XmlSerializer().SerializeToString(this); clone = new XmlDeserializer().XmlDeserializeFromString(xml); }); return clone; } public void Dispose() { DataEventPublisher.OnAfterChange -= DataEventPublisher_OnAfterChange; if (Stability != null) { Stability.Dispose(); //Stability = null; } } private SlidingCurve MinimumSafetyCurve { get { return SlidingData.CurrentZone.MinimumSafetyCurve; } } private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e) { if (HasComputationImpact(sender, e.Property)) { if (!UndoRedoManager.Instance.IsUndoingRedoing) { SlidingData = null; } } if (string.IsNullOrWhiteSpace(e.Property)) { return; } var slidingData = sender as SlidingModel; if (slidingData != null && e.Property == "CurrentZone") { DataEventPublisher.AfterChange(this, x => x.SlidingCurve); } } private static bool HasComputationImpact(object sender, string property) { return property != null && PropertyInfoSupport.GetPropertyInfo(sender.GetType(), property).GetImpact(sender) == Impact.Computation; } } }