zhangyuekai
2024-08-08 caa286712423a1a4413113d31f78f515efc400be
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
using Autodesk.Revit.DB;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using System;
using System.IO;
using System.Linq;
 
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 = 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
            };
            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;
            }
 
            var config = Newtonsoft.Json.JsonConvert.SerializeObject(records);
            File.WriteAllText(GlobalResource.RecordFilePath, config);
        }
 
        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;
            }
            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();
            }
        }
    }
}