web.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import asyncio
  2. import json
  3. import os
  4. import time
  5. import ezdxf
  6. import requests
  7. import yaml
  8. from flask import Flask, request, jsonify, send_from_directory, send_file
  9. from tqdm import tqdm
  10. from urllib3.exceptions import NewConnectionError
  11. import core
  12. from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer
  13. from drawer.AirDuctDrawer import AirDuctDrawer
  14. from drawer.AirDuctExampleDrawer import AirDuctExampleDrawer
  15. from drawer.CADJson import CADJson, layer_divide
  16. from drawer.FanLocalDrawer import FanLocalDrawer
  17. from drawer.NodeGapDrawer import NodeGapDrawer
  18. from drawer.PlgTunDrawer import PlgTunDrawer
  19. from drawer.SealedDrawer import SealedDrawer
  20. from drawer.TunTextDrawer import TunTextDrawer
  21. from drawer.WindBridgeDrawer import WindBridgeDrawer
  22. from drawer.WindFlow3DDrawer import WindFlow3DDrawer
  23. app = Flask(__name__)
  24. cur_dir = os.getcwd()
  25. # url = 'http://192.168.183.216:8008/python/tunCAD'
  26. # url = 'data/1733121175.json'
  27. # url = 'http://localhost:8008/python/tunCAD'
  28. # url_2d = "http://192.168.183.216:8008/python/tunCAD"
  29. # url_3d = "http://192.168.183.216:8008/tunLt"
  30. url_2d = "http://localhost:8008/python/tunCAD"
  31. url_3d = "http://localhost:8008/tunLt"
  32. # url = 'http://localhost:8008/python/tunCAD'
  33. # url = 'data/cad.json'
  34. @app.route('/sysvent/download/<int:model_id>/<string:type>', methods=['GET'])
  35. def download_dxf_file(model_id, type):
  36. # 指定文件的完整路径
  37. try:
  38. path = None
  39. if type == '2d':
  40. with open('data/global-2d.yaml', 'r', encoding='utf-8') as file:
  41. config = yaml.safe_load(file)
  42. path = draw_system_vent(f'{url_2d}?modelid={model_id}', model_id,config)
  43. elif type == '3d':
  44. with open('data/global-3d.yaml', 'r', encoding='utf-8') as file:
  45. config = yaml.safe_load(file)
  46. path = draw_system_vent_3d(f'{url_3d}?modelid={model_id}', model_id,config)
  47. # 使用send_file发送文件,as_attachment=True表示以附件形式发送
  48. # return send_file(path, as_attachment=True)
  49. return jsonify({"path": path}), 200
  50. except NewConnectionError as e:
  51. # 捕获连接错误,并返回自定义的错误信息
  52. error_message = "连接失败: 无法连接到服务器。错误信息: " + str(e)
  53. print(error_message)
  54. return jsonify({"error": error_message}), 500
  55. except requests.exceptions.HTTPError as http_err:
  56. # 捕获其他HTTP错误
  57. error_message = f"HTTP错误: {http_err}"
  58. print(error_message)
  59. return jsonify({"error": error_message}), 500
  60. # except Exception as req_err:
  61. # # # 捕获所有requests库抛出的异常
  62. # error_message = f"算法执行异常: {req_err}"
  63. # print(error_message)
  64. # return jsonify({"error": error_message}), 500
  65. # def calculate_route_middle(wind_flow_unit):
  66. # path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z']
  67. # path_end = wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z']
  68. # path_middle = core.find_point_on_line(path_start, path_end, 1 / 2)
  69. # route = core.calculate_angle_with_x_axis(path_start, path_end)
  70. # return path_middle, route
  71. def msp_pre_process(path, model_id,config,model_type):
  72. cad_json = CADJson(path,config)
  73. default_template_file = "template/last/default_3d.dxf"
  74. if model_type== "3d":
  75. default_template_file = "template/last/default_3d.dxf"
  76. else:
  77. default_template_file = "template/last/default_2d.dxf"
  78. if os.path.exists(default_template_file):
  79. doc = ezdxf.readfile(default_template_file)
  80. else:
  81. doc = ezdxf.new('R2013')
  82. doc.styles.add("msyh", font="data/msyh.ttc")
  83. for layer in cad_json.json['layerMap']:
  84. doc.layers.new(name=f'图层{layer[0]}')
  85. msp = doc.modelspace()
  86. return msp,doc,cad_json
  87. def draw_system_vent_3d(path, model_id,config):
  88. msp,doc, cad_json = msp_pre_process(path, model_id,config,"3d")
  89. for gaps in tqdm(cad_json.gaps_list, desc=f' 【节点空隙绘制中】'):
  90. ngpd = NodeGapDrawer(msp, gaps, 1)
  91. ngpd.draw_gap()
  92. text_list = []
  93. for tun in tqdm(cad_json.get_tun_plg_dict().values(), desc=' 【3d巷道绘制中】'):
  94. text = tun["name"] + " " + tun['fq']
  95. plg_tun_agg = PlgTunDrawer(msp, tun['vec_b']['agg'], tun['vec_r']['agg'], tun['vec_l']['agg'], text,
  96. tun['layer_id'], tun['color'], tun['from']['agg'], tun['to']['agg'], tun['width'])
  97. plg_tun_agg.draw_plg_tun()
  98. # plg_tun_agg.draw_middle_line()
  99. if text not in text_list:
  100. text_list.append(text)
  101. plg_tun_agg.draw_tun_text()
  102. # plg_tun_div = PlgTunDrawer(msp, tun['vec_b']['div'], tun['vec_r']['div'], tun['vec_l']['div'], tun["name"],
  103. # tun['layer_id'], tun['color'], tun['from']['div'], tun['to']['div'], tun['width'])
  104. # plg_tun_div.draw_plg_tun()
  105. # plg_tun_div.draw_tun_text()
  106. for air_flow in tqdm(cad_json.air_flow_list_3d, desc=f' 【风流方向绘制中】'):
  107. agg_wind_flow = WindFlow3DDrawer(msp, air_flow['layer'], 3, air_flow['center']['agg'], air_flow['route']['agg'],
  108. air_flow['type'])
  109. # div_wind_flow = WindFlowDrawer(msp, air_flow['layer'], 3, air_flow['center']['div'], air_flow['route']['div'],
  110. # air_flow['type'])
  111. agg_wind_flow.draw_wind_flow()
  112. # div_wind_flow.draw_wind_flow()
  113. for window in tqdm(cad_json.window_list_3d, desc=f' 【风窗绘制中】'):
  114. agg_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["agg"], window["route"]['agg'])
  115. # div_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["div"], window["route"]['div'])
  116. agg_w.draw_window()
  117. # div_w.draw_window()
  118. for gate in tqdm(cad_json.gates_list_3d, desc=f' 【风门绘制中】'):
  119. agg_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['agg'], gate["route"]['agg'])
  120. agg_g.draw_gate()
  121. for fan in tqdm(cad_json.fans_list_3d, desc=f' 【风扇绘制中】'):
  122. if 'fanmain' in str(fan['type']):
  123. agg_f = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
  124. agg_f.draw_fan_main()
  125. if 'fanlocal' in str(fan['type']):
  126. fsd = FanSystemDrawer(msp,fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
  127. fsd.draw_fan_system()
  128. path = f'save/{str(time.time())}.dxf'
  129. doc.saveas(path)
  130. print(f'保存文件{os.path.abspath(path)}')
  131. return os.path.abspath(path)
  132. def draw_system_vent(path, model_id,config):
  133. msp,doc, cad_json = msp_pre_process(path, model_id,config,"2d")
  134. text_list = []
  135. text_dict = {}
  136. for tun in tqdm(cad_json.tun_dict.values(), desc=' 【巷道绘制中】'):
  137. if tun['type'] == '1':
  138. shaft_center = tun['from'][0], tun['from'][1]
  139. sd = ShaftDrawer(msp, tun['layer_id'], tun['width'], shaft_center, 0)
  140. sd.draw_shaft_drawer()
  141. else:
  142. text = tun["name"] +" "+ tun['fq']
  143. td_agg = TunDrawer(msp, tun['layer_id'], tun["vec12"]["agg"], tun["vec34"]["agg"], tun["from"]['agg'],
  144. tun["to"]['agg'],
  145. text,
  146. tun["width"],
  147. tun["head_vec_from"]['agg'],
  148. tun["head_vec_to"]['agg']
  149. )
  150. td_divided = TunDrawer(msp, tun['layer_id'], tun["vec12"]["div"], tun["vec34"]["div"], tun["from"]['div'],
  151. tun["to"]['div'], text,
  152. tun["width"],
  153. tun["head_vec_from"]['div'],
  154. tun["head_vec_to"]['div']
  155. )
  156. td_agg.draw_tun()
  157. td_divided.draw_tun()
  158. # if text not in text_dict.keys():
  159. # text_dict[text]= []
  160. # else:
  161. # text_dict[text].append(tun)
  162. #
  163. # if text not in text_list:
  164. # text_list.append(text)
  165. # td_agg.draw_tun_text()
  166. # td_divided.draw_tun_text()
  167. for tun_text in tqdm(cad_json.tun_text_dict.values(), desc=' 【巷道文字绘制中】'):
  168. ttd_agg = TunTextDrawer(msp, tun_text['layer'], tun_text['tun_line']['agg'], tun_text['text'],5)
  169. ttd_div = TunTextDrawer(msp,tun_text['layer'], tun_text['tun_line']['div'],tun_text['text'],5)
  170. ttd_agg.dra_text()
  171. ttd_div.dra_text()
  172. for sealed in tqdm(cad_json.sealed_list, desc=f' 【密闭绘制中】'):
  173. agg_w = SealedDrawer(msp, sealed["width"], sealed["layer"], sealed["center"]["agg"], sealed["route"]['agg'])
  174. div_w = SealedDrawer(msp, sealed["width"], sealed["layer"], sealed["center"]["div"], sealed["route"]['div'])
  175. agg_w.drawSealed()
  176. div_w.drawSealed()
  177. for window in tqdm(cad_json.window_list, desc=f' 【风窗绘制中】'):
  178. agg_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["agg"], window["route"]['agg'])
  179. div_w = WindowDrawer(msp, window["width"], window["layer"], window["center"]["div"], window["route"]['div'])
  180. agg_w.draw_window()
  181. div_w.draw_window()
  182. for ad_list in tqdm(cad_json.ari_duct_dict.values(), desc=f' 【风筒绘制中】'):
  183. for i in range(len(ad_list)):
  184. ad = ad_list[i]
  185. agg_ad= AirDuctDrawer(msp,ad['layer'],ad['from']['agg'],ad['to']['agg'],ad['width'])
  186. div_ad= AirDuctDrawer(msp,ad['layer'],ad['from']['div'],ad['to']['div'],ad['width'])
  187. agg_ad.draw_air_duct_list()
  188. div_ad.draw_air_duct_list()
  189. if i+1<len(ad_list):
  190. dxfattribs = {"layer": f"图层{ad_list[i]['layer']}", "color": core.get_color_by_layer(ad_list[i]['layer'])}
  191. msp.add_line(ad_list[i]['to']['agg'], ad_list[i + 1]['from']['agg'],dxfattribs)
  192. msp.add_line(ad_list[i]['to']['div'], ad_list[i + 1]['from']['div'], dxfattribs)
  193. # for adi in ad_list:
  194. # msp.add_circle(adi['from']['div'], 2, {"color": "1"}) # red
  195. # msp.add_circle(adi['to']['div'],2,{"color":"2"}) #yellow
  196. # 连接首位
  197. for gate in tqdm(cad_json.gate_list, desc=f' 【风门绘制中】'):
  198. agg_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['agg'], gate["route"]['agg'])
  199. div_g = GateDrawer(msp, gate["layer"], gate["width"], gate['center']['div'], gate["route"]['div'])
  200. agg_g.draw_gate()
  201. div_g.draw_gate()
  202. for fan in tqdm(cad_json.fan_list, desc=f' 【风扇绘制中】'):
  203. if 'fanmain' in str(fan['type']):
  204. agg_f = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
  205. div_f = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center']['div'], fan["route"]['div'])
  206. agg_f.draw_fan_main()
  207. div_f.draw_fan_main()
  208. if 'fanlocal' in str(fan['type']):
  209. agg_f = FanLocalDrawer(msp,fan["layer"], fan["width"], fan['center']['agg'], fan["route"]['agg'])
  210. div_f = FanLocalDrawer(msp,fan["layer"], fan["width"], fan['center']['div'], fan["route"]['div'])
  211. agg_f.draw_fan_local()
  212. div_f.draw_fan_local()
  213. for wind_bridge in tqdm(cad_json.wind_bridge_list, desc=f' 【风桥绘制中】'):
  214. agg_wb = WindBridgeDrawer(msp, wind_bridge['center']['agg'], wind_bridge['layer'],
  215. wind_bridge['route']['agg'], wind_bridge['width'])
  216. div_wb = WindBridgeDrawer(msp, wind_bridge['center']['div'], wind_bridge['layer'],
  217. wind_bridge['route']['div'], wind_bridge['width'])
  218. agg_wb.draw_wind_bridge_drawer()
  219. div_wb.draw_wind_bridge_drawer()
  220. for air_flow in tqdm(cad_json.air_flow_list, desc=f' 【风流方向绘制中】'):
  221. agg_wind_flow = WindFlowDrawer(msp, air_flow['layer'], air_flow['len'], air_flow['center']['agg'], air_flow['route']['agg'],
  222. air_flow['type'])
  223. div_wind_flow = WindFlowDrawer(msp, air_flow['layer'], air_flow['len'], air_flow['center']['div'], air_flow['route']['div'],
  224. air_flow['type'])
  225. agg_wind_flow.draw_wind_flow()
  226. div_wind_flow.draw_wind_flow()
  227. path = f'save/{str(time.time())}.dxf'
  228. doc.saveas(path)
  229. print(f'保存文件{os.path.abspath(path)}')
  230. return os.path.abspath(path)
  231. # if __name__ == '__main__':
  232. # app.run(debug=False)
  233. download_dxf_file(1868855086453813249,'3d')