Select to view content in your preferred language

Using sys.argv in ptyhon toolbox

3524
2
Jump to solution
05-13-2015 08:05 AM
antoniogomez
Deactivated User

Hello forum:

We're launching a python toolbox tool "CreaCCAA" from a custom ArcMap menu option "Gestion de Cartografia" as shown in pic1 (see attachments)

The "CreaCCAA" tool has 6 parameters itself.

This operation is working fine

What we're trying to do (see Pic2 in attachments) is to launch "CreaCCAA" from menu "Gestion de Cartografía" with 3 first parameters initialized. We want the final user just to enter parameters 4,5 and 6 in "CreaCCAA"....but it doent work

Code for launching "CreaCCAA" in the "Gestion de Cartografia" .net side, is as follows:

       public static void ExecuteScript(string PathTOToolbox,string ScriptName, List<string> ParametersList)

        {

            try

            {

                IGeoProcessor2 gp = new GeoProcessorClass();

                IVariantArray parameters = null;

                gp.AddToolbox(PathTOToolbox);

                if (ParametersList != null)

                {

                    parameters = new VarArrayClass();

                    foreach (string param in ParametersList)

                    {

                        parameters.Add(param);

                    }

                }

                gp.Execute(ScriptName, parameters, null);

            }

            catch (Exception Err)

            {

                Connector.ConnectorExtension.WriteStaticLog("Error", Err.Message + " - " + Err.StackTrace);

            }

        }

In the "CreaCCAA" python code side, we're trying to read the 3 arguments using sys.argv but it doesn't work.

Exploring the sys.argv value we get always an empty string ['']

It makes no difference to read sys.argv in any section in the python toolbox code (__init__, getParameterInfo, updateParameters or execute): we get always the empty string ['']

Does anybody know how to solve this? Does sys.argv work "fine" in a pyhon toolbox?

We think one possible way to solve this, could be reading the 3 parameters needed in "CreaCCAA" from a text file (like cookies work) using the  "open" and "readlines" python methods.

But, because of security issues (one of the parameters to pass to "CreaCCAA" is a password) we prefer to avoid it.

Our development environment is ArcGIS 10.2.2 on Windows 7 Professional SP1

Hope you could help with this and thanks in advance

Antonio

0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

You are using the wrong approach. The execute method does what it says it executes the tool, you want to open it. Below is some VBA that shows you how to open a tool interface rather than execute it. I have not tried to use it to open a Python toolbox tool so have ago?

Public Sub OpenCalculateTool()
    ' Hook into Toolbox and get the tool
    Dim pUID As New UID
    pUID = "esriGeoprocessingUI.ArcToolboxExtension"
    Dim pArcToolboxExtension As IArcToolboxExtension
    Set pArcToolboxExtension = Application.FindExtensionByCLSID(pUID)
    Dim pArcToolbox As IArcToolbox
    Set pArcToolbox = pArcToolboxExtension.ArcToolbox
    Dim pGPTool As IGPTool
    Set pGPTool = pArcToolbox.GetToolbyNameString("CalculateField")

    ' Create messages, required by Invoke method
    Dim msgs As IGPMessages
    Set msgs = New GPMessages

    ' Get existing parameter structure
    Dim pArray As IArray
    Set pArray = pGPTool.ParameterInfo

    ' Declare Parameter interfaces
    Dim pGPParameter As IGPParameter
    Dim pGPDataType As IGPDataType
    Dim pGPParameterEdit As IGPParameterEdit

    ' Set the parameters of the tool
    Set pGPParameter = pArray.Element(0)
    Set pGPParameterEdit = pGPParameter
    Set pGPDataType = pGPParameter.DataType
    Set pGPParameterEdit.Value = pGPDataType.CreateValue("a") ' Featurelayer name
    Set pGPParameter = pArray.Element(1)
    Set pGPParameterEdit = pGPParameter
    Set pGPDataType = pGPParameter.DataType
    Set pGPParameterEdit.Value = pGPDataType.CreateValue("xx") ' Field
    Set pGPParameter = pArray.Element(3)
    Set pGPParameterEdit = pGPParameter
    Set pGPDataType = pGPParameter.DataType
    Set pGPParameterEdit.Value = pGPDataType.CreateValue("VB")

    ' Open the tool
    Dim pGPToolCommandHelper As IGPToolCommandHelper2
    Set pGPToolCommandHelper = New GPToolCommandHelper
    pGPToolCommandHelper.SetTool pGPTool
    pGPToolCommandHelper.InvokeModal 0, pArray, True, msgs
End Sub

View solution in original post

2 Replies
DuncanHornby
MVP Notable Contributor

You are using the wrong approach. The execute method does what it says it executes the tool, you want to open it. Below is some VBA that shows you how to open a tool interface rather than execute it. I have not tried to use it to open a Python toolbox tool so have ago?

Public Sub OpenCalculateTool()
    ' Hook into Toolbox and get the tool
    Dim pUID As New UID
    pUID = "esriGeoprocessingUI.ArcToolboxExtension"
    Dim pArcToolboxExtension As IArcToolboxExtension
    Set pArcToolboxExtension = Application.FindExtensionByCLSID(pUID)
    Dim pArcToolbox As IArcToolbox
    Set pArcToolbox = pArcToolboxExtension.ArcToolbox
    Dim pGPTool As IGPTool
    Set pGPTool = pArcToolbox.GetToolbyNameString("CalculateField")

    ' Create messages, required by Invoke method
    Dim msgs As IGPMessages
    Set msgs = New GPMessages

    ' Get existing parameter structure
    Dim pArray As IArray
    Set pArray = pGPTool.ParameterInfo

    ' Declare Parameter interfaces
    Dim pGPParameter As IGPParameter
    Dim pGPDataType As IGPDataType
    Dim pGPParameterEdit As IGPParameterEdit

    ' Set the parameters of the tool
    Set pGPParameter = pArray.Element(0)
    Set pGPParameterEdit = pGPParameter
    Set pGPDataType = pGPParameter.DataType
    Set pGPParameterEdit.Value = pGPDataType.CreateValue("a") ' Featurelayer name
    Set pGPParameter = pArray.Element(1)
    Set pGPParameterEdit = pGPParameter
    Set pGPDataType = pGPParameter.DataType
    Set pGPParameterEdit.Value = pGPDataType.CreateValue("xx") ' Field
    Set pGPParameter = pArray.Element(3)
    Set pGPParameterEdit = pGPParameter
    Set pGPDataType = pGPParameter.DataType
    Set pGPParameterEdit.Value = pGPDataType.CreateValue("VB")

    ' Open the tool
    Dim pGPToolCommandHelper As IGPToolCommandHelper2
    Set pGPToolCommandHelper = New GPToolCommandHelper
    pGPToolCommandHelper.SetTool pGPTool
    pGPToolCommandHelper.InvokeModal 0, pArray, True, msgs
End Sub
antoniogomez
Deactivated User

Yes, it works! It opens my python toolbox "CreaCCAA" with the three parameters correctly

Thanks, Duncan!

Antonio

0 Kudos