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
DXF in AutoCAD - which was created using the Export to CAD tool in ArcToolbox
DXF in AutoCAD - which was created using the python code below
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:
listOfLayers.append(layer)
#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?