web.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. 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 = 'http://localhost:8008/python/tunCAD'
  17. # url = 'data/cad.json'
  18. @app.route('/sysvent/download/<int:model_id>', methods=['GET'])
  19. def download_dxf_file(model_id):
  20. # 指定文件的完整路径
  21. path = draw_system_vent(f'{url}?modelid={model_id}', model_id)
  22. # 使用send_file发送文件,as_attachment=True表示以附件形式发送
  23. return send_file(path, as_attachment=True)
  24. # def calculate_route_middle(wind_flow_unit):
  25. # path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z']
  26. # path_end = wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z']
  27. # path_middle = core.find_point_on_line(path_start, path_end, 1 / 2)
  28. # route = core.calculate_angle_with_x_axis(path_start, path_end)
  29. # return path_middle, route
  30. def draw_system_vent(path, model_id):
  31. cad_json = CADJson(path)
  32. template_file = "template/" + str(model_id) + ".dxf"
  33. doc = ezdxf.readfile(template_file)
  34. doc.styles.add("msyh", font="data/msyh.ttc")
  35. for layer in cad_json.json['layerMap']:
  36. doc.layers.new(name=f'图层{layer[0]}')
  37. msp = doc.modelspace()
  38. for tun in tqdm(cad_json.tun_list, desc=' 【巷道绘制中】'):
  39. if tun['type'] == '1':
  40. shaft_center = tun['from'][0], tun['from'][1]
  41. sd = ShaftDrawer(msp, tun['layer_id'], tun['width'], shaft_center, 0)
  42. sd.draw_shaft_drawer()
  43. else:
  44. td = TunDrawer(msp, tun['layer_id'], tun["vec12"], tun["vec34"], tun["from"], tun["to"], tun["name"],
  45. tun["width"])
  46. td.draw_tun()
  47. for window in tqdm(cad_json.window_list, desc=f' 【风窗绘制中】'):
  48. wd = WindowDrawer(msp, window["width"], window["layer"], window["center"], window["route"])
  49. wd.draw_window()
  50. for gate in tqdm(cad_json.gate_list, desc=f' 【风门绘制中】'):
  51. gd = GateDrawer(msp, gate["layer"], gate["width"], gate['center'], gate["route"])
  52. gd.draw_gate()
  53. for fan in tqdm(cad_json.fan_list, desc=f' 【风扇绘制中】'):
  54. if 'fanmain' in str(fan['type']):
  55. fmd = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center'], fan["route"])
  56. fmd.draw_fan_main()
  57. if 'fansystem' in str(fan['type']):
  58. fsd = FanSystemDrawer(msp, fan["width"], fan['center'], fan["route"])
  59. fsd.draw_fan_system()
  60. wind_flow_list = cad_json.wind_flow_list
  61. for in_path in tqdm(wind_flow_list['in'], desc=f' 【进风方向绘制中】'):
  62. if len(in_path) != 0:
  63. wfd = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center'], in_path['route'], in_path['type'])
  64. wfd.draw_wind_flow()
  65. for no_path in tqdm(wind_flow_list['no'], desc=f'【未指定方向绘制中】'):
  66. if len(no_path) != 0:
  67. wfd = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center'], no_path['route'], no_path['type'])
  68. wfd.draw_wind_flow()
  69. for out_path in tqdm(wind_flow_list['out'], desc=f'【回风方向绘制中】'):
  70. if len(out_path) != 0:
  71. wfd = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center'], out_path['route'], out_path['type'])
  72. wfd.draw_wind_flow()
  73. for use_path in tqdm(wind_flow_list['use'], desc=f' 【用风方向绘制中】'):
  74. if len(use_path) != 0:
  75. wfd = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center'], use_path['route'], use_path['type'])
  76. wfd.draw_wind_flow()
  77. for wind_bridge in tqdm(cad_json.wind_bridge_list, desc=f' 【风桥绘制中】'):
  78. wbd = WindBridgeDrawer(msp, wind_bridge['center'], wind_bridge['layer'],
  79. wind_bridge['route'], wind_bridge['width'])
  80. wbd.draw_wind_bridge_drawer()
  81. path = f'save/{str(time.time())}.dxf'
  82. doc.saveas(path)
  83. return path
  84. if __name__ == '__main__':
  85. app.run(debug=True)