管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-03-17 0e5d6aaf83aa5a612f82398e41498926d1983a88
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
154
155
156
157
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
{
    /// <summary>
    /// 模型转换工具
    /// </summary>
    public class ConvertUtils
    {
        /// <summary>
        /// 工作配置
        /// </summary>
        public static string JobConfig = "{{ \"format\": \"3dtiles\", \"mode\": 0, \"outputPath\": \"{0}\", \"outputOptions\": null, \"levelOfDetail\": -1, \"levelOfDetailText\": \"Auto\", \"georeferenced\": null }}";
 
        /// <summary>
        /// Roamer.exe
        /// </summary>
        public static string RoamerExe = "\"C:\\Program Files\\Autodesk\\Navisworks Manage 2020\\Roamer.exe\"";
 
        /// <summary>
        /// 获取发布地址
        /// </summary>
        public static string GetReleaseUrl(SysMeta meta)
        {
            return "http://{host}/LFData/3d/3dtiles/" + meta.type + "/" + meta.id + "/tileset.json";
        }
 
        /// <summary>
        /// 模型转换
        /// </summary>
        public static int Convert(List<int> ids)
        {
            int count = 0;
            string uploadFolder = Tool.GetSetting("uploadFolder");
            string tilesFolder = Path.Combine(Tool.GetSetting("lfData"), "3d\\3dtiles");
 
            List<SysMeta> list = XYZUtils.selectMetas(ids, "and type in ('ifc', 'fbx', 'rvt')");
            if (null == list || list.Count == 0) return 0;
 
            foreach (SysMeta meta in list)
            {
                string modelFile = Path.Combine(uploadFolder, meta.path);
                if (!File.Exists(modelFile)) continue;
 
                string configFile = Path.Combine(Tool.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);
                count += File.Exists(jsonFile) ? 1 : 0;
 
                string path = jsonFile.Replace(Tool.GetSetting("lfData") + "\\", "");
                InsertToDB(meta, path);
            }
 
            return count;
        }
 
        /// <summary>
        /// 运行Navisworks
        /// </summary>
        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.StackTrace);
            }
            finally
            {
                if (p != null)
                {
                    p.Close();
                    p.Dispose();
                    p = null;
                }
            }
        }
 
        /// <summary>
        /// 写文本文件
        /// </summary>
        private static void WriteText(string file, string str)
        {
            File.WriteAllText(file, str);
            LogOut.Info("JobConfig:" + str);
        }
 
        /// <summary>
        /// 插入数据库
        /// </summary>
        private static void InsertToDB(SysMeta meta, string path)
        {
            if (PubDBHelper.IsPublish(meta.id)) return;
 
            SysPublish sys = NewPublish(meta);
            sys.path = path;
 
            int pubid = PubDBHelper.InsertPublish(sys);
            if (pubid > 0)
            {
                PubDBHelper.InsertMetaPub(meta.id, pubid);
            }
        }
 
        /// <summary>
        /// 创建数据发布类
        /// </summary>
        private static SysPublish NewPublish(SysMeta meta)
        {
            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 = meta.depcode;
            sp.min = 0;
            sp.max = 0;
            sp.json = null;
            sp.create_user = meta.create_user;
            sp.geom = null;
            sp.bak = null;
 
            return sp;
        }
    }
}