ExtensibleStorageExtension.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using Autodesk.Revit.DB;
  2. using Autodesk.Revit.DB.ExtensibleStorage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FWindSoft.Revit.ExtensibleStorage
  10. {
  11. /*
  12. * 可能只能存储,几个kb的值
  13. * 1、Schema需要一次创建,创建完成之后不能再进行修改
  14. * 2、api可用性很高,暂时简单封装
  15. */
  16. public static class ExtensibleStorageExtension
  17. {
  18. #region 基础扩展数据操作
  19. public static void SetExtendData<T>(this Element element, SchemaItem schemaItem, T value)
  20. {
  21. var guid = schemaItem.SchemaGuid;
  22. var schemaName = schemaItem.SchemaName;
  23. var fileName = schemaItem.FileName;
  24. Schema schema = Schema.Lookup(guid);
  25. if (schema == null)
  26. {
  27. SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
  28. schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
  29. schemaBuilder.SetWriteAccessLevel(AccessLevel.Public);
  30. //这个应该和上面重复吧
  31. schemaBuilder.SetSchemaName(schemaName);
  32. FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField(fileName, typeof(T));
  33. schema = schemaBuilder.Finish();
  34. }
  35. Entity entity = new Entity(schema);
  36. Field field = schema.GetField(fileName);
  37. entity.Set<T>(field, value);
  38. element.SetEntity(entity);
  39. }
  40. public static void SetExtendData<T>(this Element element, SchemaItem schemaItem, IList<T> values)
  41. {
  42. var guid = schemaItem.SchemaGuid;
  43. var schemaName = schemaItem.SchemaName;
  44. var fileName = schemaItem.FileName;
  45. Schema schema = Schema.Lookup(guid);
  46. if (schema == null)
  47. {
  48. SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
  49. schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
  50. schemaBuilder.SetWriteAccessLevel(AccessLevel.Public);
  51. schemaBuilder.SetSchemaName(schemaName);
  52. FieldBuilder fieldBuilder = schemaBuilder.AddArrayField(fileName, typeof(T));
  53. schema = schemaBuilder.Finish();
  54. }
  55. Entity entity = new Entity(schema);
  56. Field field = schema.GetField(fileName);
  57. entity.Set<IList<T>>(field, values);
  58. element.SetEntity(entity);
  59. }
  60. public static void SetExtendData<T, V>(this Element element, SchemaItem schemaItem, IDictionary<T, V> dict)
  61. {
  62. var guid = schemaItem.SchemaGuid;
  63. var schemaName = schemaItem.SchemaName;
  64. var fileName = schemaItem.FileName;
  65. Schema schema = Schema.Lookup(guid);
  66. if (schema == null)
  67. {
  68. SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
  69. schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
  70. schemaBuilder.SetWriteAccessLevel(AccessLevel.Public);
  71. schemaBuilder.SetSchemaName(schemaName);
  72. FieldBuilder fieldBuilder = schemaBuilder.AddMapField(fileName, typeof(T), typeof(V));
  73. schema = schemaBuilder.Finish();
  74. }
  75. Entity entity = new Entity(schema);
  76. Field field = schema.GetField(fileName);
  77. entity.Set<IDictionary<T, V>>(field, dict);
  78. element.SetEntity(entity);
  79. }
  80. public static T GetExtendData<T>(this Element element, SchemaItem schemaItem)
  81. {
  82. var guid = schemaItem.SchemaGuid;
  83. var schemaName = schemaItem.SchemaName;
  84. var fileName = schemaItem.FileName;
  85. Schema schema = Schema.Lookup(guid);
  86. if (schema != null)
  87. {
  88. Entity entity = element.GetEntity(schema);
  89. if (entity.Schema == null)
  90. {
  91. return default(T);
  92. }
  93. Field field = schema.GetField(fileName);
  94. if (!entity.RecognizedField(field))
  95. {
  96. return default(T);
  97. }
  98. return entity.Get<T>(field);
  99. }
  100. return default(T);
  101. }
  102. public static List<T> GetExtendDatas<T>(this Element element, SchemaItem schemaItem)
  103. {
  104. var guid = schemaItem.SchemaGuid;
  105. var fileName = schemaItem.FileName;
  106. Schema schema = Schema.Lookup(guid);
  107. if (schema != null)
  108. {
  109. Entity entity = element.GetEntity(schema);
  110. Field field = schema.GetField(fileName);
  111. return entity.Get<IList<T>>(field).ToList();
  112. }
  113. return null;
  114. }
  115. public static Dictionary<T, TU> GetExtendData<T, TU>(this Element element, SchemaItem schemaItem)
  116. {
  117. var guid = schemaItem.SchemaGuid;
  118. var schemaName = schemaItem.SchemaName;
  119. var fileName = schemaItem.FileName;
  120. Schema schema = Schema.Lookup(guid);
  121. if (schema != null)
  122. {
  123. Entity entity = element.GetEntity(schema);
  124. Field field = schema.GetField(fileName);
  125. return entity.Get<IDictionary<T, TU>>(field) as Dictionary<T, TU>;
  126. }
  127. return null;
  128. }
  129. #endregion
  130. /// <summary>
  131. /// 删除扩展数据
  132. /// </summary>
  133. /// <param name="element"></param>
  134. /// <param name="guid"></param>
  135. public static void DeleteStorageData(this Element element, Guid guid)
  136. {
  137. Schema schema = Schema.Lookup(guid);
  138. if (schema != null)
  139. {
  140. element.DeleteEntity(schema);
  141. }
  142. }
  143. /// <summary>
  144. /// 存储简单对象数据
  145. /// </summary>
  146. /// <typeparam name="T"></typeparam>
  147. /// <param name="element"></param>
  148. /// <param name="value"></param>
  149. public static void SetSimpleClass<T>(this Element element, T value)
  150. {
  151. var useType = typeof(T);
  152. Guid guid = useType.GUID;
  153. Schema schema = Schema.Lookup(guid);
  154. #region 初始化属性集合
  155. List<Tuple<string, Type, object>> useProperties = new List<Tuple<string, Type, object>>();
  156. if (ExtensibleStorageUtil.IsCoustomStorage(useType))
  157. {
  158. var useValue = ((ICoustomStorage)value).GetStorage();
  159. var propertyType = useValue.GetType();
  160. useProperties.Add(new Tuple<string, Type, object>(useType.Name, propertyType, useValue));
  161. }
  162. else
  163. {
  164. var properties = useType.GetProperties();
  165. foreach (var propertyInfo in properties)
  166. {
  167. var propertyType = propertyInfo.PropertyType;
  168. var useValue = propertyInfo.GetValue(value);
  169. useProperties.Add(new Tuple<string, Type, object>(propertyInfo.Name, propertyType, useValue));
  170. }
  171. }
  172. #endregion
  173. #region 创建结构
  174. if (schema == null)
  175. {
  176. #region 创建Schema
  177. SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
  178. schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
  179. schemaBuilder.SetWriteAccessLevel(AccessLevel.Public);
  180. schemaBuilder.SetSchemaName(useType.Name);
  181. #endregion
  182. foreach (var item in useProperties)
  183. {
  184. var propertyType = item.Item2;
  185. if (ExtensibleStorageUtil.IsEffectiveContent(propertyType))
  186. {
  187. FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField(item.Item1, propertyType);
  188. }
  189. else if (ExtensibleStorageUtil.IsEffectiveArray(propertyType, out Type arryContentType))
  190. {
  191. FieldBuilder fieldBuilder = schemaBuilder.AddArrayField(item.Item1, arryContentType);
  192. }
  193. else if (ExtensibleStorageUtil.IsEffectiveMap(propertyType, out Type keyType, out Type valueType))
  194. {
  195. FieldBuilder fieldBuilder = schemaBuilder.AddMapField(item.Item1, keyType, valueType);
  196. }
  197. }
  198. schema = schemaBuilder.Finish();
  199. }
  200. #endregion
  201. #region 创建对象
  202. Entity entity = new Entity(schema);
  203. var method = ExtensibleStorageUtil.GetEntitySetMethod();
  204. foreach (var item in useProperties)
  205. {
  206. var field = schema.GetField(item.Item1);
  207. if (field == null)
  208. continue;
  209. var fieldType = ExtensibleStorageUtil.GetFileType(field);
  210. MethodInfo mi = method.MakeGenericMethod(fieldType);
  211. var useValue = item.Item3;
  212. if (useValue != null)
  213. {
  214. mi.Invoke(entity, new object[] { item.Item1, useValue });
  215. }
  216. }
  217. #endregion
  218. element.SetEntity(entity);
  219. }
  220. /// <summary>
  221. /// 获取简单对象数据
  222. /// </summary>
  223. /// <typeparam name="T"></typeparam>
  224. /// <param name="element"></param>
  225. public static T GetSimpleClass<T>(this Element element)
  226. {
  227. var useType = typeof(T);
  228. Guid guid = useType.GUID;
  229. Schema schema = Schema.Lookup(guid);
  230. if (schema == null)
  231. {
  232. return default(T);
  233. }
  234. Entity entity = element.GetEntity(schema);
  235. if (entity?.Schema == null)
  236. {
  237. return default(T);
  238. }
  239. var fields = schema.ListFields();
  240. #region 创建实体对象
  241. T result = default(T);
  242. if (fields.Any())
  243. {
  244. var cons = useType.GetConstructor(Type.EmptyTypes);
  245. if (cons != null)
  246. {
  247. result = (T)cons.Invoke(null);
  248. }
  249. }
  250. if (result == null)
  251. {
  252. return default(T);
  253. }
  254. #endregion
  255. var getMethod = typeof(Entity).GetMethod("Get", new Type[] { typeof(string) });
  256. if (ExtensibleStorageUtil.IsCoustomStorage(useType))
  257. {
  258. var field = fields.FirstOrDefault(f => f.FieldName == useType.Name);
  259. if (field != null)
  260. {
  261. var useValueType = field.ValueType;
  262. if (field.ContainerType == ContainerType.Array)
  263. {
  264. useValueType = typeof(IList<>).MakeGenericType(field.ValueType);
  265. }
  266. else if (field.ContainerType == ContainerType.Map)
  267. {
  268. useValueType = typeof(IDictionary<,>).MakeGenericType(field.KeyType, field.ValueType);
  269. }
  270. MethodInfo mi = getMethod.MakeGenericMethod(useValueType);
  271. var useValue = mi.Invoke(entity, new object[] { field.FieldName });
  272. ((ICoustomStorage)result).SetStorage(useValue);
  273. }
  274. }
  275. else
  276. {
  277. foreach (var field in fields)
  278. {
  279. try
  280. {
  281. var property = useType.GetProperty(field.FieldName);
  282. if (property != null)
  283. {
  284. MethodInfo mi = getMethod.MakeGenericMethod(property.PropertyType);
  285. var useValue = mi.Invoke(entity, new object[] { field.FieldName });
  286. property.SetValue(result, useValue);
  287. }
  288. }
  289. catch (Exception)
  290. {
  291. }
  292. }
  293. }
  294. return result;
  295. }
  296. }
  297. public class SchemaItem
  298. {
  299. public Guid SchemaGuid { get; set; }
  300. public string SchemaName { get; set; }
  301. public string FileName { get; set; }
  302. }
  303. }