TunDrawer.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. def draw_tun_text(self):
  34. length = core.distance(self.from_point, self.to_point)
  35. if length > 70:
  36. route = core.calculate_route(self.from_point, self.to_point)
  37. angle = math.degrees(route)
  38. from_point, to_point = core.parallel_line(self.from_point, self.to_point, 1.5*self.width)
  39. tun_center = core.find_point_on_line(from_point, to_point, 1 / 2)
  40. dxfattribs = {
  41. 'insert': tun_center, 'style': 'msyh', 'color': core.get_color_by_layer(self.layer), "layer": f"图层{self.layer}"}
  42. self.msp.add_text(text=self.text, rotation=angle, dxfattribs=dxfattribs)