Monday, 16 May 2016

arcpy - Running Python toolbox tool both standalone from other tools within same Python toolbox?


I have created a Python toolbox for work that has two tools inside. These tools use the toolbox template and so have a init, getParameterInfo, etc.



I want to be able to run one tool both standalone and call it inside other tools within that toolbox. I can't seem to get the parameters correct though.


class foo(object)
def __init__(self)
#stuff
def getParameterInfo(self):
# list parameters for UI tool
def execute(self, parameters, messages)
print parameter[0]
return


class bar(object)
def __init__(self)
#stuff
def getParameterInfo(self):
# list parameters for UI tool
def execute(self, parameters, messages)
foo("hello, world)
return

I have tried adding a parameter to the init(self, parameter) or the foo class but I can't get it to work.



I am new to Object-Oriented Programming (OOP) and ArcGIS in general.



Answer



The simplest option is to have your execute method call a function instead of doing the actual processing. This makes it easily callable by any tool.


class Foo(object)
def __init__(self)
#stuff
def getParameterInfo(self):
# list parameters for UI tool
def execute(self, parameters, messages)
somefunc(parameters[0].value, parameters[1].value)


class Bar(object)
def __init__(self)
#stuff
def getParameterInfo(self):
# list parameters for UI tool
def execute(self, parameters, messages):
somefunc(parameters[0].value, parameters[1].value)
anotherfunc(parameters[2].value, parameters[3].value)
return


def somefunc(arg1, arg2):
#do something
return

def anotherfunc(arg1, arg2):
#do something else
return

If you want those functions contained in the tool classes:



class Foo(object)
def __init__(self)
#stuff
def getParameterInfo(self):
# list parameters for UI tool
def execute(self, parameters, messages)
self.somefunc(parameters[0].value, parameters[1].value)
def somefunc(self, arg1, arg2):
#do something
return


class Bar(object)
def __init__(self)
#stuff
def getParameterInfo(self):
# list parameters for UI tool
def execute(self, parameters, messages):
foo = Foo()
foo.somefunc(parameters[0].value, parameters[1].value)
self.anotherfunc(parameters[2].value, parameters[3].value)

return
def anotherfunc(self, arg1, arg2):
#do something else
return

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...