Trying to automate some editing tasks and then exporting the files to pdf's. I'm getting stuck with loading the layers into ArcMap.
I can do it in the Python window using the standard:
arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
newlayer = arcpy.mapping.Layer(r"H:\2.shp")
arcpy.mapping.AddLayer(df, newlayer,"TOP")
but when I try to do it by referencing the mxd path instead of "CURRENT" in Pywin or a script tool (see below), it runs through but doesn't actually load the shapefile onto the map:
arcpy.mapping.MapDocument(r"H:\map2.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
newlayer = arcpy.mapping.Layer(r"H:\2.shp")
arcpy.mapping.AddLayer(df, newlayer,"TOP")
I've tried the second code in the Arc Python window, and it doesn't work there either. Any ideas? I'm sure it's a simple issue...
Thanks!
Solved! Go to Solution.
Can you try running the following and let me know if this works for you?
import arcpy mxd = arcpy.mapping.MapDocument(r"H:\map2.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.management.MakeFeatureLayer(r"H:\2.shp").getOutput(0) arcpy.mapping.AddLayer(df, lyr, "TOP")
I would say you'd want to note two things here.
Can you try running the following and let me know if this works for you?
import arcpy mxd = arcpy.mapping.MapDocument(r"H:\map2.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.management.MakeFeatureLayer(r"H:\2.shp").getOutput(0) arcpy.mapping.AddLayer(df, lyr, "TOP")
I would say you'd want to note two things here.
Thanks for the feedback, but that doesn't seem to solve the issue. It looks
like others had this issue in 10.0, but I'm using 10.3. Seems like a major
bug...
on a lark, could you place the file in a folder rather than in the root directory
Yes, I tried moving it all over the place with no change in result
Can't find arcpy.mapping.MakeFeature layer ... there is a Layer property in the arcpy.mapping classes and no reference in the arcpy.mapping functions can you provide a link or are you referring to the datamanagement tool of the same name?
I'm assuming it was the data management tool
I was just curious whether Freddie's code worked... and order for it to persist, the layer would have to be made permanent since it won't be saved otherwise
http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/make-feature-layer.htm
Sorry about that Dan. That was a typo on my part. It should say arcpy.management.MakeFeatureLayer