I am trying to export selected features from layers from SDE into a DXF using python, but it does not retain the layer name. But if I do it manually using the ArcToolbox tool, it does work. Example below

Layers in MXD

enter image description here

DXF in AutoCAD - which was created using the Export to CAD tool in ArcToolbox

enter image description here

DXF in AutoCAD - which was created using the python code below

enter image description here

The code that I run is below

mapDoc = r"C:\temp\DXF_Test10_v5.mxd"

mxd = arcpy.mapping.MapDocument(mapDoc)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

#Go through teh bookmarks layer
fields = "FULLNAME"
#location = "City of London"
location = "Golden Lane Estate"
expression = "\"FULLNAME\" = '" + location + "'"

for layer1 in arcpy.mapping.ListLayers(mxd):
    if layer1.name == "Bookmarks":
        with arcpy.da.SearchCursor(layer1, fields,expression) as cursor:
            for row in cursor:
                #select the record
                arcpy.SelectLayerByAttribute_management(layer1, "NEW_SELECTION", expression)
                #do select by location, select all the layers apart from bookmarks
                inFeatures = ''
                listOfLayers = []
                #loop through all the layers in teh map 
                for layer in arcpy.mapping.ListLayers(mxd):
                    if layer.name <> "Bookmarks":
                        if layer.isGroupLayer == False:
                            print layer.longName
                            if layer.visible == True:
                                #select the features that intersect
                                arcpy.SelectLayerByLocation_management(layer, "INTERSECT", layer1, "", "ADD_TO_SELECTION")
                                count = int(arcpy.GetCount_management(layer).getOutput(0))
                                print count
            #print inFeatures
            #export to cad
            #arcpy.ExportCAD_conversion(listOfLayers, "DXF_R2010", r"H:\Corporate GIS and Web Mapping\Data_Repository\DXF_TEST" + os.sep + location + ".dxf", "Ignore_Filenames_in_Tables", "Overwrite_Existing_Files", "")
            arcpy.ExportCAD_conversion(listOfLayers, "DXF_R2010", r"C:\Temp\DXF_COL" + os.sep + location + ".dxf", "Ignore_Filenames_in_Tables", "Overwrite_Existing_Files", "")

del mxd

Does anyone know why when I run the script it does not honor the layer name?

2 Answers 2


You need to add reserved CAD fields to those layers and populate them prior to export. You can either do this manually or in your script or by using the Add CAD Fields tool. It's not enough to just add these fields or run this tool, you must actually populate the attributes - if you want the layer name in the target DXF then the "Layer" field needs to have the layer name for each and every feature. You can also assign colours and other properties this way.


I have found a way to name the layers within the dxf the same as the layer name within the MXD.

Once the selection has been done.

  1. Export it to an in_memory layer - This is Key
  2. Add the in_memory layer to the list
  3. Pass in list within the Export to CAD tool.

The resulting dxf, should now contain the same layer namesenter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.