systemgraph_display.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. def show_system(system_graph):
  4. '''
  5. Show one system by pyplot.
  6. :param system_graph:
  7. :return:
  8. '''
  9. g=nx.Graph()
  10. for e in system_graph.system_edges:
  11. #g.add_edge(e.start_vertex.system_data.source_id,e.end_vertex.system_data.source_id)
  12. g.add_edge(e.start_vertex, e.end_vertex)
  13. nx.draw_shell(g,with_labels=True)
  14. plt.show()
  15. def show_floor(floor_graph):
  16. '''
  17. Show all system in this floor
  18. :param floor_graph:
  19. :return:
  20. '''
  21. for s in floor_graph.system_graphs:
  22. show_system(s)
  23. def show_project(project_graph):
  24. '''
  25. Show all grouped system in project.
  26. :param project_graph:
  27. :return:
  28. '''
  29. for g in project_graph.groupedsystemgraphs:
  30. show_groupedsystem(g)
  31. def show_groupedsystem(grouped_system_graph):
  32. '''
  33. Show one grouped system.
  34. :param grouped_system_graph:
  35. :return:
  36. '''
  37. g=nx.Graph()
  38. for s in grouped_system_graph.systemgraphs:
  39. for e in s.system_edges:
  40. g.add_edge(e.start_vertex, e.end_vertex)
  41. for e in grouped_system_graph.connectedges:
  42. g.add_edge(e.start_vertex, e.end_vertex)
  43. nx.draw_spring(g,with_labels=True,font_size=8,with_label=True)
  44. plt.show()
  45. from src.system_relation import systemdatautils
  46. def show_connected_block(block_datas):
  47. '''
  48. show connected block.
  49. :param project_id:
  50. :param block_id:
  51. :return:
  52. '''
  53. g=nx.DiGraph()
  54. model_dic = {}
  55. floor_data_dic = {}
  56. for block_data in block_datas:
  57. v1=get_connected_info(model_dic,floor_data_dic,block_data,block_data.model_id1,block_data.type1,block_data.id1)
  58. v2 = get_connected_info(model_dic,floor_data_dic,block_data,block_data.model_id2, block_data.type2, block_data.id2)
  59. direction=block_data.direction
  60. if direction==1 or direction==0:
  61. g.add_edge(v1,v2)
  62. if direction==-1 or direction==0:
  63. g.add_edge(v2,v1)
  64. nx.draw_planar(g, with_labels=True, font_size=8, with_label=True)
  65. plt.show()
  66. def get_connected_info(model_dic,floor_data_dic,block_data,model_id,type,id):
  67. '''
  68. get graphvertex display info
  69. :param model_dic:
  70. :param floor_data_dic:
  71. :param block_data:
  72. :param model_id:
  73. :param type:
  74. :param id:
  75. :return:
  76. '''
  77. if model_id not in model_dic:
  78. models = systemdatautils.get_project_models(block_data.project_id,block_data.building_id)
  79. ids=[i.model_id for i in models]
  80. model_dic=dict(zip(ids,models))
  81. model=model_dic[model_id]
  82. if model_id not in floor_data_dic:
  83. datas= systemdatautils.get_element_data(model_id)
  84. floor_data_dic[model_id]=datas
  85. floor_datas = floor_data_dic[model_id]
  86. datas=list(i for i in floor_datas if i.type==type and i.id==id)
  87. str0=id
  88. if datas:
  89. str0=str(model.local_name)+':'+str(datas[0].source_id)
  90. return str0