ParseCore.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:ParseCore
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/13 18:03:03
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Autodesk.Revit.DB;
  14. using JBIM;
  15. using JBIM.Component;
  16. using JBIM.Definition;
  17. using RevitExport;
  18. using RevitExport.Export;
  19. using RevitToJBim.Common;
  20. using RevitToJBim.ParseData;
  21. using SAGA.RevitUtils.Extends;
  22. using JConnector = JBIM.Component.Connector;
  23. namespace RevitToJBim.ComponentParse
  24. {
  25. /*
  26. * 核心解析类不解析关系
  27. */
  28. /// <summary>
  29. /// 核心解析类
  30. /// </summary>
  31. public class ParseCore
  32. {
  33. //获取可用的解析类
  34. public static List<ParseBase> GetUseParsers()
  35. {
  36. List<ParseBase> result = new List<ParseBase>();
  37. Assembly assembly = Assembly.GetCallingAssembly();
  38. Type[] types = assembly.GetTypes();
  39. foreach (var type in types)
  40. {
  41. if (typeof(ParseBase).IsAssignableFrom (type))
  42. {
  43. if (type.IsAbstract || type.IsGenericTypeDefinition)
  44. continue;
  45. var attribute = type.GetCustomAttribute<UsableParseAttribute>();
  46. if (attribute == null)
  47. continue;
  48. var construstor = type.GetConstructor(Type.EmptyTypes);
  49. if (construstor == null)
  50. continue;
  51. if (construstor.Invoke(null) is ParseBase parse)
  52. result.Add(parse);
  53. }
  54. }
  55. return result;
  56. }
  57. public static void AttachObject(ComponentObject bimObj, ElementWrapper wrapper)
  58. {
  59. bimObj.SourceId = wrapper.SourceId;
  60. bimObj.Name = wrapper.RefElement?.Name;
  61. }
  62. public static JConnector CreateConnector(Autodesk.Revit.DB.Connector connector)
  63. {
  64. var result= new JConnector();
  65. result.SourceId = RevitIdGenerator.GetConnectorId(connector);
  66. switch (connector.Domain)
  67. {
  68. case Domain.DomainHvac:
  69. {
  70. result.Domain = ConnectorDomain.DomainHvac.ToString();
  71. break;
  72. }
  73. case Domain.DomainPiping:
  74. {
  75. result.Domain = ConnectorDomain.DomainPiping.ToString();
  76. break;
  77. }
  78. }
  79. result.IsConnected = connector.IsConnected;
  80. result.Description = connector.Description;
  81. result.Origin = BimConvert.ConvertToXYZ(connector.Origin);
  82. return result;
  83. }
  84. /// <summary>
  85. /// 获取Connector包装类
  86. /// </summary>
  87. /// <param name="connector"></param>
  88. /// <returns></returns>
  89. public static ElementWrapper GetConnectorWrapper(Autodesk.Revit.DB.Connector connector)
  90. {
  91. ElementWrapper wrapper = new ElementWrapper(connector, RevitIdGenerator.GetConnectorId(connector));
  92. return wrapper;
  93. }
  94. /// <summary>
  95. /// 获取元素关联的connector关联信息,若connector数量为0,则返回null
  96. /// </summary>
  97. /// <param name="element"></param>
  98. /// <param name="connectors"></param>
  99. /// <returns></returns>
  100. public static ElementOneToManyRel GetConnectorRels(Element element,List<Autodesk.Revit.DB.Connector> connectors)
  101. {
  102. ElementOneToManyRel relMany = new ElementOneToManyRel(element.Id.ToString()) { RelatedObjects = new List<string>() };
  103. relMany.SetElementType(TypeDefinition.Property_ConnectedIds);
  104. foreach (var refConnector in connectors)
  105. {
  106. relMany.RelatedObjects.Add(RevitIdGenerator.GetConnectorId(refConnector));
  107. }
  108. if (relMany.RelatedObjects.Any())
  109. {
  110. return relMany;
  111. }
  112. return null;//创建关系无效
  113. }
  114. /// <summary>
  115. /// 解析定位信息
  116. /// </summary>
  117. /// <param name="location"></param>
  118. /// <returns></returns>
  119. public static GeometryLocation GetLocation(Location location)
  120. {
  121. if (location == null)
  122. return null;
  123. if (location is LocationPoint point)
  124. {
  125. var usePoint = point.Point;
  126. return GeometryLocation.CreatePointLocation(BimConvert.ConvertToXYZ(usePoint));
  127. }
  128. else if (location is LocationCurve curve)
  129. {
  130. var useCurve = curve.Curve.GetPoints();
  131. return GeometryLocation.CreateLineLocation(useCurve.Select(xyz=>BimConvert.ConvertToXYZ(xyz)).ToList());
  132. }
  133. return null;
  134. }
  135. }
  136. }