CADJson.py 9.1 KB

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