管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-06-27 2b36cb4c4e2be350663189638b9f1132f3eccd26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
 
namespace ExportMap.cs
{
    public class TBUtils
    {
        /// <summary>
        /// 获取源目录
        /// </summary>
        public static string SourcesPath
        {
            get
            {
                return Path.Combine(Tools.BaseDir, "TerraBuilder");
            }
        }
 
        /// <summary>
        /// 时间字符串
        /// </summary>
        public static string TimeStr
        {
            get
            {
                return DateTime.Now.ToString("yyyyMMddhhmmss");
            }
        }
 
        /// <summary>
        /// Mpt路径
        /// </summary>
        public static string MptPath
        {
            get
            {
                return Tools.GetSetting("mpt");
            }
        }
 
        /// <summary>
        /// 创建MPT
        /// </summary>
        public static string CreateMpt(string sourcePath, ref string err)
        {
            string time = TimeStr;
            string targetPath = Path.Combine(Tools.TempDir, time);
 
            try
            {
                if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath);
 
                MoveFilesToTemp(sourcePath, targetPath);
                string tbp = GetNewTbp(time);
                string js = GetNewJs(time, tbp, targetPath);
 
                return null;
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                err = ex.Message;
                return null;
            }
            finally
            {
                if (Directory.Exists(targetPath)) Directory.Delete(targetPath, true);
            }
        }
 
        /// <summary>
        /// 获取新Tbp文件
        /// </summary>
        private static string GetNewTbp(string time)
        {
            string tbp = Path.Combine(SourcesPath, "tb.tbp");
            string newTbp = Path.Combine(Tools.TempDir, time, time + ".tbp");
 
            File.Copy(tbp, newTbp);
 
            return newTbp;
        }
 
        /// <summary>
        /// 移动文件至临时目录
        /// </summary>
        private static void MoveFilesToTemp(string sourcePath, string targetPath)
        {
            string imgPath = Path.Combine(targetPath, "img");
            //if (!Directory.Exists(imgPath)) Directory.CreateDirectory(imgPath);
 
            string demPath = Path.Combine(targetPath, "dem");
            //if (!Directory.Exists(demPath)) Directory.CreateDirectory(demPath);
 
            string shpPath = Path.Combine(targetPath, "shp");
            //if (!Directory.Exists(shpPath)) Directory.CreateDirectory(shpPath);
 
            File.Move(Path.Combine(sourcePath, "数字正射影像图"), imgPath);
            File.Move(Path.Combine(sourcePath, "数字高程模型"), demPath);
            File.Move(Path.Combine(sourcePath, "中线裁剪范围"), shpPath);
        }
 
        /// <summary>
        /// 获取新JS
        /// </summary>
        private static string GetNewJs(string time, string tbp, string targetPath)
        {
            string js = Path.Combine(SourcesPath, "template.js");
            string img = GetFilePath(Path.Combine(targetPath, "img"), "*.tif");
            string dem = GetFilePath(Path.Combine(targetPath, "dem"), "*.tif");
            string shp = GetFilePath(Path.Combine(targetPath, "shp"), "*.shp");
 
            string str = File.ReadAllText(js);
            str = str.Replace("{tbp}", tbp)
                .Replace("{img}", img)
                .Replace("{dem}", dem)
                .Replace("{shp}", shp)
                .Replace("\\", "\\\\");
 
            string newJs = Path.Combine(Tools.TempDir, time, time + ".js");
            File.WriteAllText(newJs, str);
 
            return newJs;
        }
 
        /// <summary>
        /// 获取文件路径
        /// </summary>
        private static string GetFilePath(string path, string ext)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] files = di.GetFiles(ext);
 
            return null == files || 0 == files.Length ? string.Empty : files[0].FullName;
        }
 
        /// <summary>
        /// 重启TB程序
        /// </summary>
        private static void ReloadTB()
        {
            List<string> list = new List<string>();
            list.Add("taskkill /f /t /im TerraBuilder.exe");
            list.Add("taskkill /f /t /im TBFuser.exe");
            list.Add("start /d \"C:\\Program Files\\Skyline\\TerraBuilder Fuser\" TBFuser.exe");
 
            Tools.ExecCmd(list, false);
        }
    }
}