web.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import asyncio
  2. import json
  3. import os
  4. from datetime import time, datetime
  5. import ezdxf
  6. import requests
  7. from flask import Flask, request, jsonify, send_from_directory, send_file
  8. from tqdm import tqdm
  9. import core
  10. from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer
  11. from drawer.CADJson import CADJson
  12. from drawer.WindBridgeDrawer import WindBridgeDrawer
  13. app = Flask(__name__)
  14. cur_dir = os.getcwd()
  15. # url = 'http://192.168.183.216:8008/python/tunCAD'
  16. url = 'data/cad.json'
  17. @app.route('/sysvent/draw/<int:model_id>', methods=['GET'])
  18. def post_json(model_id):
  19. # 从数据库中查找template
  20. # template = select_template_by_id(template_id) template = 'template/moban.dxf'
  21. data = request.json
  22. now = datetime.now()
  23. current_time_str = now.strftime("%Y_%m_%d_%H_%M_%S")
  24. json_file_name = f'{cur_dir}\\save\\{current_time_str}.json'
  25. dxf_file_name = f'{cur_dir}\\save\\{current_time_str}.DXF'
  26. with open(json_file_name, 'w', encoding='utf-8') as f:
  27. json.dump(data, f, ensure_ascii=False, indent=4)
  28. # asyncio.run(async_write_dxf_file(templateFile=template, node_list=data, save_name=dxf_file_name))
  29. res = {
  30. "code": 200,
  31. "msg": "success",
  32. 'data': {
  33. 'filename': f'{current_time_str}.DXF'
  34. }
  35. }
  36. return jsonify(res), 200
  37. @app.route('/sysvent/download/<int:model_id>', methods=['GET'])
  38. def download_dxf_file(model_id):
  39. # 指定文件的完整路径
  40. filename = str(model_id) + ".dxf"
  41. draw_system_vent(url,model_id)
  42. file_path = f'save\\{filename}'
  43. # 使用send_file发送文件,as_attachment=True表示以附件形式发送
  44. return send_file(file_path, as_attachment=True)
  45. def request_graph_point(model_id):
  46. response = requests.get(url)
  47. print(f"请求耗时 {response.elapsed.total_seconds()} 秒")
  48. cad_json = {}
  49. if response.status_code == 200:
  50. cad_json = response.json() # 将响应内容解析为JSON格式
  51. else:
  52. print(f"请求失败,状态码:{response.status_code}")
  53. return cad_json
  54. def calculate_route_middle(wind_flow_unit):
  55. path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z']
  56. path_end = wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z']
  57. path_middle = core.find_point_on_line(path_start, path_end, 1 / 2)
  58. route = core.calculate_angle_with_x_axis(path_start, path_end)
  59. return path_middle, route
  60. def draw_system_vent(path, model_id):
  61. # doc = ezdxf.new('R2000')
  62. cad_json = CADJson(path)
  63. # tun_list = cad_json.tun_list
  64. # fan_list = cad_json.fan_list
  65. # window_list = cad_json.window_list
  66. doc = ezdxf.readfile("data/moban.dxf")
  67. doc.styles.add("msyh", font="data/msyh.ttc")
  68. for layer in cad_json.json['layerMap']:
  69. doc.layers.new(name=f'图层{layer[0]}')
  70. msp = doc.modelspace()
  71. for tun in tqdm(cad_json.tun_list, desc=' 【巷道绘制中】'):
  72. if tun['type'] == '1':
  73. shaft_center = tun['from'][0], tun['from'][1]
  74. sd = ShaftDrawer(msp, tun['layer_id'], tun['width'], shaft_center, 0)
  75. sd.draw_shaft_drawer()
  76. else:
  77. td = TunDrawer(msp, tun['layer_id'], tun["vec12"], tun["vec34"], tun["from"], tun["to"], tun["name"],
  78. tun["width"])
  79. td.draw_tun()
  80. for window in tqdm(cad_json.window_list, desc=f' 【风窗绘制中】'):
  81. wd = WindowDrawer(msp, window["width"], window["layer"], window["center"], window["route"])
  82. wd.draw_window()
  83. for gate in tqdm(cad_json.gate_list, desc=f' 【风门绘制中】'):
  84. gd = GateDrawer(msp, gate["layer"], gate["width"], gate['center'], gate["route"])
  85. gd.draw_gate()
  86. for fan in tqdm(cad_json.fan_list, desc=f' 【风扇绘制中】'):
  87. if 'fanmain' in str(fan['type']):
  88. fmd = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center'], fan["route"])
  89. fmd.draw_fan_main()
  90. if 'fansystem' in str(fan['type']):
  91. fsd = FanSystemDrawer(msp, fan["width"], fan['center'], fan["route"])
  92. fsd.draw_fan_system()
  93. wind_flow_list = cad_json.wind_flow_list
  94. for in_path in tqdm(wind_flow_list['in'], desc=f' 【进风方向绘制中】'):
  95. if len(in_path) != 0:
  96. wfd = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center'], in_path['route'], in_path['type'])
  97. wfd.draw_wind_flow()
  98. for no_path in tqdm(wind_flow_list['no'], desc=f'【未指定方向绘制中】'):
  99. if len(no_path) != 0:
  100. wfd = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center'], no_path['route'], no_path['type'])
  101. wfd.draw_wind_flow()
  102. for out_path in tqdm(wind_flow_list['out'], desc=f'【回风方向绘制中】'):
  103. if len(out_path) != 0:
  104. wfd = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center'], out_path['route'], out_path['type'])
  105. wfd.draw_wind_flow()
  106. for use_path in tqdm(wind_flow_list['use'], desc=f' 【用风方向绘制中】'):
  107. if len(use_path) != 0:
  108. wfd = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center'], use_path['route'], use_path['type'])
  109. wfd.draw_wind_flow()
  110. for wind_bridge in tqdm(cad_json.wind_bridge_list, desc=f' 【风桥绘制中】'):
  111. wbd = WindBridgeDrawer(msp, wind_bridge['center'], wind_bridge['layer'],
  112. wind_bridge['route'], wind_bridge['width'])
  113. wbd.draw_wind_bridge_drawer()
  114. doc.saveas(f'save/{str(model_id)}.dxf')
  115. if __name__ == '__main__':
  116. app.run(debug=True)