2

I have written a python script to geoprocessing ArcGIS data and set the workspace, input and output parameters inside the python code as:

env.workspace = arcpy.GetParameterAsText(0)
# input feature class 
inFeatures = arcpy.GetParameterAsText(1)
# output feature class
Transect_featureclass = arcpy.GetParameterAsText(2)

Since I am new to ArcObjects using c# in Visual Studio 2010, I am trying to use ArcMap Add-in to add a button into the ArcMap desktop. But I didn't know how to call this python script from c#, and get something like a textbox popup and ask the information about the input and output parameters path. Since I directly call the python, it didn't work with the GetParameterAsText command inside the python code.

I highly appreciate if someone there can help on this case. Thanks in advance.

garnertb
  • 9,454
  • 36
  • 38
user1293655
  • 31
  • 2
  • 5
  • Which GP are you trying to use? Are you trying to set a parameter or specify an 'out' parameter? – Jordan Parmer Mar 28 '12 at 21:41
  • I am using arcpy in python 2.6 dealing with ArcMap 10 GP. I try to call my python code from Visual studio 2010 ADD-IN. So I add a button in the ArcMap 10 to run my python command. It didn't work. It seem that GetParameterAsText only work if you turn the python code into a tool in the ArcToolbox. So I didn't know how to set the input feature classes and/or output feature classes by calling Python code from c# in Visual Studio and resolve this problem. I wish I can explain more clear. Please help! – user1293655 Mar 29 '12 at 15:00

3 Answers3

1

There is an example in the .NET Developer Help at the Esri pages: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Walkthrough_Consuming_a_geoprocessing_model_tool_in_NET/0001000001sw000000/

Work through it, and you should have learned how to call a geoprocessing tool from ArcMap. To do so, you have to make a toolbox containing your python script. This can be called with .NET code (see the walkthrough).

Note: Most of this basic development stuff is already well documented at the Esri pages, but sometimes hard to find ;-)

dr pain
  • 570
  • 1
  • 5
  • 13
1

ArcPy does not support IronPython, it gives the error ”cannot load library arcgisscripting”. I believe the problem is that is that IronPython does not support pyd files which arcgisscripting contained within.

ESRI offer the following sample for leveraging ArcPy in .net

Community
  • 1
  • 1
David Wilton
  • 344
  • 2
  • 11
0

Found a couple options:

  1. Use IronPython libraries to load Python scripts and call methods directly from C# code. Good article with sample code and detailed explanation for each step found here.

    or

  2. Use Process.Start() to execute the script using Python executable

Preferred method would be option 1. It seems to be a more "complete" solution allowing Python script method calls directly from C#, while also allowing exceptions to bubble up. With option 2, you are completely dependent on the separate Python process for error handling and logging.

I hesitated to provide option 2, but it may be required if you can't get the arcpy module to play nicely with IronPython.

Community
  • 1
  • 1
Bryan
  • 17,112
  • 7
  • 57
  • 80
  • Thanks a lot for your suggestion. I did use Process.Start to call python. But the problem is that if I write the full path for workspace and input and output parameters like c:/test/shapefile.shp, inside the python script, it run with no problem. But I would like it used in the arcmap like a button, then I click the button, it will pop out a textbox asking the input and output information for parameters. How can i do that? Thanks. – user1293655 Mar 28 '12 at 19:33
  • I'm not familiar with ArcMap, but assume you have the flexibility to add some sort of interface that accepts user input. If this is the case, prompt the user for input/output values and pass these as arguments to the Python script. Take a look at `argparse` for defining and working with arguments passed to the Python script. http://docs.python.org/library/argparse.html – Bryan Mar 28 '12 at 20:19
  • Thanks anyway. I will take a look at the argparse. I think it will help. Thanks again. – user1293655 Mar 28 '12 at 20:44