CADJson.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. import json
  2. import math
  3. import requests
  4. from tqdm import tqdm
  5. import core
  6. # 旋转
  7. # global_route = -math.pi / 4
  8. global_route = 0
  9. # 缩放
  10. global_scale = 1
  11. # 偏移
  12. global_shift = 0, 0
  13. def global_process(point):
  14. point = core.rotate_point_around_another(point, (0, 0), global_route)
  15. point = core.scale_point(point[0], point[1], global_scale)
  16. point = point[0] + global_shift[0], - (point[1] + global_shift[1])
  17. return point
  18. def layer_aggregation(point, model):
  19. coefficient_x = model['cadOffsetX'] if 'cadOffsetX' in model.keys() else 0
  20. coefficient_y = model['cadOffsetY'] if 'cadOffsetY' in model.keys() else 0
  21. coefficient_route = model['cadAngle'] if 'cadAngle' in model.keys() else 0
  22. point_ = core.rotate_point_around_another(point, (0, 0), coefficient_route)
  23. point_ = point_[0] + coefficient_x, point_[1] + coefficient_y
  24. return point_
  25. def layer_divide(point, layer):
  26. coefficient_x = layer['cadOffsetX'] if 'cadOffsetX' in layer.keys() or layer['cadOffsetX'] is None else 0
  27. coefficient_y = layer['cadOffsetY'] if 'cadOffsetY' in layer.keys() or layer['cadOffsetY'] is None else 0
  28. coefficient_route = layer['cadAngle'] if 'cadAngle' in layer.keys() or layer['cadAngle'] is None else 0
  29. point_ = core.rotate_point_around_another(point, (0, 0), coefficient_route)
  30. point_ = point_[0] + coefficient_x, point_[1] + coefficient_y
  31. return point_
  32. def request_graph_point(url):
  33. response = requests.get(url, timeout=10)
  34. print(f"请求耗时 {response.elapsed.total_seconds()} 秒")
  35. cad_json = {}
  36. if response.status_code == 200:
  37. cad_json = response.json() # 将响应内容解析为JSON格式
  38. else:
  39. print(f"请求失败,状态码:{response.status_code}")
  40. return cad_json
  41. def check_node(node):
  42. flag = True
  43. if len(node["vec12"]) < 1 or len(node["vec34"]) < 1:
  44. flag = False
  45. return flag
  46. else:
  47. if node["vec12"][0]['x'] is None or node["vec12"][0]['z'] is None:
  48. flag = False
  49. if node["vec12"][1]['x'] is None or node["vec12"][1]['z'] is None:
  50. flag = False
  51. if node["vec34"][0]['x'] is None or node["vec34"][0]['z'] is None:
  52. flag = False
  53. if node["vec34"][1]['x'] is None or node["vec34"][1]['z'] is None:
  54. flag = False
  55. if node["layerVec12"][0]['x'] is None or node["layerVec12"][0]['z'] is None:
  56. flag = False
  57. if node["layerVec12"][1]['x'] is None or node["layerVec12"][1]['z'] is None:
  58. flag = False
  59. if node["layerVec34"][0]['x'] is None or node["layerVec34"][0]['z'] is None:
  60. flag = False
  61. if node["layerVec34"][1]['x'] is None or node["layerVec34"][1]['z'] is None:
  62. flag = False
  63. return flag
  64. class CADJson:
  65. def __init__(self, path):
  66. if "http://" in path or "https://" in path:
  67. self.json = request_graph_point(path)
  68. else:
  69. with open(path, 'r', encoding='utf-8') as r:
  70. self.json = json.loads(r.read())
  71. self.model = self.json["model"]
  72. self.layer_map = self.get_layerMap()
  73. # self.tun_list = self.get_tuns()
  74. self.tun_dict = self.get_tun_dict()
  75. self.fan_list = self.get_fans()
  76. self.window_list = self.get_windows()
  77. self.gate_list = self.get_gates()
  78. # self.wind_flow_list = self.get_wind_flow()
  79. self.wind_bridge_list = self.get_wind_bridge()
  80. self.tun_dict = self.get_tun_dict()
  81. self.air_flow_list = self.get_air_flow()
  82. def get_layerMap(self):
  83. layer_list = self.json["layerMap"]
  84. layer_map = {}
  85. for layer in layer_list:
  86. layer_map[layer[0]] = layer[1]
  87. return layer_map
  88. def get_tun_dict(self):
  89. node_list = self.json["tunsMap"]
  90. tun_dict = {}
  91. for node in tqdm(node_list, desc=' 【巷道读取中】'):
  92. node = node[1]
  93. if check_node(node) is False: continue
  94. tun = {
  95. "tun_id": node["ntunid"], "layer_id": node["nlayerid"], "width": node["fwidth"],
  96. "type": node["tunType"],
  97. "angle": node["angleRad"],
  98. "air_type": node["nairtype"],
  99. "from": {"agg": layer_aggregation(global_process((node["nfrom"]["x"], node["nfrom"]["z"])), self.model),
  100. "div": layer_divide(global_process((node["nfrom"]["x"], node["nfrom"]["z"])),
  101. self.layer_map[node["nlayerid"]])
  102. },
  103. "to": {"agg": layer_aggregation(global_process((node["nto"]["x"], node["nto"]["z"])), self.model),
  104. "div": layer_divide(global_process((node["nto"]["x"], node["nto"]["z"])),
  105. self.layer_map[node["nlayerid"]])},
  106. "name": node["strname"],
  107. "vec12": {"agg": [], "div": []},
  108. "vec34": {"agg": [], "div": []}
  109. }
  110. for n in node["vec12"]:
  111. agg_point = layer_aggregation(global_process((n["x"], n["z"])), self.model)
  112. tun["vec12"]["agg"].append(agg_point)
  113. for n in node["layerVec12"]:
  114. div_point = layer_divide(global_process((n["x"], n["z"])), self.layer_map[node["nlayerid"]])
  115. tun["vec12"]["div"].append(div_point)
  116. for n in node["vec34"]:
  117. agg_point = layer_aggregation(global_process((n["x"], n["z"])), self.model)
  118. tun["vec34"]["agg"].append(agg_point)
  119. for n in node["layerVec34"]:
  120. div_point = layer_divide(global_process((n["x"], n["z"])), self.layer_map[node["nlayerid"]])
  121. tun["vec34"]["div"].append(div_point)
  122. if tun["type"] != "1" and len(tun["vec12"]) > 1 and len(tun["vec34"]) > 1:
  123. seg1 = tun["vec12"]['div'][0], tun["vec12"]['div'][-1]
  124. seg2 = tun["vec34"]['div'][0], tun["vec34"]['div'][-1]
  125. tun["width"] = core.min_distance_between_segments(seg1, seg2)
  126. tun["route"] = {
  127. "agg": core.calculate_angle_with_x_axis(tun['from']['agg'],
  128. tun['to']['agg']),
  129. "div": core.calculate_angle_with_x_axis(tun['from']['div'],
  130. tun['to']['div'])
  131. }
  132. else:
  133. tun["route"] = {
  134. "agg": 0,
  135. "div": 0
  136. }
  137. tun["width"] = 3.1415936
  138. tun_dict[tun["tun_id"]] = tun
  139. return tun_dict
  140. def get_tuns(self):
  141. node_list = self.json["tunsMap"]
  142. tun_list = []
  143. for node in tqdm(node_list, desc=' 【巷道读取中】'):
  144. node = node[1]
  145. if check_node(node) is False: continue
  146. # node['vec12'] = node['layerVec12']
  147. # node['vec34'] = node['layerVec34']
  148. tun = {
  149. "tun_id": node["ntunid"],
  150. "layer_id": node["nlayerid"],
  151. "width": node["fwidth"],
  152. "type": node["tunType"],
  153. "air_type": node["nairtype"],
  154. "angle": node["angleRad"],
  155. "from": {"agg": layer_aggregation(global_process((node["nfrom"]["x"], node["nfrom"]["z"])), self.model),
  156. "div": layer_divide(global_process((node["nfrom"]["x"], node["nfrom"]["z"])),
  157. self.layer_map[node["nlayerid"]])
  158. },
  159. "to": {"agg": layer_aggregation(global_process((node["nto"]["x"], node["nto"]["z"])), self.model),
  160. "div": layer_divide(global_process((node["nto"]["x"], node["nto"]["z"])),
  161. self.layer_map[node["nlayerid"]])},
  162. "name": node["strname"],
  163. "vec12": {"agg": [], "div": []},
  164. "vec34": {"agg": [], "div": []}
  165. }
  166. for n in node["vec12"]:
  167. agg_point = layer_aggregation(global_process((n["x"], n["z"])), self.model)
  168. tun["vec12"]["agg"].append(agg_point)
  169. for n in node["layerVec12"]:
  170. div_point = layer_divide(global_process((n["x"], n["z"])), self.layer_map[node["nlayerid"]])
  171. tun["vec12"]["div"].append(div_point)
  172. for n in node["vec34"]:
  173. agg_point = layer_aggregation(global_process((n["x"], n["z"])), self.model)
  174. tun["vec34"]["agg"].append(agg_point)
  175. for n in node["layerVec34"]:
  176. div_point = layer_divide(global_process((n["x"], n["z"])), self.layer_map[node["nlayerid"]])
  177. tun["vec34"]["div"].append(div_point)
  178. if tun["type"] != "1" and len(tun["vec12"]) > 1 and len(tun["vec34"]) > 1:
  179. seg1 = tun["vec12"]['div'][0], tun["vec12"]['div'][-1]
  180. seg2 = tun["vec34"]['div'][0], tun["vec34"]['div'][-1]
  181. tun["width"] = core.min_distance_between_segments(seg1, seg2)
  182. tun["route"] = {
  183. "agg": core.calculate_angle_with_x_axis(tun['from']['agg'],
  184. tun['to']['agg']),
  185. "div": core.calculate_angle_with_x_axis(tun['from']['div'],
  186. tun['to']['div'])
  187. }
  188. else:
  189. tun["route"] = {
  190. "agg": 0,
  191. "div": 0
  192. }
  193. tun["width"] = 3.1415936
  194. print(tun['air_type'])
  195. tun_list.append(tun)
  196. return tun_list
  197. def get_windows(self):
  198. layer_map = self.json["layerMap"]
  199. window_list = []
  200. for layer in tqdm(layer_map, desc=' 【风窗读取中】'):
  201. windows = layer[1]["windows"]
  202. for w in windows:
  203. tun = self.tun_dict.get(w["ntunid"])
  204. if tun is None: continue
  205. # tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == w["ntunid"]][0]
  206. window = {"layer": w["nlayerid"], "color": core.get_color_by_layer(w["nlayerid"]),
  207. "name": w["strname"],
  208. "id": w["id"],
  209. "center": {
  210. "agg": layer_aggregation(global_process((w['x'], w['z'])), self.model),
  211. "div": layer_divide(global_process((w['x'], w['z'])),
  212. self.layer_map[w["nlayerid"]])
  213. },
  214. 'route': {"agg": tun["route"]['agg'],
  215. "div": tun["route"]['div']
  216. },
  217. 'width': tun['width']
  218. }
  219. window_list.append(window)
  220. return window_list
  221. def get_fans(self):
  222. layer_map = self.json["layerMap"]
  223. fans_list = []
  224. for layer in tqdm(layer_map, desc=' 【风扇读取中】'):
  225. fans = layer[1]["fans"]
  226. for f in fans:
  227. tun = self.tun_dict.get(f["ntunid"])
  228. if tun is None: continue
  229. fan = {"layer": f["nlayerid"], "color": core.get_color_by_layer(f["nlayerid"]),
  230. "type": f["strtype"],
  231. "name": f["strname"],
  232. "id": f["id"],
  233. "center": {
  234. "agg": layer_aggregation(global_process((f['x'], f['z'])), self.model),
  235. "div": layer_divide(global_process((f['x'], f['z'])), self.layer_map[f["nlayerid"]])
  236. },
  237. 'route': {"agg": tun["route"]['agg'],
  238. "div": tun["route"]['div']
  239. },
  240. 'width': tun['width']
  241. }
  242. fans_list.append(fan)
  243. return fans_list
  244. def get_gates(self):
  245. layer_map = self.json["layerMap"]
  246. gate_list = []
  247. for layer in tqdm(layer_map, desc=' 【风门读取中】'):
  248. gates = layer[1]["gates"]
  249. for g in gates:
  250. tun = self.tun_dict.get(g["ntunid"])
  251. if tun is None: continue
  252. gate = {"layer": g["nlayerid"], "color": core.get_color_by_layer(g["nlayerid"]),
  253. "type": g["strtype"],
  254. "name": g["strname"],
  255. "id": g["id"],
  256. "center": {
  257. "agg": layer_aggregation(global_process((g['x'], g['z'])), self.model),
  258. "div": layer_divide(global_process((g['x'], g['z'])), self.layer_map[g["nlayerid"]])
  259. },
  260. 'route': {"agg": tun["route"]['agg'],
  261. "div": tun["route"]['div']
  262. },
  263. 'width': tun['width']
  264. }
  265. gate_list.append(gate)
  266. return gate_list
  267. def calculate_route_middle(self, path, layer_id):
  268. path_start = global_process((path[0]['x'], path[0]['z']))
  269. path_end = global_process((path[-1]['x'], path[-1]['z']))
  270. path_start_agg = layer_aggregation(path_start, self.model)
  271. path_end_agg = layer_aggregation(path_end, self.model)
  272. path_start_div = layer_divide(path_start, self.layer_map[layer_id])
  273. path_end_div = layer_divide(path_end, self.layer_map[layer_id])
  274. path_middle_agg = core.find_point_on_line(path_start_agg, path_end_agg, 1 / 2)
  275. path_middle_div = core.find_point_on_line(path_start_div, path_end_div, 1 / 2)
  276. route_agg = core.calculate_angle_with_x_axis(path_start_agg, path_end_agg)
  277. route_div = core.calculate_angle_with_x_axis(path_start_div, path_end_div)
  278. return {"middle": {"agg": path_middle_agg, "div": path_middle_div},
  279. "route": {"agg": route_agg, "div": route_div}}
  280. def calculate_segment_route_middle(self, vector, layer_id):
  281. path_start = global_process(vector[0])
  282. path_end = global_process(vector[1])
  283. path_start_agg = layer_aggregation(path_start, self.model)
  284. path_end_agg = layer_aggregation(path_end, self.model)
  285. path_start_div = layer_divide(path_start, self.layer_map[layer_id])
  286. path_end_div = layer_divide(path_end, self.layer_map[layer_id])
  287. path_middle_agg = core.find_point_on_line(path_start_agg, path_end_agg, 1 / 2)
  288. path_middle_div = core.find_point_on_line(path_start_div, path_end_div, 1 / 2)
  289. route_agg = core.calculate_angle_with_x_axis(path_start_agg, path_end_agg)
  290. route_div = core.calculate_angle_with_x_axis(path_start_div, path_end_div)
  291. return {"middle": {"agg": path_middle_agg, "div": path_middle_div},
  292. "route": {"agg": route_agg, "div": route_div}}
  293. def get_air_flow(self):
  294. air_flow_list = []
  295. for tun in tqdm(self.tun_dict.values(), desc='【风流方向读取中】'):
  296. vec_agg = tun['from']['agg'], tun['to']['agg']
  297. vec_div = tun["from"]["div"], tun["to"]["div"]
  298. distance = core.distance(vec_agg[0], vec_agg[1])
  299. if 50 < distance < 100:
  300. middle_agg = core.find_point_on_line(vec_agg[0], vec_agg[1], 1 / 2)
  301. middle_div = core.find_point_on_line(vec_div[0], vec_div[1], 1 / 2)
  302. air_flow = {
  303. "layer": tun['layer_id'],
  304. "center": {
  305. "agg": middle_agg,
  306. "div": middle_div
  307. },
  308. "route": {"agg": tun["route"]['agg'],
  309. "div": tun['route']['div']},
  310. "type": tun['air_type']
  311. }
  312. air_flow_list.append(air_flow)
  313. else:
  314. space = int(distance // 100)
  315. agg_points = core.divide_segment(vec_agg[0], vec_agg[1], space)[1:-1]
  316. div_points = core.divide_segment(vec_div[0], vec_div[1], space)[1:-1]
  317. for i in range(len(agg_points)):
  318. air_flow = {
  319. "layer": tun['layer_id'],
  320. "center": {
  321. "agg": agg_points[i],
  322. "div": div_points[i]
  323. },
  324. "route": {"agg": tun["route"]['agg'],
  325. "div": tun['route']['div']},
  326. "type": tun['air_type']
  327. }
  328. air_flow_list.append(air_flow)
  329. return air_flow_list
  330. # def get_wind_flow(self):
  331. # path_point_map = self.json["pathLineMap"]
  332. # wind_flow_list = {
  333. # "in": [],
  334. # "no": [],
  335. # "out": [],
  336. # "use": []
  337. # }
  338. # for path_point in tqdm(path_point_map, desc=' 【风流方向读取中】'):
  339. # in_paths = path_point[1]['inPaths']
  340. # for path in in_paths:
  341. # middle_route = self.calculate_route_middle(path, path_point[0])
  342. # in_path = {
  343. # "layer": path_point[0],
  344. # "center": {
  345. # "agg": middle_route["middle"]["agg"],
  346. # "div": middle_route["middle"]["div"]
  347. # },
  348. # "route": {"agg": middle_route["route"]["agg"],
  349. # "div": middle_route["route"]["div"]},
  350. # "color": "3",
  351. # "type": "3"
  352. # }
  353. # wind_flow_list["in"].append(in_path)
  354. #
  355. # no_paths = path_point[1]['noPaths']
  356. # for path in no_paths:
  357. # middle_route = self.calculate_route_middle(path, path_point[0])
  358. # no_path = {
  359. # "layer": path_point[0],
  360. # "center": {
  361. # "agg": middle_route["middle"]["agg"],
  362. # "div": middle_route["middle"]["div"]
  363. # },
  364. # "route": {"agg": middle_route["route"]["agg"],
  365. # "div": middle_route["route"]["div"]},
  366. # "color": "3",
  367. # "type": "3"
  368. # }
  369. # wind_flow_list["no"].append(no_path)
  370. #
  371. # out_paths = path_point[1]['outPaths']
  372. # for path in out_paths:
  373. # middle_route = self.calculate_route_middle(path, path_point[0])
  374. # out_path = {
  375. # "layer": path_point[0],
  376. # "center": {
  377. # "agg": middle_route["middle"]["agg"],
  378. # "div": middle_route["middle"]["div"]
  379. # },
  380. # "route": {"agg": middle_route["route"]["agg"],
  381. # "div": middle_route["route"]["div"]},
  382. # "color": "1",
  383. # "type": "1"
  384. # }
  385. # wind_flow_list["out"].append(out_path)
  386. # use_paths = path_point[1]['usePaths']
  387. # for path in use_paths:
  388. # middle_route = self.calculate_route_middle(path, path_point[0])
  389. # use_path = {
  390. # "layer": path_point[0],
  391. # "center": {
  392. # "agg": middle_route["middle"]["agg"],
  393. # "div": middle_route["middle"]["div"]
  394. # },
  395. # "route": {"agg": middle_route["route"]["agg"],
  396. # "div": middle_route["route"]["div"]},
  397. # "color": "1",
  398. # "type": "1"
  399. # }
  400. # wind_flow_list["use"].append(use_path)
  401. #
  402. # return wind_flow_list
  403. def get_wind_bridge(self):
  404. layer_map = self.json["layerMap"]
  405. wind_bridge_list = []
  406. for layer in tqdm(layer_map, desc=' 【风桥读取中】'):
  407. cross_nodes = layer[1]['crossNodes']
  408. for cross_node in cross_nodes:
  409. center = cross_node['crossPoint']['x'], -cross_node['crossPoint']['y']
  410. # tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == cross_node['tun1Id']][0]
  411. tun = self.tun_dict.get(cross_node["tun1Id"])
  412. if tun is None: continue
  413. wind_bridge = {
  414. 'layer': layer[0],
  415. 'color': core.get_color_by_layer(layer[0]),
  416. "center": {
  417. "agg": layer_aggregation(global_process(center), self.model),
  418. "div": layer_divide(global_process(center), self.layer_map[layer[0]])
  419. },
  420. 'route': {"agg": tun["route"]['agg'],
  421. "div": tun['route']['div']},
  422. 'width': tun['width'] * 1.5114514
  423. }
  424. wind_bridge_list.append(wind_bridge)
  425. return wind_bridge_list