import math from ezdxf.enums import TextEntityAlignment import core class TunTextDrawer: def __init__(self, msp, layer, tun_line, text, width=5): self.msp = msp self.layer = layer self.tun_line = tun_line self.text = text self.width = width def dra_text(self): length = core.distance(self.tun_line[0], self.tun_line[1]) text_len = len(self.text) * 6.5 if length * 0.8 > text_len: if self.tun_line[1][0] > self.tun_line[0][0]: route = core.calculate_angle_with_x_axis(self.tun_line[0], self.tun_line[1]) else: route = core.calculate_angle_with_x_axis(self.tun_line[1], self.tun_line[0]) # self.msp.add_circle(center=self.tun_line[0], radius=5, dxfattribs={"color": "1"}) # self.msp.add_circle(center=self.tun_line[1], radius=5, dxfattribs={"color": "2"}) angle = math.degrees(route) from_point, to_point = core.parallel_line(self.tun_line[0], self.tun_line[1], 1.7 * 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, height=5).set_placement( tun_center, align=TextEntityAlignment.CENTER) # self.msp.add_line(self.tun_line[0], self.tun_line[1]) # self.msp.add_circle(center=tun_center, radius=5, dxfattribs={"color": "3"})