import json import math import requests import core # 旋转 global_route = - math.pi / 6 # 缩放 global_scale = 0.6 # 偏移 global_shift = 1000, 3000 def global_process(point): point = core.rotate_point_around_another(point, (0, 0), global_route) point = core.scale_point(point[0], point[1], global_scale) return point[0] + global_shift[0], point[1] + global_shift[1] 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 request_graph_point(url): response = requests.get(url) print(f"请求耗时 {response.elapsed.total_seconds()} 秒") cad_json = {} if response.status_code == 200: cad_json = response.json() # 将响应内容解析为JSON格式 else: print(f"请求失败,状态码:{response.status_code}") return cad_json class CADJson: def __init__(self, path): if "http://" in path or "http://" in path: self.json = request_graph_point(path) else: with open(path, 'r', encoding='utf-8') as r: self.json = json.loads(r.read()) self.tun_list = self.get_tuns() self.fan_list = self.get_fans() self.window_list = self.get_windows() self.gate_list = self.get_gates() self.wind_flow_list = self.get_wind_flow() self.wind_bridge_list = self.get_wind_bridge() def get_tuns(self): node_list = self.json["tunsMapCAD"] tun_list = [] for node in node_list: node = node[1] tun = {"tun_id": node["ntunid"], "layer_id": node["nlayerid"], "width": node["fwidth"], "type": node["tunType"], "angle": node["angleRad"], "from": global_process((node["nfrom"]["x"], node["nfrom"]["z"])), "to": global_process((node["nto"]["x"], node["nto"]["z"])), "name": node["strname"], "vec12": [], "vec34": []} for n in node["vec12"]: point = global_process((n["x"], n["z"])) tun["vec12"].append(point) for n in node["vec34"]: point = global_process((n["x"], n["z"])) tun["vec34"].append(point) tun["from"] = global_process((node["nfrom"]["x"], node["nfrom"]["z"])) tun["to"] = global_process((node["nto"]["x"], node["nto"]["z"])) if tun["type"] != "1": seg1 = tun["vec12"][0], tun["vec12"][-1] seg2 = tun["vec34"][0], tun["vec34"][-1] tun["width"] = core.min_distance_between_segments(seg1, seg2) tun["route"] = core.calculate_angle_with_x_axis((tun["from"][0], tun["from"][1]), (tun["to"][0], tun["to"][1])) tun_list.append(tun) return tun_list def get_windows(self): layer_map = self.json["layerMap"] window_list = [] for layer in layer_map: windows = layer[1]["windows"] for w in windows: tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == w["ntunid"]][0] window = {"layer": w["nlayerid"], "color": core.get_color_by_layer(w["nlayerid"]), "name": w["strname"], "id": w["id"], "center": global_process((w["x"], w["z"])), 'route': tun['route'], 'width': tun['width'] } window_list.append(window) return window_list def get_fans(self): layer_map = self.json["layerMap"] fans_list = [] for layer in layer_map: fans = layer[1]["fans"] for f in fans: tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == f["ntunid"]][0] fan = {"layer": f["nlayerid"], "color": core.get_color_by_layer(f["nlayerid"]), "type": f["strtype"], "name": f["strname"], "id": f["id"], "center": global_process((f["x"], f["z"])), 'route': tun["route"], 'width': tun['width'] } fans_list.append(fan) return fans_list def get_gates(self): layer_map = self.json["layerMap"] gate_list = [] for layer in layer_map: gates = layer[1]["gates"] for g in gates: tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == g["ntunid"]][0] gate = {"layer": g["nlayerid"], "color": core.get_color_by_layer(g["nlayerid"]), "type": g["strtype"], "name": g["strname"], "id": g["id"], "center": global_process((g["x"], g["z"])), 'route': tun['route'], 'width': tun['width'] } gate_list.append(gate) return gate_list def get_wind_flow(self): path_point_map = self.json["pathPointMap"] wind_flow_list = { "in": [], "no": [], "out": [], "use": [] } for path_point in path_point_map: in_paths = path_point[1]['inPaths'] for path in in_paths: path_middle, route = calculate_route_middle(path) in_path = { "layer": path_point[0], "center": global_process(path_middle), "route": route + global_route, "color": "3", "type": "3" } wind_flow_list["in"].append(in_path) no_paths = path_point[1]['noPaths'] for path in no_paths: path_middle, route = calculate_route_middle(path) no_path = { "layer": path_point[0], "center": global_process(path_middle), "route": route + global_route, "color": "3", "type": "3" } wind_flow_list["no"].append(no_path) out_paths = path_point[1]['outPaths'] for path in out_paths: path_middle, route = calculate_route_middle(path) out_path = { "layer": path_point[0], "center": global_process(path_middle), "route": route + global_route, "color": "1", "type": "1" } wind_flow_list["out"].append(out_path) use_paths = path_point[1]['usePaths'] for path in use_paths: path_middle, route = calculate_route_middle(path) use_path = { "layer": path_point[0], "center": global_process(path_middle), "route": route + global_route, "color": "1", "type": "1" } wind_flow_list["use"].append(use_path) return wind_flow_list def get_wind_bridge(self): layer_map = self.json["layerMap"] wind_bridge_list = [] for layer in layer_map: cross_nodes = layer[1]['crossNodes'] for cross_node in cross_nodes: center = cross_node['crossPoint']['x'], -cross_node['crossPoint']['y'] center = global_process(center) tun = [tun_ for tun_ in self.tun_list if tun_.get("tun_id") == cross_node['tun1Id']][0] wind_bridge = { 'layer': layer[0], 'color': core.get_color_by_layer(layer[0]), 'center': center, 'route': tun['route'], 'width': tun['width'] * 1.5114514 } wind_bridge_list.append(wind_bridge) return wind_bridge_list