1
cloudflight
2024-07-20 fab5d6e30b9a42a1f94524fca74ef227d2c963e4
Hydraulic/Hydro.MapUI/Map/MapViewer.cs
@@ -1,12 +1,16 @@
//using CloudWaterNetwork.Magnifier;
//using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Filtering;
using Hydro.CommonBase;
using Hydro.Inp;
using Hydro.Core.Model;
using Hydro.Core;
//using ConfigApp;
//using DevExpress.Diagram.Core.Layout;
//using DevExpress.DirectX.NativeInterop.Direct2D;
//using DevExpress.Utils.Extensions;
//using dict_py_Inner;
using Hydro.MapView;
using Hydro.MapView.Base;
using Hydro.MapView.Common;
using Newtonsoft.Json;
//using ReflectionManager_NameSpace;
@@ -16,20 +20,21 @@
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
//using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Remoting.Metadata;
//using System.Runtime.InteropServices.WindowsRuntime;
//using System.Runtime.Remoting.Metadata;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
//using System.Windows.Forms.DataVisualization.Charting;
//using static Hydro.Core.ObjectEnum;
using static Hydro.MapView.MapViewEnum;
using static System.Net.Mime.MediaTypeNames;
@@ -110,7 +115,9 @@
            this.showToolBar = showToolBar;
            InitializeComponent();
            //var propertyForm = new PropertyForm();
            //propertyForm.Dock = DockStyle.Right;
            //this.Controls.Add(propertyForm);
            MapCenter = PointF.Empty;
            zoom = 1.0f;
            DoubleBuffered = true;
@@ -138,6 +145,7 @@
        }
        private void MapViewer_Load(object sender, EventArgs e)
        {
            GlobalObject.PropertyForm = this.propertyForm;
            this.panel1.Visible = _showToolBar;
            this.statusStrip1.Visible = _showStatusBar;
@@ -165,10 +173,64 @@
            cb_Link_Colour.SelectedIndex = 0;
            map.Init(TC,new DrawDelegate[] { Draw, Draw, DrawBackGroud, DrawH },new MouseDelegate[] { mapMouseDown, mapMouseMove, mapMouseUp, mapMouseWheel });
            map.Init(TC, new DrawDelegate[] { Draw, Draw, DrawBackGroud, DrawH }, new MouseDelegate[] { mapMouseDown, mapMouseMove, mapMouseUp, mapMouseWheel });
            TContainer TCCube = new TContainer();
            TCCube.template = new Template();
            TCCube.template.network = new MapViewNetWork();
            TCCube.template.network.Areas = new List<AreaViewModel>();
            Cube cb = new Cube();
            foreach (var item in cb.FacesVertices)
            {
                AreaViewModel area = new AreaViewModel();
                area.ID = item.Key.ToString();
                area.InnerNodes = new List<NodeViewModel>();
                //area.Name= item.Key.ToString();
                //6个面设置不一样的颜色
                switch (item.Key)
                {
                    case 1:
                        area.color = Color.Red;
                        area.Name = "右";
                        break;
                    case 2:
                        area.color = Color.SkyBlue;
                        area.Name = "左";
                        break;
                    case 3:
                        area.color = Color.Green;
                        area.Name = "后";
                        break;
                    case 4:
                        area.color = Color.DarkBlue;
                        area.Name = "前";
                        break;
                    case 5:
                        area.color = Color.Purple;
                        area.Name = "上";
                        break;
                    case 6:
                        area.color = Color.Orange;
                        area.Name = "下";
                        break;
                    default:
                        area.color = Color.White;
                        break;
                }
                foreach (var p in item.Value)
                {
                    NodeViewModel node = new NodeViewModel();
                    node.X = p.X;
                    node.Y = p.Y;
                    node.Elev = p.Z;
                    area.InnerNodes.Add(node);
                }
                TCCube.template.network.Areas.Add(area);
            }
            CubeView.Init(TCCube, new DrawDelegate[] { Draw, Draw, DrawBackGroud, DrawH }, new MouseDelegate[] { CubeViewMouseDown, CubeViewMouseMove, CubeViewMouseUp, mapMouseWheel });
        }
@@ -393,6 +455,7 @@
            }
        }
        PointF[] getCurclePoints(int num)
        {
            PointF[] points = new PointF[num + 1];
@@ -416,12 +479,13 @@
        private void timer_draw_Tick(object sender, EventArgs e)
        {
            _mouseHoverCheckFlag = true;
            if (_timerDraw && map.Status==DrawingStatus.Ready)
            if (_timerDraw && map.Status == DrawingStatus.Ready)
            {
                this.map.Invalidate();
                this.CubeView.Invalidate();
                _timerDraw = false;
            }
        }
        protected override void OnResize(EventArgs e)
@@ -877,9 +941,8 @@
                MapObjectExtensions.AddCommand(_Network, "Add", null, list);
                _isInsertingObject = false;
                _isInsertingObject = false;
@@ -887,7 +950,6 @@
                _isInsertingObject = true;
                _undoOldValue = new PointF3D(0, 0, 0);
                _OperaNode = _newTemplate.network.Nodes.Find(node => node.ID == _newTemplate.Node1) as NodeViewModel;
                SetMapInvalidate();
                return;
@@ -928,6 +990,8 @@
                NodeViewModel minNode = null;
                foreach (NodeViewModel node in nodes)
                {
                    PointF mapPos = WorldPointToMapPoint(node);
                    PointF currentPoint = MapToScreen(mapPos);
                    float currentDist = 0;
@@ -1498,6 +1562,196 @@
        private int _lastMouseY;
        #endregion
        #region CubeView鼠标事件
        void CubeViewMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            _ClickStartPos = new PointF(e.X, e.Y);
            DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
            mousePosition = _MousePosition = ScreenToMap(new PointF(e.X, e.Y));
            if (e.Button == MouseButtons.Left && _isInsertingObject)
            {
            }
            else if (e.Button == MouseButtons.Left && _mouseState == MouseState.无)
            {
            }
            else if (e.Button == MouseButtons.Left && _mouseState == MouseState.设置底图范围)
            {
            }
            else if (e.Button == MouseButtons.Right)
            {
                RotaStartPos = new PointF(e.X, e.Y);
                Rotation0 = Rotation;
                俯视角度_start = RotationF;
                _lastCursor = this.Cursor;
                Cursor = Cursors.Hand;
                mapOption0 = mapOption.Copy();
                _isRotating = true;
            }
        }
        void CubeViewMouseMove(MouseEventArgs e)
        {
            //base.OnMouseMove(e);
            bool needInvalidate = false;
            _MousePosition = ScreenToMap(new PointF(e.X, e.Y));
            if (_isRotating)
            {
                mousePosition = _MousePosition;
                bool is下半屏幕 = RotaStartPos.Y >= this.map.Height / 2;
                if (ModifierKeys != Keys.Alt) Rotation = Rotation0 + ((float)e.X - (float)RotaStartPos.X) * 180 * 2.5 / (float)this.map.Width * (is下半屏幕 ? 1 : 1);
                if (ModifierKeys != Keys.Shift) RotationF = 俯视角度_start + ((float)e.Y - (float)RotaStartPos.Y) * 180 * 2.5 / (float)this.map.Height;
                if (RotationF > 90) RotationF = 90;
                if (RotationF < 0) RotationF = 0;
                needInvalidate = true;
            }
            //else
            /*判断是否触碰到对象*/
            if (_mouseHoverCheckFlag)
            {
                _mouseHoverCheckFlag = false;
                // 遍历所有对象,找出范围内的对象
                PointF clickedPoint = new PointF(e.X, e.Y);  //ScreenToMap(new PointF(e.X, e.Y));
                var obj = GetObj_by_ScreenPoint(clickedPoint);
                if (hoveredObjs.Count > 0 && hoveredObjs[0] == obj || hoveredObjs.Count == 0 && obj == null)
                {
                    //needInvalidate = false;
                }
                else
                {
                    needInvalidate = true;
                    hoveredObjs.ForEach(o => o.Hovered = false);
                    hoveredObjs.Clear();
                    if (obj != null)
                    {
                        obj.Hovered = true;
                        hoveredObjs.Add(obj);
                    }
                }
            }
            if (needInvalidate) this.SetMapInvalidate();
            label_mouse.Text = $"X:{e.X.ToString("0")} Y:{e.Y.ToString("0")} [Map]X:{_MousePosition.X.ToString("0.00")} Y:{_MousePosition.Y.ToString("0.00")}";
            _lastMouseX = e.X;
            _lastMouseY = e.Y;
        }
        void CubeViewMouseUp(MouseEventArgs e)
        {
            //BookMark    :鼠标抬起事件
            base.OnMouseUp(e);
            bool isMouseMoved = Get_dist(_ClickStartPos, new PointF(e.X, e.Y)) > 10;
            bool isdoubleClick = (DateTime.Now - _lastMouseUp).TotalMilliseconds <= doubleClick_Delay;
            _lastMouseUp = DateTime.Now;
            if /*点选*/(!isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.None)
            {
                _isDragging = false;
                // 遍历所有点,找出最近的点
                PointF clickedPoint = new PointF(e.X, e.Y);  //ScreenToMap(new PointF(e.X, e.Y));
                var obj = GetObj_by_ScreenPoint(clickedPoint);
                bool isJunction = obj is NodeViewModel;
                if (obj != null)
                {
                    selectedObjs.ForEach(o => o.Selected = false);
                    selectedObjs.Clear();
                    obj.Selected = true;
                    selectedObjs.Add(obj);
                    if (GlobalObject.PropertyForm != null)
                        GlobalObject.PropertyForm.SetObjs(selectedObjs);
                    _OperaNode = null;
                    SetMapInvalidate();
                    mousePosition = new PointF(0, 0);
                }
                else
                {
                    //GlobalObject.LockSelect
                    selectedObjs.ForEach(o => o.Selected = false);
                    selectedObjs.Clear();
                    if (GlobalObject.PropertyForm != null)
                        GlobalObject.PropertyForm.SetObjs(selectedObjs);
                    _OperaNode = null;
                    SetMapInvalidate();
                }
                return;
            }
            if (/*叠加点选*/!isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.Control)
            {
                _isDragging = false;
                // 遍历所有点,找出最近的点
                PointF clickedPoint = new PointF(e.X, e.Y);  //ScreenToMap(new PointF(e.X, e.Y));
                var obj = GetObj_by_ScreenPoint(clickedPoint);
                bool isJunction = obj is NodeViewModel;
                if (obj != null)
                {
                    if (selectedObjs.Contains(obj))
                    {
                        obj.Selected = false;
                        selectedObjs.Remove(obj);
                        if (GlobalObject.PropertyForm != null)
                            GlobalObject.PropertyForm.SetObjs(selectedObjs);
                        SetMapInvalidate();
                        mousePosition = new PointF(0, 0);
                    }
                    else
                    {
                        obj.Selected = true;
                        selectedObjs.Add(obj);
                        if (GlobalObject.PropertyForm != null)
                            GlobalObject.PropertyForm.SetObjs(selectedObjs);
                        SetMapInvalidate();
                        mousePosition = new PointF(0, 0);
                    }
                }
                return;
            }
            if (e.Button == MouseButtons.Right)
            {
                if (_isRotating)
                {
                    _isRotating = false;
                    this.Cursor = _lastCursor;
                    if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
                    mousePosition = new PointF(0, 0);
                }
            }
        }
        #endregion
        #endregion 一、全局
        #region 二、工具栏
@@ -1743,7 +1997,7 @@
        {
            if (_filePath == null) return;
            FileInfo fi = new FileInfo(_filePath);
            Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
            System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
            //System.Diagnostics.Process.Start("explorer.exe", fi.Directory.FullName);
        }
        private void EPA中打开ToolStripMenuItem_Click(object sender, EventArgs e)
@@ -1751,7 +2005,7 @@
            if (_filePath == null) return;
            Process.Start(@"epanetH\Epanet2wH.exe", _filePath);
            System.Diagnostics.Process.Start(@"epanetH\Epanet2wH.exe", _filePath);
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
@@ -1946,7 +2200,7 @@
        }
        bool Buzylock = false;
        private void MapViewer_KeyDown(object sender, KeyEventArgs e)
        public void MapViewer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
@@ -2469,11 +2723,198 @@
        }
        private void btn_初分配_Click(object sender, EventArgs e)
        {
            //string result = null;
            //if ((result=_Template.network.CheckValidate())!=null)
            //{
            //    MessageBox.Show(result);
            //    return;
            //}
            List<WaterEquivalentSettings> settings = new List<WaterEquivalentSettings>();
            settings.Add(new WaterEquivalentSettings()
            {
                waterEquivalents = new WaterEquivalentTemplate()
                {
                    ID = "1",
                    Name = "1",
                    WaterEquivalentCollection = new WaterEquivalentCollection()
                    {
                        new WaterEquivalent()
                        {
                            ID=1008,
                            MinRatedFlow=0.48f,
                            RatedFlow=0.72f,
                            Count=1,
                        },
                        new WaterEquivalent()
                        {
                            ID=1009,
                            MinRatedFlow=0.88f,
                            RatedFlow=0.96f,
                            Count=2,
                        },
                        new WaterEquivalent()
                        {
                            ID=1010,
                            MinRatedFlow=1.08f,
                            RatedFlow=1.08f,
                            Count=3,
                        }
                    }
                },
                Meters = new List<string>()
                {
                    //按以下规律生成数组到M10,"M1","M2"
                        "M1","M2","M3","M4","M5","M6","M7","M8","M9","M10","M11","M12","M13","M14","M15","M16",
                    "M17","M18","M19","M20","M21","M22","M23","M24","M25","M26","M27","M28","M29","M30"
                },
            });
            settings.Add(new WaterEquivalentSettings()
            {
                waterEquivalents = new WaterEquivalentTemplate()
                {
                    ID = "1",
                    Name = "1",
                    WaterEquivalentCollection = new WaterEquivalentCollection()
                    {
                        new WaterEquivalent()
                        {
                            ID=2008,
                            MinRatedFlow=0.48f,
                            RatedFlow=0.48f,
                            Count=1,
                        },
                        new WaterEquivalent()
                        {
                            ID=2009,
                            MinRatedFlow=0.12f,
                            RatedFlow=0.12f,
                            Count=5,
                        },
                        new WaterEquivalent()
                        {
                            ID=2010,
                            MinRatedFlow=0.16f,
                            RatedFlow=0.18f,
                            Count=6,
                        }
                    }
                },
                Meters = new List<string>()
                {
                    //按以下规律生成数组到M10,"M1","M2"
                       "M31","M32","M33","M34","M35","M36","M37","M38","M39","M40","M41","M42","M43","M44","M45","M46","M47","M48","M49","M50","M51","M52","M53","M54","M55","M56","M57","M58","M59","M60","M61","M62","M63","M64","M65","M66","M67","M68","M69","M70","M71","M72","M73","M74","M75","M76","M77","M78","M79","M80"
                },
            });
            var head = _Network.reservoirs.FirstOrDefault().Head;
            _Network.WaterDistribution(_Template.FullPath, GlobalPath.configPath + "config_waterDistri.wdb", settings, 40, head);
        }
        private void btn_拓扑检查_Click(object sender, EventArgs e)
        {
            Dictionary<string, List<string>> result = _Template.network.CheckValidate();
            if (result.Count > 0)
            {
                //ListBox listBox2 = new ListBox();
                //listBox2.Dock = DockStyle.Fill;
                ////listBox2的item为什么显示不出来?
                //listBox2.SelectedIndexChanged += (s, e) =>
                //{
                //    if (listBox2.SelectedIndex >= 0)
                //    {
                //        List<IBaseViewModel> Objs = new List<IBaseViewModel>();
                //        Objs.AddRange(_Nodes.Select(n=>(NodeViewModel)n));
                //        Objs.AddRange(_Links.Select(l => (LinkViewModel)l));
                //        var obj = Objs.Find(o => o.ID == listBox2.SelectedItem.ToString());
                //        if (obj != null)
                //        {
                //            obj.Selected = true;
                //            selectedObjs.Add((NodeViewModel)obj);
                //            SetMapInvalidate();
                //        }
                //    }
                //};
                ListBox listBox1 = new ListBox();
                listBox1.Dock = DockStyle.Fill;
                listBox1.Width = 200;
                listBox1.Height = 400;
                listBox1.Items.AddRange(result.Keys.ToArray());
                listBox1.SelectedIndexChanged += (s, e) =>
                {
                    //listBox2.Items.Clear();
                    if (listBox1.SelectedIndex >= 0)
                    {
                        var arr = result[listBox1.SelectedItem.ToString()].ToHashSet();
                        List<IBaseViewModel> Objs = new List<IBaseViewModel>();
                        Objs.AddRange(_Nodes.Select(n => (NodeViewModel)n));
                        Objs.AddRange(_Links.Select(l => (LinkViewModel)l));
                        var nodes = _Nodes.FindAll(o => arr.Contains(o.ID));
                        var links = _Links.FindAll(o => arr.Contains(o.ID));
                        //listBox2.Items.AddRange(result[listBox1.SelectedItem.ToString()].ToArray());
                        selectedObjs.ForEach(obj => obj.Selected = false);
                        selectedObjs.Clear();
                        nodes.ForEach(obj =>
                        {
                            obj.Selected = true;
                            selectedObjs.Add((NodeViewModel)obj);
                        });
                        links.ForEach(obj =>
                        {
                            obj.Selected = true;
                            selectedObjs.Add((LinkViewModel)obj);
                        });
                        if (GlobalObject.PropertyForm != null)
                            GlobalObject.PropertyForm.SetObjs(selectedObjs);
                        SetMapInvalidate();
                    }
                };
                Form ResultForm = new Form();
                ResultForm.Text = "拓扑检查结果";
                ResultForm.Width = 300;
                ResultForm.Height = 400;
                ResultForm.StartPosition = FormStartPosition.CenterScreen;
                ResultForm.Controls.Add(listBox1);
                ResultForm.MinimizeBox = false;
                ResultForm.MaximizeBox = false;
                //ResultForm.Controls.Add(listBox2);
                //listBox2.Invalidate();
                ResultForm.Show();
                //新建一个窗口,显示检查结果
                //CheckForm checkForm = new CheckForm();
                //checkForm.Show();
            }
            else
            {
                MessageBox.Show("拓扑检查通过");
            }
            //if (!string.IsNullOrEmpty(result))
            //{
            //    MessageBox.Show(result);
            //    return;
            //}
        }
        public void toolStripButton_计算_Click(object sender, EventArgs e)
        {
            //LoadData();
            if (_Template == null)
            {
                MessageBox.Show("请先打开文件");
                return;
            }
            _Network.Calc(_Template.FullPath, GlobalPath.configPath + "config_calc.wdb");
        }
@@ -3517,7 +3958,7 @@
            {
                var backgroundImagePath = openFileDialog.FileName;
                Global.ClearFileReadOnly(_Template.BackGroundImg_FullPath);
                File.Copy(backgroundImagePath, _Template.BackGroundImg_FullPath, true);
                FileCopy.Copy(backgroundImagePath, _Template.BackGroundImg_FullPath, true);
                设置底图ToolStripMenuItem_Click(1, new EventArgs());
            }
        }
@@ -3575,13 +4016,13 @@
        {
            if (_filePath == null) return;
            FileInfo fi = new FileInfo(_filePath);
            Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
            System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
        }
        private void label_file_Click(object sender, EventArgs e)
        {
            if (_filePath == null) return;
            FileInfo fi = new FileInfo(_filePath);
            Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
            System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
        }
        private void 设为立管点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
@@ -3755,7 +4196,7 @@
        bool _isShowDirection = false;
        private void 颜色分级管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form_Colour form_Colour = new Form_Colour(_Template.Colours,_Template);
            Form_Colour form_Colour = new Form_Colour(_Template.Colours, _Template);
            form_Colour.Show(this);
        }
@@ -3766,18 +4207,30 @@
        private void cb_Link_Colour_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_Template == null) return;
            var type = cb_Link_Colour.SelectedItem == null ? ColourType.管线流量 : (ColourType)cb_Link_Colour.SelectedItem;
            mapOption.ColourLink = type;
            var doubles = _Template.network.getMinMax(type);
            var colour = new Colour(type, null, type.ToString());
            List<ColourItem> colours = Colour.GetColourItems((float)doubles[0], (float)doubles[1], 5, Color.Gray, Color.Red);
            colour.Items = colours;
            LinkColour = colour;
            this.SetMapInvalidate();
        }
        private void cb_Node_Colour_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_Template == null) return;
            var type = cb_Node_Colour.SelectedItem == null ? ColourType.节点自由压力 : (ColourType)cb_Node_Colour.SelectedItem;
            mapOption.ColourNode = type;
            var doubles = _Template.network.getMinMax(type);
            var colour = new Colour(type, null, type.ToString());
            List<ColourItem> colours = Colour.GetColourItems((float)doubles[0], (float)doubles[1], 5, Color.Gray, Color.Red);
            colour.Items = colours;
            NodeColour = colour;
            this.SetMapInvalidate();
        }
        private void label_ZZ_Click(object sender, EventArgs e)