| | |
| | | using Autodesk.Revit.DB; |
| | | 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(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; |
| | | foreach (var pair in record.Record)
|
| | | 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;
|
| | | using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
|
| | | using System.Net.Http;
|
| | | using System.Net.Http.Headers;
|
| | |
|
| | | 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;
|
| | |
|
| | | 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).Close();
|
| | | }
|
| | |
|
| | | 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);
|
| | |
|
| | | 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);
|
| | | }
|
| | |
|
| | | public static bool UploadFile(Document _doc, string desc, out string err)
|
| | | {
|
| | | err = "";
|
| | | 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";
|
| | | var title = _doc.Title + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".ywrvt";
|
| | | var zipFilePath = tempDir + "\\" + title;
|
| | | try
|
| | | {
|
| | | |
| | | 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.SetAttributes(modelFile, FileAttributes.Normal);
|
| | | File.Copy(GlobalResource.ExportFilePath, structFile);
|
| | | File.SetAttributes(structFile, FileAttributes.Normal);
|
| | | var strConfig = File.ReadAllText(GlobalResource.SettingFilePath);
|
| | | var ret = Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigSettingModel>(strConfig);
|
| | | 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);
|
| | | using (var client = new HttpClient())
|
| | | {
|
| | | Common.GlobalResource.ElementIds.AddRange(pair.Value);
|
| | | } |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | var content = new MultipartFormDataContent();
|
| | | //content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
|
| | | content.Add(new StringContent("循环水Revit"), "UploadUserName");
|
| | | content.Add(new StringContent(""), "FileCode");
|
| | | content.Add(new StringContent(desc), "Description");
|
| | | content.Add(new ByteArrayContent(File.ReadAllBytes(zipFilePath)), "File", title);
|
| | | try
|
| | | {
|
| | | var res = client.PostAsync(ret.UploadUrl, content).Result.Content.ReadAsStringAsync().Result;
|
| | | var result = JsonHelper.ToObject<HttpResultViewModel>(res);
|
| | | File.Delete(zipFilePath);
|
| | | if (result.Code == 0)
|
| | | return true;
|
| | | else
|
| | | {
|
| | | err = "上传失败,请检查网络设置";// result.Message;
|
| | | return false;
|
| | | }
|
| | | }
|
| | | catch (Exception ex)
|
| | | {
|
| | | err = "上传错误,请检查网络设置!";
|
| | | File.Delete(zipFilePath);
|
| | | File.Delete(structFile);
|
| | | File.Delete(otherFile);
|
| | | File.Delete(modelFile);
|
| | | File.Delete(settingFile);
|
| | | File.Delete(otherFile);
|
| | | return false;
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | catch (Exception ex)
|
| | | {
|
| | | File.Delete(structFile);
|
| | | File.Delete(otherFile);
|
| | | File.Delete(modelFile);
|
| | | File.Delete(settingFile);
|
| | | File.Delete(otherFile);
|
| | | File.Delete(zipFilePath);
|
| | | err = "上传失败,请检查网络设置";// JsonHelper.ToJson(ex);
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | 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";
|
| | | var userFile = GlobalResource.LoginUserFilePath;
|
| | |
|
| | | 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.SetAttributes(modelFile, FileAttributes.Normal);
|
| | | File.Copy(GlobalResource.ExportFilePath, structFile);
|
| | | File.SetAttributes(structFile, FileAttributes.Normal);
|
| | |
|
| | | 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, userFile };
|
| | | 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
|
| | | {
|
| | | 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();
|
| | | var notContains = new List<string>() { "图例构件", "楼板", "墙", "栏杆扶手", "楼梯" };
|
| | | var nameContains = new List<string>() { "混凝土底座" };
|
| | | var c3 = new Autodesk.Revit.DB.FilteredElementCollector(document).WhereElementIsNotElementType();//.WhereElementIsElementType();
|
| | | var c4 = new Autodesk.Revit.DB.FilteredElementCollector(document).WhereElementIsElementType();
|
| | |
|
| | | c3.Concat(c4);
|
| | | //var ids = c3.Where(c => c.Category != null && !notContains.Any(c.Category.Name.Contains)).Select(c => new ElementId(int.Parse(c.Id.ToString()))).Distinct().ToList();
|
| | | var ids = c3.Where(c => (c.Category != null && notContains.Any(c.Category.Name.Contains)) || nameContains.Any(c.Name.Contains)).Select(c => new ElementId(int.Parse(c.Id.ToString()))).Distinct().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("错误", "显隐失败,可能存在不能设置隐藏的构件!");
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|