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 from urllib3.exceptions import NewConnectionError import core from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer from drawer.CADJson import CADJson, layer_divide 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 = 'http://localhost:8008/python/tunCAD' # url = 'data/cad.json' @app.route('/sysvent/download/', methods=['GET']) def download_dxf_file(model_id): # 指定文件的完整路径 try: path = draw_system_vent(f'{url}?modelid={model_id}', model_id) # 使用send_file发送文件,as_attachment=True表示以附件形式发送 # return send_file(path, as_attachment=True) return jsonify({"path": path}), 200 except NewConnectionError as e: # 捕获连接错误,并返回自定义的错误信息 error_message = "连接失败: 无法连接到服务器。错误信息: " + str(e) print(error_message) return jsonify({"error": error_message}), 500 except requests.exceptions.HTTPError as http_err: # 捕获其他HTTP错误 error_message = f"HTTP错误: {http_err}" print(error_message) return jsonify({"error": error_message}), 500 # except Exception as req_err: # # # 捕获所有requests库抛出的异常 # error_message = f"算法执行异常: {req_err}" # print(error_message) # return jsonify({"error": error_message}), 500 # 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/last/" + str(model_id) + ".dxf" if os.path.exists(template_file): doc = ezdxf.readfile(template_file) else: doc = ezdxf.new('R2008') 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_dict.values(), 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_agg = TunDrawer(msp, tun['layer_id'], tun["vec12"]["agg"], tun["vec34"]["agg"], tun["from"]['agg'], tun["to"]['agg'], tun["name"], tun["width"]) td_agg.draw_tun() td_agg.draw_tun_text() td_divided = TunDrawer(msp, tun['layer_id'], tun["vec12"]["div"], tun["vec34"]["div"], tun["from"]['div'], tun["to"]['div'], tun["name"], tun["width"]) td_divided.draw_tun() td_divided.draw_tun_text() for window in tqdm(cad_json.window_list, desc=f' 【风窗绘制中】'): agg_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["agg"], window["route"]['agg']) div_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["div"], window["route"]['div']) agg_w.draw_window() div_w.draw_window() for gate in tqdm(cad_json.gate_list, desc=f' 【风门绘制中】'): agg_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['agg'], gate["route"]['agg']) div_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['div'], gate["route"]['div']) agg_g.draw_gate() div_g.draw_gate() for fan in tqdm(cad_json.fan_list, desc=f' 【风扇绘制中】'): if 'fanmain' in str(fan['type']): agg_f = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg']) agg_d = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['div'], fan["route"]['div']) agg_f.draw_fan_main() agg_d.draw_fan_main() # if 'fansystem' in str(fan['type']): # fsd = FanSystemDrawer(msp, fan["width"], fan['center'], fan["route"]) # fsd.draw_fan_system() for wind_bridge in tqdm(cad_json.wind_bridge_list, desc=f' 【风桥绘制中】'): agg_wb = WindBridgeDrawer(msp, wind_bridge['center']['agg'], wind_bridge['layer'], wind_bridge['route']['agg'], wind_bridge['width']) div_wb = WindBridgeDrawer(msp, wind_bridge['center']['div'], wind_bridge['layer'], wind_bridge['route']['div'], wind_bridge['width']) agg_wb.draw_wind_bridge_drawer() div_wb.draw_wind_bridge_drawer() for air_flow in tqdm(cad_json.air_flow_list, desc=f' 【风流方向绘制中】'): agg_wind_flow = WindFlowDrawer(msp, air_flow['layer'], 3, air_flow['center']['agg'], air_flow['route']['agg'], air_flow['type']) div_wind_flow = WindFlowDrawer(msp, air_flow['layer'], 3, air_flow['center']['div'], air_flow['route']['div'], air_flow['type']) agg_wind_flow.draw_wind_flow() div_wind_flow.draw_wind_flow() # wind_flow_list = cad_json.wind_flow_list # for in_path in tqdm(wind_flow_list['in'], desc=f' 【进风方向绘制中】'): # if len(in_path) != 0: # agg_wf = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center']['agg'], in_path['route']['agg'], # in_path['type']) # div_wf = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center']['div'], in_path['route']['div'], # in_path['type']) # agg_wf.draw_wind_flow() # div_wf.draw_wind_flow() # for no_path in tqdm(wind_flow_list['no'], desc=f'【未指定方向绘制中】'): # if len(no_path) != 0: # agg_wf = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center']['agg'], no_path['route']['agg'], # no_path['type']) # div_wf = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center']['div'], no_path['route']['div'], # no_path['type']) # agg_wf.draw_wind_flow() # div_wf.draw_wind_flow() # for out_path in tqdm(wind_flow_list['out'], desc=f'【回风方向绘制中】'): # if len(out_path) != 0: # agg_wf = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center']['agg'], out_path['route']['agg'], # out_path['type']) # div_wf = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center']['div'], out_path['route']['div'], # out_path['type']) # agg_wf.draw_wind_flow() # div_wf.draw_wind_flow() # for use_path in tqdm(wind_flow_list['use'], desc=f' 【用风方向绘制中】'): # if len(use_path) != 0: # agg_wf = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center']['agg'], use_path['route']['agg'], # use_path['type']) # div_wf = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center']['div'], use_path['route']['div'], # use_path['type']) # agg_wf.draw_wind_flow() # div_wf.draw_wind_flow() path = f'save/{str(time.time())}.dxf' doc.saveas(path) print(f'保存文件{os.path.abspath(path)}') return os.path.abspath(path) if __name__ == '__main__': app.run(debug=False)