// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of the D-Soil Model application. // // The D-Soil Model application 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; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using Deltares.DSoilModel.Data; using Deltares.Geometry; using Deltares.Geometry.Forms; using Deltares.Mathematics; using Deltares.Standard.EventPublisher; using Deltares.Standard.Forms; using Deltares.Standard.Logging; namespace Deltares.DSoilModel.Forms { /// /// Class for drawing a SoilSegmentSplitLocation on a 2D geometry. /// public class DrawingSoilSegmentSplitLocation : DrawingObject { private const double percentageWidthForDrawing = 0.005; private SoilSegmentSplitLocation splitLocation; private GeometryPoint insertionPoint = new GeometryPoint(0, 0, 0); private double bottom = -20; private object dataObj; private double top = 10; private double width = 1; /// /// Gets the local X coordinate (meters). /// public double X { get { return (splitLocation != null) ? splitLocation.XCoordinate : insertionPoint.X; } } /// /// Gets the left edge X coordinate (meters). /// public double Left { get { return X - width / 2; } } /// /// Gets the right edge X coordinate (meters). /// public double Right { get { return X + width / 2; } } /// /// Top coordinate for drawing (meters). /// public double TopForDrawing() { if (splitLocation.SoilProfile2D == null || splitLocation.SoilProfile2D.Geometry == null) { return 0; } var diff = GetOffsetFromGeometryHeight(); return splitLocation.SoilProfile2D.Geometry.MaxGeometryPointsZ + 0.05 * diff; } /// /// Bottom coordinate for drawing (meters). /// public double BottomForDrawing() { if (splitLocation.SoilProfile2D == null || splitLocation.SoilProfile2D.Geometry == null) { return 0; } var diff = GetOffsetFromGeometryHeight(); return Math.Min(splitLocation.SoilProfile2D.Geometry.MinGeometryPointsZ, splitLocation.SoilProfile2D.Geometry.Bottom) - 0.05 * diff; } /// /// Width for drawing (meters). /// public double WidthForDrawing() { if (splitLocation.SoilProfile2D == null || splitLocation.SoilProfile2D.Geometry == null) { return 1; } return Math.Abs(splitLocation.SoilProfile2D.Geometry.MaxGeometryPointsX - splitLocation.SoilProfile2D.Geometry.MinGeometryPointsX); } /// /// Sets the data object (SoilSegmentSplitLocation). /// public override void SetDataObject(object dataObject) { dataObj = dataObject; splitLocation = null; var segmentSplitLocation = dataObject as SoilSegmentSplitLocation; if (segmentSplitLocation != null) { splitLocation = segmentSplitLocation; top = TopForDrawing(); bottom = BottomForDrawing(); width = WidthForDrawing() * percentageWidthForDrawing; insertionPoint = new GeometryPoint(splitLocation.XCoordinate, 0, bottom); } } /// /// Gets the data object (SoilSegmentSplitLocation). /// public override object GetDataObject() { return dataObj; } /// /// Draws the drawing object. /// public override void Draw3D(GraphicsInfo info) { try { if (double.IsNaN(splitLocation.XCoordinate)) { InitializeInsertionPoint(info); } DrawingSupport.SetIEPDraw3D(info.Graphics); var topLeft = info.Projection.ConvertToScreen(new Point2D(Left, top)); var bottomRight = info.Projection.ConvertToScreen(new Point2D(Right, bottom)); var clipRect = new Rectangle(topLeft.X - 1, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); var isValid = RealTimeBackgroundValidator.ValidationResults.All(vr => vr.Subject != splitLocation); var rectangleColor = isValid ? Color.WhiteSmoke : Color.OrangeRed; var rectangleColor2 = isValid ? Color.LightGray : Color.LightCoral; var penColor = isValid ? Color.LawnGreen : Color.Red; if (info.Selected) { info.Graphics.FillRectangle(new HatchBrush(HatchStyle.LargeCheckerBoard, rectangleColor, rectangleColor2), clipRect); info.Graphics.DrawRectangle(new Pen(penColor, 2), clipRect); } else { info.Graphics.FillRectangle(new SolidBrush(rectangleColor), clipRect); info.Graphics.DrawRectangle(new Pen(penColor, 1), clipRect); } } catch (Exception ex) { LogManager.Add(new LogMessage(LogMessageType.Error, ex, ex.Message)); } } /// /// Draws the label. /// public override void DrawLabel(GraphicsInfo info) { var color = Color.LightGray; var text = splitLocation.ToString(); var textBrush = Brushes.Black; var backgroundBrush = new SolidBrush(DrawingSupport.GetLighterColor(color)); var borderBrush = new SolidBrush(DrawingSupport.GetDarkerColor(color)); var rectangle = GetExtents(info.Projection); if (rectangle != null) { var rect = (Rectangle)rectangle; var centerX = (rect.Left + rect.Right) / 2; var centerY = (rect.Top + rect.Bottom) / 2; DrawingSupport.DrawLabelAtPoint(info, new Point(centerX, centerY), text, textBrush, backgroundBrush, borderBrush); } } /// /// Called when the user initiates a drag-drop operation. /// public override void OnBeginDrag(IProjection projection, Point3D aBeginDragDropPoint, bool isIndividualDrag) { if (splitLocation != null) { DataEventPublisher.BeforeChange(splitLocation, "xCoordinate"); DrawingPoint.BeginMovePoints(); } } /// /// Updates the data during drag drop. /// public override void UpdateDataDragDrop(Point3D beginDragPoint, Vector3D vector) { if (splitLocation != null) { insertionPoint.X = splitLocation.XCoordinate; DrawingPoint.Move(insertionPoint, vector, beginDragPoint); splitLocation.XCoordinate = insertionPoint.X; insertionPoint.Z = bottom; } } /// /// Called when [end drag]. /// public override void OnEndDrag(IProjection projection, Point3D aCurrentDragPoint, Vector3D aVect3D) { if (splitLocation != null) { splitLocation.XCoordinate = insertionPoint.X; DataEventPublisher.AfterChange(splitLocation, "xCoordinate"); insertionPoint.Z = 0; } } /// /// Determines whether the specified projection contains the given point. /// public override bool ContainsPoint(IProjection projection, Point point) { var worldPoint = projection.ConvertToWorld(point); return (worldPoint.X >= Left) && (worldPoint.X <= Right) && (worldPoint.Y >= bottom) && (worldPoint.Y <= top); } /// /// Gets the extents in pixel coordinates. /// public override Rectangle? GetExtents(IProjection projection) { var topLeft = new Point2D(Left, top); var bottomRight = new Point2D(Right, bottom); var topLeftScreen = projection.ConvertToScreen(topLeft); var bottomRightScreen = projection.ConvertToScreen(bottomRight); return new Rectangle(topLeftScreen.X, topLeftScreen.Y, bottomRightScreen.X - topLeftScreen.X, bottomRightScreen.Y - topLeftScreen.Y); } private double GetOffsetFromGeometryHeight() { var diff = splitLocation.SoilProfile2D.Geometry.MaxGeometryPointsZ - Math.Min(splitLocation.SoilProfile2D.Geometry.MinGeometryPointsZ, splitLocation.SoilProfile2D.Geometry.Bottom); return diff; } private void InitializeInsertionPoint(GraphicsInfo info) { // Todo: Should take mouse position from before the context menu item was clicked // or the x coordinate of the left border of the context menu, but for now we // use the current mouse position. var worldCoordinatesX = info.Projection.ConvertToWorld(info.MousePosition).X; var soilProfile2D = splitLocation.SoilProfile2D; // Check whether mouse position is within the bounds of the soil profile if (soilProfile2D.Geometry.Left < worldCoordinatesX && worldCoordinatesX < soilProfile2D.Geometry.Right) { // If the mouse position is within bounds, insert the // Specific Mechanism Point Location near that X coordinate insertionPoint.X = Math.Floor(worldCoordinatesX); } else { // If the mouse position is outside the soil profile, insert the // Specific Mechanism Point Location in the middle of the soil profile var midLength = Math.Abs(soilProfile2D.Geometry.Right - soilProfile2D.Geometry.Left) * 0.5; insertionPoint.X = soilProfile2D.Geometry.Left + midLength; } splitLocation.XCoordinate = insertionPoint.X; } } }