Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

How to get layers created in a custom toolbox python script to add to the mxd

$
0
0

I’m trying to create a simple search tool so that my colleagues can easily query a database. My first version is like this.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
SearchTerm = arcpy.GetParameterAsText(0)
Fuzzy = arcpy.GetParameterAsText(1)

if Fuzzy == False:
     NewLayer = arcpy.MakeFeatureLayer_management(r"W:Source.lyr","Search Results (" + SearchTerm + ")","PROC_NAME LIKE '%" + SearchTerm + "%'")
else:
    NewLayer = arcpy.MakeFeatureLayer_management(r"W:Source.lyr","Search Results (" + SearchTerm + ")","PROC_NAME = '" + SearchTerm + "'")

This works as expected if I just run the code through the python window (with the parameters hardcoded) but if I run it as a script in toolbox nothing happens. I suppose ArcMap doesnt automatically add the layer you create in a script, so I’ve added:

arcpy.mapping.AddLayer(df,NewLayer,"TOP")

This doesn’t give add a layer to my mxd and gives me the error message: Runtime error <type 'exceptions.AssertionError'>:

Surely this should be pretty simple. Is there a better way of doing it?


Viewing all articles
Browse latest Browse all 767

Trending Articles