1

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

Please give some advice.

Lee Mac
  • 15,615
  • 6
  • 32
  • 80
leez
  • 85
  • 8

1 Answers1

2

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)
Lee Mac
  • 15,615
  • 6
  • 32
  • 80
  • To select multiple entity types use: "TEXT MTEXT[layer==...]" – mozman Dec 06 '19 at 16:28
  • @mozman I initially thought that the query language string may have been implemented with similar logic to an AutoLISP `ssget` filter list, wherein the list has an implicit `and` logic, hence `TEXT and MTEXT` would never obtain any results since the entity type cannot be both. Are you saying that ezdxf implements an implicit inclusive `or` logic, such that `TEXT MTEXT` returns either `TEXT` or `MTEXT` (or both)? Also, is `*TEXT` valid to match both? – Lee Mac Dec 06 '19 at 22:49
  • The entity query is implemented as implicit `OR` operation and returns all listed entity types. The `*` is a special name, not a wildcard, and stands for ALL entity types, because I thought wildcards for entity types make no sense. `"* TEXT"` is valid, but returns all entity types, and `"*TEXT"` returns nothing, because searches for literally `*TEXT`. But I added an exclude feature in v0.10: `"* !LINE"`, returns all entity types except `LINE`, which may be useful. – mozman Dec 07 '19 at 11:01
  • @Lee_Mac - testing mention Lee Mac, does this work? `@LeeMac` disappears. – mozman Dec 07 '19 at 11:21
  • @mozman Thank you for the clarification - I have updated my answer above. I would have thought it would need `'TEXT[layer=="YourLayerHere"] MTEXT[layer=="YourLayerHere"]'` otherwise I would have assumed that it would obtain `TEXT` on *any* layer and `MTEXT` on the specified layer. Regarding the mention: since the post owner is always notified, the mention becomes redundant and so disappears automatically. – Lee Mac Dec 07 '19 at 11:52
  • Thanks for your advice. I tried to get location information. By the way, can I get location information along with text contents? – leez Dec 10 '19 at 07:29
  • @leez To obtain the content, use the `text` property, as described [here](https://ezdxf.readthedocs.io/en/latest/dxfentities/text.html#ezdxf.entities.Text.dxf.text) & [here](https://ezdxf.readthedocs.io/en/latest/dxfentities/mtext.html#ezdxf.entities.MText.text). – Lee Mac Dec 10 '19 at 13:18