8

I have a GIS script tool that creates a copy of an mxd, sets a definition query and exports a set of data driven pages based on the definition query. After it's finished exporting it deletes the mxd copy, some variables and a bunch of database connections.

Is there some way to code an escape code which would cancel the data driven pages exports wherever they are but instead of cancelling the whole script, it would just jump to the end and still delete the mxd copy etc. The block of code looks something like this:

 mxd = arcpy.mapping.MapDocument(mxd_master)
 mxd_folder = os.path.dirname(mxd_master)
 mxd_copy = mxd_folder + "\\WORKING.mxd"
 mxd.saveACopy(mxd_copy)
 mxd = arcpy.mapping.MapDocument(mxd_copy)
 ddp = mxd.dataDrivenPages
 for page_num in range (1, ddp.pageCount + 1):
 ddp.currentPageID = page_num
 row = ddp.pageRow
 mapsheet = row.getValue(ddp.pageNameField.name)
 arcpy.mapping.ExportToPDF(pdf export parameters....)
 del mxd, mxd_master
 arcpy.Delete_management(mxd_copy)

Basically I want to be able to hit the "Q" key (or whatever key) and the pdf export loop would break and the del/Delete_management block would still run. I don't think it's possible since it's running the script through a tool but I figured I'd see if it was as it would stop me from having to manually delete things if I decide to stop the export early.

I am using ArcGIS Desktop 10.4.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Aug 28, 2017 at 0:31
3
  • 4
    @Maddison I haven't tried this so not posting an answer, but I think you should have a look at the ArcGIS Help topic Understanding cancellation behavior in script tools. Commented Aug 28, 2017 at 1:17
  • Can you edit your question and specify which version of ArcGIS Desktop you are using please. See also gis.stackexchange.com/q/172598/2856 Commented Aug 28, 2017 at 2:16
  • Thank you for the links and I will update the question now. Commented Aug 31, 2017 at 15:03

1 Answer 1

2

Take a look here.

In short your code should look like this:

 # Start of code
 try:
 #for loop
 except KeyboardInterrupt:
 pass
 #Rest of code

According to the linked Stack Overflow question Ctrl-C should work to set off KeyboardInterrupt.

answered Oct 17, 2017 at 19:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.