kangyu 2 maanden geleden
bovenliggende
commit
0f9f3aa1ac
15 gewijzigde bestanden met toevoegingen van 550 en 113 verwijderingen
  1. 95 0
      core.py
  2. BIN
      data/msyh.ttc
  3. BIN
      data/msyhbd.ttc
  4. BIN
      data/msyhl.ttc
  5. 203 0
      drawer/CADJson.py
  6. 6 5
      drawer/FanMainDrawer.py
  7. 4 3
      drawer/GateDrawer.py
  8. 47 0
      drawer/MeshTemplateDrawer.py
  9. 6 5
      drawer/ShaftDrawer.py
  10. 31 8
      drawer/TunDrawer.py
  11. 9 10
      drawer/WindBridgeDrawer.py
  12. 7 4
      drawer/WindFlowDrawer.py
  13. 4 2
      drawer/WindowDrawer.py
  14. 56 76
      main_2.py
  15. 82 0
      main_3.py

+ 95 - 0
core.py

@@ -42,6 +42,21 @@ def find_point_on_line(A, B, fraction):
     return (x, y)
 
 
+def scale_point(x, y, k):
+    """
+    缩放二维点坐标。
+
+    参数:
+    x, y: 原点的x和y坐标。
+    k: 缩放因子。
+
+    返回:
+    新的x'和y'坐标。
+    """
+    x_prime = k * x
+    y_prime = k * y
+    return x_prime, y_prime
+
 # 计算 a点到b点之间1/x处(距离a点)的c点坐标
 def calculate_point_c(point_a, point_b, x):
     x_a, y_a = point_a
@@ -160,3 +175,83 @@ def symmetric_point(A, B):
     y_c = 2 * y_b - y_a
 
     return x_c, y_c
+
+
+def parallel_line(start, end, distance):
+    """
+    计算与给定线段平行且距离为distance的线段(只返回上方的一条)。
+
+    参数:
+    start -- 原始线段的起点坐标,形式为(x, y)
+    end -- 原始线段的终点坐标,形式为(x, y)
+    distance -- 平行线段与原始线段的距离
+
+    返回:
+    tuple -- 平行线段的起点和终点坐标,形式为((new_start_x, new_start_y), (new_end_x, new_end_y))
+    """
+    # 计算原始线段的斜率
+    if start[0] == end[0]:  # 斜率不存在(垂直线)
+        # 对于垂直线,我们只需在x坐标上保持不变,y坐标增减distance
+        new_start = (start[0], start[1] + distance)
+        new_end = (end[0], end[1] + distance)
+    else:
+        # 计算斜率
+        slope = (end[1] - start[1]) / (end[0] - start[0])
+
+        # 计算法线的斜率(负倒数)
+        normal_slope = -1 / slope
+
+        # 计算法线方向上从起点到距离distance的点的偏移量
+        # 注意:这里我们使用勾股定理的简化形式,因为距离是垂直于线段的
+        # 实际上,我们只需要y方向的偏移量,因为x方向的偏移量由斜率决定
+        dx = distance / math.sqrt(1 + slope ** 2)
+        dy = slope * dx
+
+        # 计算新线段的起点和终点
+        # 注意:这里我们假设距离是正值,只计算上方的平行线
+        # 如果需要下方的平行线,可以将dy的符号取反
+        new_start = (start[0] - dy / slope, start[1] + dx)
+        new_end = (end[0] - dy / slope, end[1] + dx)
+
+        # 注意:上面的计算方式在斜率为负时可能不正确,因为它没有考虑线段的方向
+        # 一种更健壮的方法是分别计算与start和end距离为distance的点,然后取这两个点作为新线段的端点
+        # 但为了简单起见,这里我们假设斜率不是负数或垂直线的情况已经处理
+
+    # 注意:上面的计算方式在斜率为负或接近0时可能不准确,下面是一个更准确的版本
+    # 它直接计算与start和end等距的点
+    # 考虑到斜率可能接近无穷(即几乎垂直),我们使用向量叉积的概念来避免除以0
+    vector = (end[0] - start[0], end[1] - start[1])
+    norm = (-vector[1], vector[0])  # 法线向量
+    norm_length = math.sqrt(norm[0] ** 2 + norm[1] ** 2)
+    unit_norm = (norm[0] / norm_length, norm[1] / norm_length)  # 单位法线向量
+    offset_point_start = (start[0] + unit_norm[0] * distance, start[1] + unit_norm[1] * distance)
+    offset_point_end = (end[0] + unit_norm[0] * distance, end[1] + unit_norm[1] * distance)
+
+    return (offset_point_start, offset_point_end)
+
+
+def shrink_line(start, end, scale_factor):
+    """
+    将线段缩小指定的倍数。
+
+    参数:
+    start -- 原始线段的起点坐标,形式为(x, y)
+    end -- 原始线段的终点坐标,形式为(x, y)
+    scale_factor -- 缩小倍数,例如0.5表示缩小到原来的一半
+
+    返回:
+    tuple -- 缩小后的线段的起点和终点坐标,形式为((new_start_x, new_start_y), (new_end_x, new_end_y))
+    """
+    # 计算原始线段的长度和方向向量
+    length = ((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) ** 0.5
+    direction = ((end[0] - start[0]) / length, (end[1] - start[1]) / length)
+
+    # 计算缩小后的线段的长度
+    new_length = length * scale_factor
+
+    # 计算缩小后的线段的起点和终点
+    new_start = start
+    new_end = (start[0] + direction[0] * new_length, start[1] + direction[1] * new_length)
+
+    return (new_start, new_end)
+

BIN
data/msyh.ttc


BIN
data/msyhbd.ttc


BIN
data/msyhl.ttc


+ 203 - 0
drawer/CADJson.py

@@ -0,0 +1,203 @@
+import json
+import math
+
+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
+
+
+class CADJson:
+    def __init__(self, path):
+        print()
+        with open(path, 'r', encoding='utf-8') as r:
+            cad_json = json.loads(r.read())
+
+        self.json = cad_json
+
+        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

+ 6 - 5
drawer/FanMainDrawer.py

@@ -4,7 +4,8 @@ import core
 
 
 class FanMainDrawer:
-    def __init__(self, msp, gap, center, route):
+    def __init__(self, msp,layer, gap, center, route):
+        self.layer = layer
         self.msp = msp
         self.gap = gap
         self.center = center
@@ -15,7 +16,7 @@ class FanMainDrawer:
         a = math.pi / 8
         b = 10 / 3 * rad
 
-        self.msp.add_circle(center=self.center, radius=rad)
+        self.msp.add_circle(center=self.center, radius=rad,dxfattribs={"layer":f"图层{self.layer}"})
 
         A = (rad * math.cos(a) + self.center[0], rad * math.sin(a) + self.center[1])
 
@@ -30,8 +31,8 @@ class FanMainDrawer:
         C = core.rotate_point_around_another(C, self.center, self.route)
         D = core.rotate_point_around_another(D, self.center, self.route)
 
-        self.msp.add_line(A, B)
+        self.msp.add_line(A, B,dxfattribs={"layer":f"图层{self.layer}"})
 
-        self.msp.add_line(B, D)
+        self.msp.add_line(B, D,dxfattribs={"layer":f"图层{self.layer}"})
 
-        self.msp.add_line(C, D)
+        self.msp.add_line(C, D,dxfattribs={"layer":f"图层{self.layer}"})

+ 4 - 3
drawer/GateDrawer.py

@@ -3,7 +3,8 @@ from ezdxf import math as mt
 
 
 class GateDrawer:
-    def __init__(self, msp, gap,center,route):
+    def __init__(self, msp,layer, gap,center,route):
+        self.layer = layer
         self.msp = msp
         self.gap = gap
         self.center = center
@@ -43,5 +44,5 @@ class GateDrawer:
     def add_gate_part(self, start_point, end_point, def_point):
         carc = mt.arc.ConstructionArc()  # 假设这是一个可用的类
         b = carc.from_3p(start_point=start_point, def_point=def_point, end_point=end_point)
-        self.msp.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle)
-        self.msp.add_line(start_point, end_point)
+        self.msp.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle,dxfattribs={"layer":f"图层{self.layer}"})
+        self.msp.add_line(start_point, end_point,dxfattribs={"layer":f"图层{self.layer}"})

+ 47 - 0
drawer/MeshTemplateDrawer.py

@@ -0,0 +1,47 @@
+import core
+
+
+class MeshTemplateDrawer:
+
+    def __init__(self, msp, grid, left_top, right_bottom, color):
+        self.msp = msp
+        self.grid = grid
+        self.left_top = left_top
+        self.right_bottom = right_bottom
+        self.margin_left = 2876
+        self.margin_bottom = 1847
+        self.color = color
+
+    def draw_mesh(self):
+        left_edge = self.left_top[0]
+        right_edge = self.right_bottom[0]
+        top_edge = self.left_top[1]
+        bottom_edge = self.right_bottom[1]
+        left_index = left_edge - self.margin_left
+        bottom_index = bottom_edge - self.margin_bottom
+        top_list = []
+        bottom_list = []
+        left_list = []
+        right_list = []
+        while left_index < right_edge:
+            # top_list.append(core.rotate_point_around_another((left_index,self.left_top[1]),(0,0),self.route))
+            top_list.append((left_index, self.left_top[1]+self.margin_bottom))
+            # bottom_list.append(core.rotate_point_around_another((left_index,self.right_bottom[1]),(0,0),self.route))
+            bottom_list.append((left_index, self.right_bottom[1]-self.margin_bottom))
+            left_index = left_index + self.grid
+
+        while bottom_index < top_edge:
+            # left_list.append(core.rotate_point_around_another((self.left_top[0], bottom_index), (0, 0), self.route))
+            left_list.append((self.left_top[0], bottom_index))
+            right_list.append((self.right_bottom[0], bottom_index))
+            # right_list.append(
+            #     (core.rotate_point_around_another((self.right_bottom[0], bottom_index), (0, 0), self.route)))
+            bottom_index = bottom_index + self.grid
+
+        for top_element, bottom_element in zip(top_list, bottom_list):
+            self.msp.add_line(top_element, bottom_element)
+
+        for left_element, right_element in zip(left_list, right_list):
+            self.msp.add_line(left_element, right_element)
+
+        # def draw_graph_unit():

+ 6 - 5
drawer/ShaftDrawer.py

@@ -7,7 +7,8 @@ import ezdxf
 
 
 class ShaftDrawer:
-    def __init__(self, msp, gap, center, route, color):
+    def __init__(self, msp ,layer, gap, center, route, color=42):
+        self.layer = layer
         self.msp = msp
         self.gap = gap
         self.center = center
@@ -73,10 +74,10 @@ class ShaftDrawer:
     def draw_polygon(self, points):
         i = 0
         while i < len(points) - 1:
-            self.msp.add_line(points[i], points[i + 1], {"color": self.color})
+            self.msp.add_line(points[i], points[i + 1], {"color": self.color,"layer": f"图层{self.layer}"})
             i = i + 1
         self.msp.add_line(points[-1], points[0], {
-            "color": self.color,
+            "color": self.color,"layer": f"图层{self.layer}"
         })
 
     def draw_shaft_drawer(self):
@@ -93,8 +94,8 @@ class ShaftDrawer:
 
         carc = mt.arc.ConstructionArc()
         b = carc.from_3p(start_point=points8_in[1], def_point=points8_in[3], end_point=points8_in[5])
-        self.msp.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle)
-        hatch = self.msp.add_hatch(color=self.color)
+        self.msp.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle,dxfattribs={"layer":f"图层{self.layer}"})
+        hatch = self.msp.add_hatch(color=self.color,dxfattribs={"layer":f"图层{self.layer}"})
         edge_path = hatch.paths.add_edge_path()
         edge_path.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle)
 

+ 31 - 8
drawer/TunDrawer.py

@@ -1,28 +1,51 @@
+import math
+
+import core
+
+
 class TunDrawer:
-    def __init__(self, msp, layer, color, vec12, vec34, from_point, to_point):
+    def __init__(self, msp, layer, vec12, vec34, from_point, to_point, text, width):
+        self.width = width
+        self.text = text
         self.msp = msp
         self.layer = layer
-        self.color = color
         self.vec12 = vec12
         self.vec34 = vec34
         self.from_point = from_point
         self.to_point = to_point
 
     def draw_tun(self):
+        color = core.get_color_by_layer(self.layer)
         for i in range(0, len(self.vec12), 2):
             pair = self.vec12[i:i + 2]
-            point1 = pair[0]["x"], pair[0]["y"]
-            point2 = pair[1]["x"], pair[1]["y"]
+            point1 = pair[0][0], pair[0][1]
+            point2 = pair[1][0], pair[1][1]
             # 取出每两个元素
+
             self.msp.add_line(point1, point2, dxfattribs={
-                'color': self.color
+                'color': color,
+                'layer': f'图层{self.layer}'
             })
 
         for i in range(0, len(self.vec34), 2):
             pair = self.vec34[i:i + 2]  # 取出每两个元素
-            point1 = pair[0]["x"], pair[0]["y"]
-            point2 = pair[1]["x"], pair[1]["y"]
+            point1 = pair[0][0], pair[0][1]
+            point2 = pair[1][0], pair[1][1]
+
             self.msp.add_line(point1, point2, dxfattribs={
-                'color': self.color
+                'color': color,
+                'layer': f'图层{self.layer}'
             })
 
+        self.draw_tun_text()
+
+    def draw_tun_text(self):
+        length = core.distance(self.from_point, self.to_point)
+        if length > 70:
+            route = core.calculate_route(self.from_point, self.to_point)
+            angle = math.degrees(route)
+            from_point, to_point = core.parallel_line(self.from_point, self.to_point, self.width)
+            tun_center = core.find_point_on_line(from_point, to_point, 1 / 2)
+            dxfattribs = {
+                'insert': tun_center, 'style': 'msyh', 'color': core.get_color_by_layer(self.layer), "layer": f"图层{self.layer}"}
+            self.msp.add_text(text=self.text, rotation=angle, dxfattribs=dxfattribs)

+ 9 - 10
drawer/WindBridgeDrawer.py

@@ -8,15 +8,14 @@ from drawer import WindFlowDrawer
 
 
 class WindBridgeDrawer:
-    def __init__(self, msp, center, tun1_id, tun2_id, layer_id, route, gap, color=0, is_cross=True):
+    def __init__(self, msp, center, layer_id, route, gap, color=0, is_cross=True):
         self.msp = msp
         self.gap = gap
         self.route = route
         self.is_cross = is_cross
         self.color = color
-        self.layer_id = layer_id
-        self.tun2_id = tun2_id
-        self.tun1_id = tun1_id
+        self.layer = layer_id
+
         self.center = center
 
     def draw_wind_bridge_drawer(self):
@@ -53,12 +52,12 @@ class WindBridgeDrawer:
         line_bottom_right = core.rotate_point_around_another(line_bottom_right, self.center, self.route)
         line_bottom_right_2 = core.rotate_point_around_another(line_bottom_right_2, self.center, self.route)
 
-        self.msp.add_line(line_top_left, line_top_left_2)
-        self.msp.add_line(line_top_right, line_top_left)
-        self.msp.add_line(line_top_right, line_top_right_2)
-        self.msp.add_line(line_bottom_left, line_bottom_right)
-        self.msp.add_line(line_bottom_right, line_bottom_right_2)
-        self.msp.add_line(line_bottom_left, line_bottom_left_2)
+        self.msp.add_line(line_top_left, line_top_left_2,dxfattribs={"layer":f"图层{self.layer}"})
+        self.msp.add_line(line_top_right, line_top_left,dxfattribs={"layer":f"图层{self.layer}"})
+        self.msp.add_line(line_top_right, line_top_right_2,dxfattribs={"layer":f"图层{self.layer}"})
+        self.msp.add_line(line_bottom_left, line_bottom_right,dxfattribs={"layer":f"图层{self.layer}"})
+        self.msp.add_line(line_bottom_right, line_bottom_right_2,dxfattribs={"layer":f"图层{self.layer}"})
+        self.msp.add_line(line_bottom_left, line_bottom_left_2,dxfattribs={"layer":f"图层{self.layer}"})
 
 
 if __name__ == '__main__':

+ 7 - 4
drawer/WindFlowDrawer.py

@@ -8,7 +8,8 @@ from core import rotate_point_around_another, calculate_route, calculate_angle_w
 
 
 class WindFlowDrawer:
-    def __init__(self, msp, gap, center, route, type):
+    def __init__(self, msp,layer, gap, center, route, type):
+        self.layer = layer
         self.msp = msp
         self.gap = gap
         self.center = center
@@ -20,7 +21,7 @@ class WindFlowDrawer:
         point_end = self.center[0] - line_length / 2, self.center[1]
         point_arrow = self.center[0] + line_length / 2, self.center[1]
 
-        point_text = self.center[0] - 0.1 * line_length, self.center[1] + 0.0725 * line_length
+        point_text = self.center[0] - 0.5 * line_length, self.center[1] + 0.1 * line_length
 
         arrow = ezdxf.ARROWS.ez_arrow_filled
         color = str(self.type)
@@ -30,17 +31,19 @@ class WindFlowDrawer:
 
         angle = math.degrees(self.route)
 
-        if self.type == 1:
+        if self.type == '1':
             dxfattribs = {
-                'insert': point_text, 'style': 'LiberationSerif', 'color': color}
+                'insert': point_text, 'style': 'LiberationSerif', 'color': color, "layer":f"图层{self.layer}"}
             self.msp.add_text(text="S", rotation=angle - 90, dxfattribs=dxfattribs)
 
         self.msp.add_line(point_end, point_arrow, {
             "color": color,
+            "layer":f"图层{self.layer}"
         })
 
         self.msp.add_arrow(arrow, point_arrow, 2, angle, {
             "color": color,
+            "layer": f"图层{self.layer}"
         })
 
 

+ 4 - 2
drawer/WindowDrawer.py

@@ -1,8 +1,10 @@
+import core
 from core import rotate_point_around_another
 
 
 class WindowDrawer:
-    def __init__(self, msp, gap, center,route):
+    def __init__(self, msp, gap,layer, center,route):
+        self.layer = layer
         self.msp = msp  # 假设 msp 是绘图空间的引用
         self.gap = gap  # 转换系数
         self.center = center
@@ -51,6 +53,6 @@ class WindowDrawer:
         ]
 
         for line in lines:
-            self.msp.add_line(line[0], line[1])
+            self.msp.add_line(line[0], line[1],dxfattribs={"layer":f"图层{self.layer}"})
 
         return self.msp

+ 56 - 76
main_2.py

@@ -1,4 +1,6 @@
 import json
+import math
+import sys
 import time
 
 import ezdxf
@@ -6,12 +8,17 @@ from tqdm import tqdm
 
 import core
 from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer
+from drawer.CADJson import CADJson
+from drawer.MeshTemplateDrawer import MeshTemplateDrawer
 from drawer.WindBridgeDrawer import WindBridgeDrawer
 
 import requests
 
 url = 'http://192.168.183.216:8008/python/tunCAD'
 
+global_route = - math.pi / 6
+global_scale = 0.6
+global_shift = -1000,3000
 
 def calculate_route_middle(wind_flow_unit):
     path_start = wind_flow_unit[0]['x'], wind_flow_unit[0]['z']
@@ -22,101 +29,67 @@ def calculate_route_middle(wind_flow_unit):
 
 
 if __name__ == '__main__':
-    response = requests.get(url)
-    print(f"请求耗时 {response.elapsed.total_seconds()} 秒")
-    cad_json = {}
-    if response.status_code == 200:
-        cad_json = response.json()  # 将响应内容解析为JSON格式
-        # print(json.dumps(result, indent=4))  # 使用json.dumps来美化打印结果
-    else:
-        print(f"请求失败,状态码:{response.status_code}")
-
-    # doc = ezdxf.readfile("data/moban.dxf", encoding="utf-8")
-    doc = ezdxf.new('R2000')
-    doc.styles.add("LiberationSerif", font="LiberationSerif.ttf")
+    # doc = ezdxf.new('R2000')
+
+    cad_json = CADJson("data/Cad.json")
+
+    tun_list = cad_json.tun_list
+    fan_list = cad_json.fan_list
+    window_list = cad_json.window_list
+    doc = ezdxf.readfile("data/moban.dxf")
+    doc.styles.add("msyh", font="data/msyh.ttc")
     msp = doc.modelspace()
 
-    # with open("data/Cad.json", 'r', encoding='utf-8') as r:
-    #     cad_json = json.loads(r.read())
+    with open("data/Cad.json", 'r', encoding='utf-8') as r:
+        cad_json = json.loads(r.read())
+    layer_map = cad_json["layerMap"]
+    for (k, v) in layer_map:
+        doc.layers.new(name=f'图层{k}')
+
     node_list = cad_json["tunsMapCAD"]
-    tun_list = []
+    # tun_list = []
     window_list = []
     gate_list = []
     fan_list = []
+
+
     for node in node_list:
         node = node[1]
-        tun = {
-            "vec12": node["vec12"],
-            "vec34": node["vec34"],
-            "tun_id": node["ntunid"],
-            "from": (node["nfrom"]["x"], node["nfrom"]["z"]),
-            "to": (node["nto"]["x"], node["nto"]["z"]),
-            "layer_id": node["nlayerid"],
-            "width": node["fwidth"],
-            "type": node["tunType"],
-            "angle": node["angleRad"],
-            "route": core.calculate_angle_with_x_axis((node["nfrom"]["x"], node["nfrom"]["z"]),
-                                                      (node["nto"]["x"], node["nto"]["z"]))
-        }
-        tun_list.append(tun)
-        for window in node["windows"]:
-            window["route"] = tun["route"]
-            window["gap"] = node["fwidth"]
-
-        window_list.extend(node["windows"])
-        for gate in node["gates"]:
-            gate["route"] = tun["route"]
-            gate["gap"] = node["fwidth"]
-        gate_list.extend(node["gates"])
-
-        for fan in node["fans"]:
-            fan["route"] = tun["route"]
-            fan["gap"] = node["fwidth"]
 
         fan_list.extend(node["fans"])
-
     for tun in tqdm(tun_list, desc=' 【巷道绘制中】'):
         if tun['type'] == '1':
             shaft_center = tun['from'][0], tun['from'][1]
-            sd = ShaftDrawer(msp, tun['width'], shaft_center, tun["route"], 42)
+            # shaft_center = core.rotate_point_around_another(shaft_center, (0, 0), global_route)
+            sd = ShaftDrawer(msp,tun['layer_id'], tun['width'], shaft_center, tun["route"] + global_route, 42)
             sd.draw_shaft_drawer()
         else:
-            vec12 = []
-            vec34 = []
-            for item in tun['vec12']:
-                vec12.append({
-                    "x": item["x"],
-                    "y": item["z"]
-                })
-            for item in tun['vec34']:
-                vec12.append({
-                    "x": item["x"],
-                    "y": item["z"]
-                })
-            color = core.get_color_by_layer(tun['layer_id'])
-
-            td = TunDrawer(msp, tun['layer_id'], color, vec12, vec34, tun["from"], tun["to"])
+            td = TunDrawer(msp, tun['layer_id'], tun["vec12"], tun["vec34"], tun["from"], tun["to"], tun["name"], global_route,tun["width"])
             td.draw_tun()
 
     for window in tqdm(window_list, desc=f' 【风窗绘制中】'):
         point_c = window['x'], window['z']
-        wd = WindowDrawer(msp, window["gap"], point_c, window["route"])
+        point_c = core.rotate_point_around_another(point_c, (0, 0), global_route)
+        wd = WindowDrawer(msp, window["gap"], window["nlayerid"],point_c, window["route"] + global_route)
         wd.draw_window()
 
-    for gate in tqdm(gate_list, desc=f' 【风绘制中】'):
+    for gate in tqdm(gate_list, desc=f' 【风门绘制中】'):
         point_c = gate['x'], gate['z']
-        gd = GateDrawer(msp, gate["gap"], point_c, gate["route"])
+        point_c = core.rotate_point_around_another(point_c, (0, 0), global_route)
+        gd = GateDrawer(msp,gate["nlayerid"], gate["gap"], point_c, gate["route"] + global_route)
         gd.draw_gate()
 
     for fan in tqdm(fan_list, desc=f' 【风扇绘制中】'):
         if 'fanmain' in str(fan['strtype']):
             point_c = fan['x'], fan['z']
-            fmd = FanMainDrawer(msp, fan["gap"], point_c, fan["route"])
-            fmd.draw_fan_main()
+            point_c = core.rotate_point_around_another(point_c, (0, 0), global_route)
+            # fmd = FanMainDrawer(msp, fan["nlayerid"],fan["gap"], point_c, fan["route"] + global_route)
+            # fmd.draw_fan_main()
         if 'fansystem' in str(fan['strtype']):
             point_c = fan['x'], fan['z']
-            fsd = FanSystemDrawer(msp, fan["gap"], point_c, fan["route"])
-            fsd.draw_fan_system()
+            point_c = core.rotate_point_around_another(point_c, (0, 0), global_route)
+            # fsd = FanSystemDrawer(msp, fan["gap"], point_c, fan["route"] + global_route)
+            # fsd.draw_fan_system()
 
     path_point_layers = cad_json["pathPointMap"]
 
@@ -126,25 +99,30 @@ if __name__ == '__main__':
         for in_path in tqdm(in_paths, desc=f'图层{path_point[0]} 【进风方向绘制中】'):
             if len(in_path) != 0:
                 path_middle, route = calculate_route_middle(in_path)
-                wfd = WindFlowDrawer(msp, 5, path_middle, route, 3)
+                path_middle = core.rotate_point_around_another(path_middle, (0, 0), global_route)
+                wfd = WindFlowDrawer(msp,path_point[0],5, path_middle, route + global_route, 3)
                 wfd.draw_wind_flow()
         no_paths = path_point[1]['noPaths']
         for no_path in tqdm(no_paths, desc=f'图层{path_point[0]} 【未指定方向绘制中】'):
             if len(no_path) != 0:
                 path_middle, route = calculate_route_middle(no_path)
-                wfd = WindFlowDrawer(msp, 5, path_middle, route, 3)
+                path_middle = core.rotate_point_around_another(path_middle, (0, 0), global_route)
+                wfd = WindFlowDrawer(msp,path_point[0], 5, path_middle, route + global_route, 3)
+
                 wfd.draw_wind_flow()
         out_paths = path_point[1]['outPaths']
         for out_path in tqdm(out_paths, desc=f'图层{path_point[0]} 【回风方向绘制中】'):
             if len(out_path) != 0:
                 path_middle, route = calculate_route_middle(out_path)
-                wfd = WindFlowDrawer(msp, 5, path_middle, route, 1)
+                path_middle = core.rotate_point_around_another(path_middle, (0, 0), global_route)
+                wfd = WindFlowDrawer(msp, path_point[0],5, path_middle, route + global_route, 1)
                 wfd.draw_wind_flow()
         use_paths = path_point[1]['usePaths']
         for use_path in tqdm(use_paths, desc=f'图层{path_point[0]} 【用风方向绘制中】'):
             if len(use_path) != 0:
                 path_middle, route = calculate_route_middle(use_path)
-                wfd = WindFlowDrawer(msp, 5, path_middle, route, 1)
+                path_middle = core.rotate_point_around_another(path_middle, (0, 0), global_route)
+                wfd = WindFlowDrawer(msp,path_point[0], 5, path_middle, route + global_route, 1)
 
     layer_map = cad_json["layerMap"]
 
@@ -154,12 +132,14 @@ if __name__ == '__main__':
             center = cross_node['crossPoint']['x'], -cross_node['crossPoint']['y']
             tun = [tun_ for tun_ in tun_list if tun_.get("tun_id") == cross_node['tun1Id']][0]
             route = core.calculate_angle_with_x_axis(tun["from"], tun["to"])
-            line_1 = (tun["vec12"][0]["x"], tun["vec12"][0]["z"]), (tun["vec12"][-1]["x"], tun["vec12"][-1]["z"])
-            line_2 = (tun["vec34"][0]["x"], tun["vec34"][0]["z"]), (tun["vec34"][-1]["x"], tun["vec34"][-1]["z"])
-            gap = core.min_distance_between_segments(line_1,
-                                                     line_2)
-            wbd = WindBridgeDrawer(msp, center, cross_node['tun1Id'], cross_node['tun2Id'], layer[0], tun["angle"], gap)
-            wbd.draw_wind_bridge_drawer()
+            # line_1 = (tun["vec12"][0]["x"], tun["vec12"][0]["z"]), (tun["vec12"][-1]["x"], tun["vec12"][-1]["z"])
+            # line_2 = (tun["vec34"][0]["x"], tun["vec34"][0]["z"]), (tun["vec34"][-1]["x"], tun["vec34"][-1]["z"])
+            # gap = core.min_distance_between_segments(line_1,
+            #                                          line_2)
+            # center = core.rotate_point_around_another(center, (0, 0), global_route)
+            # wbd = WindBridgeDrawer(msp, center, cross_node['tun1Id'], cross_node['tun2Id'], layer[0],
+            #                        tun["angle"] + global_route, gap)
+            # wbd.draw_wind_bridge_drawer()
 
     a = time.time()
     doc.saveas(f'save/tuns{str(a)}.dxf')

+ 82 - 0
main_3.py

@@ -0,0 +1,82 @@
+import json
+import math
+import sys
+import time
+
+import ezdxf
+from tqdm import tqdm
+
+import core
+from drawer import ShaftDrawer, TunDrawer, WindowDrawer, GateDrawer, FanMainDrawer, FanSystemDrawer, WindFlowDrawer
+from drawer.CADJson import CADJson
+from drawer.MeshTemplateDrawer import MeshTemplateDrawer
+from drawer.WindBridgeDrawer import WindBridgeDrawer
+
+import requests
+
+if __name__ == '__main__':
+    # doc = ezdxf.new('R2000')
+
+    cad_json = CADJson("data/Cad.json")
+
+    # tun_list = cad_json.tun_list
+    # fan_list = cad_json.fan_list
+    # window_list = cad_json.window_list
+
+    doc = ezdxf.readfile("data/moban.dxf")
+    doc.styles.add("msyh", font="data/msyh.ttc")
+    for layer in cad_json.json['layerMap']:
+        doc.layers.new(name=f'图层{layer[0]}')
+
+    msp = doc.modelspace()
+
+    for tun in tqdm(cad_json.tun_list, desc=' 【巷道绘制中】'):
+        if tun['type'] == '1':
+            shaft_center = tun['from'][0], tun['from'][1]
+            sd = ShaftDrawer(msp, tun['layer_id'], tun['width'], shaft_center, 0)
+            sd.draw_shaft_drawer()
+        else:
+            td = TunDrawer(msp, tun['layer_id'], tun["vec12"], tun["vec34"], tun["from"], tun["to"], tun["name"],
+                           tun["width"])
+            td.draw_tun()
+
+    for window in tqdm(cad_json.window_list, desc=f' 【风窗绘制中】'):
+        wd = WindowDrawer(msp, window["width"], window["layer"], window["center"], window["route"])
+        wd.draw_window()
+
+    for gate in tqdm(cad_json.gate_list, desc=f' 【风门绘制中】'):
+        gd = GateDrawer(msp, gate["layer"], gate["width"], gate['center'], gate["route"])
+        gd.draw_gate()
+
+    for fan in tqdm(cad_json.fan_list, desc=f' 【风扇绘制中】'):
+        if 'fanmain' in str(fan['type']):
+            fmd = FanMainDrawer(msp, fan["layer"], fan["width"], fan['center'], fan["route"])
+            fmd.draw_fan_main()
+        if 'fansystem' in str(fan['type']):
+            fsd = FanSystemDrawer(msp, fan["width"], fan['center'], fan["route"])
+            fsd.draw_fan_system()
+    wind_flow_list = cad_json.wind_flow_list
+    for in_path in tqdm(wind_flow_list['in'], desc=f' 【进风方向绘制中】'):
+        if len(in_path) != 0:
+            wfd = WindFlowDrawer(msp, in_path['layer'], 3, in_path['center'], in_path['route'], in_path['type'])
+            wfd.draw_wind_flow()
+    for no_path in tqdm(wind_flow_list['no'], desc=f'【未指定方向绘制中】'):
+        if len(no_path) != 0:
+            wfd = WindFlowDrawer(msp, no_path['layer'], 3, no_path['center'], no_path['route'], no_path['type'])
+            wfd.draw_wind_flow()
+    for out_path in tqdm(wind_flow_list['out'], desc=f'【回风方向绘制中】'):
+        if len(out_path) != 0:
+            wfd = WindFlowDrawer(msp, out_path['layer'], 3, out_path['center'], out_path['route'], out_path['type'])
+            wfd.draw_wind_flow()
+    for use_path in tqdm(wind_flow_list['use'], desc=f' 【用风方向绘制中】'):
+        if len(use_path) != 0:
+            wfd = WindFlowDrawer(msp, use_path['layer'], 3, use_path['center'], use_path['route'], use_path['type'])
+            wfd.draw_wind_flow()
+
+    for wind_bridge in tqdm(cad_json.wind_bridge_list, desc=f' 【风桥绘制中】'):
+
+        wbd = WindBridgeDrawer(msp, wind_bridge['center'], wind_bridge['layer'],
+                               wind_bridge['route'], wind_bridge['width'])
+        wbd.draw_wind_bridge_drawer()
+    a = time.time()
+    doc.saveas(f'save/tuns{str(a)}.dxf')