JBimParseContext.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:JBimParseContext
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/13 16:50:43
  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 JBIM.Component;
  14. using JBIM.Relationship;
  15. using Newtonsoft.Json;
  16. using Newtonsoft.Json.Linq;
  17. using RevitExport;
  18. using RevitToJBim.JsonConverter;
  19. namespace RevitToJBim
  20. {
  21. /// <summary>
  22. /// JBimParseContext解析上下文
  23. /// </summary>
  24. public class JBimParseContext: ParseContext
  25. {
  26. public JBimParseContext(List<IParseElement<JBimParseContext, BimId>> parsers)
  27. {
  28. Parser = new RevitToJBimParser(this, parsers);
  29. Document = new BimDocument();
  30. RelationShips = new List<RelationshipBase>();
  31. }
  32. public RevitToJBimParser Parser { get; private set; }
  33. public BimDocument Document { get;private set; }
  34. #region 数据缓存相关
  35. private Dictionary<string, BimId> m_RevitIdMap = new Dictionary<string, BimId>();
  36. public BimId GetBimId(string revitSourceId)
  37. {
  38. if (string.IsNullOrWhiteSpace(revitSourceId))
  39. {
  40. return null;
  41. }
  42. m_RevitIdMap.TryGetValue(revitSourceId, out BimId result);
  43. return result;
  44. }
  45. public BimId AddBimObject(BimObject obj)
  46. {
  47. var id= this.Document.NewObject(obj)?.Id;
  48. if (obj is ComponentObject cObject&&!string.IsNullOrWhiteSpace(cObject.SourceId))
  49. {
  50. m_RevitIdMap[cObject.SourceId] = id;
  51. }
  52. return id;
  53. }
  54. #endregion
  55. #region 关系数据统一存储
  56. public List<RelationshipBase> RelationShips { get; private set; }
  57. #endregion
  58. #region 解析方法
  59. public List<BimId> Parse(ElementWrapper wrapper)
  60. {
  61. return Parser.ParseElement(wrapper);
  62. }
  63. #endregion
  64. #region 序列化相关信息
  65. public string Serialize()
  66. {
  67. #region 关系数据处理
  68. /*
  69. * 当前关系数据是一个临时数据,需要不全信息之后才能真实使用
  70. * 临时数据中,存储的是SourceId的对应关系
  71. */
  72. var useShips = new List<RelationshipBase>();
  73. foreach (var relationshipBase in RelationShips)
  74. {
  75. if (!relationshipBase.IsProperty)
  76. {
  77. Document.NewObject(relationshipBase);
  78. }
  79. else
  80. {
  81. //var bimId = GetBimId(relationshipBase.re)
  82. }
  83. }
  84. #endregion
  85. return BimJsonUtil.Serializer(Document);
  86. }
  87. private RelationshipBase UpdateRelationShipInfo(RelationshipBase ship)
  88. {
  89. if (ship is OneToOneRel singleRel)
  90. {
  91. var relatingid = GetBimId(singleRel.RelatingObject);
  92. var relatedid = GetBimId(singleRel.RelatedObject);
  93. }
  94. else if (ship is OneToManyRel manyRel)
  95. {
  96. var relatingid = GetBimId(manyRel.RelatingObject);
  97. List<BimId> relatedIds = new List<BimId>();
  98. if (manyRel.RelatedObjects != null)
  99. {
  100. foreach (var manyRelRelatedObject in manyRel.RelatedObjects)
  101. {
  102. var relatedid = GetBimId(manyRelRelatedObject);
  103. if (relatedid != null)
  104. {
  105. relatedIds.Add(relatedid);
  106. }
  107. }
  108. }
  109. }
  110. return ship;
  111. }
  112. #endregion
  113. }
  114. }