I am trying to make an add-in tool for ArcMap that will allow a user to draw a rectangle to export the visible layers in their MXD to shapefiles. I used the Python Add-in Wizard to create my add-in, and I modified the python script that executes the business logic of my tool like this:
import arcpy
import pythonaddins
class ToolClass2(object):
"""Implementation for AddinTest20200513_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.cursor = 3
self.shape = "Rectangle"
def onRectangle(self, rectangle_geometry):
extent = rectangle_geometry
output_location = arcpy.env.workspace = r'C:\Users\sipes_gene\Desktop\AddinTest20200513'
a = arcpy.Array()
a.add(extent.lowerLeft)
a.add(extent.lowerRight)
a.add(extent.upperRight)
a.add(extent.upperLeft)
a.add(extent.lowerLeft)
boundary_poly = arcpy.Polygon(a)
mxd = arcpy.mapping.MapDocument("Current")
lyrList = arcpy.mapping.ListLayers(mxd)
for lyr in lyrList:
if lyr.visible is True:
out_shp = output_location + str(lyr) + ".shp"
arcpy.SelectLayerByLocation_management(lyr, 'Intersect', boundary_poly, 0, 'New_Selection')
copyFCs = arcpy.CopyFeatures_management(lyr, output_location, out_shp)
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
arcpy.RefreshActiveView()
return copyFCs
I know I am very close to pulling this off because when I run it in ArcMap it will return the first feature class in my TOC as a shapefile, based on the rectangle I drew, but then it stops and does not proceed to the rest of my feature classes. Somehow my loop isn't working correctly. I'm wondering if I need to put the array inside the loop?
#arcpy scripting