JBimParseContext.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 JBIM;
  10. using JBIM.Common;
  11. using JBIM.Component;
  12. using JBIM.Relationship;
  13. using RevitExport;
  14. using RevitToJBim.ComponentParse;
  15. using RevitToJBim.JsonConverter;
  16. namespace RevitToJBim
  17. {
  18. /// <summary>
  19. /// JBimParseContext解析上下文
  20. /// </summary>
  21. public class JBimParseContext: ParseContext
  22. {
  23. public JBimParseContext(List<ParseBase> parsers)
  24. {
  25. Parser = new RevitToJBimParser(this, parsers);
  26. Document = new BimDocument();
  27. RelationShips = new List<ElementRelationShip>();
  28. }
  29. public RevitToJBimParser Parser { get; private set; }
  30. public BimDocument Document { get;private set; }
  31. #region 数据缓存相关
  32. private Dictionary<string, BimId> m_RevitIdMap = new Dictionary<string, BimId>();
  33. public BimId GetBimId(string revitSourceId)
  34. {
  35. if (string.IsNullOrWhiteSpace(revitSourceId))
  36. {
  37. return null;
  38. }
  39. m_RevitIdMap.TryGetValue(revitSourceId, out BimId result);
  40. return result;
  41. }
  42. public List<BimId> GetBimIds(List<string> sourceIds)
  43. {
  44. List<BimId> relatedIds = null;
  45. if (sourceIds != null)
  46. {
  47. relatedIds = new List<BimId>();
  48. foreach (var sourceId in sourceIds)
  49. {
  50. var relatedid = GetBimId(sourceId);
  51. if (relatedid != null)
  52. {
  53. relatedIds.Add(relatedid);
  54. }
  55. }
  56. }
  57. return relatedIds;
  58. }
  59. public BimId AddBimObject(BimObject obj)
  60. {
  61. var id= this.Document.NewObject(obj)?.Id;
  62. if (obj is ComponentObject cObject&&!string.IsNullOrWhiteSpace(cObject.SourceId))
  63. {
  64. m_RevitIdMap[cObject.SourceId] = id;
  65. }
  66. return id;
  67. }
  68. #endregion
  69. #region 关系数据统一存储
  70. public List<ElementRelationShip> RelationShips { get; private set; }
  71. #endregion
  72. #region 解析方法
  73. public List<BimId> Parse(ElementWrapper wrapper)
  74. {
  75. return Parser.ParseElement(wrapper);
  76. }
  77. #endregion
  78. #region 序列化相关信息
  79. public string Serialize()
  80. {
  81. #region 关系数据处理
  82. /*
  83. * 当前关系数据是一个临时数据,需要不全信息之后才能真实使用
  84. * 临时数据中,存储的是SourceId的对应关系
  85. */
  86. foreach (var relationshipBase in RelationShips)
  87. {
  88. if (relationshipBase.IsProperty)
  89. {
  90. UpdateRelationShipInfo(relationshipBase);
  91. }
  92. else
  93. {
  94. var bimRelation = ConvertRelationShipInfo(relationshipBase);
  95. Document.NewObject(bimRelation);
  96. }
  97. }
  98. #endregion
  99. return BimJsonUtil.Serializer(Document);
  100. }
  101. private RelationshipBase ConvertRelationShipInfo(ElementRelationShip ship)
  102. {
  103. var relatingid = GetBimId(ship.RelatingObject);
  104. RelationshipBase useShip = null;
  105. if (relatingid == null)
  106. {
  107. return null;
  108. }
  109. if (ship is ElementOneToOneRel singleRel)
  110. {
  111. var relatedid = GetBimId(singleRel.RelatedObject);
  112. if (relatedid != null)
  113. {
  114. useShip = new OneToOneRel(relatingid, relatedid);
  115. }
  116. }
  117. else if (ship is ElementOneToManyRel manyRel)
  118. {
  119. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  120. if (relatedIds != null)
  121. {
  122. useShip = new OneToManyRel(relatingid, relatedIds);
  123. }
  124. }
  125. return useShip;
  126. }
  127. private void UpdateRelationShipInfo(ElementRelationShip ship)
  128. {
  129. var relatingid = GetBimId(ship.RelatingObject);
  130. if (relatingid == null)
  131. {
  132. return;
  133. }
  134. var bimObject = Document.GetBimObject(relatingid);
  135. if (bimObject == null)
  136. {
  137. return;
  138. }
  139. if (ship is ElementOneToOneRel singleRel)
  140. {
  141. var relatedid = GetBimId(singleRel.RelatedObject);
  142. if (relatedid != null)
  143. {
  144. BimObjectUtil.AcceptRelation(bimObject, ship.PropertyName, relatedid);
  145. }
  146. }
  147. else if (ship is ElementOneToManyRel manyRel)
  148. {
  149. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  150. if (relatedIds != null)
  151. {
  152. BimObjectUtil.AcceptRelations(bimObject, ship.PropertyName, relatedIds);
  153. }
  154. }
  155. return;
  156. }
  157. #endregion
  158. }
  159. }