/arcpy-cpp-interop

Python code showing communication between python tool and a c/c++ library.

Primary LanguagePythonApache License 2.0Apache-2.0

ArcGIS geoprocessing tools have rich support for messaging and reporting progress within the ArcGIS family of applications. With script tools implemented with python the coding patterns are well established and documented.

The code within this repo shows how to call from python to a C/C++ library (dll) and have the dll drive the ArcGIS application progressor, pass messages to the app, and query if the user has cancelled the operation (in order to cancel processing and gracefully exit the dll).

Basic flow of code

  1. gptool_script.py calls my_cpp_function in the dll with two arguments
  2. a string to show passing argument from python to the dll
  3. a callback function for the dll to push feedback to python
  4. my-lib.dll my_cpp_function calls back into python to
  5. push messages (strings)
  6. move the progress bar
  7. query if arcpy.env.isCancelled is True (user has cancelled operation)

To use with ArcMap

  1. open my-lib.sln with Visual Studio
  2. in the BUILD menu, select Configuration Manager...
  3. set the Active solution configuration to Release
  4. set the Active solution platform to Win32
  5. build my-lib.dll
  6. run gptool_script.py using python27 (to test)
  7. Open ArcGIS Pro
  8. add arcpy-cpp-interop.pyt to the project
  9. run the 'cpp interop example' tool (note progress & messaging)

To use with ArcGIS Pro

  1. open my-lib.sln with Visual Studio
  2. in the BUILD menu, select Configuration Manager...
  3. set the Active solution configuration to Release
  4. set the Active solution platform to x64
  5. build my-lib.dll
  6. run gptool_script.py using python34 (to test)
  7. Open ArcGIS Pro
  8. add arcpy-cpp-interop.pyt to the project
  9. run the 'cpp interop example' tool (note progress & messaging)

Tool progress and messages

Notes

At Pro 1.1 (planned for ArcGIS 10.4) the arcpy.env.autoCancelling and arcpy.env.isCancelled have been added. By setting arcpy.env.autoCancelling = False the code in the dll is able to run code based on the user having hit cancel on the operation arcpy.env.isCancelled = True .

Before Pro 1.1/ArcGIS 104, the properties mentioned above are missing progress and messaging will work, but the callback will fail if user has cancelled the tool, this failure with the Callback could be trapped for as indication of a cancel situation.

Reference