123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- import asyncio
- import json
- import os
- import time
- import ezdxf
- import requests
- import yaml
- 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.AirDuctDrawer import AirDuctDrawer
- from drawer.AirDuctExampleDrawer import AirDuctExampleDrawer
- from drawer.CADJson import CADJson, layer_divide
- from drawer.FanLocalDrawer import FanLocalDrawer
- from drawer.NodeGapDrawer import NodeGapDrawer
- from drawer.PlgTunDrawer import PlgTunDrawer
- from drawer.SealedDrawer import SealedDrawer
- from drawer.TunTextDrawer import TunTextDrawer
- from drawer.WindBridgeDrawer import WindBridgeDrawer
- from drawer.WindFlow3DDrawer import WindFlow3DDrawer
- app = Flask(__name__)
- cur_dir = os.getcwd()
- # url = 'http://192.168.183.216:8008/python/tunCAD'
- # url = 'data/1733121175.json'
- # url = 'http://localhost:8008/python/tunCAD'
- # url_2d = "http://192.168.183.216:8008/python/tunCAD"
- # url_3d = "http://192.168.183.216:8008/tunLt"
- url_2d = "http://localhost:8008/python/tunCAD"
- url_3d = "http://localhost:8008/tunLt"
- # url = 'http://localhost:8008/python/tunCAD'
- # url = 'data/cad.json'
- @app.route('/sysvent/download/<int:model_id>/<string:type>', methods=['GET'])
- def download_dxf_file(model_id, type):
- # 指定文件的完整路径
- try:
- path = None
- if type == '2d':
- with open('data/global-2d.yaml', 'r', encoding='utf-8') as file:
- config = yaml.safe_load(file)
- path = draw_system_vent(f'{url_2d}?modelid={model_id}', model_id,config)
- elif type == '3d':
- with open('data/global-3d.yaml', 'r', encoding='utf-8') as file:
- config = yaml.safe_load(file)
- path = draw_system_vent_3d(f'{url_3d}?modelid={model_id}', model_id,config)
- # 使用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 msp_pre_process(path, model_id,config,model_type):
- cad_json = CADJson(path,config)
- default_template_file = "template/last/default_3d.dxf"
- if model_type== "3d":
- default_template_file = "template/last/default_3d.dxf"
- else:
- default_template_file = "template/last/default_2d.dxf"
- if os.path.exists(default_template_file):
- doc = ezdxf.readfile(default_template_file)
- else:
- doc = ezdxf.new('R2013')
- 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()
- return msp,doc,cad_json
- def draw_system_vent_3d(path, model_id,config):
- msp,doc, cad_json = msp_pre_process(path, model_id,config,"3d")
- for gaps in tqdm(cad_json.gaps_list, desc=f' 【节点空隙绘制中】'):
- ngpd = NodeGapDrawer(msp, gaps, 1)
- ngpd.draw_gap()
- text_list = []
- for tun in tqdm(cad_json.get_tun_plg_dict().values(), desc=' 【3d巷道绘制中】'):
- text = tun["name"] + " " + tun['fq']
- plg_tun_agg = PlgTunDrawer(msp, tun['vec_b']['agg'], tun['vec_r']['agg'], tun['vec_l']['agg'], text,
- tun['layer_id'], tun['color'], tun['from']['agg'], tun['to']['agg'], tun['width'])
- plg_tun_agg.draw_plg_tun()
- # plg_tun_agg.draw_middle_line()
- if text not in text_list:
- text_list.append(text)
- plg_tun_agg.draw_tun_text()
- # plg_tun_div = PlgTunDrawer(msp, tun['vec_b']['div'], tun['vec_r']['div'], tun['vec_l']['div'], tun["name"],
- # tun['layer_id'], tun['color'], tun['from']['div'], tun['to']['div'], tun['width'])
- # plg_tun_div.draw_plg_tun()
- # plg_tun_div.draw_tun_text()
- for air_flow in tqdm(cad_json.air_flow_list_3d, desc=f' 【风流方向绘制中】'):
- agg_wind_flow = WindFlow3DDrawer(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()
- for window in tqdm(cad_json.window_list_3d, 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.gates_list_3d, desc=f' 【风门绘制中】'):
- agg_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['agg'], gate["route"]['agg'])
- agg_g.draw_gate()
- for fan in tqdm(cad_json.fans_list_3d, desc=f' 【风扇绘制中】'):
- if 'fanmain' in str(fan['type']):
- agg_f = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
- agg_f.draw_fan_main()
- if 'fanlocal' in str(fan['type']):
- fsd = FanSystemDrawer(msp,fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
- fsd.draw_fan_system()
- path = f'save/{str(time.time())}.dxf'
- doc.saveas(path)
- print(f'保存文件{os.path.abspath(path)}')
- return os.path.abspath(path)
- def draw_system_vent(path, model_id,config):
- msp,doc, cad_json = msp_pre_process(path, model_id,config,"2d")
- text_list = []
- text_dict = {}
- 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:
- text = tun["name"] +" "+ tun['fq']
- td_agg = TunDrawer(msp, tun['layer_id'], tun["vec12"]["agg"], tun["vec34"]["agg"], tun["from"]['agg'],
- tun["to"]['agg'],
- text,
- tun["width"],
- tun["head_vec_from"]['agg'],
- tun["head_vec_to"]['agg']
- )
- td_divided = TunDrawer(msp, tun['layer_id'], tun["vec12"]["div"], tun["vec34"]["div"], tun["from"]['div'],
- tun["to"]['div'], text,
- tun["width"],
- tun["head_vec_from"]['div'],
- tun["head_vec_to"]['div']
- )
- td_agg.draw_tun()
- td_divided.draw_tun()
- # if text not in text_dict.keys():
- # text_dict[text]= []
- # else:
- # text_dict[text].append(tun)
- #
- # if text not in text_list:
- # text_list.append(text)
- # td_agg.draw_tun_text()
- # td_divided.draw_tun_text()
- for tun_text in tqdm(cad_json.tun_text_dict.values(), desc=' 【巷道文字绘制中】'):
- ttd_agg = TunTextDrawer(msp, tun_text['layer'], tun_text['tun_line']['agg'], tun_text['text'],5)
- ttd_div = TunTextDrawer(msp,tun_text['layer'], tun_text['tun_line']['div'],tun_text['text'],5)
- ttd_agg.dra_text()
- ttd_div.dra_text()
- for sealed in tqdm(cad_json.sealed_list, desc=f' 【密闭绘制中】'):
- agg_w = SealedDrawer(msp, sealed["width"], sealed["layer"], sealed["center"]["agg"], sealed["route"]['agg'])
- div_w = SealedDrawer(msp, sealed["width"], sealed["layer"], sealed["center"]["div"], sealed["route"]['div'])
- agg_w.drawSealed()
- div_w.drawSealed()
- 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 ad_list in tqdm(cad_json.ari_duct_dict.values(), desc=f' 【风筒绘制中】'):
- for i in range(len(ad_list)):
- ad = ad_list[i]
- agg_ad= AirDuctDrawer(msp,ad['layer'],ad['from']['agg'],ad['to']['agg'],ad['width'])
- div_ad= AirDuctDrawer(msp,ad['layer'],ad['from']['div'],ad['to']['div'],ad['width'])
- agg_ad.draw_air_duct_list()
- div_ad.draw_air_duct_list()
- if i+1<len(ad_list):
- dxfattribs = {"layer": f"图层{ad_list[i]['layer']}", "color": core.get_color_by_layer(ad_list[i]['layer'])}
- msp.add_line(ad_list[i]['to']['agg'], ad_list[i + 1]['from']['agg'],dxfattribs)
- msp.add_line(ad_list[i]['to']['div'], ad_list[i + 1]['from']['div'], dxfattribs)
- # for adi in ad_list:
- # msp.add_circle(adi['from']['div'], 2, {"color": "1"}) # red
- # msp.add_circle(adi['to']['div'],2,{"color":"2"}) #yellow
- # 连接首位
- 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'])
- div_f = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['div'], fan["route"]['div'])
- agg_f.draw_fan_main()
- div_f.draw_fan_main()
- if 'fanlocal' in str(fan['type']):
- agg_f = FanLocalDrawer(msp,fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
- div_f = FanLocalDrawer(msp,fan["layer"], fan["width"], fan['center']['div'], fan["route"]['div'])
- agg_f.draw_fan_local()
- div_f.draw_fan_local()
- 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'], air_flow['len'], air_flow['center']['agg'], air_flow['route']['agg'],
- air_flow['type'])
- div_wind_flow = WindFlowDrawer(msp, air_flow['layer'], air_flow['len'], air_flow['center']['div'], air_flow['route']['div'],
- air_flow['type'])
- agg_wind_flow.draw_wind_flow()
- div_wind_flow.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)
- download_dxf_file(1868855086453813249,'3d')
|