Use python script tool from .Net ArcGIS Pro

4587
12
Jump to solution
10-25-2017 01:31 PM
MatthewDriscoll
MVP Alum

Using a C# .Net add-in I need to run a custom python script tool from my toolbox from with-in the map in Pro.   The sample "CallScriptFromNet" will not work because it runs the Python script from outside of the Pro environment.  I have things in the script that uses many layers from the "CURRENT" map so it fails running it the way the sample shows. The script works great when I run it from the toolbox, but I cannot figure out how to run it from the .Net add-in. I am thinking I need to use some sort of combination of getting a Geoprocessing Project Item and Executing it?  

1 Solution

Accepted Solutions
NobbirAhmed
Esri Regular Contributor

Put the toolbox and python script in a folder just under the .Net project folder. You can then refer to the path of the toolbox from the solution.

View solution in original post

12 Replies
KatherineDalton
Esri Regular Contributor

Hi Matt,

I'm going to mention the Pro SDK group so they should see your question and can hopefully help out! https://community.esri.com/groups/arcgis-pro-sdk?sr=search&searchId=af02e762-8402-4f43-9f22-3fed9887...

Katy

Katy Dalton | Technical Consultant
THE SCIENCE OF WHERE™
0 Kudos
NobbirAhmed
Esri Regular Contributor

Put the toolbox and python script in a folder just under the .Net project folder. You can then refer to the path of the toolbox from the solution.

MatthewDriscoll
MVP Alum

Is there a way to use a relative path to that folder?   Not everyone I share the project with will have the same file, folder, directories.

 

Replace the "string tool_path" with a relative path. 

public async Task<IGPResult> ExecuteModel()
 {
     string tool_path = @"C:\Test_Folder\Base_Maps\Base_Maps.tbx\TestScript";

     var parameters = Geoprocessing.MakeValueArray();

     IGPResult gp_result = await Geoprocessing.ExecuteToolAsync(tool_path, parameters);

     Geoprocessing.ShowMessageBox(gp_result.Messages, "GP Messages", gp_result.IsFailed ? GPMessageBoxStyle.Error : GPMessageBoxStyle.Default);

     return gp_result
 }

;

0 Kudos
NobbirAhmed
Esri Regular Contributor

Assuming you are trying to deploy a .Net Add-in, you can use System.IO.Directory.GetCurrentDirectory() method to get the path of the ArcGIS Pro Project where the default geodatabase and the toolbox reside:

string spath = System.IO.Directory.GetCurrentDirectory();

The Project folder looks something like this:

Project folder structure

For this particular folder structure, the spath value (under C: directory) will be:

C:\temp\AddinMovedHere\OneDown\TwoDown

From there, using C# (or VB) concatenating functions, you can derive other paths such as to Geodatabase and Toolbox.

gauthierverlin
New Contributor III

Hello,

I found your topic after asking a kind of similar question here :

Use python script tool from NET Arcgis Pro 

I tried to put the python script in a folder just under the .Net project folder but I keep getting the error.

If you have any clue about it, it would help me a lot.

Best regards

Gauthier

0 Kudos
NobbirAhmed
Esri Regular Contributor

Hi Gauthier,

Most probably it’s a bug – I have also seen this error yesterday ☹ Could you please do this and let me know what happens?

Run the same python code in Pro app’s Python Window while keeping the point layer displayed.

0 Kudos
NobbirAhmed
Esri Regular Contributor

The error returned is the correct error as when a table is open in a Map its schema is locked (AddField is trying to change the schema) for any external program (such as a Python script). 

There is a screen shot of the error message (ERROR 000464) here:

https://community.esri.com/thread/232293-use-python-script-tool-from-net-arcgis-pro

0 Kudos
gauthierverlin
New Contributor III

Hello,

The script does work if I run it in the python window from Arcgis Pro while the point layer is displayed.

Is there a way to unlock the schema ?

0 Kudos
NobbirAhmed
Esri Regular Contributor

You can change schema from within the app (such as from Python window) as the app knows the state.

But, if you are outside of app, such as in a python script (called from a .Net Addin) then you are foreign to the app ☹

I’ll play with your script and get back to you ☺

0 Kudos