15

Are there any available examples that show how python can be integrated into a .NET ArcGIS Addin or Extension? I have a series of python scripts that I would like to call from form events (e.g. button clicks) in .NET. I would also like to be able to set variables in the script from a .NET form.

Should I bite the bullet and re-write these scripts in .NET, or is there a simple way to wrap the python code?

2 Answers 2

25

Keep in mind that Arcpy is essentially a wrapper around ArcObjects. But if you're just trying to call some Python scripts that you don't want to have to rewrite you can spawn a process that calls the python executable with your args.

var startInfo = new ProcessStartInfo() {
   CreateNoWindow = false,
   UseShellExecute = false,
   FileName = pathToPythonRuntime,
   WindowStyle = ProcessWindowStyle.Hidden,
   Arguments = pathToYourPythonScriptYouDoNotWantToRewrite+" "+yourOtherArgsEtc
 };
var exeProcess= Process.Start(startInfo);
//If you need synchronous execution you can do this
exeProcess.WaitForExit();

Note that you can do some pretty cool multithreading stuff with this too.

0
0

From your Question I am not sure how wedded to .NET you are but, if you are using ArcGIS 10.1 (or later) for Desktop, then I think a simpler approach will be to use a Python AddIn:

ArcGIS 10.1 introduces Python to the list of languages for authoring Desktop add-ins, providing you with an easy solution to extend desktop functionality. To simplify the development of Python add-ins, you must download and use the Python Add-In Wizard to declare the type of customization. The wizard will generate all the required files necessary for the add-in to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.