web.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import asyncio
  2. import json
  3. import os
  4. import time
  5. import ezdxf
  6. import requests
  7. from flask import Flask, request, jsonify, send_from_directory, send_file
  8. from tqdm import tqdm
  9. from urllib3.exceptions import NewConnectionError
  10. import core
  11. from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer
  12. from drawer.CADJson import CADJson, layer_divide
  13. from drawer.WindBridgeDrawer import WindBridgeDrawer
  14. app = Flask(__name__)
  15. cur_dir = os.getcwd()
  16. url = 'http://192.168.183.216:8008/python/tunCAD'
  17. # url = 'http://localhost:8008/python/tunCAD'
  18. # url = 'http://localhost:8008/python/tunCAD'
  19. # url = 'data/cad.json'
  20. @app.route('/sysvent/download/<int:model_id>', methods=['GET'])
  21. def download_dxf_file(model_id):
  22. # 指定文件的完整路径
  23. try:
  24. path = draw_system_vent(f'{url}?modelid={model_id}', model_id)
  25. # 使用send_file发送文件,as_attachment=True表示以附件形式发送
  26. # return send_file(path, as_attachment=True)
  27. return jsonify({"path": path}), 200
  28. except NewConnectionError as e:
  29. # 捕获连接错误,并返回自定义的错误信息
  30. error_message = "连接失败: 无法连接到服务器。错误信息: " + str(e)
  31. print(error_message)
  32. return jsonify({"error": error_message}), 500
  33. except requests.exceptions.HTTPError as http_err:
  34. # 捕获其他HTTP错误
  35. error_message = f"HTTP错误: {http_err}"
  36. print(error_message)
  37. return jsonify({"error": error_message}), 500
  38. # except Exception as req_err:
  39. # # # 捕获所有requests库抛出的异常
  40. # error_message = f"算法执行异常: {req_err}"
  41. # print(error_message)
  42. # return jsonify({"error": error_message}), 500
  43. # def calculate_route_middle(wind_flow_unit):
  44. # path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z']
  45. # path_end = wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z']
  46. # path_middle = core.find_point_on_line(path_start, path_end, 1 / 2)
  47. # route = core.calculate_angle_with_x_axis(path_start, path_end)
  48. # return path_middle, route
  49. def draw_system_vent(path, model_id):
  50. cad_json = CADJson(path)
  51. template_file = "template/last/" + str(model_id) + ".dxf"
  52. if os.path.exists(template_file):
  53. doc = ezdxf.readfile(template_file)
  54. else:
  55. doc = ezdxf.new('R2008')
  56. doc.styles.add("msyh", font="data/msyh.ttc")
  57. for layer in cad_json.json['layerMap']:
  58. doc.layers.new(name=f'图层{layer[0]}')
  59. msp = doc.modelspace()
  60. for tun in tqdm(cad_json.tun_dict.values(), desc=' 【巷道绘制中】'):
  61. if tun['type'] == '1':
  62. shaft_center = tun['from'][0], tun['from'][1]
  63. sd = ShaftDrawer(msp, tun['layer_id'], tun['width'], shaft_center, 0)
  64. sd.draw_shaft_drawer()
  65. else:
  66. td_agg = TunDrawer(msp, tun['layer_id'], tun["vec12"]["agg"], tun["vec34"]["agg"], tun["from"]['agg'],
  67. tun["to"]['agg'],
  68. tun["name"],
  69. tun["width"])
  70. td_agg.draw_tun()
  71. td_agg.draw_tun_text()
  72. td_divided = TunDrawer(msp, tun['layer_id'], tun["vec12"]["div"], tun["vec34"]["div"], tun["from"]['div'],
  73. tun["to"]['div'], tun["name"],
  74. tun["width"])
  75. td_divided.draw_tun()
  76. td_divided.draw_tun_text()
  77. for window in tqdm(cad_json.window_list, desc=f' 【风窗绘制中】'):
  78. agg_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["agg"], window["route"]['agg'])
  79. div_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["div"], window["route"]['div'])
  80. agg_w.draw_window()
  81. div_w.draw_window()
  82. for gate in tqdm(cad_json.gate_list, desc=f' 【风门绘制中】'):
  83. agg_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['agg'], gate["route"]['agg'])
  84. div_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['div'], gate["route"]['div'])
  85. agg_g.draw_gate()
  86. div_g.draw_gate()
  87. for fan in tqdm(cad_json.fan_list, desc=f' 【风扇绘制中】'):
  88. if 'fanmain' in str(fan['type']):
  89. agg_f = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
  90. agg_d = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['div'], fan["route"]['div'])
  91. agg_f.draw_fan_main()
  92. agg_d.draw_fan_main()
  93. # if 'fansystem' in str(fan['type']):
  94. # fsd = FanSystemDrawer(msp, fan["width"], fan['center'], fan["route"])
  95. # fsd.draw_fan_system()
  96. for wind_bridge in tqdm(cad_json.wind_bridge_list, desc=f' 【风桥绘制中】'):
  97. agg_wb = WindBridgeDrawer(msp, wind_bridge['center']['agg'], wind_bridge['layer'],
  98. wind_bridge['route']['agg'], wind_bridge['width'])
  99. div_wb = WindBridgeDrawer(msp, wind_bridge['center']['div'], wind_bridge['layer'],
  100. wind_bridge['route']['div'], wind_bridge['width'])
  101. agg_wb.draw_wind_bridge_drawer()
  102. div_wb.draw_wind_bridge_drawer()
  103. for air_flow in tqdm(cad_json.air_flow_list, desc=f' 【风流方向绘制中】'):
  104. agg_wind_flow = WindFlowDrawer(msp, air_flow['layer'], 3, air_flow['center']['agg'], air_flow['route']['agg'],
  105. air_flow['type'])
  106. div_wind_flow = WindFlowDrawer(msp, air_flow['layer'], 3, air_flow['center']['div'], air_flow['route']['div'],
  107. air_flow['type'])
  108. agg_wind_flow.draw_wind_flow()
  109. div_wind_flow.draw_wind_flow()
  110. # wind_flow_list = cad_json.wind_flow_list
  111. # for in_path in tqdm(wind_flow_list['in'], desc=f' 【进风方向绘制中】'):
  112. # if len(in_path) != 0:
  113. # agg_wf = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center']['agg'], in_path['route']['agg'],
  114. # in_path['type'])
  115. # div_wf = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center']['div'], in_path['route']['div'],
  116. # in_path['type'])
  117. # agg_wf.draw_wind_flow()
  118. # div_wf.draw_wind_flow()
  119. # for no_path in tqdm(wind_flow_list['no'], desc=f'【未指定方向绘制中】'):
  120. # if len(no_path) != 0:
  121. # agg_wf = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center']['agg'], no_path['route']['agg'],
  122. # no_path['type'])
  123. # div_wf = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center']['div'], no_path['route']['div'],
  124. # no_path['type'])
  125. # agg_wf.draw_wind_flow()
  126. # div_wf.draw_wind_flow()
  127. # for out_path in tqdm(wind_flow_list['out'], desc=f'【回风方向绘制中】'):
  128. # if len(out_path) != 0:
  129. # agg_wf = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center']['agg'], out_path['route']['agg'],
  130. # out_path['type'])
  131. # div_wf = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center']['div'], out_path['route']['div'],
  132. # out_path['type'])
  133. # agg_wf.draw_wind_flow()
  134. # div_wf.draw_wind_flow()
  135. # for use_path in tqdm(wind_flow_list['use'], desc=f' 【用风方向绘制中】'):
  136. # if len(use_path) != 0:
  137. # agg_wf = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center']['agg'], use_path['route']['agg'],
  138. # use_path['type'])
  139. # div_wf = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center']['div'], use_path['route']['div'],
  140. # use_path['type'])
  141. # agg_wf.draw_wind_flow()
  142. # div_wf.draw_wind_flow()
  143. path = f'save/{str(time.time())}.dxf'
  144. doc.saveas(path)
  145. print(f'保存文件{os.path.abspath(path)}')
  146. return os.path.abspath(path)
  147. if __name__ == '__main__':
  148. app.run(debug=False)