5

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)
  • 1
    You 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 Nov 17 '14 at 14:35
11

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 Nov 17 '14 at 13:07
  • @Dan_h_b: that makes sense, see updated answer. – GISGe Nov 17 '14 at 13:17
3

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

-1

Additional options are:

  1. 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)

  2. Call IGeoProcessor.AddOutputsToMap Property (ArcObjects via python)

    Accessing ArcObjects from Python?

  • 2
    A bit overkill using AO. Just set arcpy.env.addOutputsToMap = False – code base 5000 Nov 18 '14 at 12:36

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.