0

I created a toolbox with a bunch of scripts for ArcGIS Pro. I created a separate tool, RunAll, that runs all the tools within that toolbox. It runs at a good speed when I just run it as a python script, but is very slow in ArcGIS pro as a tool. Any tips/suggestions for a tool to run all tools in toolbox? Example code for stand alone script:

import arcpy
import os
arcpy.ImportToolbox(pyt_path, "myToolbox")
# get parameters
gdb_path = r"C:\Path\To\GDB.gdb"

# Run all tools
print(dir(arcpy.myToolbox))
for t in dir(arcpy.myToolbox):
    if t.startswith("_"):
        continue
    arcpy.AddMessage("Running {}".format(t))
    try:
        tool = getattr(arcpy.myToolbox,t)
        tool(gdb_path)
    except Exception as e:
        arcpy.AddError("{} failed...".format(t))
        arcpy.AddError(e)
        arcpy.AddError("-------------")

Example code below for RunAll tool to run the above script within ArcGIS Pro:

import arcpy
from arcpy import Parameter
arcpy.env.overwriteOutput = True
import os


class RunAll(object):
    def __init__(self):
        self.label = "Run all"
        self.description = """Runs all Tools"""
        self.canRunInBackground = True

    def getParameterInfo(self):
        """Define parameter definitions"""
        input_gdb = Parameter(
            displayName='Input geodatabase',
            name='input_gdb',
            datatype='DEWorkspace',
            parameterType='Required',
            direction='Input')

        input_gdb.filter.list = ['Local Database']
        return [input_gdb]

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        pyt_path = r"C:\Path\to\custom\toolbox.pyt"
        arcpy.ImportToolbox(pyt_path, "myToolbox")
        # get parameters
        gdb_path = str(parameters[0].value)

        # Run all tools
        print(dir(arcpy.myToolbox))
        for t in dir(arcpy.myToolbox):
            if t.startswith("_"):
                continue
            arcpy.AddMessage("Running {}".format(t))
            try:
                tool = getattr(arcpy.myToolbox,t)
                tool(gdb_path)
            except Exception as e:
                arcpy.AddError("{} failed...".format(t))
                arcpy.AddError(e)
                arcpy.AddError("-------------")
                    
        return

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

0

Your Answer

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

Browse other questions tagged or ask your own question.