duct_drawer.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import math
  2. from ezdxf.enums import TextEntityAlignment
  3. import core
  4. from drawer.BaseDrawer import BaseDrawer, VentGraphDrawer
  5. from entity.primitives import Window, AirDuct
  6. class DuctDrawer(BaseDrawer):
  7. WIDTH_MULTIPLIER = 1.5
  8. HEIGHT_MULTIPLIER = 1 / 6
  9. def __init__(self, obj, msp, style):
  10. super().__init__(obj, msp, style)
  11. if self.style == 1:
  12. self.obj.color = (0, 255, 255)
  13. self.line_height = 50
  14. if self.style == 0:
  15. self.line_height = 0
  16. self.duct_drawers = {
  17. 0: self.draw_duct_style_0,
  18. 1: self.draw_duct_style_1,
  19. }
  20. def draw_duct_style_0(self, duct_type):
  21. """
  22. 绘制风管路径(支持聚合线和分散线)
  23. :param duct_type: 绘制类型,可选 'agg'(聚合线)或 'div'(分散线)
  24. """
  25. assert isinstance(self.obj, AirDuct)
  26. assert duct_type in {'agg', 'div'}, "Invalid duct type. Must be 'agg' or 'div'."
  27. # 根据类型选择对应的线段属性
  28. line_attr = 'agg_line' if duct_type == 'agg' else 'div_line'
  29. # 遍历路径列表并绘制
  30. for i in range(len(self.obj.path_list)):
  31. path = self.obj.path_list[i]
  32. start_point, end_point = getattr(path, line_attr) # 获取起点和终点
  33. self.draw_air_duct_list_0(start_point, end_point) # 绘制当前线段
  34. # 如果不是最后一段,绘制连接线
  35. if i + 1 < len(self.obj.path_list):
  36. next_start_point = getattr(self.obj.path_list[i + 1], line_attr)[0]
  37. connection_line = self.msp.add_line(end_point, next_start_point)
  38. connection_line.rgb = self.obj.color # 设置连接线颜色
  39. def draw_duct_style_1(self, duct_type):
  40. assert isinstance(self.obj, AirDuct)
  41. assert duct_type in {'agg', 'div'}, "Invalid duct type. Must be 'agg' or 'div'."
  42. # 根据类型选择对应的线段属性
  43. line_attr = 'agg_line' if duct_type == 'agg' else 'div_line'
  44. # 遍历路径列表并绘制
  45. for i in range(len(self.obj.path_list)):
  46. path = self.obj.path_list[i]
  47. start_point, end_point = getattr(path, line_attr) # 获取起点和终点
  48. self.draw_air_duct_list_2(start_point, end_point) # 绘制当前线段
  49. # 如果不是最后一段,绘制连接线
  50. if i + 1 < len(self.obj.path_list):
  51. next_start_point = getattr(self.obj.path_list[i + 1], line_attr)[0]
  52. connection_line = self.msp.add_line(end_point, next_start_point,
  53. dxfattribs={"layer": f"图层{self.obj.layer_id}", "lineweight": 50})
  54. connection_line.rgb = self.obj.color # 设置连接线颜色
  55. def draw_agg(self):
  56. self.duct_drawers[self.style]('agg')
  57. def draw_div(self):
  58. self.duct_drawers[self.style]('div')
  59. def initialize_data(self):
  60. pass
  61. # d1 a1 b1
  62. # c1|----------c2|F|c3
  63. # d2 center a2 b2
  64. def draw_air_duct_list_0(self, from_, to_):
  65. distance = core.distance(from_, to_)
  66. ad_len = self.get_draw_air_duct_len()
  67. ad_height = self.obj.width * 0.3
  68. ad_width = self.obj.width * 3
  69. # if from_[0] < to_[0]:
  70. # route_ = core.calculate_angle_with_x_axis(from_, to_)
  71. # else:
  72. # route_ = core.calculate_angle_with_x_axis(to_, from_)
  73. #
  74. route_ = core.calculate_angle_with_x_axis(from_, to_)
  75. seg_from = from_
  76. while distance > ad_len:
  77. seg_to = core.find_point_on_segment(seg_from, to_, ad_len)
  78. center_ = core.find_point_on_line(seg_from, seg_to, 1 / 8)
  79. self.draw_single_duct_0(center_, route_, ad_width, ad_height)
  80. seg_from = seg_to
  81. distance = distance - ad_len
  82. self.draw_single_duct_0(seg_from, route_, distance, ad_height)
  83. def draw_air_duct_list_2(self, from_, to_):
  84. distance = core.distance(from_, to_)
  85. ad_len = self.get_draw_air_duct_len()
  86. ad_height = self.obj.width * 0.3
  87. ad_width = self.obj.width * 3
  88. route_ = core.calculate_angle_with_x_axis(from_, to_)
  89. seg_from = from_
  90. while distance > ad_len:
  91. seg_to = core.find_point_on_segment(seg_from, to_, ad_len)
  92. self.draw_single_duct_1(seg_from, route_, ad_width, ad_height)
  93. seg_from = seg_to
  94. distance = distance - ad_len
  95. self.draw_single_duct_1(seg_from, route_, distance, ad_height)
  96. def draw_line(self, start, end):
  97. dxfattribs = {"layer": f"图层{self.obj.layer_id}", "lineweight": self.line_height}
  98. line = self.msp.add_line(start, end, dxfattribs=dxfattribs)
  99. line.rgb = self.obj.color
  100. return line
  101. def draw_air_duct(self, center, route):
  102. c1 = center[0] - self.obj.width * self.WIDTH_MULTIPLIER, center[1]
  103. c2 = center[0] + self.obj.width, center[1]
  104. c1 = core.rotate_point_around_another(c1, center, route)
  105. c2 = core.rotate_point_around_another(c2, center, route)
  106. line = self.msp.add_line(c1, c2,
  107. dxfattribs={"layer": f"图层{self.obj.layer_id}"})
  108. line.rgb = self.obj.color
  109. d1 = center[0] - self.obj.width * 1.5, center[1] + self.obj.width * 1 / 6
  110. d2 = center[0] - self.obj.width * 1.5, center[1] - self.obj.width * 1 / 6
  111. d1 = core.rotate_point_around_another(d1, center, route)
  112. d2 = core.rotate_point_around_another(d2, center, route)
  113. line = self.msp.add_line(d1, d2,
  114. dxfattribs={"layer": f"图层{self.obj.layer_id}"})
  115. line.rgb = self.obj.color
  116. a1 = center[0] + self.obj.width * 1, center[1] + self.obj.width * 1 / 6
  117. a2 = center[0] + self.obj.width * 1, center[1] - self.obj.width * 1 / 6
  118. a1 = core.rotate_point_around_another(a1, center, route)
  119. a2 = core.rotate_point_around_another(a2, center, route)
  120. line = self.msp.add_line(a1, a2,
  121. dxfattribs={"layer": f"图层{self.obj.layer_id}",
  122. })
  123. line.rgb = self.obj.color
  124. f = center[0] + self.obj.width * 1.25, center[1]
  125. f = core.rotate_point_around_another(f, center, route)
  126. if route < 0: route = route + math.pi
  127. angle = math.degrees(route)
  128. dxfattribs = {
  129. 'insert': f, 'style': 'msyh',
  130. "layer": f"图层{self.obj.layer_id}",
  131. }
  132. text = self.msp.add_text("F", rotation=angle, dxfattribs=dxfattribs, height=self.obj.width / 6).set_placement(f,
  133. align=TextEntityAlignment.MIDDLE_CENTER)
  134. text.rgb = self.obj.color
  135. b1 = center[0] + self.obj.width * 1.5, center[1] + self.obj.width * 1 / 6
  136. b2 = center[0] + self.obj.width * 1.5, center[1] - self.obj.width * 1 / 6
  137. b1 = core.rotate_point_around_another(b1, center, route)
  138. b2 = core.rotate_point_around_another(b2, center, route)
  139. line = self.msp.add_line(b1, b2,
  140. dxfattribs={"layer": f"图层{self.obj.layer_id}"})
  141. line.rgb = self.obj.color
  142. def get_draw_air_duct_len(self):
  143. return self.obj.width * 3.5
  144. # a1 a3
  145. # c1|---|c2
  146. # a2 a4
  147. def draw_single_duct_1(self, center, route, ad_len, ad_height):
  148. c1 = center
  149. c2 = center[0] + ad_len, center[1]
  150. a1 = center[0], center[1] + ad_height
  151. a2 = center[0], center[1] - ad_height
  152. a3 = c2[0], c2[1] + ad_height
  153. a4 = c2[0], c2[1] - ad_height
  154. c2 = core.rotate_point_around_another(c2, center, route)
  155. a1 = core.rotate_point_around_another(a1, center, route)
  156. a2 = core.rotate_point_around_another(a2, center, route)
  157. a3 = core.rotate_point_around_another(a3, center, route)
  158. a4 = core.rotate_point_around_another(a4, center, route)
  159. self.draw_line(c1, c2)
  160. self.draw_line(a1, a2)
  161. self.draw_line(a3, a4)
  162. # a1a3
  163. # |F|c1---c2
  164. # a2a4
  165. def draw_single_duct_0(self, center, route, ad_len, ad_height):
  166. f_p = center
  167. angle = math.degrees(route)
  168. if 180 < angle: angle = angle + 180
  169. dxfattribs = {
  170. 'insert': f_p, 'style': 'msyh',
  171. "layer": f"图层{self.obj.layer_id}",
  172. }
  173. text = self.msp.add_text("F", rotation=angle, dxfattribs=dxfattribs, height=self.obj.width / 6).set_placement(
  174. f_p,
  175. align=TextEntityAlignment.MIDDLE_CENTER)
  176. text.rgb = self.obj.color
  177. c1 = center[0] + 1 / 8 * ad_len, center[1]
  178. c2 = center[0] + ad_len, center[1]
  179. a1 = center[0] - 1 / 8 * ad_len, center[1] + ad_height
  180. a2 = center[0] - 1 / 8 * ad_len, center[1] - ad_height
  181. a3 = center[0] + 1 / 8 * ad_len, c2[1] + ad_height
  182. a4 = center[0] + 1 / 8 * ad_len, c2[1] - ad_height
  183. c1 = core.rotate_point_around_another(c1, center, route)
  184. c2 = core.rotate_point_around_another(c2, center, route)
  185. a1 = core.rotate_point_around_another(a1, center, route)
  186. a2 = core.rotate_point_around_another(a2, center, route)
  187. a3 = core.rotate_point_around_another(a3, center, route)
  188. a4 = core.rotate_point_around_another(a4, center, route)
  189. self.draw_line(c1, c2)
  190. self.draw_line(a1, a2)
  191. self.draw_line(a3, a4)