Search code examples
pythonezdxf

Extract text from dxf that is not TEXT or MTEXT entity using ezdxf


I am trying to get numbers from a specific dxf file using ezdxf.

However, a query on the modelspace for msp.query('TEXT') or msp.query('MTEXT') yields no results, if I iterate over all entities, I can see they are all of type INSERT.

In the dxf file, I can see numbers next to a contour for every insert, and I can copy one insert into a new file, which results in the contour and the number being copied. However, I can not figure out where the information on the number is stored. This is what the dxf file looks like viewed in LibreCAD: dxf file viewed in LibreCAD

Link to dxf file on google drive

Does anyone know where in the INSERT type entity the number might be stored? It is not in the entity.dxf.name, which yields a completely different string.


Solution

  • This is an unusual way to implement block reference attributes and does not use the ATTIB entity:

    Use the ezdxf browse <file.dxf> command line tool to examine the DXF content.

    enter image description here

    going to the block definitions, each block reference has it's own block definition:

    enter image description here

    This script extracts the TEXT entities from the block definitions:

    enter image description here

    import ezdxf
    
    doc = ezdxf.readfile("auflager.dxf")
    msp = doc.modelspace()
    for insert in msp.query("INSERT"):
        print(str(insert))
        for attrib in insert.attribs:
            # there are no ATTRIB entities:
            print("  ATTRIB: " + str(attrib.dxf.text))
        # each block reference has it's own block definition
        block = insert.block()
        if block is None:
            continue
    
        text = block.query("TEXT").first
        if text:
            print("  TEXT: " + str(text.dxf.text))
    
    

    Output:

    INSERT(#14E)
      TEXT: 1
    INSERT(#14F)
      TEXT: 1
    INSERT(#150)
      TEXT: 2
    INSERT(#151)
      TEXT: 3
    INSERT(#152)
      TEXT: 4
    ...
    

    EDIT:

    This is another tool to examine DXF files: https://alek-tron.com/DxfExplorer/

    It runs in the browser and doesn't need Python nor ezdxf. The processing is done locally on your computer - no upload to a cloud service.