I am trying to add a prefix to the layer names in a .dxf
file. Here is my code:
import ezdxf
doc = ezdxf.readfile('infile.dxf')
layer_names = [layer.dxf.name for layer in doc.layers]
for old_name in layer_names:
try:
layer = doc.layers.get(old_name)
new_name = 'MY_PREFIX_' + old_name
layer.rename(new_name)
print(f'Layer "{old_name}" renamed to "{new_name}".')
except ValueError as exc:
print(exc)
doc.saveas('outfile.dxf')
The layer list of infile.dxf
looks like this:
When I run the snippet above I get this output:
Can not rename layer "0".
Layer "Layer1" renamed to "MY_PREFIX_Layer1".
Layer "Layer2" renamed to "MY_PREFIX_Layer2".
Layer "Layer3" renamed to "MY_PREFIX_Layer3".
Layer "Layer4" renamed to "MY_PREFIX_Layer4".
Can not rename layer "Defpoints".
Apparently the code works fine, but when I open outfile.dxf
with AutoCAD, the list of layers includes two unwanted layers:
Defpoints
(I can live with this).Layer1
, which is empty. Please note that Layer1
was the current layer when I saved infile.dxf
from the AutoCAD application.How can I avoid that these two layers show up in the list of layers of the Layer Property Manager?
The current layer is set to the old layer name "Layer1" and AutoCAD creates a new layer "Layer1", BricsCAD doesn't do that.
Rename the current layer to "0":
doc.header["$CLAYER"] = "0"
The "Defpoint" layer is protected and cannot be (easily) renamed or deleted.