BimJsonUtil.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:BimJsonUtil
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/18 11:23:16
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using JBIM;
  13. using Newtonsoft.Json;
  14. using Newtonsoft.Json.Converters;
  15. using Newtonsoft.Json.Linq;
  16. namespace RevitToJBim.JsonConverter
  17. {
  18. public static class BimJsonUtil
  19. {
  20. public static string Serializer(BimDocument document)
  21. {
  22. JsonSerializerSettings jsetting = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
  23. jsetting.Converters.Add(new BimIdConverter());
  24. //mxg 显示x:,y:,z:格式的
  25. //jsetting.Converters.Add(new XYZConverter());
  26. jsetting.Converters.Add(new StringEnumConverter());
  27. jsetting.Converters.Add(new ParameterConverter());
  28. var bimObjects = document.BimObjects;
  29. var group = bimObjects.GroupBy(bim => bim.ElementType);
  30. Dictionary<string, List<BimObject>> dic = new Dictionary<string, List<BimObject>>();
  31. foreach (var collection in group)
  32. {
  33. dic[collection.Key + "s"] = collection.ToList();
  34. //jobject.Add(collection.Key, JArray.FromObject(collection.Value, serializer));
  35. }
  36. //var settings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
  37. string dataStr = JsonConvert.SerializeObject(dic, jsetting);
  38. return dataStr;
  39. }
  40. }
  41. }