Shuxia Ning
2024-12-23 a97513ccbb97b2c471e90b032fabdd2e709b7f32
WinFrmUI/Yw.WinFrmUI.Hydro.Q3d.Core/Map/Drawer.cs
@@ -1,6 +1,4 @@
using Yw.WinFrmUI.Q3d;
using Newtonsoft.Json;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
@@ -18,15 +16,16 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Yw.WinFrmUI.Q3d.MapViewEnum;
using Yw.WinFrmUI.Q3d;
using Yw.WinFrmUI.Q3d;
using Yw.WinFrmUI.Q3d;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.AxHost;
using static System.Windows.Forms.LinkLabel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
using static Yw.WinFrmUI.Q3d.MapViewEnum;
using Cursor = System.Windows.Forms.Cursor;
using Yw.WinFrmUI.Q3d;
using Yw.WinFrmUI.Q3d;
namespace Yw.WinFrmUI.Q3d
{
@@ -47,12 +46,24 @@
            InitializeComponent();
            this.DoubleBuffered = true;
            MapCenter = PointF.Empty;
            MapCenter = new PointF3D(0, 0, 0);
            zoom = 1.0f;
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
        }
        public void SetDefaultView()
        {
            mapOption0 = mapOption.Copy();
            InitCenter();
            RotationF = 45;
            Rotation = -45;
            MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
            SetMapInvalidate();
        }
        public void SetEditMode(bool isEditMode)
        {
@@ -82,7 +93,7 @@
            this.Inited = true;
            this.Status = DrawingStatus.Ready;
            SetDefaultView();
            //TContainer TCCube = new TContainer();
            //TCCube.settings = new Settings();
            //TCCube.settings.network = new MapViewNetWork();
@@ -170,7 +181,7 @@
        public void Clear()
        {
            _Template = null;
            MapCenter = PointF.Empty;
            MapCenter = new PointF3D(0, 0, 0);
            zoom = 1.0f;
            Rotation = 0;
            RotationF = 90;
@@ -213,19 +224,21 @@
            //将RotationF.ToString("0")通过RotationFChanged传出
            RotationFChanged?.Invoke(this, RotationF.ToString("0"));
            SetDefaultView();
        }
        private void InitCenter()
        {
            //MapCenter
            var p = PointF.Empty;
            var p = new PointF3D(0, 0, 0);
            float x0 = 99999999999f, y0 = 99999999999f, x1 = -99999999999f, y1 = -99999999999f;
            foreach (NodeViewModel junction in _Nodes)
            {
                p.X += (float)junction.X;
                p.Y += (float)junction.Y;
                p.Z += (float)junction.Z;
                if (x0 > junction.X) x0 = junction.X;
                if (y0 > junction.Y) y0 = junction.Y;
                if (x1 < junction.X) x1 = junction.X;
@@ -246,6 +259,7 @@
            {
                p.X /= _Nodes.Count;
                p.Y /= _Nodes.Count;
                p.Z /= _Nodes.Count;
            }
            MapCenter = p;
        }
@@ -411,7 +425,8 @@
            {
                _mouseState = MouseState.无;
                DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
                BackGroudPicLeftPos = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y)), _Template.BackGroundElev);
                var p3d = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y)), _Template.BackGroundElev);
                BackGroudPicLeftPos = new PointF(p3d.X, p3d.Y);
                _isSettingBackGroundPictur = true;
            }
            else if (e.Button == MouseButtons.Right)
@@ -424,6 +439,12 @@
                Cursor = Cursors.Hand;
                mapOption0 = mapOption.Copy();
                _isRotating = true;
                //如果有锚点
                if (hoveredObjs.Count > 0 && hoveredObjs[0] is NodeViewModel node)
                {
                    _OperaNode = node;
                    _HookPoint = MapToScreen(WorldPointToMapPoint(node));
                }
            }
        }
@@ -475,12 +496,12 @@
            }
            else if (_isPanning)
            {
                var vector = GetRotateVector(new PointF(e.X, e.Y), new PointF(_lastMouseX, _lastMouseY));
                MapCenter = new PointF(MapCenter.X - vector.X / Zoom.X, MapCenter.Y - vector.Y / Zoom.Y);
                var vector = GetWorldVectorByScreenPoints(new PointF(e.X, e.Y), new PointF(_lastMouseX, _lastMouseY));
                MapCenter = new PointF3D(MapCenter.X - vector.X, MapCenter.Y - vector.Y, MapCenter.Z - vector.Z);
                //将$"{MapCenter.X.ToString("0.00")},{MapCenter.Y.ToString("0.00")}"通过CenterChanged传出
                CenterChanged?.Invoke(this, $"{MapCenter.X.ToString("0.00")},{MapCenter.Y.ToString("0.00")}");
                CenterChanged?.Invoke(this, $"{MapCenter.X.ToString("0.00")},{MapCenter.Y.ToString("0.00")},{MapCenter.Z.ToString("0.00")}");
                needInvalidate = true;
            }
@@ -630,7 +651,8 @@
            if /*设置背景*/(e.Button == MouseButtons.Left && _isSettingBackGroundPictur)
            {
                _Template.BackGroundPoint1 = BackGroudPicLeftPos;
                _Template.BackGroundPoint2 = MapPointToWorldPoint(mousePosition, _Template.BackGroundElev);
                var p3d = MapPointToWorldPoint(mousePosition, _Template.BackGroundElev);
                _Template.BackGroundPoint2 = new PointF(p3d.X, p3d.Y);
                _isSettingBackGroundPictur = false;
                mapOption.isShowPic = true;
@@ -874,6 +896,9 @@
                _isRotating = false;
                this.Cursor = _lastCursor;
                if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
                _OperaNode = null;
                _HookPoint = new PointF(-1, -1);
                MapCenter0 = MapCenter;
                mousePosition = new PointF(0, 0);
                //将Rotation.ToString("0")通过RotationChanged传出
@@ -887,7 +912,8 @@
            z = 0;
            if (RotationF != 0)
            {
                p = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y), z));
                var p3d = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y), z));
                p = new PointF(p3d.X, p3d.Y);
            }
            else
@@ -902,7 +928,8 @@
            z = j.Z;
            if (RotationF != 0)
            {
                p = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y), j.Z), j.Z);
                var p3d = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y), j.Z), j.Z);
                p = new PointF(p3d.X, p3d.Y);
            }
            else
@@ -1682,10 +1709,11 @@
                position = obj.Position;
            PointF currentPos = MapToScreen(WorldPointToMapPoint(position, obj.Z));
            PointF centerScreen = new PointF(this.Width / 2, this.Height / 2);
            var vector = GetRotateVector(centerScreen, currentPos);
            MapCenter = new PointF(
            MapCenter.X - vector.X / Zoom.X,
            MapCenter.Y - vector.Y / Zoom.Y);
            var vector = GetWorldVectorByScreenPoints(centerScreen, currentPos);
            MapCenter = new PointF3D(
            MapCenter.X - vector.X,
            MapCenter.Y - vector.Y,
            MapCenter.Z - vector.Z);
        }
        bool ctrlPressed = false;
@@ -2664,6 +2692,27 @@
            return projectedPoints;
        }
        public void SetSelectObj(List<string>  ids)
        {
            //构造两个字典,一个是节点,一个是管道
            var dict_nodes = _Nodes.ToDictionary(o => o.ID);
            var dict_links = _Links.ToDictionary(o => o.ID);
            //遍历所有的id,找到对应的对象
            foreach (var id in ids)
            {
                if (dict_nodes.ContainsKey(id))
                {
                    dict_nodes[id].Selected = true;
                    selectedObjs.Add(dict_nodes[id]);
                }
                else if (dict_links.ContainsKey(id))
                {
                    dict_links[id].Selected = true;
                    selectedObjs.Add(dict_links[id]);
                }
            }
        }
        bool _isSettingBackGroundPictur = false;