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