TunDrawer.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import math
  2. import core
  3. class TunDrawer:
  4. def __init__(self, msp, layer, vec12, vec34, from_point, to_point, text, width):
  5. self.width = width
  6. self.text = text
  7. self.msp = msp
  8. self.layer = layer
  9. self.vec12 = vec12
  10. self.vec34 = vec34
  11. self.from_point = from_point
  12. self.to_point = to_point
  13. def draw_tun(self):
  14. color = core.get_color_by_layer(self.layer)
  15. for i in range(0, len(self.vec12), 2):
  16. pair = self.vec12[i:i + 2]
  17. point1 = pair[0][0], pair[0][1]
  18. point2 = pair[1][0], pair[1][1]
  19. # 取出每两个元素
  20. self.msp.add_line(point1, point2, dxfattribs={
  21. 'color': color,
  22. 'layer': f'图层{self.layer}'
  23. })
  24. for i in range(0, len(self.vec34), 2):
  25. pair = self.vec34[i:i + 2] # 取出每两个元素
  26. point1 = pair[0][0], pair[0][1]
  27. point2 = pair[1][0], pair[1][1]
  28. self.msp.add_line(point1, point2, dxfattribs={
  29. 'color': color,
  30. 'layer': f'图层{self.layer}'
  31. })
  32. self.draw_tun_text()
  33. # self.draw_middle_line()
  34. def draw_tun_text(self):
  35. length = core.distance(self.from_point, self.to_point)
  36. if length > 70:
  37. route = core.calculate_route(self.from_point, self.to_point)
  38. angle = math.degrees(route)
  39. from_point, to_point = core.parallel_line(self.from_point, self.to_point, 1.5*self.width)
  40. tun_center = core.find_point_on_line(from_point, to_point, 1 / 2)
  41. dxfattribs = {
  42. 'insert': tun_center, 'style': 'msyh', 'color': core.get_color_by_layer(self.layer), "layer": f"图层{self.layer}"}
  43. self.msp.add_text(text=self.text, rotation=angle, dxfattribs=dxfattribs)
  44. def draw_middle_line(self):
  45. self.msp.add_line(self.from_point, self.to_point, dxfattribs={
  46. 'color': '1',
  47. 'layer': f'图层{self.layer}'
  48. })