systemgraph_display.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 systemrelation import systemdatautils
  46. def show_connected_block(project_id,block_id):
  47. '''
  48. show connected block.
  49. :param project_id:
  50. :param block_id:
  51. :return:
  52. '''
  53. block_datas=systemdatautils.get_connected_block_data(project_id,block_id)
  54. g=nx.DiGraph()
  55. model_dic = {}
  56. floor_data_dic = {}
  57. for block_data in block_datas:
  58. v1=get_connected_info(model_dic,floor_data_dic,block_data.project_id,block_data.model_id1,block_data.type1,block_data.id1)
  59. v2 = get_connected_info(model_dic,floor_data_dic,block_data.project_id,block_data.model_id2, block_data.type2, block_data.id2)
  60. direction=block_data.direction
  61. if direction==1 or direction==0:
  62. g.add_edge(v1,v2)
  63. if direction==-1 or direction==0:
  64. g.add_edge(v2,v1)
  65. nx.draw_planar(g, with_labels=True, font_size=8, with_label=True)
  66. plt.show()
  67. def get_connected_info(model_dic,floor_data_dic,project_id,model_id,type,id):
  68. '''
  69. get graphvertex display info
  70. :param model_dic:
  71. :param floor_data_dic:
  72. :param project_id:
  73. :param model_id:
  74. :param type:
  75. :param id:
  76. :return:
  77. '''
  78. if model_id not in model_dic:
  79. models = systemdatautils.get_project_models(project_id)
  80. ids=[i.file_id for i in models]
  81. model_dic=dict(zip(ids,models))
  82. model=model_dic[model_id]
  83. if model_id not in floor_data_dic:
  84. datas=systemdatautils.get_element_data(model_id)
  85. floor_data_dic[model_id]=datas
  86. floor_datas = floor_data_dic[model_id]
  87. datas=list(i for i in floor_datas if i.type==type and i.id==id)
  88. str0=id
  89. if datas:
  90. str0=str(model.floor_name)+':'+str(datas[0].source_id)
  91. return str0