stephan@114: # Author:  ESRI
stephan@114: # Date:    July 5, 2010
stephan@114: # Version: ArcGIS 10.0
stephan@114: # Summary: This script will add a layer file into a map document. The script
stephan@114: #          must be run from within ArcMap because it references the CURRENT
stephan@114: #          map document.  The purpose of the script is to create a user
stephan@114: #          friendly tool that allows users to simply add a layer from a list
stephan@114: #          of existing layer files all stored in a common location. A validation
stephan@114: #          script is used to automatically populate two of the parameters.
stephan@114: #          The parameters are:
stephan@114: #               1) Browse to a folder that contains layer files. This could be
stephan@114: #                  hard coded within the validation script eliminating the need
stephan@114: #                  for entering this parameter.
stephan@114: #               2) Select a layer file from the list.  This is auto populated
stephan@114: #                  using a validation script.
stephan@114: #               3) Select a data frame.  This is also auto populated using a 
stephan@114: #                  validation script.
stephan@114: #               4) Select one of 3 placement positions.
stephan@114: #
stephan@114: # Note: This script tool will only work if background processing is disabled.
stephan@114: #       because CURRENT is being used.
stephan@114: # Note: To run the script from ArcMap either run the script tool from the
stephan@114: #       Catalog window from within ArcMap or add the script tool into the UI
stephan@114: #       via the customize dialog box [Geoprocessing Tools].
stephan@114: 
stephan@114: import arcpy
stephan@114: import shlex, subprocess
stephan@114: 
stephan@114: #Helper for executing an external shell-scrip or programm
stephan@114: def executeCommand(command):
stephan@114:     success = False
stephan@114:     try:
stephan@114:         args = shlex.split(command)
stephan@114:         p = subprocess.Popen(args)
stephan@114:         p.wait()
stephan@114: 
stephan@114:         returnCode = p.returncode
stephan@114: 
stephan@114:         if (returnCode == 0):
stephan@114:             success = True
stephan@114:         else:
stephan@114:             print "Subproccess terminates with %s. This is an error"\
stephan@114:                        % returnCode
stephan@114:     except OSError, err:
stephan@114:         success = False
stephan@114: 
stephan@114:     return success
stephan@114: 
stephan@114: try:
stephan@114:     #Read parameters from dialog
stephan@114:     inputMXD = arcpy.GetParameterAsText(0)
stephan@114:     outputMXD = arcpy.GetParameterAsText(1)
stephan@114:     template = arcpy.GetParameterAsText(2)
stephan@114: 
stephan@114:     print "InputMXD: %s,\nOutputMXD: %s" % (inputMXD, outputMXD)
stephan@114: 
stephan@114:     # execute Java-command for converting
stephan@114:     javacmd = "-Dmapserver.library.name=lib/mapscript -jar mxd-testbed_sh.jar"
stephan@114:     javafiles = "-mxd %s -map %s -template %s " % (inputMXD, outputMXD, template)
stephan@114: 
stephan@114:     cmd = "java "+ javafiles + javacmd
stephan@114:     print cmd
stephan@114:     executeCommand(cmd)
stephan@114:     
stephan@114: except Exception, e:
stephan@114:   import traceback
stephan@114:   map(arcpy.AddError, traceback.format_exc().split("\n"))
stephan@114:   arcpy.AddError(str(e))