qin
2024-09-28 e358beb08f5be49703009b64f058ecfbcfeefbd9
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using Autodesk.Revit.DB;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections.Generic;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.UI;
 
namespace HStation.RevitDev.RevitDataExport.Utility
{
    public class CacheUtil
    {
        public static void InitCache(Document doc)
        {
            var filePath = doc.PathName;
            var cache = File.ReadAllText(GlobalResource.RecordFilePath);
            if (string.IsNullOrEmpty(cache)) { return; }
 
            Records records = Newtonsoft.Json.JsonConvert.DeserializeObject<Records>(cache);
            if (records == null || records.ConfigRecords == null || records.ConfigRecords.Count == 0) { return; }
 
            ConfigRecord record = records.ConfigRecords.Where(x => x.FilePath == filePath)?.FirstOrDefault();
            if (record != null)
            {
                GlobalResource.RevitModels.Add(new Tuple<string, Dictionary<RevitType, List<string>>>(doc.Title, record.Record));
            }
        }
 
        public static void SaveCache(Document doc)
        {
            GlobalResource.LastFilePath = doc.PathName;
 
            ExportModelHelper exportModelHelper = new ExportModelHelper(doc);
            var revitJson = exportModelHelper.Export();
 
            var dir = Path.GetDirectoryName(GlobalResource.ExportFilePath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
 
            if (File.Exists(GlobalResource.ExportFilePath))
            {
                File.Delete(GlobalResource.ExportFilePath);
            }
 
            File.WriteAllText(GlobalResource.ExportFilePath, revitJson);
            var record = new ConfigRecord
            {
                FilePath = GlobalResource.LastFilePath,
                Record = GlobalResource.RevitModels.Find(x => x.Item1 == doc.Title)?.Item2
            };
            if (!File.Exists(GlobalResource.RecordFilePath))
            {
                File.Create(GlobalResource.RecordFilePath);
            }
 
            var cache = File.ReadAllText(GlobalResource.RecordFilePath);
            Records records = new Records();
            if (!string.IsNullOrEmpty(cache))
            {
                records = Newtonsoft.Json.JsonConvert.DeserializeObject<Records>(cache);
            }
            var configRecord = records.ConfigRecords?.Where(x => x.FilePath == doc.PathName)?.FirstOrDefault();
            if (configRecord == null)
            {
                records.ConfigRecords.Add(record);
            }
            else
            {
                configRecord.Record = record.Record;
            }
 
            var config = Newtonsoft.Json.JsonConvert.SerializeObject(records);
            File.WriteAllText(GlobalResource.RecordFilePath, config);
        }
 
        public static bool ExportZipFile(Document _doc, out string err)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            err = "";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var tempDir = Path.Combine(Path.GetDirectoryName(GlobalResource.BinDirectory), "temp"); //"d:\\temp";
                if (!Directory.Exists(tempDir))
                    Directory.CreateDirectory(tempDir);
                var structFile = tempDir + "\\struct.json";
                var modelFile = tempDir + "\\model.rvt";
                var settingFile = tempDir + "\\setting.json";
                var otherFile = tempDir + "\\struct_others.json";
 
                try
                {
                    var zipFilePath = dialog.SelectedPath + "\\model.ywrvt";
                    if (File.Exists(structFile)) File.Delete(structFile);
 
                    if (File.Exists(modelFile)) File.Delete(modelFile);
                    if (!File.Exists(settingFile)) File.Create(settingFile).Close();
                    if (!File.Exists(otherFile)) File.Create(otherFile).Close();
                    File.Copy(_doc.PathName, modelFile);
                    File.Copy(GlobalResource.ExportFilePath, structFile);
 
                    var notContains = new List<string>() { "管道附件", "管道", "管件 ", "管道附件", "管道系统" };
                    var c3 = new Autodesk.Revit.DB.FilteredElementCollector(_doc).WhereElementIsNotElementType();//.WhereElementIsElementType();
                    var c4 = new Autodesk.Revit.DB.FilteredElementCollector(_doc).WhereElementIsElementType();
 
                    c3.Concat(c4);
                    var r = c3.Where(c => c.Category != null && !notContains.Any(c.Category.Name.Contains)).Select(c => new { ID = c.Id.ToString(), Name = c.Name, CategoryName = c.Category.Name, CategoryID = c.Category.Id.ToString() }).Distinct().ToList();
                    var t = JsonHelper.ToJson(r);
                    File.WriteAllText(otherFile, t);
 
                    var setting = new SettingsViewModel() { Version = "1" };
                    File.WriteAllText(settingFile, JsonHelper.ToJson(setting));
 
                    var files = new string[] { structFile, modelFile, settingFile, otherFile };
                    ZipFiles(files, zipFilePath);
 
                    File.Delete(structFile);
                    File.Delete(otherFile);
                    File.Delete(modelFile);
                    File.Delete(settingFile);
                    File.Delete(otherFile);
                    return true;
                }
                catch (Exception ex)
                {
                    File.Delete(structFile);
                    File.Delete(otherFile);
                    File.Delete(modelFile);
                    File.Delete(settingFile);
                    File.Delete(otherFile);
                    err = JsonHelper.ToJson(ex);
                    return false;
                }
            }
            else return false;
 
        }
 
        private static void ZipFiles(string[] files, string zipFilePath)
        {
            using (var zipOutputStream = new ZipOutputStream(File.Create(zipFilePath)))
            {
                zipOutputStream.SetLevel(9); // 设置压缩级别
 
                foreach (var file in files)
                {
                    var entry = new ZipEntry(Path.GetFileName(file))
                    {
                        DateTime = DateTime.Now,
                        Size = File.ReadAllBytes(file).Length
                    };
 
                    zipOutputStream.PutNextEntry(entry);
 
                    using (var fileStream = File.OpenRead(file))
                    {
                        int sourceBytes;
                        byte[] buffer = new byte[4096];
 
                        while ((sourceBytes = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            zipOutputStream.Write(buffer, 0, sourceBytes);
                        }
                    }
                }
 
                zipOutputStream.Finish();
                zipOutputStream.Close();
            }
        }
 
        public static void HideOrShowModels(Document document)
        {
            if (document == null) { return; }
 
            var ids = GlobalResource.RevitModels.GetIds()?.Select(x => new ElementId(int.Parse(x))).ToList();
            if (ids.Count == 0)
            {
                return;
            }
            try
            {
                GlobalResource.HideMode = !GlobalResource.HideMode;
                using (Transaction trans = new Transaction(document, "hide elements"))
                {
                    trans.Start();
                    if (GlobalResource.HideMode)
                    {
                        document.ActiveView.HideElements(ids);
                    }
                    else
                    {
                        document.ActiveView.UnhideElements(ids);
                    }
                    trans.Commit();
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", "显隐失败,可能存在不能设置隐藏的构件!");
            }
        }
 
        public static void HideOrShowOtherModels(Document document)
        {
            if (document == null) { return; }
 
            var ids = GlobalResource.RevitModels.GetOtherIds()?.Select(x => new ElementId(int.Parse(x))).ToList();
            if (ids.Count == 0)
            {
                return;
            }
            try
            {
                GlobalResource.IsOtherHidden = !GlobalResource.IsOtherHidden;
                using (Transaction trans = new Transaction(document, "hide other elements"))
                {
                    trans.Start();
                    if (GlobalResource.IsOtherHidden)
                    {
                        document.ActiveView.HideElements(ids);
                    }
                    else
                    {
                        document.ActiveView.UnhideElements(ids);
                    }
                    trans.Commit();
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", "显隐失败,可能存在不能设置隐藏的构件!");
            }
        }
    }
}