JBimParseContext.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /// <summary>
  102. /// 获取关联对象的BimId
  103. /// </summary>
  104. /// <param name="ship"></param>
  105. /// <returns></returns>
  106. private BimId GetRelatingBimId(ElementRelationShip ship)
  107. {
  108. BimId relatingid = null;
  109. if (ship.RelatingObjectIsBimId)
  110. {
  111. relatingid = new BimId(ship.RelatingObject);
  112. }
  113. else
  114. {
  115. relatingid = GetBimId(ship.RelatingObject);
  116. }
  117. return relatingid;
  118. }
  119. private RelationshipBase ConvertRelationShipInfo(ElementRelationShip ship)
  120. {
  121. BimId relatingid = GetRelatingBimId(ship);
  122. RelationshipBase useShip = null;
  123. if (relatingid == null)
  124. {
  125. return null;
  126. }
  127. if (ship is ElementOneToOneRel singleRel)
  128. {
  129. var relatedid = GetBimId(singleRel.RelatedObject);
  130. if (relatedid != null)
  131. {
  132. useShip = new OneToOneRel(relatingid, relatedid);
  133. }
  134. }
  135. else if (ship is ElementOneToManyRel manyRel)
  136. {
  137. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  138. if (relatedIds != null)
  139. {
  140. useShip = new OneToManyRel(relatingid, relatedIds);
  141. }
  142. }
  143. return useShip;
  144. }
  145. private void UpdateRelationShipInfo(ElementRelationShip ship)
  146. {
  147. var relatingid = GetRelatingBimId(ship);
  148. if (relatingid == null)
  149. {
  150. return;
  151. }
  152. var bimObject = Document.GetBimObject(relatingid);
  153. if (bimObject == null)
  154. {
  155. return;
  156. }
  157. if (ship is ElementOneToOneRel singleRel)
  158. {
  159. var relatedid = GetBimId(singleRel.RelatedObject);
  160. if (relatedid != null)
  161. {
  162. BimObjectUtil.AcceptRelation(bimObject, ship.PropertyName, relatedid);
  163. }
  164. }
  165. else if (ship is ElementOneToManyRel manyRel)
  166. {
  167. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  168. if (relatedIds != null)
  169. {
  170. BimObjectUtil.AcceptRelations(bimObject, ship.PropertyName, relatedIds);
  171. }
  172. }
  173. return;
  174. }
  175. #endregion
  176. }
  177. }