From a18b907beff8b21fb4c9d6fb72678ac5e9f7b80d Mon Sep 17 00:00:00 2001 From: zhangyuekai <zhangyuekai@126.com> Date: 星期六, 10 八月 2024 21:05:33 +0800 Subject: [PATCH] fix-bug nullreference --- HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs | 167 ++++++++++++++++++++++++++++++++----------------------- 1 files changed, 97 insertions(+), 70 deletions(-) diff --git a/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs b/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs index ea90886..4655601 100644 --- a/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs +++ b/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs @@ -1,70 +1,97 @@ -锘縰sing Autodesk.Revit.DB; -using HStation.RevitDev.RevitDataExport.Entity; -using System.IO; -using System.Linq; - -namespace HStation.RevitDev.RevitDataExport.Utility -{ - public class CacheUtil - { - public static void ReadCache(Document doc) - { - var filePath = doc.PathName; - var cache = File.ReadAllText(Common.GlobalResource.ConfigFilePath); - 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) - { - Common.GlobalResource.RevitModels = record.Record; - } - } - - public static void SaveCache(Document doc) - { - Common.GlobalResource.LastFilePath = doc.PathName; - - ExportModelHelper exportModelHelper = new ExportModelHelper(doc); - var revitJson = exportModelHelper.Export(); - - var dir = Path.GetDirectoryName(Common.GlobalResource.ExportFilePath); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - - if (File.Exists(Common.GlobalResource.ExportFilePath)) - { - File.Delete(Common.GlobalResource.ExportFilePath); - } - - File.WriteAllText(Common.GlobalResource.ExportFilePath, revitJson); - var record = new ConfigRecord - { - FilePath = Common.GlobalResource.LastFilePath, - Record = Common.GlobalResource.RevitModels - }; - var cache = File.ReadAllText(Common.GlobalResource.ConfigFilePath); - 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(Common.GlobalResource.ConfigFilePath, config); - } - } -} +锘縰sing 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(); + } + } + } +} -- Gitblit v1.9.3