import json import math import requests import core # 旋转 # global_route = -math.pi / 4 global_route = 0 # 缩放 global_scale = 1 # 偏移 global_shift = 0, 0 def global_process(point, layer): point = core.rotate_point_around_another(point, (0, 0), global_route) point = core.scale_point(point[0], point[1], global_scale) point = point[0] + global_shift[0], - (point[1] + global_shift[1]) point = layer_divide(point,layer) return point def layer_divide(point, layer): coefficient_x = layer['cadOffsetX'] coefficient_y = layer['cadOffsetY'] coefficient_route = layer['cadAngle'] point = core.rotate_point_around_another(point, (0, 0), coefficient_route) point = point[0] + coefficient_x, point[1] + coefficient_y return point def calculate_route_middle(wind_flow_unit, layer): path_start = global_process((wind_flow_unit[0]['x'], wind_flow_unit[0]['z']), layer) path_end = global_process((wind_flow_unit[-1]['x'], wind_flow_unit[-1]['z']), layer) path_middle = core.find_point_on_line(path_start, path_end, 1 / 2) route = core.calculate_angle_with_x_axis(path_start, path_end) route = route 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 "https://" 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.layer_map = self.get_layerMap() 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_layerMap(self): layer_list = self.json["layerMap"] layer_map = {} for layer in layer_list: layer_map[layer[0]] = layer[1] return layer_map def get_tuns(self): node_list = self.json["tunsMap"] 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"]), self.layer_map[node['nlayerid']]), "to": global_process((node["nto"]["x"], node["nto"]["z"]), self.layer_map[node['nlayerid']]), "name": node["strname"], "vec12": [], "vec34": []} for n in node["vec12"]: point = global_process((n["x"], n["z"]), self.layer_map[node['nlayerid']]) tun["vec12"].append(point) for n in node["vec34"]: point = global_process((n["x"], n["z"]), self.layer_map[node['nlayerid']]) tun["vec34"].append(point) tun["from"] = global_process((node["nfrom"]["x"], node["nfrom"]["z"]), self.layer_map[node['nlayerid']]) tun["to"] = global_process((node["nto"]["x"], node["nto"]["z"]), self.layer_map[node['nlayerid']]) 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"]), self.layer_map[w["nlayerid"]]), '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"]), self.layer_map[f["nlayerid"]]), '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"]), self.layer_map[g["nlayerid"]]), 'route': tun['route'], 'width': tun['width'] } gate_list.append(gate) return gate_list def get_wind_flow(self): path_point_map = self.json["pathLineMap"] 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, self.layer_map[path_point[0]]) in_path = { "layer": path_point[0], "center": path_middle, "route": 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, self.layer_map[path_point[0]]) no_path = { "layer": path_point[0], "center": path_middle, "route": 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, self.layer_map[path_point[0]]) out_path = { "layer": path_point[0], "center": path_middle, "route": 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, self.layer_map[path_point[0]]) use_path = { "layer": path_point[0], "center": path_middle, "route": 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, self.layer_map[layer[0]]) 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