JBimParseContext.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. protected 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 整理document数据
  82. private void ArrangeBimDocument()
  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. }
  103. /// <summary>
  104. /// 获取关联对象的BimId
  105. /// </summary>
  106. /// <param name="ship"></param>
  107. /// <returns></returns>
  108. private BimId GetRelatingBimId(ElementRelationShip ship)
  109. {
  110. BimId relatingid = null;
  111. if (ship.RelatingObjectIsBimId)
  112. {
  113. relatingid = new BimId(ship.RelatingObject);
  114. }
  115. else
  116. {
  117. relatingid = GetBimId(ship.RelatingObject);
  118. }
  119. return relatingid;
  120. }
  121. private RelationshipBase ConvertRelationShipInfo(ElementRelationShip ship)
  122. {
  123. BimId relatingid = GetRelatingBimId(ship);
  124. RelationshipBase useShip = null;
  125. if (relatingid == null)
  126. {
  127. return null;
  128. }
  129. if (ship is ElementOneToOneRel singleRel)
  130. {
  131. var relatedid = GetBimId(singleRel.RelatedObject);
  132. if (relatedid != null)
  133. {
  134. useShip = new OneToOneRel(relatingid, relatedid);
  135. }
  136. }
  137. else if (ship is ElementOneToManyRel manyRel)
  138. {
  139. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  140. if (relatedIds != null)
  141. {
  142. useShip = new OneToManyRel(relatingid, relatedIds);
  143. }
  144. }
  145. return useShip;
  146. }
  147. private void UpdateRelationShipInfo(ElementRelationShip ship)
  148. {
  149. var relatingid = GetRelatingBimId(ship);
  150. if (relatingid == null)
  151. {
  152. return;
  153. }
  154. var bimObject = Document.GetBimObject(relatingid);
  155. if (bimObject == null)
  156. {
  157. return;
  158. }
  159. if (ship is ElementOneToOneRel singleRel)
  160. {
  161. var relatedid = GetBimId(singleRel.RelatedObject);
  162. if (relatedid != null)
  163. {
  164. BimObjectUtil.AcceptRelation(bimObject, ship.PropertyName, relatedid);
  165. }
  166. }
  167. else if (ship is ElementOneToManyRel manyRel)
  168. {
  169. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  170. if (relatedIds != null)
  171. {
  172. BimObjectUtil.AcceptRelations(bimObject, ship.PropertyName, relatedIds);
  173. }
  174. }
  175. return;
  176. }
  177. #endregion
  178. /// <summary>
  179. /// 获取结果Document
  180. /// </summary>
  181. /// <returns></returns>
  182. public BimDocument GetResultDocument()
  183. {
  184. //没有经过整理的document是半成品,所以经document对应的属性取消公开
  185. ArrangeBimDocument();
  186. return Document;
  187. }
  188. }
  189. }