CADJson.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import json
  2. import math
  3. import requests
  4. import core
  5. # 旋转
  6. global_route = - math.pi / 6
  7. # 缩放
  8. global_scale = 0.6
  9. # 偏移
  10. global_shift = 1000, 3000
  11. def global_process(point):
  12. point = core.rotate_point_around_another(point, (0, 0), global_route)
  13. point = core.scale_point(point[0], point[1], global_scale)
  14. return point[0] + global_shift[0], point[1] + global_shift[1]
  15. def calculate_route_middle(wind_flow_unit):
  16. path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z']
  17. path_end = wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z']
  18. path_middle = core.find_point_on_line(path_start, path_end, 1 / 2)
  19. route = core.calculate_angle_with_x_axis(path_start, path_end)
  20. return path_middle, route
  21. def request_graph_point(url):
  22. response = requests.get(url)
  23. print(f"请求耗时 {response.elapsed.total_seconds()} 秒")
  24. cad_json = {}
  25. if response.status_code == 200:
  26. cad_json = response.json() # 将响应内容解析为JSON格式
  27. else:
  28. print(f"请求失败,状态码:{response.status_code}")
  29. return cad_json
  30. class CADJson:
  31. def __init__(self, path):
  32. if "http://" in path or "http://" in path:
  33. self.json = request_graph_point(path)
  34. else:
  35. with open(path, 'r', encoding='utf-8') as r:
  36. self.json = json.loads(r.read())
  37. self.tun_list = self.get_tuns()
  38. self.fan_list = self.get_fans()
  39. self.window_list = self.get_windows()
  40. self.gate_list = self.get_gates()
  41. self.wind_flow_list = self.get_wind_flow()
  42. self.wind_bridge_list = self.get_wind_bridge()
  43. def get_tuns(self):
  44. node_list = self.json["tunsMapCAD"]
  45. tun_list = []
  46. for node in node_list:
  47. node = node[1]
  48. tun = {"tun_id": node["ntunid"], "layer_id": node["nlayerid"], "width": node["fwidth"],
  49. "type": node["tunType"], "angle": node["angleRad"],
  50. "from": global_process((node["nfrom"]["x"], node["nfrom"]["z"])),
  51. "to": global_process((node["nto"]["x"], node["nto"]["z"])),
  52. "name": node["strname"], "vec12": [], "vec34": []}
  53. for n in node["vec12"]:
  54. point = global_process((n["x"], n["z"]))
  55. tun["vec12"].append(point)
  56. for n in node["vec34"]:
  57. point = global_process((n["x"], n["z"]))
  58. tun["vec34"].append(point)
  59. tun["from"] = global_process((node["nfrom"]["x"], node["nfrom"]["z"]))
  60. tun["to"] = global_process((node["nto"]["x"], node["nto"]["z"]))
  61. if tun["type"] != "1":
  62. seg1 = tun["vec12"][0], tun["vec12"][-1]
  63. seg2 = tun["vec34"][0], tun["vec34"][-1]
  64. tun["width"] = core.min_distance_between_segments(seg1, seg2)
  65. tun["route"] = core.calculate_angle_with_x_axis((tun["from"][0], tun["from"][1]),
  66. (tun["to"][0], tun["to"][1]))
  67. tun_list.append(tun)
  68. return tun_list
  69. def get_windows(self):
  70. layer_map = self.json["layerMap"]
  71. window_list = []
  72. for layer in layer_map:
  73. windows = layer[1]["windows"]
  74. for w in windows:
  75. tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == w["ntunid"]][0]
  76. window = {"layer": w["nlayerid"], "color": core.get_color_by_layer(w["nlayerid"]), "name": w["strname"],
  77. "id": w["id"], "center": global_process((w["x"], w["z"])),
  78. 'route': tun['route'],
  79. 'width': tun['width']
  80. }
  81. window_list.append(window)
  82. return window_list
  83. def get_fans(self):
  84. layer_map = self.json["layerMap"]
  85. fans_list = []
  86. for layer in layer_map:
  87. fans = layer[1]["fans"]
  88. for f in fans:
  89. tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == f["ntunid"]][0]
  90. fan = {"layer": f["nlayerid"], "color": core.get_color_by_layer(f["nlayerid"]),
  91. "type": f["strtype"],
  92. "name": f["strname"],
  93. "id": f["id"], "center": global_process((f["x"], f["z"])),
  94. 'route': tun["route"],
  95. 'width': tun['width']
  96. }
  97. fans_list.append(fan)
  98. return fans_list
  99. def get_gates(self):
  100. layer_map = self.json["layerMap"]
  101. gate_list = []
  102. for layer in layer_map:
  103. gates = layer[1]["gates"]
  104. for g in gates:
  105. tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == g["ntunid"]][0]
  106. gate = {"layer": g["nlayerid"], "color": core.get_color_by_layer(g["nlayerid"]),
  107. "type": g["strtype"],
  108. "name": g["strname"],
  109. "id": g["id"], "center": global_process((g["x"], g["z"])),
  110. 'route': tun['route'],
  111. 'width': tun['width']
  112. }
  113. gate_list.append(gate)
  114. return gate_list
  115. def get_wind_flow(self):
  116. path_point_map = self.json["pathPointMap"]
  117. wind_flow_list = {
  118. "in": [],
  119. "no": [],
  120. "out": [],
  121. "use": []
  122. }
  123. for path_point in path_point_map:
  124. in_paths = path_point[1]['inPaths']
  125. for path in in_paths:
  126. path_middle, route = calculate_route_middle(path)
  127. in_path = {
  128. "layer": path_point[0],
  129. "center": global_process(path_middle),
  130. "route": route + global_route,
  131. "color": "3",
  132. "type": "3"
  133. }
  134. wind_flow_list["in"].append(in_path)
  135. no_paths = path_point[1]['noPaths']
  136. for path in no_paths:
  137. path_middle, route = calculate_route_middle(path)
  138. no_path = {
  139. "layer": path_point[0],
  140. "center": global_process(path_middle),
  141. "route": route + global_route,
  142. "color": "3",
  143. "type": "3"
  144. }
  145. wind_flow_list["no"].append(no_path)
  146. out_paths = path_point[1]['outPaths']
  147. for path in out_paths:
  148. path_middle, route = calculate_route_middle(path)
  149. out_path = {
  150. "layer": path_point[0],
  151. "center": global_process(path_middle),
  152. "route": route + global_route,
  153. "color": "1",
  154. "type": "1"
  155. }
  156. wind_flow_list["out"].append(out_path)
  157. use_paths = path_point[1]['usePaths']
  158. for path in use_paths:
  159. path_middle, route = calculate_route_middle(path)
  160. use_path = {
  161. "layer": path_point[0],
  162. "center": global_process(path_middle),
  163. "route": route + global_route,
  164. "color": "1",
  165. "type": "1"
  166. }
  167. wind_flow_list["use"].append(use_path)
  168. return wind_flow_list
  169. def get_wind_bridge(self):
  170. layer_map = self.json["layerMap"]
  171. wind_bridge_list = []
  172. for layer in layer_map:
  173. cross_nodes = layer[1]['crossNodes']
  174. for cross_node in cross_nodes:
  175. center = cross_node['crossPoint']['x'], -cross_node['crossPoint']['y']
  176. center = global_process(center)
  177. tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == cross_node['tun1Id']][0]
  178. wind_bridge = {
  179. 'layer': layer[0],
  180. 'color': core.get_color_by_layer(layer[0]),
  181. 'center': center,
  182. 'route': tun['route'],
  183. 'width': tun['width'] * 1.5114514
  184. }
  185. wind_bridge_list.append(wind_bridge)
  186. return wind_bridge_list