Friday, 31 March 2017

arcgis 10.0 - How to attach GP script tools, standard tools, and Python script to a toolbar?


I need to build a custom toolbar that can launch the following items:



  • A Python script tool - shouldn't be a problem, as you can already attach these to a toolbar

  • Out of the box functions such as the Editor Toolbar maybe or a standard GP tool - again, should be do-able as you can already attach these to a toolbar

  • A Python script that launches a PyQT/wxPython/Tk GUI. This is the tricky one I think. Now, this GUI does not need to interact with ArcMap. My other custom tools will do all of that, this GUI is for the user to setup parameters that will get written to a XML config file. It will also do some arcpy calls in the background and slurp some data and values out of a geodatabase and push them to the same XML config file.



enter image description here


So the question is - how to do this as simply as possible? It appears I cannot attach a Python script (that is not encased in a tool) to a toolbar in ArcMap. I'm thinking I'm going to have to go the Add-In route and create a simple Toolbar with buttons to kick off each of the items listed above. VBA is not an option. Thoughts?



Answer



@ChadCooper, perhaps I'm overlooking something, but with regard to your 3rd case:



A Python script that launches a PyQT/wxPython/Tk GUI..



Is there a reason why you couldn't use the .Net Process API (see System.Diagnostics) to execute your custom Python script over Standard In/Out? Of course, this sort of architecture will assume a proper version of Python is installed and equipped with any additional libs you require. I think you'd just want to extend an ICommand object and use the Process API in its Click() event. This example (which was an experiment only) called a .py script that used dbfpy to read the columns in a DBF file and return them as a list to a .Net application.


private static List _readDbfOutput;


public static List ReadDbfTable(string dbfPath, int limit)
{
// start a fresh output object..
_readDbfOutput = new List();
_readDbfOutput.Clear();

// ready..
string executable = "C:\Python27\python.exe";
string fullUtilPath = "C:\some_app\utils\read_dbf_table.py";


// The path to the python file and it's arguments become a single input
// submitted to the python runtime. In this case, I've got somethign like:
// C:\>C:/Python27/python.exe C:\some_app\utils\read_dbf_table.py -f C:/data/some_shapefiles.dbf -l 100

// dbfPath and limit were method arguments..
string exeArguments = fullUtilPath + " -f " + dbfPath + " -l " + limit.ToString();

ProcessStartInfo startInfo = new ProcessStartInfo(executable, exeArguments);
startInfo.UseShellExecute = false;

startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true; // necessary if you need to listen..
startInfo.RedirectStandardInput = false; // necessary if you send more inputs..
startInfo.StandardOutputEncoding = System.Text.Encoding.ASCII;

Process proc = new Process();

// ReadingDbfTable is my redirected standard input..
proc.OutputDataReceived += new DataReceivedEventHandler(ReadingDbfTable);
proc.StartInfo = startInfo;

proc.Start();
proc.BeginOutputReadLine();

// WaitForExit() may need to be wrapped in a Try or Using block.
// Will it run indefinitely if the script fails?
proc.WaitForExit();
proc.Close();

return _readDbfOutput;
}


/// Handle redirected STDOUT.
private static void ReadingDbfTable(object sendingProc, DataReceivedEventArgs stdOutput)
{
if ( ! String.IsNullOrEmpty(stdOutput.Data) )
{
_readDbfOutput.Add(stdOutput.Data);
}
}

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...