TunTextDrawer.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import math
  2. from ezdxf.enums import TextEntityAlignment
  3. import core
  4. class TunTextDrawer:
  5. def __init__(self, msp, layer, tun_line, text, width=5):
  6. self.msp = msp
  7. self.layer = layer
  8. self.tun_line = tun_line
  9. self.text = text
  10. self.width = width
  11. def dra_text(self):
  12. length = core.distance(self.tun_line[0], self.tun_line[1])
  13. text_len = len(self.text) * 6.5
  14. if length * 0.8 > text_len:
  15. if self.tun_line[1][0] > self.tun_line[0][0]:
  16. route = core.calculate_angle_with_x_axis(self.tun_line[0], self.tun_line[1])
  17. else:
  18. route = core.calculate_angle_with_x_axis(self.tun_line[1], self.tun_line[0])
  19. # self.msp.add_circle(center=self.tun_line[0], radius=5, dxfattribs={"color": "1"})
  20. # self.msp.add_circle(center=self.tun_line[1], radius=5, dxfattribs={"color": "2"})
  21. angle = math.degrees(route)
  22. from_point, to_point = core.parallel_line(self.tun_line[0], self.tun_line[1], 1.7 * self.width)
  23. tun_center = core.find_point_on_line(from_point, to_point, 1 / 2)
  24. dxfattribs = {
  25. 'insert': tun_center, 'style': 'msyh', 'color': core.get_color_by_layer(self.layer),
  26. "layer": f"图层{self.layer}"}
  27. self.msp.add_text(text=self.text, rotation=angle, dxfattribs=dxfattribs, height=5).set_placement(
  28. tun_center, align=TextEntityAlignment.CENTER)
  29. # self.msp.add_line(self.tun_line[0], self.tun_line[1])
  30. # self.msp.add_circle(center=tun_center, radius=5, dxfattribs={"color": "3"})