JBimParseContext.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. try
  85. {
  86. /*
  87. * 当前关系数据是一个临时数据,需要不全信息之后才能真实使用
  88. * 临时数据中,存储的是SourceId的对应关系
  89. */
  90. foreach (var relationshipBase in RelationShips)
  91. {
  92. if (relationshipBase.IsProperty)
  93. {
  94. UpdateRelationShipInfo(relationshipBase);
  95. }
  96. else
  97. {
  98. var bimRelation = ConvertRelationShipInfo(relationshipBase);
  99. Document.NewObject(bimRelation);
  100. }
  101. }
  102. #endregion
  103. }
  104. catch (Exception e)
  105. {
  106. Console.WriteLine(e);
  107. }
  108. #region 关系数据处理
  109. }
  110. /// <summary>
  111. /// 获取关联对象的BimId
  112. /// </summary>
  113. /// <param name="ship"></param>
  114. /// <returns></returns>
  115. private BimId GetRelatingBimId(ElementRelationShip ship)
  116. {
  117. BimId relatingid = null;
  118. if (ship.RelatingObjectIsBimId)
  119. {
  120. relatingid = new BimId(ship.RelatingObject);
  121. }
  122. else
  123. {
  124. relatingid = GetBimId(ship.RelatingObject);
  125. }
  126. return relatingid;
  127. }
  128. private RelationshipBase ConvertRelationShipInfo(ElementRelationShip ship)
  129. {
  130. BimId relatingid = GetRelatingBimId(ship);
  131. RelationshipBase useShip = null;
  132. if (relatingid == null)
  133. {
  134. return null;
  135. }
  136. if (ship is ElementOneToOneRel singleRel)
  137. {
  138. var relatedid = GetBimId(singleRel.RelatedObject);
  139. if (relatedid != null)
  140. {
  141. useShip = new OneToOneRel(relatingid, relatedid);
  142. }
  143. }
  144. else if (ship is ElementOneToManyRel manyRel)
  145. {
  146. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  147. if (relatedIds != null)
  148. {
  149. useShip = new OneToManyRel(relatingid, relatedIds);
  150. }
  151. }
  152. return useShip;
  153. }
  154. private void UpdateRelationShipInfo(ElementRelationShip ship)
  155. {
  156. var relatingid = GetRelatingBimId(ship);
  157. if (relatingid == null)
  158. {
  159. return;
  160. }
  161. var bimObject = Document.GetBimObject(relatingid);
  162. if (bimObject == null)
  163. {
  164. return;
  165. }
  166. if (ship is ElementOneToOneRel singleRel)
  167. {
  168. var relatedid = GetBimId(singleRel.RelatedObject);
  169. if (relatedid != null)
  170. {
  171. BimObjectUtil.AcceptRelation(bimObject, ship.PropertyName, relatedid);
  172. }
  173. }
  174. else if (ship is ElementOneToManyRel manyRel)
  175. {
  176. var relatedIds = GetBimIds(manyRel.RelatedObjects);
  177. if (relatedIds != null)
  178. {
  179. BimObjectUtil.AcceptRelations(bimObject, ship.PropertyName, relatedIds);
  180. }
  181. }
  182. return;
  183. }
  184. #endregion
  185. /// <summary>
  186. /// 获取结果Document
  187. /// </summary>
  188. /// <returns></returns>
  189. public BimDocument GetResultDocument()
  190. {
  191. //没有经过整理的document是半成品,所以经document对应的属性取消公开
  192. ArrangeBimDocument();
  193. return Document;
  194. }
  195. }
  196. }