0

The following script has the function of inserting shapefile in an mxd project based on a previous one.

import arcpy
from arcpy import mapping

arcpy.env.workspace = arcpy.GetParameterAsText(0)
mxd_input = arcpy.GetParameterAsText(1)
shp_input = arcpy.GetParameterAsText(2)
layer_output = arcpy.GetParameterAsText(3)
Test_mxd_out = arcpy.GetParameterAsText(4)

#Variables
shp_input = "hellow.shp"
layer_output = "test"

arcpy.env.workspace = r"C:\GIS\Test"
mxd_input = arcpy.mapping.MapDocument(r"C:\GIS\Test\Mxd_to_shape.mxd")
df = arcpy.mapping.ListDataFrames(mxd_input)[0]

arcpy.MakeFeatureLayer_management(shp_input,layer_output)
layer = arcpy.mapping.Layer(layer_output)
arcpy.mapping.AddLayer(df,layer,"AUTO_ARRANGE")

Test_mxd_output = mxd_input.saveACopy(r"C:\GIS\Test\Mxd_a_shape2.mxd")

del shp_input
del layer_output
del mxd_input
del df
del layer
del Test_mxd_out

arcpy.AddMessage("Ended process")

I have made this a "script tool" and the parameters are the following:

enter image description here

The characteristics of the folders are as follows:

enter image description here

I put the parameters in the script tool:

enter image description here

And when executing it:

enter image description here

It does not carry out the mxd project that it says in the parameters nor does it follow the path that I require, but rather it stays with the values that are established in the script (instead of Mxd_to_shape_2.mxd, it does the Mxd_a_shape2.mxd).

Why does it take the values of the script if in the script tool I tell it to take other values? Are there any actions or tasks that you are not doing?

1
  • 2
    You have hard wired saveACopy to "C:\GIS\Test\Mxd_a_shape2.mxd" I think it should be your string variable Test_mxd_out
    – Hornbydd
    Sep 30, 2021 at 17:35

1 Answer 1

1

You are getting confused between variables and the variable value (what you are getting as a parameter). Your script should read something like this:

import arcpy

my_workspace = arcpy.GetParameterAsText(0)
mxd_input = arcpy.GetParameterAsText(1)
shp_input = arcpy.GetParameterAsText(2)
mxd_output = arcpy.GetParameterAsText(3)

arcpy.env.workspace = my_workspace
mxd = arcpy.mapping.MapDocument(mxd_input)
df = arcpy.mapping.ListDataFrames(mxd)[0]

lyr = arcpy.management.MakeFeatureLayer(shp_input)[0]
arcpy.mapping.AddLayer(df, lyr, "AUTO_ARRANGE")

mxd.saveACopy(mxd_output)

del mxd

arcpy.AddMessage("Ended process")

You can use the layer file created by the MakeFeatureLayer directly. you are not using the workspace at all so you could also remove it from the script.

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.