duheng
2025-03-21 c0be7c001375f73e2c3b49a1b1d447b6fc297bdd
WinFrmUI/PBS.WinFrmUI.Hydro/02-quick-modeling/02-place/QuickModelingPlaceWizardPage.cs
@@ -1,4 +1,6 @@
namespace PBS.WinFrmUI.Hydro
using Microsoft.Web.WebView2.Core;
namespace PBS.WinFrmUI.Hydro
{
    public partial class QuickModelingPlaceWizardPage : DevExpress.XtraEditors.XtraUserControl, IWizardPageAsync<QuickModelingViewModel>
    {
@@ -6,9 +8,7 @@
        {
            InitializeComponent();
            this.layoutControl1.SetupLayoutControl();
            this.mapSetSimpleMarkerContainer1.LoadCompletedEvent += MapSetSimpleMarkerContainer1_LoadCompletedEvent;
            this.mapSetSimpleMarkerContainer1.SetMarkerEvent += MapSetSimpleMarkerContainer1_SetMarkerEvent;
            this.imgCmbPlaceType.Properties.AddEnum<PBS.ePlaceType>();
             this.imgCmbPlaceType.Properties.AddEnum<PBS.ePlaceType>();
        }
        /// <summary>
@@ -18,7 +18,7 @@
        private QuickModelingViewModel _vm = null;//操作对象
        private bool _isCompleted = false;//是否创建完成 
        private bool _isInitialMap = false;//是否初始化地图
        /// <summary>
@@ -33,9 +33,10 @@
            _vm = vm;  
            _isCompleted = false;
            if (!this.mapSetSimpleMarkerContainer1.IsInitialized)
            if (!_isInitialMap)
            {
                await this.mapSetSimpleMarkerContainer1.InitialContainer();
                this.webView.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "/web/html/map_select.html");
                this.webView.WebMessageReceived += WebView_WebMessageReceived;
            } 
            this.txtName.EditValue = _vm.Place.Name;
@@ -51,22 +52,220 @@
            this.PageStateChangedEvent?.Invoke();
        }
        //加载完成事件
        private async void MapSetSimpleMarkerContainer1_LoadCompletedEvent()
        public class AmapLocationViewModel
        {
            if (_vm.Marker != null)
            public AmapLocationViewModel()
            {
                await this.mapSetSimpleMarkerContainer1.LoadMarker(_vm.Marker);
                location = new Location();
            }
            public string id { get; set; }
            public string name { get; set; }
            public string address { get; set; }
            public Location location { get; set; }
            public long BuildTypeID { get; set; }
            public string NorthEast { get; set; }
            public string SouthWest { get; set; }
        }
        public class FixationReceivedViewModel
        {
            public string Oper { get; set; }
            public string Json { get; set; }
        }
        public new class Location
        {
            public float lng { get; set; }
            public float lat { get; set; }
        }
        public class Coordinate
        {
            public Coordinate(double lat, double lon)
            {
                this.lat = lat;
                this.lon = lon;
            }
            /// <summary>
            /// 纬度
            /// </summary>
            public double lat { get; set; }
            /// <summary>
            /// 经度
            /// </summary>
            public double lon { get; set; }
        }
        public static class AmapBounds
        {
            public static double Distance(Coordinate coordinate1, Coordinate coordinate2)
            {
                var a = coordinate1;
                var lat1 = a.lat;
                var lon1 = a.lon;
                var b = coordinate2;
                var lat2 = b.lat;
                var lon2 = b.lon;
                if (lat1 == lat2 && lon1 == lon2)
                {
                    return 0;
                }
                a.lon = VC(a.lon, -180, 180);
                a.lat = aD(a.lat, -74, 74);
                b.lon = VC(b.lon, -180, 180);
                b.lat = aD(b.lat, -74, 74);
                double ret;
                ret = JF(JK(a.lon), JK(b.lon), JK(a.lat), JK(b.lat));
                return ret;
            }
            public static double aD(double a, double b, double c)
            {
                if (b != null)
                {
                    a = Math.Max(a, b);
                }
                if (b != null)
                {
                    a = Math.Min(a, c);
                }
                return a;
            }
            public static double VC(double a, double b, double c)
            {
                if (a > c)
                {
                    a -= c - b;
                }
                if (a < b)
                {
                    a += c - b;
                }
                return a;
            }
            public static double JK(double a)
            {
                double ret = Math.PI * a / 180.0;
                return ret;
            }
            public static double JF(double a, double b, double c, double e)
            {
                const double Ou = 6370996.81;
                return Ou * Math.Acos(Math.Sin(c) * Math.Sin(e) + Math.Cos(c) * Math.Cos(e) * Math.Cos(b - a));
            }
        }
        //设置
        private void MapSetSimpleMarkerContainer1_SetMarkerEvent(Yw.Model.Map.Marker obj)
        private async void CaptureImage()
        {
            _vm.Marker = obj;
            this.txtAddress.EditValue = obj.Address;
            await webView.EnsureCoreWebView2Async();
            CoreWebView2 coreWebView = webView.CoreWebView2;
            int pageWidth = webView.ClientSize.Width;
            int pageHeight = webView.ClientSize.Height;
            webView.Width = pageWidth;
            webView.Height = pageHeight;
            var directory = Path.GetDirectoryName(_vm.tempBackgroundImageUrl);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            var fileStream = new FileStream(_vm.tempBackgroundImageUrl + "_t.png", FileMode.Create);
            await coreWebView.CapturePreviewAsync(CoreWebView2CapturePreviewImageFormat.Png, fileStream);
            var bt = new Bitmap(fileStream);
            var pWidth = pageWidth - 50;
            var pHeight = pageHeight - 50;
            try
            {
                bt.Clone(new Rectangle(50, 50, pWidth, pHeight), System.Drawing.Imaging.PixelFormat.DontCare).Save(_vm.tempBackgroundImageUrl);
            }
            catch (Exception ex)
            {
            }
            fileStream.Close();
            var screen = Screen.PrimaryScreen;
            var bound = screen.Bounds;
            var bo = (double)bound.Height / bound.Width;
            var dpi = Math.Round(bo, 3);//0.56
            fileStream.Dispose();
            #region 长宽计算
            double lat = Convert.ToDouble(_vm.NorthEast.Split(',')[1]);
            double lon = Convert.ToDouble(_vm.NorthEast.Split(',')[0]);
            //右上角坐标
            Coordinate coordinate1 = new Coordinate(lat, lon);
            double lat2 = Convert.ToDouble(_vm.SouthWest.Split(',')[1]);
            double lon2 = Convert.ToDouble(_vm.SouthWest.Split(',')[0]);
            //左下角坐标
            Coordinate coordinate2 = new Coordinate(lat2, lon2);
            //右下角坐标
            Coordinate coordinate3 = new Coordinate(coordinate1.lat, coordinate2.lon);
            //左上角坐标
            Coordinate coordinate4 = new Coordinate(coordinate2.lat, coordinate1.lon);
            //调用Distance方法计算两点之间的距离
            //帮我计算coordinate1和coordinate4之间的距离
            double distanceHeight = AmapBounds.Distance(coordinate1, coordinate4);
            double distanceWidth = AmapBounds.Distance(coordinate1, coordinate3);
            _vm.BackgroundWidth = distanceWidth;
            _vm.BackgroundHeight = distanceHeight;
            var w = pageWidth / _vm.BackgroundWidth;
            var h = pageHeight / _vm.BackgroundHeight;
            _vm.Zoom = w > h ? w : h;
            #endregion 长宽计算
            _vm.IsCaptureImage = true;
            File.Delete(_vm.tempBackgroundImageUrl + "_t.png");
        }
        private void WebView_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
        {
            var j = JsonHelper.Json2Object<FixationReceivedViewModel>(e.WebMessageAsJson);
            switch (j.Oper)
            {
                case "loadCompleted":
                    var obl = JsonHelper.Json2Object<AmapLocationViewModel>(j.Json);
                    _vm.NorthEast = obl.NorthEast;
                    _vm.SouthWest = obl.SouthWest;
                    break;
                case "save":
                    if (string.IsNullOrEmpty(_vm.SouthWest))
                    {
                        TipFormHelper.ShowWarn("先请输入关键字查找到建筑!");
                        return;
                    }
                    CaptureImage();
                    break;
                case "getMapBounds":
                    var ob = JsonHelper.Json2Object<AmapLocationViewModel>(j.Json);
                    _vm.NorthEast = ob.NorthEast;
                    _vm.SouthWest = ob.SouthWest;
                    break;
                case "getLocation":
                    var o = JsonHelper.Json2Object<AmapLocationViewModel>(j.Json);
                    _vm.Marker = new Yw.Model.Map.Marker();
                    _vm.Marker.Point = new Yw.Model.Map.Point(o.location.lng, o.location.lat);
                    _vm.Marker.Address = o.name + o.address;
                    this.txtAddress.Text = _vm.Marker.Address;
                    break;
                case "showMessage":
                    TipFormHelper.ShowInfo(j.Json.ToString());
                    break;
            }
        }
        //场所类型变换
@@ -93,11 +292,16 @@
                {
                    isExist = false;
                }
                if (_vm.Marker==null)
                if (!_vm.IsCaptureImage)
                {
                    isExist = false;
                    TipFormHelper.ShowWarn("请设置地图信息!");
                }
                //if (_vm.Marker==null)
                //{
                //    isExist = false;
                //    TipFormHelper.ShowWarn("请设置地图信息!");
                //}
                return isExist;
            });
        }