systemutils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """system calc utils.
  2. 1. get connectors where model_id='@model_id' and belong_type='Equipment' and mep_system_type='@system_name'
  3. 2. get path start with connector,end with triple ,cross,equipment,end
  4. """
  5. from systemrelation import revit_const
  6. from functools import cmp_to_key
  7. import vg
  8. from systemrelation import graph_model
  9. def path2edge(elements):
  10. """
  11. Convert path to GraphtEdge
  12. :param elements:
  13. :return:
  14. """
  15. pass
  16. def graph_arrange(graph):
  17. """
  18. Combine Vertex that connect to virtul Vertex
  19. :param graph:
  20. :return:
  21. """
  22. pass
  23. def connector_sort(connector1, connector2):
  24. """Use Define Sort rule"""
  25. dic = {revit_const.EQUIPMENT: 0, revit_const.COMPONENT: 1, revit_const.OTHER: 2, revit_const.PIPE: 3,
  26. revit_const.DUCT: 4}
  27. x = dic[connector1.belong_type]
  28. y = dic[connector2.belong_type]
  29. return 1 if x > y else -1
  30. def sort_connectors(connectors):
  31. """Sort Connector order by Equipment>EquipPart>Other>Pipe>Duct"""
  32. return sorted(connectors, key=cmp_to_key(lambda x, y: connector_sort(x, y)))
  33. def is_break_condition(element):
  34. """Path end Condition."""
  35. res = element.type in revit_const.FACILITY
  36. return res
  37. def dic2obj(d):
  38. """Convert Dict to Object.
  39. """
  40. top = type('new', (object,), d)
  41. seqs = tuple, list, set, frozenset
  42. for i, j in d.items():
  43. if isinstance(j, dict):
  44. setattr(top, i, dic2obj(j))
  45. elif isinstance(j, seqs):
  46. setattr(top, i,
  47. type(j)(dic2obj(sj) if isinstance(sj, dict) else sj for sj in j))
  48. else:
  49. setattr(top, i, j)
  50. return top
  51. def list_to_dict(lis):
  52. ids = [idx.id for idx in lis]
  53. dic = dict(zip(ids, lis))
  54. return dic
  55. def add_attr_is_used(x):
  56. """
  57. Add is_used attribute.
  58. :param x:
  59. :return:
  60. """
  61. x.is_used = False
  62. return x
  63. def is_xyz_equal(origin1, origin2):
  64. """
  65. Judge the two origin are almost equal.
  66. :param origin1:
  67. :param origin2:
  68. :return:
  69. """
  70. v1 = [origin1.X, origin1.Y, origin1.Z]
  71. v2 = [origin2.X, origin2.Y, origin2.Z]
  72. return vg.almost_equal(v1, v2, atol=1e-08)
  73. def combine_systemgraph(floorgraphs):
  74. """
  75. Consider the riser merger
  76. :param floorgraphs:
  77. :return:projectgraph
  78. """
  79. # get all open vertical pipe
  80. open_vertical_pipes = []
  81. for floorgraph in floorgraphs:
  82. for systemgraph in floorgraph.system_graphs:
  83. open_vertical_pipes.extend(systemgraph.get_open_vertical_pipes())
  84. # group systemvertex
  85. projectgraph = graph_model.ProjectGraph()
  86. for i in range(len(open_vertical_pipes)):
  87. v1 = open_vertical_pipes[i]
  88. group_pipes = [v1]
  89. for j in range(i + 1, len(open_vertical_pipes)):
  90. v2 = open_vertical_pipes[j]
  91. if is_xyz_equal(v1.connector.origin, v2.connector.origin):
  92. group_pipes.append(v2)
  93. # combine systemgraph
  94. for k in range(len(group_pipes)):
  95. vertex = group_pipes[k]
  96. systemgraph = get_specify_type_parent(vertex,graph_model.SystemGraph)
  97. if not systemgraph:
  98. continue
  99. projectgraph.remove_groupedsystemgraphs(systemgraph)
  100. if (k == 0):
  101. temp_groupedsystemgraphs = graph_model.GroupedSystemGraph(systemgraph)
  102. else:
  103. prev_vertex = group_pipes[k - 1]
  104. temp_groupedsystemgraphs.add_system(systemgraph)
  105. temp_groupedsystemgraphs.add_edge(prev_vertex, vertex)
  106. if temp_groupedsystemgraphs:
  107. projectgraph.groupedsystemgraphs.append(temp_groupedsystemgraphs)
  108. return projectgraph
  109. def get_specify_type_parent(vertex, type):
  110. parent = vertex
  111. try:
  112. while not isinstance(parent, type):
  113. parent = parent.parent
  114. except :
  115. parent=None
  116. return parent