Search code examples
python-3.xautocaddxfezdxf

Is it possible to use ezdxf to locate text on an existing layer?


I want to get multiple TEXT or MTEXT location information on a layer, but I don't know how.

Please give some advice.


Solution

  • Tutorials and examples demonstrating how to accomplish this may be found in the excellent documentation for .

    For example, to obtain the set of all single-line text (TEXT) and multiline text (MTEXT) residing on a specific layer in Modelspace, you might use:

    msp = doc.modelspace()
    textset = msp.query('TEXT MTEXT[layer=="YourLayerHere"]')
    

    After obtaining this set, you can then iterate over the entities and query the insertion point:

    for e in msp.query('TEXT MTEXT[layer=="YourLayerHere"]'):
        if e.dxftype() == 'MTEXT' or (e.dxf.valign == 0 and e.dxf.halign in [0,3,5]):
            print("Position: %s\n" % e.dxf.insert)
        else:
            print("Position: %s\n" % e.dxf.align_point)