123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- /*-------------------------------------------------------------------------
- * 功能描述:ExtensibleStorageUtil
- * 作者:xulisong
- * 创建时间: 2019/7/1 9:39:17
- * 版本号:v1.0
- * -------------------------------------------------------------------------*/
- using Autodesk.Revit.DB;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.Revit.DB.ExtensibleStorage;
- namespace FWindSoft.Revit.ExtensibleStorage
- {
- /// <summary>
- /// 扩展存储工具类
- /// </summary>
- public class ExtensibleStorageUtil
- {
- /// <summary>
- /// 扩展数据内容兼容类型
- /// </summary>
- private static HashSet<Type> ContentTypes { get; set; } = new HashSet<Type>();
- /// <summary>
- /// 扩展数据键值兼容类型
- /// </summary>
- private static HashSet<Type> KeyTypes { get; set; } = new HashSet<Type>();
- static ExtensibleStorageUtil()
- {
- InitData();
- }
- private static void InitData()
- {
- #region 内容类型
- ContentTypes.Add(typeof(bool));
- ContentTypes.Add(typeof(byte));
- ContentTypes.Add(typeof(Int16));
- ContentTypes.Add(typeof(Int32));
- ContentTypes.Add(typeof(float));
- ContentTypes.Add(typeof(double));
- ContentTypes.Add(typeof(ElementId));
- ContentTypes.Add(typeof(Guid));
- ContentTypes.Add(typeof(string));
- ContentTypes.Add(typeof(XYZ));
- ContentTypes.Add(typeof(UV));
- ContentTypes.Add(typeof(Entity));
- #endregion
- #region 键值类型
- KeyTypes.Add(typeof(bool));
- KeyTypes.Add(typeof(byte));
- KeyTypes.Add(typeof(Int16));
- KeyTypes.Add(typeof(Int32));
- KeyTypes.Add(typeof(ElementId));
- KeyTypes.Add(typeof(Guid));
- KeyTypes.Add(typeof(string));
- #endregion
-
- }
- /// <summary>
- /// 判定是否是有效的content类型
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static bool IsEffectiveContent(Type type)
- {
- return ContentTypes.Contains(type);
- }
- /// <summary>
- /// 判定是否是有效的key类型
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static bool IsEffectiveKey(Type type)
- {
- return KeyTypes.Contains(type);
- }
- public static bool IsEffectiveArray(Type type,out Type contentType)
- {
- contentType = null;
- var baseType = type.GetInterface(typeof(IList<>).FullName);
- if (baseType == null)
- {
- return false;
- }
- var firstType = baseType.GetGenericArguments()?.FirstOrDefault();
- var flag= firstType != null && IsEffectiveContent(firstType);
- if (flag)
- {
- contentType = firstType;
- }
- return flag;
- }
- public static bool IsEffectiveMap(Type type,out Type outKeyType,out Type outValueTYpe)
- {
- outKeyType = null;
- outValueTYpe = null;
- var baseType = type.GetInterface(typeof(IDictionary<,>).FullName);
- if (baseType == null)
- {
- return false;
- }
- var keyType = baseType.GetGenericArguments()?.FirstOrDefault();
- var valueType = baseType.GetGenericArguments()?.FirstOrDefault();
- var flag= keyType != null && valueType != null && IsEffectiveKey(keyType) && IsEffectiveContent(valueType);
- if (flag)
- {
- outKeyType = keyType;
- outValueTYpe = valueType;
- }
- return flag;
- }
- /// <summary>
- /// 是否为自定义解析存储
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static bool IsCoustomStorage(Type type)
- {
- var baseType = type.GetInterface(typeof(ICoustomStorage).FullName);
- return baseType != null;
- }
- /// <summary>
- /// 获取可用set方法
- /// </summary>
- /// <returns></returns>
- public static MethodInfo GetEntitySetMethod()
- {
- var methods= typeof(Entity).GetMethods(BindingFlags.Public | BindingFlags.Instance);
- foreach (var methodInfo in methods)
- {
- if (methodInfo.Name != "Set")
- {
- continue;
- }
- var parameters = methodInfo.GetParameters();
- if (parameters.Length == 2 && parameters[0].ParameterType == typeof(string))
- {
- return methodInfo;
- }
- }
- return null;
- }
- public static Type GetFileType(Field field)
- {
- var useType = field.ValueType;
- if (field.ContainerType == ContainerType.Array)
- {
- useType = typeof(IList<>).MakeGenericType(field.ValueType);
- }
- else if (field.ContainerType == ContainerType.Map)
- {
- useType = typeof(IDictionary<,>).MakeGenericType(field.KeyType, field.ValueType);
- }
- return useType;
- }
- }
- }
|