0

I would like to know on which paperspace each entity in a DXF file would be located. I tried to find this by the code below, but the layout name I got is always the same one for each entity, while there are multiple number of paperspace in the DXF file. How can I get the paperspace for each entity in a DXF file?

# Read a DXF file
doc = ezdxf.readfile(file_path, encoding='shift-jis')

# Print names of Paperspaces
print(doc.layout_names_in_taborder())
# -> ['Model', '0A_Cover', '0B_Number', '0C_List', '01', '01H', '02', '02H', '03', '03H', '04', '04H', '05', '05H', '06', '06H', '07', '07H']

# Print the name of paperspace for each entity in the DXF file 
for e in doc.entities:
    # Paperspace Name
    print(e.drawing.layout().name)

# -> 0A_Cover
#    0A_Cover
#    0A_Cover
#    0A_Cover
#    0A_Cover
#    ...
#    ...
# The paperspace name is always '0A_Cover' for all entities.

enter image description here

isaku
  • 1
  • 1
  • Your question is a bit confusing, Paper Space will will have a VIEWPORT to the model space. In that viewport you will have a window to your model space entities. – Andrew Truckle May 23 '20 at 06:44
  • Each paper space layout can also have it's own entities (like a title block for example). Maybe show us a screen shot on the context of your issue. – Andrew Truckle May 23 '20 at 06:45
  • Hi Andrew, thank you for your reply. For example, one DXF file have three sheest which are named 'model', 'layout1' and 'layout3'. Suppose 'layout1' has two triangles and 'layout2' has three circles respectively. If I need to know what entities are on 'layout2', how can I get the information of those entities by EZDXF library, whose result should be info about three circles? – isaku May 25 '20 at 06:40
  • Hi, Andrew. Please refer to the screen shots I attached above. Regards, isaku – isaku May 25 '20 at 06:43

1 Answers1

2

Get the layout you want by the name as shown in tabs:

layout = doc.layout('0A_Cover')

Iterate over entitites of this layout:

for e in layout:
    print(str(e))

or use an entity query:

lines = layout.query('LINE')
for line in lines:
    print(str(line))

doc.entities contains only the entities of the model space and the actual active paper space also known as the ENTITIES section of the DXF file.

To iterate over all entities in layouts (including 'Model') use:

for name in doc.layout_names_in_taborder():
    for e in doc.layout(name):
        print(str(e))

To iterate over all entities in layouts and blocks use:

for e in doc.chain_layouts_and_blocks():
    print(str(e))

Iterate over all DXF entities of a DXF document use the entity database:

for e in doc.entitydb.values():
    print(str(e))

mozman
  • 2,001
  • 8
  • 23
  • I tried some of your codes and see almost all of entities belog to 'Model' layout. For example, I see all of LINEs are on 'Model' layout and no LINEs on any paperspace layout. But as far as I see the DXF file by TrueView, there are many LINEs on '01' sheet and '01H' sheet. How can I know LINE entities on '01' or'01H' sheet? doc = ezdxf.readfile(file_path, encoding='shift-jis') for l in doc.layout_names_in_taborder(): print(l) lines = doc.layout(l).query('LINE') print(len(lines), '\n') – isaku May 15 '20 at 09:37
  • Results are as follows: Model 5472 0A_Cover 0 01 0 01H 0 – isaku May 15 '20 at 09:38
  • Maybe the LINES on paperspace are located in a block reference. – mozman May 15 '20 at 12:38