From dbff3d984eb99f474ff63ee6daaadc2c00eee620 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 04 九月 2024 13:49:06 +0800
Subject: [PATCH] 1

---
 TEWin/FrmWin.cs |  421 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 401 insertions(+), 20 deletions(-)

diff --git a/TEWin/FrmWin.cs b/TEWin/FrmWin.cs
index 6bdb6cb..88d7491 100644
--- a/TEWin/FrmWin.cs
+++ b/TEWin/FrmWin.cs
@@ -7,7 +7,9 @@
 using System.Drawing;
 using System.IO;
 using System.Linq;
+using System.Runtime.InteropServices;
 using System.Text;
+using System.Threading;
 using System.Windows.Forms;
 using TerraExplorerX;
 using TETree;
@@ -24,27 +26,51 @@
 
         FrmTool tool;
 
-        string mainUrl;
+        bool isAngle;
+
+        int angleCount;
+
+        double angleSize;
 
         public SGWorld71 SG;
 
-        private bool isAngle;
+        System.Timers.Timer timer;
 
-        private int angleCount;
+        ITerrainLabel71 angleLabel;
 
-        private double angleSize;
+        ITerrainPolyline71 angleLine;
 
-        private ITerrainLabel71 angleLabel;
+        string startName = "OnStart";
 
-        private ITerrainPolyline71 angleLine;
+        string angleGroupName = "瑙掗噺绠�";
 
-        private string angleGroupName = "瑙掗噺绠�";
+        string shpGroupName = "鐭㈤噺鏁版嵁";
+
+        string mainUrl = Application.StartupPath;
 
         public FrmWin()
         {
             InitializeComponent();
+            //this.KeyPreview = true;
+            //this.KeyDown += FrmWin_KeyDown;
             this.Load += FrmWeb_Load;
-            this.FormClosed += FrmWin_FormClosed;
+            this.FormClosing += FrmWin_FormClosing;
+
+            timer = new System.Timers.Timer(1000);
+            timer.Elapsed += timer_Elapsed;
+        }
+
+        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
+        {
+            MoveWinPosition("MeshLayerTool", 583, 304);
+        }
+
+        void FrmWin_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.F1)
+            {
+                MoveWinPosition("MeshLayerTool", 400, 400);
+            }
         }
 
         void FrmWeb_Load(object sender, EventArgs e)
@@ -52,10 +78,10 @@
             try
             {
                 LogOut.Info("寮�濮嬭繍琛�..");
-                mainUrl = Application.StartupPath;
                 bool isTest = "1" == ConfigurationManager.AppSettings["isTest"];
 
-                this.fly = isTest ? ConfigurationManager.AppSettings["testFly"] : Path.Combine(Application.StartupPath, "Resources\\sample.fly");
+                this.fly = ConfigurationManager.AppSettings["fly"] ?? ConfigurationManager.AppSettings["testFly"];
+                if (!this.fly.Contains(":\\")) this.fly = Path.Combine(Application.StartupPath, this.fly);
                 if (!File.Exists(this.fly))
                 {
                     LogOut.Error("Fly鏂囦欢涓嶅瓨鍦細" + this.fly);
@@ -92,15 +118,30 @@
 
         void SG_OnLoadFinished(bool bSuccess)
         {
-            this.SG.OnLoadFinished -= SG_OnLoadFinished;
+            try
+            {
+                //this.SG.OnLoadFinished -= SG_OnLoadFinished;
+
+                string id = this.SG.ProjectTree.FindItem(startName);
+                if (string.IsNullOrEmpty(id)) return;
+
+                var obj = this.SG.ProjectTree.GetObject(id);
+                if (obj.ObjectType == ObjectTypeCode.OT_LOCATION)
+                {
+                    this.SG.Navigate.FlyTo(id);
+                }
+            }
+            catch { }
         }
 
-        void FrmWin_FormClosed(object sender, FormClosedEventArgs e)
+        void FrmWin_FormClosing(object sender, FormClosingEventArgs e)
         {
+            ExecCmd(new List<string> { "taskkill /f /t /im OSGBLab.exe" });
             LogOut.Info("鍏抽棴绋嬪簭.");
         }
         #endregion
 
+        #region invoke
         protected override void DefWndProc(ref Message m)
         {
             switch (m.Msg)
@@ -125,6 +166,7 @@
 
         public void InvokeByCode(int code)
         {
+            LogOut.Info("InvokeByCode > " + code);
             switch (code)
             {
                 case 0xc0: // 閲嶇疆榧犳爣妯″紡
@@ -132,7 +174,7 @@
                 case 0xc1: // 鎵撳紑鏁版嵁鏂规/鎵撳紑鏄剧ず鍦烘櫙
                     SG.Command.Execute(1001, null); break;
                 case 0xc2: // 淇濆瓨鏁版嵁鏂规/淇濆瓨鏄剧ず鍦烘櫙
-                    SG.Command.Execute(1003, null); break;
+                    Save(); break;
                 case 0xc3: // 鐭㈤噺鏁版嵁(榛樿涓�*.shp锛屾敮鎸丒xcel)
                     SG.Command.Execute(1013, 5); break;
                 case 0xc4: // 褰卞儚鏁版嵁(榛樿涓�*.tif)
@@ -141,11 +183,14 @@
                     SG.Command.Execute(1014, 26); break;
                 case 0xc6: // 妯″瀷鏁版嵁(*.osgb)
                     ExecCmd(new List<string> { "taskkill /f /t /im MeshLayerTool.exe" }); // SG.Command.Execute(2342, null);
-                    RunExe(Path.Combine(ConfigurationManager.AppSettings["tePath"], "MeshLayerTool.exe"), " -RunConvertor -Mode ImportMeshLayer -Type osgb", false); // -RunApp TerraExplorer
+                    //RunExe(Path.Combine(ConfigurationManager.AppSettings["tePath"], "MeshLayerTool.exe"), " -RunConvertor -Mode ImportMeshLayer -Type osgb", false); // -RunApp TerraExplorer
+                    RunExe(Path.Combine(ConfigurationManager.AppSettings["tePath"], "MeshLayerTool.exe"), " -RunApp TerraExplorer -RunConvertor -Mode ImportMeshLayer -Type osgb", false); // -RunApp TerraExplorer
+                    timer.Stop();
+                    timer.Start();
                     break;
                 case 0xc7: // 妯″瀷鏁版嵁(*.obj)
                     SG.Command.Execute(1012, 13); break;
-                case 0xc8: // 鍩虹缂栬緫(閫夋嫨瀵硅薄锛屽紑鍚紪杈�)
+                case 0xc8: // 缂栬緫鏁版嵁(閫夋嫨瀵硅薄锛屽紑鍚紪杈�)
                     SG.Command.Execute(1021, null); break;
                 case 0xc9: // 鐐归噺绠�(鍧愭爣+楂樼▼)
                     SG.Command.Execute(1023, null); break;
@@ -178,9 +223,145 @@
                     InvokeOsgbLab(); break;
                 case 0xd7: // 鎵撳紑fly
                     SG.Open(this.fly); break;
+                case 0xd8: // 鍔犺浇shp
+                    LoadShp(); break;
+                case 0xd9: // 璇诲彇shp
+                    ReadShp(); break;
+                case 0xda: // 鎸夌収灞炴�ф樉绀�
+                    DisplayByAttribute(0, "鎸夌収灞炴�ф樉绀�"); break;
+                case 0xdb: // 鎸夌収灞炴�ц缃鑹�
+                    DisplayByAttribute(1, "鎸夌収灞炴�ц缃鑹�"); break;
+                case 0xdc: // 鎵撳紑鏁版嵁鏂规
+                    OpenPrj(); break;
+                case 0xdd: // 淇濆瓨椤圭洰
+                    SavePrj(); break;
             }
         }
+        #endregion
 
+        #region user32.dll
+        public const int SWP_NOSIZE = 0x0001;
+
+        public const int SWP_NOMOVE = 0x0002;
+
+        public const int SWP_NOZORDER = 0x0004;
+
+        public const int SWP_SHOWWINDOW = 0x0040;
+
+        public const int HWND_TOPMOST = -1;
+
+        public const int HWND_NOTOPMOST = -2;
+
+        [DllImport("user32.dll", SetLastError = true)]
+        [return: MarshalAs(UnmanagedType.Bool)]
+        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
+
+        [DllImport("user32.dll", SetLastError = true)]
+        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
+
+        [Serializable]
+        [StructLayout(LayoutKind.Sequential)]
+        public struct RECT
+        {
+            public int Left;
+            public int Top;
+            public int Right;
+            public int Bottom;
+        }
+
+        public void MoveWinPosition(String name, int left, int right)
+        {
+            Process[] processes = Process.GetProcessesByName(name);
+            if (processes.Length > 0)
+            {
+                Process process = processes[0];
+                IntPtr hWnd = process.MainWindowHandle;
+
+                // 鑾峰彇绐椾綋鐨勫潗鏍�
+                RECT rect;
+                GetWindowRect(hWnd, out rect);
+
+                if (rect.Left < 0 || rect.Left > 1920 || rect.Top < 0 || rect.Top > 1080)
+                {
+                    timer.Stop();
+                    // 绉诲姩绐楀彛鍒板睆骞曚笂鐨勪綅缃�:
+                    SetWindowPos(hWnd, IntPtr.Zero, left, right, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
+                }
+            }
+        }
+        #endregion
+
+        #region OSGBLab
+        const int GCLP_HICON = -14;
+        const int ICON_SMALL = 0;
+        const int ICON_BIG = 1;
+
+        [DllImport("user32.dll", SetLastError = true)]
+        static extern IntPtr FindWindow(string lpClassName, string lpWindowsName);
+
+        [DllImport("user32.dll", SetLastError = true)]
+        public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);
+
+        [DllImport("user32.dll", SetLastError = true)]
+        static extern bool SetWindowText(IntPtr hWnd, String lpString);
+
+        [DllImport("user32.dll", SetLastError = true)]
+        public static extern IntPtr SetClassLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
+
+        public void DealOsgbLab()
+        {
+            /*Process[] processes = Process.GetProcessesByName("OSGBLab");
+            if (processes.Length > 0)
+            {
+                IntPtr hWnd = processes[0].MainWindowHandle;
+                SetWindowText(hWnd, "OSGB鏁版嵁澶勭悊宸ュ叿");
+            }*/
+
+            IntPtr hWnd = FindWindow(null, "OSGBLab(鍊炬枩浼翠荆) (Windows) 鍏嶈垂鐗�");
+            if (hWnd == IntPtr.Zero) return;
+
+            DealOsgbLab(hWnd);
+        }
+
+        public void DealOsgbLab(Process p)
+        {
+            do
+            {
+                if (p.MainWindowHandle != IntPtr.Zero)
+                {
+                    DealOsgbLab(p.MainWindowHandle);
+                    break;
+                }
+
+                Thread.Sleep(50);
+            } while (true);
+        }
+
+        [DllImport("user32.dll", EntryPoint = "SetClassLongPtr", SetLastError = true)]
+        static extern IntPtr SetClassLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
+
+        [DllImport("user32.dll", EntryPoint = "SetClassLong", SetLastError = true)]
+        static extern int SetClassLong32(IntPtr hWnd, int nIndex, int dwNewLong);
+
+        [DllImport("user32.dll")]
+        static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr iconName);
+
+        [DllImport("user32.dll")]
+        [return: MarshalAs(UnmanagedType.Bool)]
+        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
+
+        public void DealOsgbLab(IntPtr hWnd)
+        {
+            //ShowWindow(hWnd, 0); // 5
+            SetWindowText(hWnd, ConfigurationManager.AppSettings["osgbLabTitle"]);
+
+            Icon icon = new Icon(Path.Combine(mainUrl, @"Resources\favicon.ico"));
+            SetClassLongPtr(hWnd, GCLP_HICON, icon.Handle);
+            //SetClassLongPtr(hWnd, GCLP_HICON + ICON_BIG, icon.Handle);
+        }
+        #endregion
+
+        #region ShowHtml+RunExe
         public void ShowHtmlByid(int id, int arg)
         {
             switch (id)
@@ -228,14 +409,16 @@
             SG.Window.ShowPopup(popup);
         }
 
-        public void RunExe(string path, string args, bool noWindow = true)
+        public Process RunExe(string path, string args, bool noWindow = true)
         {
             Process p = new Process();
             p.StartInfo.FileName = path;
-            p.StartInfo.Arguments = args;
+            if (!string.IsNullOrEmpty(args)) p.StartInfo.Arguments = args;
             p.StartInfo.CreateNoWindow = noWindow;
             p.StartInfo.UseShellExecute = false;
             p.Start();
+
+            return p;
         }
 
         public static string ExecCmd(List<string> list)
@@ -283,8 +466,9 @@
 
             return str;
         }
+        #endregion
 
-        #region 瑙掗噺绠�+绌洪棿缁熻+绌洪棿鍒嗘瀽+osgblab
+        #region 瑙掗噺绠�
         private void AngleMeasurement()
         {
             if (isAngle)
@@ -468,10 +652,19 @@
 
             return true;
         }
+        #endregion
+
+        #region 璋冪敤Html
+        private string GetAbsolutePath(string key)
+        {
+            string str = ConfigurationManager.AppSettings[key];
+
+            return str.Contains(":\\") ? str : Path.Combine(Application.StartupPath, str);
+        }
 
         private void SpaceStatistics()
         {
-            //
+            this.ShowHtml("绌洪棿缁熻", mainUrl + @"\Resources\SpaceStatistics\SpaceStatistics.html", 20, 20, 420, 285);
         }
 
         private void SpatialAnalysis()
@@ -481,7 +674,195 @@
 
         private void InvokeOsgbLab()
         {
-            //
+            ExecCmd(new List<string> { "taskkill /f /t /im OSGBLab.exe" });
+            Process p = RunExe(GetAbsolutePath("osgblabPath"), null, false);
+            DealOsgbLab(p);
+        }
+
+        private void DisplayByAttribute(int type, String name)
+        {
+            this.ShowHtml(name, mainUrl + @"\Resources\DisplayByAttribute\DisplayByAttribute.html?Type=" + type + "&lang=2052", 20, 20, 365, 355);
+        }
+        #endregion
+
+        #region 鍔犺浇shp + 璇诲彇shp
+        private void LoadShp()
+        {
+            OpenFileDialog dialog = new OpenFileDialog();
+            dialog.Filter = "Shapefile鏂囦欢 (*.shp)|*.shp";
+            dialog.RestoreDirectory = true;
+
+            if (dialog.ShowDialog() == DialogResult.OK)
+            {
+                string shpFile = dialog.FileName;
+                if (LoadShp(shpFile))
+                {
+                    sendShpFile(shpFile);
+                }
+            }
+        }
+
+        private bool LoadShp(string shpFile)
+        {
+            try
+            {
+                string gid = GetGroupIdByName(shpGroupName);
+                string conn = "FileName=" + shpFile + ";TEPlugName=OGR;"; // 杩炴帴瀛楃涓�
+                string shpName = Path.GetFileNameWithoutExtension(shpFile);
+
+                IFeatureLayer71 layer = SG.Creator.CreateFeatureLayer(shpName, conn, gid); // 鍔犺浇shp
+                layer.Refresh();
+                SG.Navigate.FlyTo(layer.ID);
+
+                return true;
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+                return false;
+            }
+        }
+
+        private string GetGroupIdByName(string groupName)
+        {
+            string gid = SG.ProjectTree.FindItem(groupName);
+            if (string.IsNullOrEmpty(gid) || !SG.ProjectTree.IsGroup(gid))
+            {
+                gid = SG.ProjectTree.CreateGroup(groupName, SG.ProjectTree.RootID);
+            }
+
+            return gid;
+        }
+
+        private void sendShpFile(string shpFile)
+        {
+            try
+            {
+                string shpTxtPath = GetShpTxtPath();
+                using (StreamWriter sw = new StreamWriter(shpTxtPath, false))
+                {
+                    sw.Write(shpFile.Replace("\\", "/"));
+                }
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+            }
+        }
+
+        private void ReadShp()
+        {
+            try
+            {
+                string shpTxtPath = GetShpTxtPath();
+                if (string.IsNullOrEmpty(shpTxtPath) || !File.Exists(shpTxtPath))
+                {
+                    return;
+                }
+
+                string shpFile = File.ReadAllText(shpTxtPath);
+                if (string.IsNullOrEmpty(shpFile) || !shpFile.ToLower().EndsWith(".shp") || !File.Exists(shpFile))
+                {
+                    return;
+                }
+
+                LoadShp(shpFile);
+                File.Delete(shpTxtPath);
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+            }
+        }
+
+        private string GetShpTxtPath()
+        {
+            string shpTxtPath = ConfigurationManager.AppSettings["shpTxtPath"];
+            if (!shpTxtPath.Contains(":\\"))
+            {
+                shpTxtPath = Path.Combine(Application.StartupPath, shpTxtPath);
+            }
+
+            return shpTxtPath;
+        }
+        #endregion
+
+        #region 鏂规绠$悊
+        private void Save()
+        {
+            //SG.Command.Execute(1003, null);
+            SaveLocation();
+            this.SG.Project.Save();
+        }
+
+        private void OpenPrj()
+        {
+            string flyPath = ReadPrjPath();
+            if (String.IsNullOrEmpty(flyPath)) flyPath = Path.Combine(mainUrl, "see.fly");
+
+            if (!File.Exists(flyPath))
+            {
+                flyPath = ConfigurationManager.AppSettings["fly"] ?? ConfigurationManager.AppSettings["testFly"];
+                if (!flyPath.Contains(":\\")) flyPath = Path.Combine(mainUrl, flyPath);
+            }
+
+            this.SG.Open(flyPath);
+        }
+
+        private void SavePrj()
+        {
+            try
+            {
+                string flyPath = ReadPrjPath();
+                if (String.IsNullOrEmpty(flyPath)) flyPath = Path.Combine(mainUrl, "see.fly");
+
+                this.Save();
+                //this.SG.Project.SaveAs(flyPath);
+                if (this.fly.Equals(flyPath)) return;
+                if (File.Exists(flyPath)) File.Delete(flyPath);
+
+                string cmd = string.Format("mklink \"{0}\" \"{1}\"", flyPath, this.fly);
+                ExecCmd(new List<string> { cmd });
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+            }
+        }
+
+        private void SaveLocation()
+        {
+            try
+            {
+                string id = this.SG.ProjectTree.FindItem(startName);
+                if (!string.IsNullOrEmpty(id)) this.SG.ProjectTree.DeleteItem(id);
+
+                var pos = this.SG.Navigate.GetPosition();
+                var loc = this.SG.Creator.CreateLocationHere(this.SG.ProjectTree.RootID, startName);
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+            }
+        }
+
+        private String ReadPrjPath()
+        {
+            String txt = Path.Combine(mainUrl, "fly.txt");
+            if (!File.Exists(txt)) return null;
+
+            try
+            {
+                using (StreamReader sr = new StreamReader(txt))
+                {
+                    return sr.ReadLine();
+                }
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+                return null;
+            }
         }
         #endregion
     }

--
Gitblit v1.9.3