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.
-
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.user2856– user28562017年08月28日 01:17:12 +00:00Commented 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/2856user2856– user28562017年08月28日 02:16:18 +00:00Commented Aug 28, 2017 at 2:16
-
Thank you for the links and I will update the question now.Maddison Lussin– Maddison Lussin2017年08月31日 15:03:57 +00:00Commented Aug 31, 2017 at 15:03
1 Answer 1
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.
Explore related questions
See similar questions with these tags.