main_2.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import json
  2. import time
  3. import ezdxf
  4. from tqdm import tqdm
  5. import core
  6. from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer
  7. from drawer.WindBridgeDrawer import WindBridgeDrawer
  8. import requests
  9. url = 'http://192.168.183.216:8008/python/tunCAD'
  10. def calculate_route_middle(wind_flow_unit):
  11. path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z']
  12. path_end = wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z']
  13. path_middle = core.find_point_on_line(path_start, path_end, 1 / 2)
  14. route = core.calculate_angle_with_x_axis(path_start, path_end)
  15. return path_middle, route
  16. if __name__ == '__main__':
  17. response = requests.get(url)
  18. print(f"请求耗时 {response.elapsed.total_seconds()} 秒")
  19. cad_json = {}
  20. if response.status_code == 200:
  21. cad_json = response.json() # 将响应内容解析为JSON格式
  22. # print(json.dumps(result, indent=4)) # 使用json.dumps来美化打印结果
  23. else:
  24. print(f"请求失败,状态码:{response.status_code}")
  25. # doc = ezdxf.readfile("data/moban.dxf", encoding="utf-8")
  26. doc = ezdxf.new('R2000')
  27. doc.styles.add("LiberationSerif", font="LiberationSerif.ttf")
  28. msp = doc.modelspace()
  29. # with open("data/Cad.json", 'r', encoding='utf-8') as r:
  30. # cad_json = json.loads(r.read())
  31. node_list = cad_json["tunsMapCAD"]
  32. tun_list = []
  33. window_list = []
  34. gate_list = []
  35. fan_list = []
  36. for node in node_list:
  37. node = node[1]
  38. tun = {
  39. "vec12": node["vec12"],
  40. "vec34": node["vec34"],
  41. "tun_id": node["ntunid"],
  42. "from": (node["nfrom"]["x"], node["nfrom"]["z"]),
  43. "to": (node["nto"]["x"], node["nto"]["z"]),
  44. "layer_id": node["nlayerid"],
  45. "width": node["fwidth"],
  46. "type": node["tunType"],
  47. "angle": node["angleRad"],
  48. "route": core.calculate_angle_with_x_axis((node["nfrom"]["x"], node["nfrom"]["z"]),
  49. (node["nto"]["x"], node["nto"]["z"]))
  50. }
  51. tun_list.append(tun)
  52. for window in node["windows"]:
  53. window["route"] = tun["route"]
  54. window["gap"] = node["fwidth"]
  55. window_list.extend(node["windows"])
  56. for gate in node["gates"]:
  57. gate["route"] = tun["route"]
  58. gate["gap"] = node["fwidth"]
  59. gate_list.extend(node["gates"])
  60. for fan in node["fans"]:
  61. fan["route"] = tun["route"]
  62. fan["gap"] = node["fwidth"]
  63. fan_list.extend(node["fans"])
  64. for tun in tqdm(tun_list, desc=' 【巷道绘制中】'):
  65. if tun['type'] == '1':
  66. shaft_center = tun['from'][0], tun['from'][1]
  67. sd = ShaftDrawer(msp, tun['width'], shaft_center, tun["route"], 42)
  68. sd.draw_shaft_drawer()
  69. else:
  70. vec12 = []
  71. vec34 = []
  72. for item in tun['vec12']:
  73. vec12.append({
  74. "x": item["x"],
  75. "y": item["z"]
  76. })
  77. for item in tun['vec34']:
  78. vec12.append({
  79. "x": item["x"],
  80. "y": item["z"]
  81. })
  82. color = core.get_color_by_layer(tun['layer_id'])
  83. td = TunDrawer(msp, tun['layer_id'], color, vec12, vec34, tun["from"], tun["to"])
  84. td.draw_tun()
  85. for window in tqdm(window_list, desc=f' 【风窗绘制中】'):
  86. point_c = window['x'], window['z']
  87. wd = WindowDrawer(msp, window["gap"], point_c, window["route"])
  88. wd.draw_window()
  89. for gate in tqdm(gate_list, desc=f' 【风窗绘制中】'):
  90. point_c = gate['x'], gate['z']
  91. gd = GateDrawer(msp, gate["gap"], point_c, gate["route"])
  92. gd.draw_gate()
  93. for fan in tqdm(fan_list, desc=f' 【风扇绘制中】'):
  94. if 'fanmain' in str(fan['strtype']):
  95. point_c = fan['x'], fan['z']
  96. fmd = FanMainDrawer(msp, fan["gap"], point_c, fan["route"])
  97. fmd.draw_fan_main()
  98. if 'fansystem' in str(fan['strtype']):
  99. point_c = fan['x'], fan['z']
  100. fsd = FanSystemDrawer(msp, fan["gap"], point_c, fan["route"])
  101. fsd.draw_fan_system()
  102. path_point_layers = cad_json["pathPointMap"]
  103. for path_point in path_point_layers:
  104. in_paths = path_point[1]['inPaths']
  105. for in_path in tqdm(in_paths, desc=f'图层{path_point[0]} 【进风方向绘制中】'):
  106. if len(in_path) != 0:
  107. path_middle, route = calculate_route_middle(in_path)
  108. wfd = WindFlowDrawer(msp, 5, path_middle, route, 3)
  109. wfd.draw_wind_flow()
  110. no_paths = path_point[1]['noPaths']
  111. for no_path in tqdm(no_paths, desc=f'图层{path_point[0]} 【未指定方向绘制中】'):
  112. if len(no_path) != 0:
  113. path_middle, route = calculate_route_middle(no_path)
  114. wfd = WindFlowDrawer(msp, 5, path_middle, route, 3)
  115. wfd.draw_wind_flow()
  116. out_paths = path_point[1]['outPaths']
  117. for out_path in tqdm(out_paths, desc=f'图层{path_point[0]} 【回风方向绘制中】'):
  118. if len(out_path) != 0:
  119. path_middle, route = calculate_route_middle(out_path)
  120. wfd = WindFlowDrawer(msp, 5, path_middle, route, 1)
  121. wfd.draw_wind_flow()
  122. use_paths = path_point[1]['usePaths']
  123. for use_path in tqdm(use_paths, desc=f'图层{path_point[0]} 【用风方向绘制中】'):
  124. if len(use_path) != 0:
  125. path_middle, route = calculate_route_middle(use_path)
  126. wfd = WindFlowDrawer(msp, 5, path_middle, route, 1)
  127. layer_map = cad_json["layerMap"]
  128. for layer in layer_map:
  129. cross_nodes = layer[1]['crossNodes']
  130. for cross_node in tqdm(cross_nodes, desc=f'图层{layer[0]} 【风桥绘制中】'):
  131. center = cross_node['crossPoint']['x'], -cross_node['crossPoint']['y']
  132. tun = [tun_ for tun_ in tun_list if tun_.get("tun_id") == cross_node['tun1Id']][0]
  133. route = core.calculate_angle_with_x_axis(tun["from"], tun["to"])
  134. line_1 = (tun["vec12"][0]["x"], tun["vec12"][0]["z"]), (tun["vec12"][-1]["x"], tun["vec12"][-1]["z"])
  135. line_2 = (tun["vec34"][0]["x"], tun["vec34"][0]["z"]), (tun["vec34"][-1]["x"], tun["vec34"][-1]["z"])
  136. gap = core.min_distance_between_segments(line_1,
  137. line_2)
  138. wbd = WindBridgeDrawer(msp, center, cross_node['tun1Id'], cross_node['tun2Id'], layer[0], tun["angle"], gap)
  139. wbd.draw_wind_bridge_drawer()
  140. a = time.time()
  141. doc.saveas(f'save/tuns{str(a)}.dxf')