Is there no way to delete an empty layer from the layer list? I want to do the same thing as the autocad purge command.
I tried to write the code but it didn't work.
del_lay = []
for layer in dwg.layers:
s = layer.dxf.name
lay_= re.search(layer.dxf.name, s)
if lay_:
L = lay_.group()
del_lay.append(L)
del_lay.remove("0") #0 layer cannot be deleted, so remove it from the list
for Lay in del_lay:
all_entities = dwg.modelspace().query('*[layer=="%s"]' % Lay)
print(all_entities)
for entity in all_entities: #If there is no entity in the layer
if entity not in all_entities:
delete_name = layer.dxf.name
my_lines = dwg.layers.get(delete_name)
dwg.layers.remove(my_lines)
When I check it myself, there is a layer where entity does not exist, but it is not executed.
NameError: name 'delete_name' is not defined