CADJson.py 7.6 KB

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