From 5f80de49c4a00416591054180bf809a12a76d686 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 07 十一月 2024 16:40:53 +0800
Subject: [PATCH] 1

---
 TEWin/FrmWin.cs |  357 +++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 295 insertions(+), 62 deletions(-)

diff --git a/TEWin/FrmWin.cs b/TEWin/FrmWin.cs
index fff7089..87c9072 100644
--- a/TEWin/FrmWin.cs
+++ b/TEWin/FrmWin.cs
@@ -34,17 +34,23 @@
 
         public SGWorld71 SG;
 
+        System.Timers.Timer timer;
+
         ITerrainLabel71 angleLabel;
 
+        ITerrainLabel71 aLabel;
+
+        ITerrainLabel71 bLabel;
+
         ITerrainPolyline71 angleLine;
+
+        string startName = "OnStart";
 
         string angleGroupName = "瑙掗噺绠�";
 
         string shpGroupName = "鐭㈤噺鏁版嵁";
 
         string mainUrl = Application.StartupPath;
-
-        System.Timers.Timer timer;
 
         public FrmWin()
         {
@@ -78,13 +84,14 @@
                 LogOut.Info("寮�濮嬭繍琛�..");
                 bool isTest = "1" == ConfigurationManager.AppSettings["isTest"];
 
-                this.fly = ConfigurationManager.AppSettings["fly"] ?? ConfigurationManager.AppSettings["testFly"];
+                this.fly = getFly();
+                /*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);
                     return;
-                }
+                }*/
 
                 SG = new SGWorld71();
                 SG.OnLoadFinished += SG_OnLoadFinished;
@@ -99,14 +106,13 @@
                     tool = new FrmTool(this);
                     tool.Show();
 
-                    SG.Open(this.fly);
+                    //SG.Open(this.fly);
                 }
                 //else
                 //{
                 //    this.WindowState = FormWindowState.Maximized;
                 //    tree.WindowState = FormWindowState.Maximized;
                 //}
-
             }
             catch (Exception ex)
             {
@@ -116,12 +122,26 @@
 
         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_FormClosing(object sender, FormClosingEventArgs e)
         {
             ExecCmd(new List<string> { "taskkill /f /t /im OSGBLab.exe" });
+            ExecCmd(new List<string> { "taskkill /f /t /im MeshLayerTool.exe" });
             LogOut.Info("鍏抽棴绋嬪簭.");
         }
         #endregion
@@ -151,6 +171,7 @@
 
         public void InvokeByCode(int code)
         {
+            LogOut.Info("InvokeByCode > 0x" + code.ToString("x"));
             switch (code)
             {
                 case 0xc0: // 閲嶇疆榧犳爣妯″紡
@@ -158,7 +179,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)
@@ -166,12 +187,7 @@
                 case 0xc5: // 鍦板舰鏁版嵁(榛樿涓�*.tif)
                     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"), " -RunApp TerraExplorer -RunConvertor -Mode ImportMeshLayer -Type osgb", false); // -RunApp TerraExplorer
-                    timer.Stop();
-                    timer.Start();
-                    break;
+                    ExportOsgb(); break;
                 case 0xc7: // 妯″瀷鏁版嵁(*.obj)
                     SG.Command.Execute(1012, 13); break;
                 case 0xc8: // 缂栬緫鏁版嵁(閫夋嫨瀵硅薄锛屽紑鍚紪杈�)
@@ -215,7 +231,45 @@
                     DisplayByAttribute(0, "鎸夌収灞炴�ф樉绀�"); break;
                 case 0xdb: // 鎸夌収灞炴�ц缃鑹�
                     DisplayByAttribute(1, "鎸夌収灞炴�ц缃鑹�"); break;
+                case 0xdc: // 鎵撳紑鏁版嵁鏂规
+                    Open(); break;
+                case 0xdd: // 鍙﹀瓨涓�
+                    SaveAs(); break;
+                case 0xde: // 閫夋嫨瀵硅薄
+                    SG.Command.Execute(1021, 0); break;
+                case 0xdf: // 瀵煎嚭妯″瀷(鍏跺畠)
+                    ExportOther(); break;
+                case 0xe0: // 鍒锋柊鍥惧眰
+                    SG.Command.Execute(1087, 0); break;
+                case 0xe1: // 淇濆瓨鍥惧眰
+                    SG.Command.Execute(1088, 0); break;
+                case 0xe2: // 澶嶅埗
+                    SG.Command.Execute(1031, 0); break;
+                case 0xe3: // 鍓垏
+                    SG.Command.Execute(1032, 0); break;
+                case 0xe4: // 鍒犻櫎
+                    SG.Command.Execute(1033, 0); break;
+                case 0xe5: // 绮樿创鑷冲睆骞曚腑蹇�
+                    SG.Command.Execute(1030, 0); break;
             }
+        }
+
+        private void ExportOsgb()
+        {
+            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"), " -RunApp TerraExplorer -RunConvertor -Mode ImportMeshLayer -Type osgb", false); // -RunApp TerraExplorer
+            timer.Stop();
+            timer.Start();
+        }
+
+        private void ExportOther()
+        {
+            ExecCmd(new List<string> { "taskkill /f /t /im MeshLayerTool.exe" });
+            //RunExe(Path.Combine(ConfigurationManager.AppSettings["tePath"], "slmeshconverter.exe"), " -cmd convert -in \"D:/SEM/osgb/xizhan.3dml\" -f 3dml -out \"C:/Export.3dml\" -vdat convert_z -wktout -ui 1", false);
+            RunExe(Path.Combine(ConfigurationManager.AppSettings["tePath"], "MeshLayerTool.exe"), " -RunApp TerraExplorer -RunConvertor -Mode ExportMeshLayer", false);
+            timer.Stop();
+            timer.Start();
         }
         #endregion
 
@@ -523,26 +577,7 @@
 
                 if (angleCount % 3 == 2)
                 {
-                    IPoint firstPoint = (IPoint)line.Points[line.Points.Count - 3];
-                    IPoint secondPoint = (IPoint)line.Points[line.Points.Count - 2];
-                    IPoint nowPoint = (IPoint)line.Points[line.Points.Count - 1];
-                    var firstPosition = SG.Creator.CreatePosition(firstPoint.X, firstPoint.Y);
-                    var secondPosition = SG.Creator.CreatePosition(secondPoint.X, secondPoint.Y);
-                    var nowPosition = SG.Creator.CreatePosition(nowPoint.X, nowPoint.Y);
-
-                    nowPosition = SG.CoordServices.GetAimingAngles(nowPosition, secondPosition);
-                    secondPosition = SG.CoordServices.GetAimingAngles(firstPosition, secondPosition);
-                    if (secondPosition.Yaw > nowPosition.Yaw)
-                    {
-                        angleSize = secondPosition.Yaw - nowPosition.Yaw;
-                        if (angleSize > 180) angleSize = 360 - secondPosition.Yaw + nowPosition.Yaw;
-                    }
-                    else
-                    {
-                        angleSize = nowPosition.Yaw - secondPosition.Yaw;
-                        if (angleSize > 180) angleSize = 360 + secondPosition.Yaw - nowPosition.Yaw;
-                    }
-                    angleSize = Math.Round(angleSize, 2);
+                    angleSize = getAngle(line);
                     angleLabel.Text = angleSize.ToString() + "掳";
                 }
             }
@@ -550,6 +585,37 @@
             {
                 LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
             }
+        }
+
+        private double getAngle(ILineString line, bool getB = false)
+        {
+            IPoint p1 = (IPoint)line.Points[0];
+            IPoint p2 = (IPoint)line.Points[1];
+            IPoint p3 = (IPoint)line.Points[2];
+            var pos1 = SG.Creator.CreatePosition(p1.X, p1.Y);
+            var pos2 = SG.Creator.CreatePosition(p2.X, p2.Y);
+            var pos3 = SG.Creator.CreatePosition(p3.X, p3.Y);
+
+            //var first = SG.CoordServices.GetAimingAngles(pos1, pos2);
+            var first = SG.CoordServices.GetAimingAngles(pos1, pos2);
+            var sencod = SG.CoordServices.GetAimingAngles(pos2, pos3);
+            if (getB) return Math.Round(sencod.Yaw, 2);
+
+            double angle = 0;
+            if (first.Yaw >= 0 && first.Yaw < 180)
+            {
+                angle = first.Yaw + 180 - sencod.Yaw;
+                if (angle > 180) angle = 360 - angle;
+                if (angle < 0) angle = -angle;
+            }
+            else
+            {
+                angle = sencod.Yaw - (first.Yaw - 180);
+                if (angle > 180) angle = 360 - angle;
+                if (angle < 0) angle = -angle;
+            }
+
+            return Math.Round(angle, 2);
         }
 
         bool Angle_OnLButtonDown(int Flags, int X, int Y)
@@ -562,45 +628,41 @@
                 var gid = GetGroupId(angleGroupName);
                 if (angleLine == null)
                 {
-                    IList<double> lineVertex = new List<double>();
-                    lineVertex.Add(CursorCoord.Position.X);
-                    lineVertex.Add(CursorCoord.Position.Y);
-                    lineVertex.Add(0);
-                    lineVertex.Add(CursorCoord.Position.X);
-                    lineVertex.Add(CursorCoord.Position.Y);
-                    lineVertex.Add(0);
-
-                    double[] px = new double[lineVertex.Count];
-                    lineVertex.CopyTo(px, 0);
-                    var myGeometry = SG.Creator.GeometryCreator.CreateLineStringGeometry(px);
-
-                    angleLine = SG.Creator.CreatePolyline(myGeometry, SG.Creator.CreateColor(255, 255, 0, 255), AltitudeTypeCode.ATC_TERRAIN_RELATIVE, gid, angleCount.ToString());
-                    angleLine.LineStyle.Width = -2;
+                    angleLine = CreateLine(CursorCoord.Position, gid);
                     angleLine.Geometry.StartEdit();
                     angleCount++;
 
                     return true;
                 }
 
+                ILineString line = (ILineString)(angleLine.Geometry);
                 if (angleCount % 3 == 1)
                 {
-                    angleLabel = SG.Creator.CreateLabel(CursorCoord.Position, angleSize.ToString(), "", null, gid, angleCount.ToString());
-                    var LableColor = SG.Creator.CreateColor();
-                    LableColor.FromHTMLColor("#FFFF00");
-                    LableColor.SetAlpha(0.6);
-                    angleLabel.Style.TextColor = LableColor;
-                    angleLabel.Style.Bold = true;
-                    angleLabel.Style.FontName = "榛戜綋";
-                    angleLabel.Style.FontSize = 12;
-                    // angleLabel.Style.LineToGround = true;
-                    angleLabel.Style.LineToGroundType = LineType.LINE_TYPE_TO_GROUND;
-                    angleLabel.Style.Scale = 5000;
+                    angleLabel = CreateLabel(CursorCoord.Position, gid, "#FFFF00");
+
+                    IPoint firstPoint = (IPoint)line.Points[0];
+                    IPoint secondPoint = (IPoint)line.Points[1];
+                    var firstPos = SG.Creator.CreatePosition(firstPoint.X, firstPoint.Y);
+                    var secondPos = SG.Creator.CreatePosition(secondPoint.X, secondPoint.Y);
+
+                    aLabel = CreateLabel(firstPos, gid, "#00FFFF");
+                    aLabel.Text = Math.Round(SG.CoordServices.GetAimingAngles(firstPos, secondPos).Yaw, 2) + "掳";
                 }
 
-                if (angleCount % 3 == 2) angleLabel.Text = angleSize.ToString() + "掳";
+                if (angleCount % 3 == 2)
+                {
+                    //IPoint secondPoint = (IPoint)line.Points[line.Points.Count - 2];
+                    //var secondPos = SG.Creator.CreatePosition(secondPoint.X, secondPoint.Y);
+                    IPoint nowPoint = (IPoint)line.Points[line.Points.Count - 1];
+                    var newPos = SG.Creator.CreatePosition(nowPoint.X, nowPoint.Y);
+                    bLabel = CreateLabel(newPos, GetGroupId(angleGroupName), "#00FFFF");
+                    bLabel.Text = getAngle(line, true) + "掳";
+
+                    angleSize = getAngle(line);
+                    angleLabel.Text = angleSize.ToString() + "掳";
+                }
                 angleCount++;
 
-                ILineString line = (ILineString)(angleLine.Geometry);
                 IPoint p = (IPoint)line.Points[line.Points.Count - 1];
                 p.X = CursorCoord.Position.X; ;
                 p.Y = CursorCoord.Position.Y;
@@ -612,6 +674,8 @@
                     angleCount = 0;
                     angleLine = null;
                     angleLabel = null;
+                    aLabel = null;
+                    bLabel = null;
                 }
             }
             catch (Exception ex)
@@ -620,6 +684,44 @@
             }
 
             return true;
+        }
+
+        private ITerrainPolyline71 CreateLine(IPosition71 pos, String gid)
+        {
+            IList<double> lineVertex = new List<double>();
+            lineVertex.Add(pos.X);
+            lineVertex.Add(pos.Y);
+            lineVertex.Add(0);
+            lineVertex.Add(pos.X);
+            lineVertex.Add(pos.Y);
+            lineVertex.Add(0);
+
+            double[] px = new double[lineVertex.Count];
+            lineVertex.CopyTo(px, 0);
+            var myGeometry = SG.Creator.GeometryCreator.CreateLineStringGeometry(px);
+
+            var line = SG.Creator.CreatePolyline(myGeometry, SG.Creator.CreateColor(255, 255, 0, 255), AltitudeTypeCode.ATC_TERRAIN_RELATIVE, gid, angleCount.ToString());
+            line.LineStyle.Width = -2;
+
+            return line;
+        }
+
+        private ITerrainLabel71 CreateLabel(IPosition71 pos, String gid, String color)
+        {
+            var lableColor = SG.Creator.CreateColor();
+            lableColor.FromHTMLColor(color);
+            lableColor.SetAlpha(0.6);
+
+            var label = SG.Creator.CreateLabel(pos, "0", "", null, gid, "");
+            label.Style.TextColor = lableColor;
+            label.Style.Bold = true;
+            label.Style.FontName = "榛戜綋";
+            label.Style.FontSize = 12;
+            // label.Style.LineToGround = true;
+            label.Style.LineToGroundType = LineType.LINE_TYPE_TO_GROUND;
+            label.Style.Scale = 5000;
+
+            return label;
         }
 
         bool Angle_OnRButtonUp(int Flags, int X, int Y)
@@ -644,7 +746,7 @@
 
         private void SpaceStatistics()
         {
-            this.ShowHtml("绌洪棿缁熻", mainUrl + @"\Resources\SpaceStatistics\SpaceStatistics.html", 20, 20, 420, 285);
+            this.ShowHtml("绌洪棿缁熻", mainUrl + @"\Resources\SpaceStatistics\SpaceStatistics.html", 20, 20, 420, 305);
         }
 
         private void SpatialAnalysis()
@@ -766,5 +868,136 @@
             return shpTxtPath;
         }
         #endregion
+
+        #region 鏂规绠$悊
+        private void Save()
+        {
+            string emptyFly = Path.Combine(Application.StartupPath, "Resources", "empty.fly");
+            if (emptyFly.Contains("empty.fly")) //if (emptyFly.Equals(this.fly))
+            {
+                SaveAs();
+                return;
+            }
+
+            SaveLocation();
+            this.SG.Command.Execute(1019, 0); // Capture Tree State
+            SG.Command.Execute(1003, 0); // this.SG.Project.Save(); // 
+        }
+
+        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 void Open()
+        {
+            this.fly = getFly();
+            this.SG.Open(this.fly);
+        }
+
+        private String getFly()
+        {
+            string flyPath = ReadPrjPath();
+            if (String.IsNullOrEmpty(flyPath)) flyPath = "empty.fly";
+            if (!flyPath.Contains(":\\")) flyPath = Path.Combine(Application.StartupPath, "Resources", flyPath);
+
+            return flyPath;
+        }
+
+        private String ReadPrjPath()
+        {
+            try
+            {
+                String txt = Path.Combine(mainUrl, "Resources", "fly.txt");
+                if (!File.Exists(txt)) return null;
+
+                using (StreamReader sr = new StreamReader(txt))
+                {
+                    return sr.ReadLine();
+                }
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+                return null;
+            }
+        }
+
+        private void SaveAs()
+        {
+            string flyPath = ReadPrjPath();
+            string fileName = String.IsNullOrEmpty(flyPath) ? null : Path.GetFileName(flyPath);
+
+            string dest = SaveAs(fileName);
+            if (string.IsNullOrEmpty(dest)) return;
+
+            this.fly = dest;
+            string sourcePath = Path.Combine(mainUrl, "Resources");
+            if (string.IsNullOrEmpty(flyPath) || (dest.Contains(sourcePath) && dest.IndexOf("\\", sourcePath.Length + 1) == -1)) return;
+
+            CreateShortcut(flyPath, this.fly);
+            //ExecCmd(new List<string> { string.Format("mklink \"{0}\" \"{1}\"", flyPath, this.fly) });
+        }
+
+        private String SaveAs(String fileName)
+        {
+            try
+            {
+                if (String.IsNullOrEmpty(fileName) || "empty.fly".Equals(fileName)) fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".fly";
+
+                SaveLocation();
+                this.SG.Command.Execute(1019, 0); // Capture Tree State
+                this.SG.Project.SaveAs(fileName);
+
+                string source = Path.Combine(GetSavePath(), fileName);
+                if (!File.Exists(source))
+                {
+                    LogOut.Error(fileName + "锛屾壘涓嶅埌");
+                }
+
+                string dest = Path.Combine(Application.StartupPath, "Resources", fileName);
+                if (File.Exists(dest)) File.Delete(dest);
+                File.Move(source, dest);
+
+                return dest;
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+                return null;
+            }
+        }
+
+        private String GetSavePath()
+        {
+            string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
+            string tePathName = ConfigurationManager.AppSettings["tePath"].Contains("SmartEarth") ? "SmartEarth" : "Skyline";
+
+            return Path.Combine(appData, tePathName, "TerraExplorer");
+        }
+
+        public static void CreateShortcut(string shortcutPath, string targetPath, string description = null, string iconLocation = null)
+        {
+            var shell = new IWshRuntimeLibrary.WshShell();
+            var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath + ".lnk"); // 鍒涘缓蹇嵎鏂瑰紡瀵硅薄
+            shortcut.TargetPath = targetPath; // 鎸囧畾鐩爣璺緞
+            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); // 璁剧疆璧峰浣嶇疆
+            shortcut.WindowStyle = 1; // 璁剧疆杩愯鏂瑰紡锛岄粯璁や负甯歌绐楀彛
+            shortcut.Description = description; // 璁剧疆澶囨敞
+            shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation; // 璁剧疆鍥炬爣璺緞
+            shortcut.Save(); // 淇濆瓨蹇嵎鏂瑰紡
+        }
+        #endregion
     }
 }

--
Gitblit v1.9.3