using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace ExportMap.cs { public class TBUtils { /// /// 获取源目录 /// public static string SourcesPath { get { return Path.Combine(Tools.BaseDir, "TerraBuilder"); } } /// /// 时间字符串 /// public static string TimeStr { get { return DateTime.Now.ToString("yyyyMMddhhmmss"); } } /// /// Mpt路径 /// public static string MptPath { get { return Tools.GetSetting("mpt"); } } /// /// 创建MPT /// 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); } } /// /// 获取新Tbp文件 /// 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; } /// /// 移动文件至临时目录 /// private static void MoveFilesToTemp(string sourcePath, string targetPath) { CopyFolder(Path.Combine(sourcePath, "数字正射影像图"), Path.Combine(targetPath, "img")); CopyFolder(Path.Combine(sourcePath, "数字高程模型"), Path.Combine(targetPath, "dem")); CopyFolder(Path.Combine(sourcePath, "中线裁剪范围"), Path.Combine(targetPath, "shp")); } /// /// 复制目录 /// private static void CopyFolder(string sourcePath, string targetPath) { if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); DirectoryInfo di = new DirectoryInfo(sourcePath); FileInfo[] files = di.GetFiles(); if (null == files || 0 == files.Length) return; foreach (FileInfo fi in files) { File.Move(fi.FullName, Path.Combine(targetPath, fi.Name)); } } /// /// 获取新JS /// 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; } /// /// 获取文件路径 /// 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; } /// /// 重启TB程序 /// private static void ReloadTB() { List list = new List(); 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); } } }