Sunday, 21 August 2016

arcmap - Auto-refreshing triggered by changes in directory for ArcPy?


I have a directory change monitor python script which monitors any changes happened in a directory, I want to trigger Arcmap auto refreshing whenever a change has been detected (eg. a new line added to a file). I tried to launch the monitor program within arcmap python command window using execfile("myprogram.py"). The problem I encountered is that once the script starts to monitor and auto-refresh, the arcmap main window was locked and frozen, no way to stop it except shutting down the arcmap, although I can see the auto-refresh happened whenever there is a change. Is there a way to run the monitor script in a separate process or background without locking or freezing the main window? Here is my monitor script which is borrowed from Tim Golden,


    import os
import win32file
import win32con
import arcpy

# Search for the new data

ACTION = {
1: "Created",
2: "Deleted",
3: "Updated",
4: "Renamed from something",
5: "Renamed to something"
}


FILE_LIST_DIRECTORY = 0X0001

path_to_watch = "C:\Directory"

hDir = win32file.CreateFile(
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None

)


while 1:
results = win32file.ReadDirectoryChangesW(
hDir,
1024,
True,
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |

win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_SIZE |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY,
None,
None
)

for action, file in results:
full_filename = os.path.join(path_to_watch, file)

print full_filename, ACTION.get(action, "Unknown")
arcpy.RefreshActiveView()


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...