4

With Python Add-In Wizard I created a Button which:

  1. looks in a specific directory for the last created/modified folder (which is created with another tool)
  2. saves the current mxd with a new name in this folder
  3. starts the new project

What I want to fulfill is, that I don't need to open ArcGIS again for starting the new mxd and instead of this open the new mxd in the current ArcGIS session. Does anybody have experience with this and can help me?

Here is my code:

import os, arcpy
class SaveNew(object):
"""Implementation for Python_Addins_addin.btn_speichernneu (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        #searching for last created/modified folder in directory
        directory =  r"\\VMSERV2\Geodata_Export"
        newfolder = max([os.path.join(directory,d) for d in os.listdir(directory)], key=os.path.getmtime)
        #set MapDocument and Saving and renaming in new folder
        mxd = arcpy.mapping.MapDocument("CURRENT")
        mxd.saveACopy(os.path.join(newfolder+"\NewProject"))
        # open new mxd  -> here I want to open the “NewProject.mxd” in the current ArcGIS session
        os.startfile(os.path.join(newfolder," NewProject.mxd"))
2

If you want open mxd in new process you can use

import subprocess
subprocess.Popen(["C:/Program Files (x86)/ArcGIS/Desktop10.3/bin/ArcMap.exe", 'D:/temp/test.mxd']

for a syncronous process

import subprocess
subprocess.Call(["C:/Program Files (x86)/ArcGIS/Desktop10.3/bin/ArcMap.exe", 'D:/temp/test.mxd']

But you need close the new before the first.

Don't use

os.system("TASKKILL /F /IM ArcMap.exe")

because it close all ArcMap process windows

but you can use

import os
import signal
os.kill(os.getpid(), signal.SIGTERM)

That create a new windows... But that do the work

  • I tried your code and it works. It seems that there are no other way instead of opening a new window. Your right, the os.kill(os.getpid(), signal.SIGTERM) is better than the TASKKILL. – Franziska0601 Dec 21 '15 at 10:02
  • @Franziska0601, There is others solutions. For that, you need use ctypes and other than win32com : to call arcobject librairies, or create a dot.net dll and use interop solution. The solution I have proposed to you is just the simplest – GeoStoneMarten Dec 21 '15 at 12:32
1

I don't think you can do this with arcpy; it seems to be designed primarily for automation of map documents rather than the ArcMap application itself. As such, there's no real functionality for opening and closing documents in the active application. You might be able to do something like this if you used ArcObjects instead, though.

1

Here the code I'm using now:

class SaveNew(object):
    """Implementation for Python_Addins_addin.btn_savenew (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        #searching for last created/modified folder in directory
        directory =  r"\\VMSERV2\Geodata_Export"
        newfolder = max([os.path.join(directory,d) for d in os.listdir(directory)], key=os.path.getmtime)
        #set MapDocument and Saving and renaming in new folder
        mxd = arcpy.mapping.MapDocument("CURRENT")
        mxd.saveACopy(os.path.join(newfolder+"\NewProject"))
        newmxd = os.path.join(newfolder,"NewProject.mxd")
        # open new mxd -> only works with Messagebox
        pythonaddins.MessageBox("The following file will be open soon: " + os.path.join(newfolder,"NewProject.mxd"),"Info",0)
        os.startfile(os.path.join(newfolder,"NewProject.mxd"))
        # closing old project
        os.kill(os.getpid(),signal.SIGTERM)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.