I have a python addin, which as part of its function checks to see if a template point feature class exists on the user's machine. If it doesn't, it creates this file. This template file is used elsewhere in the addin to create other files. I do not want this template file to be added to the map document.
When the addin is run, if the template file doesn't exist it gets created and automatically added into the currently active data frame. Is there something I can add to the scrip to prevent this from happening? Or is the neatest way to let the feature class be added to the data frame and then add a line to the script to remove it from the map document?
My code is currently as follows:
coordsys = arcpy.SpatialReference(27700)
#Define the schema file for the output
schemafile = defgdb + "/Template"
#Check to see if the schema file exists, if it doesn't, create it.
if arcpy.Exists(schemafile):
pass
else:
arcpy.CreateFeatureclass_management(defgdb, "Template", "POINT", "", "", "", coordsys)
My current method for removing the layer from the map document (rather than preventing the layer from being added) is:
for layer in arcpy.mapping.ListLayers(mxd, "Template"):
arcpy.mapping.RemoveLayer(df,layer)
-
1You could tighten up the code a bit by using: if not arcpy.Exists(schemafile):... Allows you to skip the pass and else. Just an option, will work as written.recurvata– recurvata2014年11月17日 14:35:48 +00:00Commented Nov 17, 2014 at 14:35
3 Answers 3
Try unchecking the 'Add results of geoprocessing operations to the display' in the Geoprocessing Options in ArcMap.
enter image description here
You can also access this option via the addOutputsToMap property of the env class:
just add arcpy.env.addOutputsToMap = 0
in the beginning of your script.
-
Thanks for the comment. Unfortunately this won't work for me. As it is an addin I need the flexibility to be able to give it to other GI users, so I need the script to be self sufficient i.e. no user set-up should be required.Dan_h_b– Dan_h_b2014年11月17日 13:07:28 +00:00Commented Nov 17, 2014 at 13:07
-
@Dan_h_b: that makes sense, see updated answer.GISGe– GISGe2014年11月17日 13:17:34 +00:00Commented Nov 17, 2014 at 13:17
I think you can use the addOutputsToMap (=False) property from the env class in arcpy.
Further details here: http://resources.arcgis.com/en/help/main/10.2/index.html#//018z0000004s000000
Additional options are:
Do as your doing, but use conditional logic to see if layer exists or not in map (that way you will not get an error if layer does not exist):
for layer in arcpy.mapping.ListLayers(mxd, "Template"): if layer.name == "MyNewLayer": arcpy.mapping.RemoveLayer(df,layer)
Call IGeoProcessor.AddOutputsToMap Property (ArcObjects via python)
-
4A bit overkill using AO. Just set arcpy.env.addOutputsToMap = Falsecode base 5000– code base 50002014年11月18日 12:36:08 +00:00Commented Nov 18, 2014 at 12:36