JBimParseContext.cs 6.0 KB

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