using ExportMap.db; using ExportMap.Models; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Web; namespace ExportMap.cs { /// /// 模型转换工具 /// public class ConvertUtils { /// /// 工作配置 /// public static string JobConfig = "{{ \"format\": \"3dtiles\", \"mode\": 0, \"outputPath\": \"{0}\", \"outputOptions\": null, \"levelOfDetail\": -1, \"levelOfDetailText\": \"Auto\", \"georeferenced\": null }}"; /// /// Roamer.exe /// public static string RoamerExe = "\"C:\\Program Files\\Autodesk\\Navisworks Manage 2020\\Roamer.exe\""; /// /// 获取发布地址 /// public static string GetReleaseUrl(SysMeta meta) { return "http://{host}/LFData/3d/3dtiles/" + meta.type + "/" + meta.id + "/tileset.json"; } /// /// 模型转换 /// public static List Convert(XYZArgs args) { string uploadFolder = Tools.GetSetting("uploadFolder"); string tilesFolder = Path.Combine(Tools.GetSetting("lfData"), "3d\\3dtiles"); List list = XYZUtils.SelectMetas(args.ids, "and type in ('ifc', 'fbx', 'rvt')"); if (null == list || list.Count == 0) return null; List ids = new List(); foreach (SysMeta meta in list) { string modelFile = Path.Combine(uploadFolder, meta.path); if (!File.Exists(modelFile)) continue; string configFile = Path.Combine(Tools.TempDir, ExportUtil.DateStr + ".json"); string outPath = Path.Combine(tilesFolder, meta.type, meta.id.ToString()); string jsonFile = Path.Combine(outPath, "tileset.json"); if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath); if (File.Exists(jsonFile)) File.Delete(jsonFile); WriteText(configFile, string.Format(JobConfig, outPath.Replace("\\", "\\\\"))); ExecNavisworks(modelFile, configFile); File.Delete(configFile); if (File.Exists(jsonFile)) { string path = jsonFile.Replace(Tools.GetSetting("lfData") + "\\", ""); int pubid = InsertToDB(meta, args, path); if (pubid > 0) ids.Add(pubid); } } return ids; } /// /// 运行Navisworks /// public static void ExecNavisworks(string modelFile, string configFile) { Process p = null; try { //string log = Path.Combine(ExportUtil.SourcesPath, "ns_log.txt"); //string args = string.Format("-licensing AdLM -OpenFile \"{0}\" -ExecuteAddInPlugin \"EngineBatch_Sample.SmartEarth\" \"{1}\" -log \"{2}\" -NoGui -Exit", modelFile, configFile, log); string args = string.Format("-licensing AdLM -OpenFile \"{0}\" -ExecuteAddInPlugin \"EngineBatch_Sample.SmartEarth\" \"{1}\" -NoGui -Exit", modelFile, configFile); LogOut.Info("Args:" + args); // 启动进程 p = Process.Start(RoamerExe, args); // 让组件等候相关的进程进入闲置状态 p.WaitForInputIdle(); // 让组件无限期地等待关联进程退出 p.WaitForExit(); } catch (Exception ex) { LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); } finally { if (p != null) { p.Close(); p.Dispose(); p = null; } } } /// /// 写文本文件 /// private static void WriteText(string file, string str) { File.WriteAllText(file, str); LogOut.Info("JobConfig:" + str); } /// /// 插入数据库 /// private static int InsertToDB(SysMeta meta, XYZArgs args, string path) { if (PubDBHelper.IsPublish(meta.id)) return 0; SysPublish sys = NewPublish(meta, args); sys.path = path; int pubid = PubDBHelper.InsertPublish(sys); if (pubid > 0) { sys.id = pubid; PubDBHelper.InsertLayer(sys, meta); PubDBHelper.InsertMetaPub(meta.id, pubid, args.userId); } return pubid; } /// /// 创建数据发布类 /// private static SysPublish NewPublish(SysMeta meta, XYZArgs args) { SysPublish sp = new SysPublish(); sp.name = meta.name; sp.url = GetReleaseUrl(meta); sp.type = meta.type; sp.status = 3; sp.dirid = meta.dircode; sp.depid = args.depcode; sp.min = 0; sp.max = 0; sp.json = null; sp.create_user = args.userId; sp.geom = null; sp.bak = null; return sp; } } }