1

I use C# for creating an AddIn for ArcGIS Pro 3.1.

I got a feature request to start a python script (which is part of a toolbox) and pass some arguments.

Unfortunately I found only descriptions about Python scripts in ArcGIS Pro but none of them work with an AddIn.

2 Answers 2

1

Suggest you explore the sample scripts for the SDK, they have many examples on github such as CallScriptFromNet.

0

With the help of the example provided from @Hornbydd I created my solution like this:

class RunProcess
{
    private Process process;
    private StringBuilder output = new();
    private StringBuilder error = new();

    public (string Output, string Error, int ErrCode) RunProcessGrabOutput(string Executable, string Arguments, string WorkingDirectory)
    {
        int exitCode = -1;
        try
        {
            output.Clear();
            error.Clear();
            process = new Process();
            process.StartInfo.FileName = Executable;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WorkingDirectory = WorkingDirectory;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
            process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.EnvironmentVariables.Add("PYTHONUNBUFFERED", "TRUE");

            if (!string.IsNullOrEmpty(Arguments))
                process.StartInfo.Arguments = Arguments;

            process.EnableRaisingEvents = true;
            process.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
            process.ErrorDataReceived += new DataReceivedEventHandler(ProcessErrorHandler);
            process.Start();

            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            // You can set the priority only AFTER the you started the process.
            process.PriorityClass = ProcessPriorityClass.BelowNormal;
            process.WaitForExit();
            exitCode = process.ExitCode;
        }
        catch
        {
            // This is how we indicate that something went wrong.
            throw;
        }

        return (output.ToString(), error.ToString(), exitCode);
    }

    private void ProcessOutputHandler(object SendingProcess, DataReceivedEventArgs OutLine)
    {
        output.AppendLine(OutLine.Data);
    }

    private void ProcessErrorHandler(object SendingProcess, DataReceivedEventArgs OutLine)
    {
        error.AppendLine(OutLine.Data);
    }
}

This function is called like this:

    var workingDir = "C:\\Daten\\RadSvn\\Prototypes\\StartPythonScript\\StartPythonScript\\Scripts";

    var myArguments = "-c \"from Test import Main;Main('Hallo')\"";

    var process = new RunProcess();
    var processOutcome = process.RunProcessGrabOutput(executable,
    myArguments, workingDir);

while Test.py is the name of the script and Main is a function in there which has a parameter called "dataset":

import arcpy
import string, sys, os
import ctypes

logfile ="C:\\Daten\\RadSvn\\Prototypes\\StartPythonScript\\StartPythonScript\\Scripts\\Logfile.log"

def Main(dataset):
    print ("Started with argument " + dataset)
    datei = open(logfile, 'a')
    datei.write("Started with argument " + dataset)
2
  • where is executable coming from?
    – Alex Gray
    Commented Oct 11 at 15:39
  • This is the Python executable/start File. In my case its: Path.Combine(ArcGISProBinPath, @"Python\Scripts\propy.bat");
    – Gener4tor
    Commented Oct 14 at 6:35

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.