ArcPy and Geoprocessing… it’s for Developers too

10 has introduced a lot more functionality for automation and analysis.  It’s chalked with plenty of new tools a really cool and powerful ArcPy Python site package.  All this new functionality is not just for the GIS users, developers can leverage this too.

A perfect example of this is the printing functionality that is offered through the ArcPy Mapping module.  Printing through ArcObjects is definitely doable, but somewhat cumbersome to write the code correctly.  You can find a great example of using ArcMap’s printing functionality through a custom command by looking at the Print Active View sample on the Resource Center.

This code works great, but takes a bit of effort to customize and a bit more time to really understand what is going on.  Instead of all this code, if we already have a map document that we want to print, either through ArcGIS Engine or any custom command you might create for ArcMap, why not just use Python to print our map document?  What does the equivalent code in Python look like?

import arcpy.mapping as map
mxd = map.MapDocument(“path to the map document”)
printerName = “the name of the printer”
map.PrintMap(mxd, printerName, “PAGE_LAYOUT”)

This is fairly easy code and if you have the “Use Printer Paper Settings” checkbox checked in your map documents Page and Print Setup properties, then the printing properties that you specify in your map document are saved with the map and used when you print from ArcPy.  (Such as using the ArcPress Printer Engine and printer driver settings)

So, how do you leverage this in an ArcObjects application?  Just create a script tool and call that tool through the Geoprocessor. 

Let’s go ahead and modify this code a little to make it work as a script tool and at the same time, we’ll add a little code to give us the option of printing or exporting to pdf.

import arcpy
import arcpy.mapping as map
# MXD
mapDoc = arcpy.GetParameterAsText(0)
# Export or PrintoutType = arcpy.GetParameterAsText(1)
# Pinter NameprinterName = arcpy.GetParameterAsText(2)mxd = map.MapDocument(mapDoc)
if outType == “Print”:
map.PrintMap(mxd, printerName, “PAGE_LAYOUT”)
elsif outType == “Export”:    map.ExportToPDF(mxd,    printerName)
else:
arcpy.AddMessage(“Wrong output type”)

We can write this code out with your favorite Python IDE and save it to a .py file.  Then in ArcCatalog create a Toolbox and add reference this script as a tool. 

We can see that we have another tool in this Toolbox “List Printer Names”.  This script tool uses the ArcPy.Mapping.ListPrintersNames()  to get a list of printers that we can then use as an input to the “Print or Export the Map” tool.

So, now we have a toolbox that references that script as a script tool.  So how do we run this tool through our ArcObjects application?

 ’Create the Geoprocessor
Dim gp As Geoprocessor = New Geoprocessor()
‘Add my script tool
gp.AddToolbox(“C:PrintMapSamplePrintingToolbox.tbx”)
Dim parameters As IVariantArray = New VarArrayClass()
‘Map Document name
parameters.Add(“c:PrintMapSampleTest.mxd”)parameters.Add(“Print”)
parameters.Add(“my printer”)
‘Execute the gp tool
gp.Execute(“PrintorExportMap”, parameters, Nothing)  

And to use the same script to export we just pass in some different parameters.

parameters.Add(“c:PrintMapSampleTest.mxd”)parameters.Add(“Export”)
parameters.Add(“c:PrintMapSampleExportTest.pdf”)
gp.Execute(“PrintorExportMap”, parameters, Nothing) 

 This is a very simple example, but we can extend this example for any situation where we need to do some GIS analysis.  There are many situations where we might be able to write our application’s business logic in Python or Model Builder instead of writing ArcObjects. 

Content provide by Ralf Gottschalk 

This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

One Comment

  1. nilsbabel says:

    So using arcpy directly in ArcObjects is not possible? What about other geoprocessing commands. I’m trying to call a command in the geoprocessor from ArcObjects that is part of the spatial analyst tools. It seems this can’t be done with the normal gp.execute because the sa tools return a raster instead of have the output be a parameter as in 9.3.1. The tool is now part of the arcpy.sa site package and it seems you can’t call it with the gp.execute command. Am I wrong?