管道基础大数据平台系统开发-【CS】-ExportMap
11
13693261870
2024-09-03 e05f48fc7ba368d3561d8702b18d65ee021b1ae2
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;
@@ -18,31 +20,55 @@
    public partial class FrmWin : Form
    {
        #region 成员变量+构造函数+事件
        string fly;
        FrmTree tree;
        FrmTool tool;
        string mainUrl;
        bool isAngle;
        public SGWorld74 SG;
        int angleCount;
        private bool isAngle;
        double angleSize;
        private int angleCount;
        public SGWorld71 SG;
        private double angleSize;
        ITerrainLabel71 angleLabel;
        private ITerrainLabel74 angleLabel;
        ITerrainPolyline71 angleLine;
        private ITerrainPolyline74 angleLine;
        string angleGroupName = "角量算";
        private string angleGroupName = "角量算";
        string shpGroupName = "矢量数据";
        string mainUrl = Application.StartupPath;
        System.Timers.Timer timer;
        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)
@@ -50,19 +76,18 @@
            try
            {
                LogOut.Info("开始运行..");
                mainUrl = Application.StartupPath;
                bool isTest = "1" == ConfigurationManager.AppSettings["isTest"];
                string fly = isTest ? ConfigurationManager.AppSettings["testFly"] : Path.Combine(Application.StartupPath, "Resources\\sample.fly");
                if (!File.Exists(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文件不存在:" + fly);
                    LogOut.Error("Fly文件不存在:" + this.fly);
                    return;
                }
                SG = new SGWorld74();
                SG = new SGWorld71();
                SG.OnLoadFinished += SG_OnLoadFinished;
                SG.Open(fly);
                tree = new FrmTree();
                tree.Show(this);
@@ -73,12 +98,14 @@
                    tree.FormBorderStyle = FormBorderStyle.Sizable;
                    tool = new FrmTool(this);
                    tool.Show();
                    SG.Open(this.fly);
                }
                else
                {
                    this.WindowState = FormWindowState.Maximized;
                    tree.WindowState = FormWindowState.Maximized;
                }
                //else
                //{
                //    this.WindowState = FormWindowState.Maximized;
                //    tree.WindowState = FormWindowState.Maximized;
                //}
            }
            catch (Exception ex)
@@ -89,15 +116,17 @@
        void SG_OnLoadFinished(bool bSuccess)
        {
            //
            this.SG.OnLoadFinished -= SG_OnLoadFinished;
        }
        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)
@@ -138,11 +167,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"), " -RunApp TerraExplorer -RunConvertor -Mode ImportMeshLayer -Type osgb", false);
                    //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;
@@ -167,24 +199,144 @@
                    this.ShowHtmlByid(1093, 0); break;
                case 0xd3: // DEM分析(等值线)
                    SG.Command.Execute(1039, null); break;
                case 0xd4: // 三维模型分析(剖面分析+剖切分析)
                case 0xd4: // 三维模型分析(剖切分析)
                    this.ShowHtmlByid(1149, 39); break;
                case 0xd5: // 地理场景产品组装(抽取指定区域并发布离线数据包)
                    SG.Command.Execute(1028, null); break;
                case 0xd6: // 传统4D产品派生(集成osgblab)
                    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;
            }
        }
        #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);
        }
        public void DealOsgbLab(IntPtr hWnd)
        {
            SetWindowText(hWnd, "OSGB数据处理工具");
            Icon icon = new Icon(Path.Combine(mainUrl, @"Resources\favicon.ico"));
            SetClassLongPtr(hWnd, GCLP_HICON + ICON_SMALL, icon.Handle);
            SetClassLongPtr(hWnd, GCLP_HICON + ICON_BIG, icon.Handle);
        }
        #endregion
        #region ShowHtml+RunExe
        public void ShowHtmlByid(int id, int arg)
        {
            switch (id)
            {
                case 1034:
                    this.ShowHtml("距离测量", mainUrl + @"\Resources\DistanceMeasurement\distanceMeasurement.html", 20, 20, 400, 245);
                    this.ShowHtml("距离测量", mainUrl + @"\Resources\DistanceMeasurement\distanceMeasurement.html", 20, 20, 400, 275);
                    break;
                case 1037:
                    this.ShowHtml("面积测量", mainUrl + @"/Resources\AreaMeasurement\AreaMeasurement.html", 20, 20, 400, 245);
                    this.ShowHtml("面积测量", mainUrl + @"/Resources\AreaMeasurement\AreaMeasurement.html", 20, 20, 400, 205);
                    break;
                case 1149:
                    switch (arg)
@@ -192,7 +344,7 @@
                        case 24:
                            this.ShowHtml("坡向分析", mainUrl + @"\Resources\SlopeCursor\SlopeCursor.html", 20, 20, 400, 330); break;
                        case 28:
                            this.ShowHtml("剖面分析", mainUrl + @"\Resources\TerrainProfile\TerrainProfile.html", 20, 20, 400, 290); break;
                            this.ShowHtml("剖面分析", mainUrl + @"\Resources\TerrainProfile\TerrainProfile.html", 20, 20, 400, 265); break;
                        case 39:
                            this.ShowHtml("剖切分析", mainUrl + @"\Resources\CrossSection\CrossSection.html", 20, 20, 400, 225); break;
                    }
@@ -201,7 +353,7 @@
                    this.ShowHtml("坡度分析", mainUrl + @"\Resources\SlopeMapQuery\SlopeMapQuery.html", 20, 20, 350, 365);
                    break;
                case 1045:
                    this.ShowHtml("体积分析", mainUrl + @"\Resources\Volume\Volume.html", 20, 20, 350, 255);
                    this.ShowHtml("体积分析", mainUrl + @"\Resources\Volume\Volume.html", 20, 20, 350, 225);
                    break;
            }
        }
@@ -223,14 +375,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)
@@ -278,8 +432,9 @@
            return str;
        }
        #endregion
        #region 角量算+空间统计+空间分析+osgblab
        #region 角量算
        private void AngleMeasurement()
        {
            if (isAngle)
@@ -463,10 +618,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()
@@ -476,7 +640,116 @@
        private void InvokeOsgbLab()
        {
            //
            ExecCmd(new List<string> { "taskkill /f /t /im OSGBLab.exe" });
            osgbProc = RunExe(GetAbsolutePath("osgblabPath"), null, false);
            DealOsgbLab(osgbProc);
        }
        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
    }