BimJsonUtil.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = CreateDefaultSettings();
  23. var bimObjects = document.BimObjects;
  24. var group = bimObjects.GroupBy(bim => bim.ElementType);
  25. Dictionary<string, List<BimObject>> dic = new Dictionary<string, List<BimObject>>();
  26. foreach (var collection in group)
  27. {
  28. dic[collection.Key + "s"] = collection.ToList();
  29. }
  30. var documentObject = new {MBIName = "PjXXX_001", PlanName = "F1-saga", Elements = dic};
  31. //var serializer = JsonSerializer.Create(jsetting);
  32. //var jobject=JObject.FromObject(dic, serializer);
  33. //jobject.ToString();
  34. string dataStr =JsonConvert.SerializeObject(documentObject, jsetting);
  35. return dataStr;
  36. }
  37. private static JsonSerializerSettings CreateDefaultSettings()
  38. {
  39. JsonSerializerSettings jsetting = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
  40. jsetting.Converters.Add(new BimIdConverter());
  41. //mxg 显示x:,y:,z:格式的
  42. //jsetting.Converters.Add(new XYZConverter());
  43. jsetting.Converters.Add(new StringEnumConverter());
  44. jsetting.Converters.Add(new ParameterConverter());
  45. return jsetting;
  46. }
  47. private static DocumentDto ConverToDto(BimDocument document)
  48. {
  49. DocumentDto dto = new DocumentDto();
  50. var bimObjects = document.BimObjects;
  51. var group = bimObjects.GroupBy(bim => bim.ElementType);
  52. Dictionary<string, List<BimObject>> dic = new Dictionary<string, List<BimObject>>();
  53. foreach (var collection in group)
  54. {
  55. dic[collection.Key + "s"] = collection.ToList();
  56. }
  57. dto.MBIName = "PjXXX_001";//待读取
  58. dto.PlanName = "F1-saga";
  59. dto.Elements = dic;
  60. return dto;
  61. }
  62. }
  63. /// <summary>
  64. /// 项目传输数据结构
  65. /// </summary>
  66. public class DocumentDto
  67. {
  68. public string MBIName { get; set; }
  69. public string PlanName { get; set; }
  70. public Dictionary<string, List<BimObject>> Elements { get; set; }
  71. }
  72. }