From e358beb08f5be49703009b64f058ecfbcfeefbd9 Mon Sep 17 00:00:00 2001 From: qin <a@163.com> Date: 星期六, 28 九月 2024 14:27:52 +0800 Subject: [PATCH] 测试standard2.1 --- HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs | 192 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 167 insertions(+), 25 deletions(-) diff --git a/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs b/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs index 28b694f..0b43d1c 100644 --- a/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs +++ b/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs @@ -1,8 +1,14 @@ 锘縰sing 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 { @@ -11,7 +17,7 @@ public static void InitCache(Document doc) { var filePath = doc.PathName; - var cache = File.ReadAllText(Common.GlobalResource.RecordFilePath); + var cache = File.ReadAllText(GlobalResource.RecordFilePath); if (string.IsNullOrEmpty(cache)) { return; } Records records = Newtonsoft.Json.JsonConvert.DeserializeObject<Records>(cache); @@ -20,39 +26,40 @@ ConfigRecord record = records.ConfigRecords.Where(x => x.FilePath == filePath)?.FirstOrDefault(); if (record != null) { - Common.GlobalResource.RevitModels = record.Record; - foreach (var pair in record.Record) - { - Common.GlobalResource.ElementIds.AddRange(pair.Value.Select(x=>x.Id)); - } + GlobalResource.RevitModels.Add(new Tuple<string, Dictionary<RevitType, List<string>>>(doc.Title, record.Record)); } } public static void SaveCache(Document doc) { - Common.GlobalResource.LastFilePath = doc.PathName; + GlobalResource.LastFilePath = doc.PathName; ExportModelHelper exportModelHelper = new ExportModelHelper(doc); var revitJson = exportModelHelper.Export(); - var dir = Path.GetDirectoryName(Common.GlobalResource.ExportFilePath); + var dir = Path.GetDirectoryName(GlobalResource.ExportFilePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } - if (File.Exists(Common.GlobalResource.ExportFilePath)) + if (File.Exists(GlobalResource.ExportFilePath)) { - File.Delete(Common.GlobalResource.ExportFilePath); + File.Delete(GlobalResource.ExportFilePath); } - File.WriteAllText(Common.GlobalResource.ExportFilePath, revitJson); + File.WriteAllText(GlobalResource.ExportFilePath, revitJson); var record = new ConfigRecord { - FilePath = Common.GlobalResource.LastFilePath, - Record = Common.GlobalResource.RevitModels + FilePath = GlobalResource.LastFilePath, + Record = GlobalResource.RevitModels.Find(x => x.Item1 == doc.Title)?.Item2 }; - var cache = File.ReadAllText(Common.GlobalResource.RecordFilePath); + if (!File.Exists(GlobalResource.RecordFilePath)) + { + File.Create(GlobalResource.RecordFilePath); + } + + var cache = File.ReadAllText(GlobalResource.RecordFilePath); Records records = new Records(); if (!string.IsNullOrEmpty(cache)) { @@ -65,34 +72,169 @@ } else { - configRecord = record; + configRecord.Record = record.Record; } var config = Newtonsoft.Json.JsonConvert.SerializeObject(records); - File.WriteAllText(Common.GlobalResource.RecordFilePath, config); + 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 = Common.GlobalResource.ElementIds.Select(x => new ElementId(int.Parse(x))).ToList(); + + var ids = GlobalResource.RevitModels.GetIds()?.Select(x => new ElementId(int.Parse(x))).ToList(); if (ids.Count == 0) { return; } - Common.GlobalResource.HideMode = !Common.GlobalResource.HideMode; - using (Transaction trans = new Transaction(document, "hide elements")) + try { - trans.Start(); - if (Common.GlobalResource.HideMode) + GlobalResource.HideMode = !GlobalResource.HideMode; + using (Transaction trans = new Transaction(document, "hide elements")) { - document.ActiveView.HideElements(ids); + trans.Start(); + if (GlobalResource.HideMode) + { + document.ActiveView.HideElements(ids); + } + else + { + document.ActiveView.UnhideElements(ids); + } + trans.Commit(); } - else + } + 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")) { - document.ActiveView.UnhideElements(ids); + trans.Start(); + if (GlobalResource.IsOtherHidden) + { + document.ActiveView.HideElements(ids); + } + else + { + document.ActiveView.UnhideElements(ids); + } + trans.Commit(); } - trans.Commit(); + } + catch (Exception ex) + { + TaskDialog.Show("閿欒", "鏄鹃殣澶辫触锛屽彲鑳藉瓨鍦ㄤ笉鑳借缃殣钘忕殑鏋勪欢锛�"); } } } -- Gitblit v1.9.3