Search code examples
pythondxf

How to write mirrored text with 'ezdxf'?


I need to write text in DXF drawing mirrored. I use python and ezdxf module. According to docs there are some flags to be set but I always get DXFAttributeError.

I try to use 'text_generation_flags': 2 and 'text_direction': (-1, 0, 0)

here is my code (works well without mirroring attempst)


def publish_face_no_bolts(poly, label, filename):

    t = poly.get_default_transformation()
    trans_poly = poly.transform(t)
    # trans_poly = trans_poly.make_coordinates_positive()
    points = transformation.points_3d_to_2d(trans_poly.poly_points)
    points.append(points[0]) # must close polygon

    drawing = ezdxf.new(dxfversion='AC1024')  # or use the AutoCAD release name ezdxf.new(dxfversion='R2010')
    modelspace = drawing.modelspace()

    modelspace.add_lwpolyline(points, dxfattribs={'color': 7})

    drawing.layers.new('TEXTLAYER', dxfattribs={'color': 1})

    # use set_pos() for proper TEXT alignment - the relations between halign, valign, insert and align_point are tricky.


    # drawing.styles.new('mirrored', dxfattribs={'text_generation_flags': 2})
    # 'text_direction': (0, 1, 0),  # write in y direction
    drawing.styles.new('mirrored', dxfattribs={ 'text_direction': (-1, 0, 0)})

    err, cx, cy = polygon.centroid2d(points)

    modelspace.add_text(label, dxfattribs={'layer': 'TEXTLAYER', 'text_direction': (-1, 0, 0), 'height': 4}).set_pos((cx, cy), align='CENTER')
    drawing.saveas(filename)

Which flag to use and how to set it in a proper way?


Solution

  • I've not used ezdxf, but text_direction is a property of an MTEXT entity (DXF group 11), and is another way to effectively control the rotation of the MText.

    To mirror a single-line TEXT entity, you'll want to set DXF group 71 to 2, which, after briefly looking over the code for ezdxf, looks to be implemented as as the text_generation_flag parameter.

    Hence, I would suggest:

    modelspace.add_text(label, dxfattribs={'layer': 'TEXTLAYER', 'text_generation_flag': 2, 'height': 4}).set_pos((cx, cy), align='CENTER')