From f0e02d57957827a48013c0291b89371839788075 Mon Sep 17 00:00:00 2001 From: zhangyk-c <zhangyk-c@glodon.com> Date: 星期三, 31 七月 2024 09:31:12 +0800 Subject: [PATCH] miss file commit --- HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs | 70 ++++++++ HStation.RevitDev/RevitDataExport/Utility/ExportModelHelper.cs | 90 +++++++++++ HStation.RevitDev/HStation.RevitDev.Model/ModelEnum/Unit.cs | 12 + HStation.RevitDev/RevitDataExport/Entity/RecordClass.cs | 17 ++ HStation.RevitDev/RevitDataExport/Utility/ElementExtense.cs | 266 +++++++++++++++++++++++++++++++++ 5 files changed, 455 insertions(+), 0 deletions(-) diff --git a/HStation.RevitDev/HStation.RevitDev.Model/ModelEnum/Unit.cs b/HStation.RevitDev/HStation.RevitDev.Model/ModelEnum/Unit.cs new file mode 100644 index 0000000..6d12c13 --- /dev/null +++ b/HStation.RevitDev/HStation.RevitDev.Model/ModelEnum/Unit.cs @@ -0,0 +1,12 @@ +锘縩amespace HStation.RevitDev.Model.ModelEnum +{ + public enum ParameterUnit + { + PU_Unknown, + PU_MM, + PU_M, + PU_MMM, + PU_Pa, + PU_MPa, + } +} diff --git a/HStation.RevitDev/RevitDataExport/Entity/RecordClass.cs b/HStation.RevitDev/RevitDataExport/Entity/RecordClass.cs new file mode 100644 index 0000000..d60c2a1 --- /dev/null +++ b/HStation.RevitDev/RevitDataExport/Entity/RecordClass.cs @@ -0,0 +1,17 @@ +锘縰sing HStation.RevitDev.Model.ModelEnum; +using System.Collections.Generic; + +namespace HStation.RevitDev.RevitDataExport.Entity +{ + public class Records + { + public List<ConfigRecord> ConfigRecords { get; set; } + } + + public class ConfigRecord + { + public string FilePath { get; set; } + + public Dictionary<RevitType, List<string>> Record { get; set; } + } +} diff --git a/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs b/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs new file mode 100644 index 0000000..ea90886 --- /dev/null +++ b/HStation.RevitDev/RevitDataExport/Utility/CacheUtil.cs @@ -0,0 +1,70 @@ +锘縰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); + } + } +} diff --git a/HStation.RevitDev/RevitDataExport/Utility/ElementExtense.cs b/HStation.RevitDev/RevitDataExport/Utility/ElementExtense.cs new file mode 100644 index 0000000..f24bbb5 --- /dev/null +++ b/HStation.RevitDev/RevitDataExport/Utility/ElementExtense.cs @@ -0,0 +1,266 @@ +锘縰sing Autodesk.Revit.DB; +using Glodon.Revit.Utility; +using HStation.Model; +using HStation.RevitDev.Model.AttributeClass; +using HStation.RevitDev.Model.Common; +using HStation.RevitDev.Model.ModelEnum; +using HStation.RevitDev.RevitDataExport.Common; +using Newtonsoft.Json.Linq; +using System; +using System.Reflection; + +namespace HStation.RevitDev.RevitDataExport.Utility +{ + public static class ElementExtense + { + public static JObject Export(this Element elem, RevitType revitType) + { + var exportType = ElementTypeMapping.GetExportType(revitType); + var elemType = GetElementType(exportType); + var ret = SetElementPropertys(elemType, elem); + return ret; + } + + private static JObject SetElementPropertys(Type elemType, Element elem) + { + var ret = new JObject(); + var props = elemType.GetProperties(); + foreach (PropertyInfo propInfo in props) + { + var attr = propInfo.GetCustomAttribute<ParameterAttribute>(); + if (attr == null) { continue; } + + string paramName = attr.m_parameterName; + ParameterUnit unit = attr.m_unit; + var paramType = propInfo.PropertyType; + if (!AddManualParameter(elem, ret, paramName)) + { + AddGeneralParameter(elem, ret, propInfo, paramName, paramType, unit); + } + } + return ret; + } + + private static bool AddManualParameter(Element elem, JObject ret, string paramName) + { + return ParameterOperator.ProcessingManualParameter(elem, ret, paramName); + } + + private static void AddGeneralParameter(Element elem, JObject ret, PropertyInfo propInfo, string paramName, Type paramType, ParameterUnit unit) + { + var propType = propInfo.PropertyType; + object revitParamValue = GetParameterValue(elem, paramName, propType); + if (revitParamValue == null) { return; } + + object outParamValue; + + switch (paramType.Name) + { + case "Int32": + outParamValue = revitParamValue.ConvertTo<int>(); + ret[paramName] = (int)outParamValue; + break; + case "Double": + switch (unit) + { + case ParameterUnit.PU_Unknown: + outParamValue = revitParamValue.ConvertTo<double>(); + ret[paramName] = (double)outParamValue; + break; + case ParameterUnit.PU_MM: + outParamValue = revitParamValue.ConvertTo<double>(); + ret[paramName] = (double)outParamValue.InnerToMM(); + break; + case ParameterUnit.PU_M: + break; + case ParameterUnit.PU_MMM: + break; + case ParameterUnit.PU_Pa: + break; + case ParameterUnit.PU_MPa: + break; + default: + break; + } + break; + case "String": + outParamValue = revitParamValue.ConvertTo<string>(); + ret[paramName] = (string)outParamValue; + break; + case "Boolean": + outParamValue = revitParamValue.ConvertTo<bool>(); + ret[paramName] = (bool)outParamValue; + break; + default: + throw new Exception("鏈煡绫诲瀷锛�"); + } + } + + private static T ConvertTo<T>(this object inObj) + { + var inTypeName = inObj.GetType().Name; + var outTypeName = typeof(T).Name; + switch (inTypeName) + { + case "String": + switch (outTypeName) + { + case "String": + return (T)inObj; + case "Int32": + if (int.TryParse((string)inObj, out int iValue)) + { + return (T)(object)iValue; + } + else + { + throw new Exception($"鍙傛暟绫诲瀷{inTypeName} 杞崲{outTypeName}寮傚父锛�"); + } + case "Double": + if (double.TryParse((string)inObj, out double dValue)) + { + return (T)(object)dValue; + } + else + { + throw new Exception($"鍙傛暟绫诲瀷{inTypeName} 杞崲{outTypeName}寮傚父锛�"); + } + case "Boolean": + if (bool.TryParse((string)inObj, out bool bValue)) + { + return (T)(object)bValue; + } + else + { + throw new Exception($"鍙傛暟绫诲瀷{inTypeName} 杞崲{outTypeName}寮傚父锛�"); + } + default: + break; + } + break; + case "Int32": + switch (outTypeName) + { + case "String": + return (T)(object)inObj.ToString(); + case "Int32": + return (T)inObj; + case "Double": + return (T)inObj; + case "Boolean": + throw new Exception($"鏃犳硶杞崲鍙傛暟绫诲瀷{inTypeName}鍒皗outTypeName}锛�"); + default: + break; + } + break; + case "Double": + switch (outTypeName) + { + case "String": + return (T)(object)inObj.ToString(); + case "Int32": + case "Double": + return (T)inObj; + case "Boolean": + throw new Exception($"鏃犳硶杞崲鍙傛暟绫诲瀷{inTypeName}鍒皗outTypeName}锛�"); + default: + break; + } + break; + case "Boolean": + switch (outTypeName) + { + case "String": + return (T)(object)inObj.ToString(); + case "Int32": + case "Double": + case "Boolean": + return (T)inObj; + default: + break; + } + break; + default: + return default; + } + return default; + } + + private static object GetParameterValue(Element elem, string paramName, Type propType) + { + object paramValue = null; + try + { + if (propType.Name == "String") + { + var strValue = ParameterOperator.GetParameterValueAsString(elem, paramName); + paramValue = strValue == null ? string.Empty : strValue; + } + else if (propType.Name == "Int32") + { + paramValue = ParameterOperator.GetParameterValueAsInt(elem, paramName); + } + else if (propType.Name == "Double") + { + paramValue = ParameterOperator.GetParameterValueAsDouble(elem, paramName); + } + else if (propType.Name == "Boolean") + { + int? value = ParameterOperator.GetParameterValueAsInt(elem, paramName); + if (value == 1) + { + paramValue = true; + } + else + { + paramValue = false; + } + + } + } + catch (Exception ex) + { + return paramValue; + } + + return paramValue; + } + + private static Type GetElementType(ExportType exportType) + { + var types = Assembly.GetAssembly(typeof(RevitModel)).GetTypes(); + foreach (var type in types) + { + var attr = type.GetCustomAttribute<ExportTypeAttribute>(); + if (attr == null) { continue; } + + if (attr.m_exportType == exportType) + { + return type; + } + } + return null; + } + + public static Level GetLevel(this Element element) + { + var levelId1 = element.LevelId; + if (levelId1 != null) + { + Level level1 = element.Document.GetElement(levelId1) as Level; + return level1; + } + var levelParam = element.LookupParameter("鏍囬珮"); + if (levelParam != null) + { + ElementId levelId2 = levelParam.AsElementId(); + if (levelId2 != null) + { + Level level2 = element.Document.GetElement(levelId2) as Level; + return level2; + } + } + return null; + } + } +} diff --git a/HStation.RevitDev/RevitDataExport/Utility/ExportModelHelper.cs b/HStation.RevitDev/RevitDataExport/Utility/ExportModelHelper.cs new file mode 100644 index 0000000..628285b --- /dev/null +++ b/HStation.RevitDev/RevitDataExport/Utility/ExportModelHelper.cs @@ -0,0 +1,90 @@ +锘縰sing Autodesk.Revit.DB; +using HStation.Model; +using HStation.RevitDev.Model.AttributeClass; +using HStation.RevitDev.Model.Common; +using HStation.RevitDev.Model.ModelEnum; +using HStation.RevitDev.RevitDataExport.Common; +using Newtonsoft.Json.Linq; +using System; +using System.Reflection; + +namespace HStation.RevitDev.RevitDataExport.Utility +{ + public class ExportModelHelper + { + Document m_doc; + + public ExportModelHelper(Document doc) + { + m_doc = doc; + } + + public string Export() + { + var modelMap = GlobalResource.RevitModels; + JObject jobj = new JObject(); + jobj["鍚嶇О"] = m_doc.PathName; + jobj["鎻忚堪"] = m_doc.PathName; + foreach (var modelPair in modelMap) + { + var revitType = modelPair.Key; + var jArray = ExportRevitModel(revitType); + + string propName = GetRevitModelPropertyName(revitType); + JArray array = jobj[propName] as JArray; + if (array == null) + { + jobj[propName] = jArray; + } + else + { + foreach (var item in jArray) + { + array.Add(item); + } + } + } + return jobj.ToString(); + } + + private string GetRevitModelPropertyName(RevitType revitType) + { + Type revitModelType = typeof(RevitModel); + ExportType expType = ElementTypeMapping.GetExportType(revitType); + var props = revitModelType.GetProperties(); + foreach (var prop in props) + { + var attr = prop.GetCustomAttribute<ExportTypeAttribute>(); + if (attr == null) { continue; } + + var attrExpType = attr.m_exportType; + if (expType == attrExpType) + { + return prop.Name; + } + } + return null; + } + + private JArray ExportRevitModel(RevitType revitType) + { + var revitModels = GlobalResource.RevitModels[revitType]; + var outElems = new JArray(); + + foreach (var elemId in revitModels) + { + ElementId id = null; + if (int.TryParse(elemId, out int elementId)) + { + id = new ElementId(elementId); + } + Element rvtElem = m_doc.GetElement(id); + if (rvtElem == null) { continue; } + + JObject outElem = rvtElem.Export(revitType); + outElems.Add(outElem); + } + return outElems; + } + } +} -- Gitblit v1.9.3