5

I've build a neat little search arcpy script. The user opens any mxd, zooms to a location, chooses a corporate fGDB, and runs the tool. The user gets a list of all the populated feature class that intersects the current extent. Later I aim to write a process that the user can choose which layers they want to add. However I digress. I'm looking at embedding the arcpy into an addin, my first issue is learning what the equivalent of a arcpy.GetParameterAsText is, as I want the user to specify the work-space they need to interrogate.

import arcpy
import pythonaddins
class ButtonClass1(object):
"""Implementation for AddIn_Test4_addin.button (Button)"""
def __init__(self):
    self.enabled = True
    self.checked = False
def onClick(self):
    ################## MAP DOCUMENT DATA FRAME EXTENT

    StagingGDB = arcpy.GetParameterAsText(0)
    #StagingGDB = "C:\\Data\\Projects\\SearchTool\\CorpData1.gdb"
    arcpy.env.workspace = StagingGDB
    ExtentFC = StagingGDB +"\\ExtentFC"

    ############ Clean Up & Pre Check
    if arcpy.Exists(ExtentFC):
        arcpy.Delete_management(ExtentFC)

    ################## MAP DOCUMENT DATA FRAME EXTENT
    mxd = arcpy.mapping.MapDocument("current")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    currentExtent = df.extent

    ################## EXTENT FC
    pnt1 = arcpy.Point(currentExtent.XMin , currentExtent.YMin)
    pnt2 = arcpy.Point(currentExtent.XMax , currentExtent.YMin)
    pnt3 = arcpy.Point(currentExtent.XMax , currentExtent.YMax)
    pnt4 = arcpy.Point(currentExtent.XMin , currentExtent.YMax)
    array = arcpy.Array()
    array.add(pnt1)
    array.add(pnt2)
    array.add(pnt3)
    array.add(pnt4)
    array.add(pnt1)
    DisplayExtent = arcpy.Polygon(array)
    arcpy.FeatureClassToFeatureClass_conversion(DisplayExtent , StagingGDB ,"ExtentFC", "", "", "")
    pass

Any ideas?

1
  • maybe sys.argv ?
    – radouxju
    Commented Mar 7, 2014 at 6:17

1 Answer 1

5

pythonaddins is limited in getting parameters directly from a user that are not associated with the table of contents as pythonaddins is mainly a way to offer python developers a way to interact with user events. That being said the main mechanism for a developer to get parameters from a user in the same way as arcpy.GetParameterAsText is through pythonaddins.OpenFileDialog. Just like get GetParameterAsText, pythonaddins.OpenFileDialog takes the user input and returns a string to the developer. So in your case you could prompt the user with OpenFileDialog and then use the os and glob modules in python to further get at which files you want. Like this:

inFileGDB = pythonaddins.OpenDialog('Open File Geodatabase',False, r'','Open GDB')

Another option would be to have the user select a layer in the table of contents that lives in the file geodatabase of interest and click your button. Behind the scenes you would use pythonaddins.GetSelectedTOCLayerOrDataFrame() to access further information about the file, i.e. the path to the geodatabase that it lives in. Like this:

    lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()
    lyrDescribe = arcpy.Describe(lyr)

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.