8

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
  • 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
    Commented Nov 17, 2014 at 14:35

3 Answers 3

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.

2
  • 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
    Commented Nov 17, 2014 at 13:07
  • @Dan_h_b: that makes sense, see updated answer.
    – GISGe
    Commented Nov 17, 2014 at 13:17
6

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?

1
  • 4
    A bit overkill using AO. Just set arcpy.env.addOutputsToMap = False Commented Nov 18, 2014 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.