import asyncio import json import os import time import ezdxf import requests from flask import Flask, request, jsonify, send_from_directory, send_file from tqdm import tqdm import core from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer from drawer.CADJson import CADJson from drawer.WindBridgeDrawer import WindBridgeDrawer app = Flask(__name__) cur_dir = os.getcwd() # url = 'http://192.168.183.216:8008/python/tunCAD' url = 'http://localhost:8008/python/tunCAD' # url = 'data/cad.json' @app.route('/sysvent/download/', methods=['GET']) def download_dxf_file(model_id): # 指定文件的完整路径 path = draw_system_vent(f'{url}?modelid={model_id}', model_id) # 使用send_file发送文件,as_attachment=True表示以附件形式发送 return send_file(path, as_attachment=True) # def calculate_route_middle(wind_flow_unit): # path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z'] # path_end = wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z'] # path_middle = core.find_point_on_line(path_start, path_end, 1 / 2) # route = core.calculate_angle_with_x_axis(path_start, path_end) # return path_middle, route def draw_system_vent(path, model_id): cad_json = CADJson(path) template_file = "template/" + str(model_id) + ".dxf" doc = ezdxf.readfile(template_file) doc.styles.add("msyh", font="data/msyh.ttc") for layer in cad_json.json['layerMap']: doc.layers.new(name=f'图层{layer[0]}') msp = doc.modelspace() for tun in tqdm(cad_json.tun_list, desc=' 【巷道绘制中】'): if tun['type'] == '1': shaft_center = tun['from'][0], tun['from'][1] sd = ShaftDrawer(msp, tun['layer_id'], tun['width'], shaft_center, 0) sd.draw_shaft_drawer() else: td = TunDrawer(msp, tun['layer_id'], tun["vec12"], tun["vec34"], tun["from"], tun["to"], tun["name"], tun["width"]) td.draw_tun() for window in tqdm(cad_json.window_list, desc=f' 【风窗绘制中】'): wd = WindowDrawer(msp, window["width"], window["layer"], window["center"], window["route"]) wd.draw_window() for gate in tqdm(cad_json.gate_list, desc=f' 【风门绘制中】'): gd = GateDrawer(msp, gate["layer"], gate["width"], gate['center'], gate["route"]) gd.draw_gate() for fan in tqdm(cad_json.fan_list, desc=f' 【风扇绘制中】'): if 'fanmain' in str(fan['type']): fmd = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center'], fan["route"]) fmd.draw_fan_main() if 'fansystem' in str(fan['type']): fsd = FanSystemDrawer(msp, fan["width"], fan['center'], fan["route"]) fsd.draw_fan_system() wind_flow_list = cad_json.wind_flow_list for in_path in tqdm(wind_flow_list['in'], desc=f' 【进风方向绘制中】'): if len(in_path) != 0: wfd = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center'], in_path['route'], in_path['type']) wfd.draw_wind_flow() for no_path in tqdm(wind_flow_list['no'], desc=f'【未指定方向绘制中】'): if len(no_path) != 0: wfd = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center'], no_path['route'], no_path['type']) wfd.draw_wind_flow() for out_path in tqdm(wind_flow_list['out'], desc=f'【回风方向绘制中】'): if len(out_path) != 0: wfd = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center'], out_path['route'], out_path['type']) wfd.draw_wind_flow() for use_path in tqdm(wind_flow_list['use'], desc=f' 【用风方向绘制中】'): if len(use_path) != 0: wfd = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center'], use_path['route'], use_path['type']) wfd.draw_wind_flow() for wind_bridge in tqdm(cad_json.wind_bridge_list, desc=f' 【风桥绘制中】'): wbd = WindBridgeDrawer(msp, wind_bridge['center'], wind_bridge['layer'], wind_bridge['route'], wind_bridge['width']) wbd.draw_wind_bridge_drawer() path = f'save/{str(time.time())}.dxf' doc.saveas(path) return path if __name__ == '__main__': app.run(debug=True)