1

ArcToolbox geoprocessing tools (and therefore arcpy) tend to run either on an entire dataset (if there is no selection) or on a selection set (if it exists). How can I set up an arcpy script that will have some tools run on the entire dataset while others run on just the selection set?

Scenario - I have a script that does some geometric network traces based on a selection of water mains. When this is run the script modifies the Enabled flag on a number of network points to modify the trace results. I have found that I also need to modify some of the network lines, however when I run the tool that updates this flag it only runs on selected lines not on the entire dataset.

How can I make this portion of my script update lines anywhere in my network (whether selected or not) without changing the selection state of the currently selected records (needed for later in the script)?

I thought maybe I could do this with Make Feature Layer however this also uses just the currently selected features.

The only two ways I've come up with so far are:

  • to store the selection from the FIDset, clear the selection, make the modification, then re-select
  • Duplicate the layer in ArcMap and have the script reference both

Neither of these are optimal, and I think things would work better if I could just specify somehow (env setting maybe?) for arcpy to honour or ignore the selection set.

  • I'll get a snippet of code to add, although I'm not sure if makes much difference here – Midavalo Feb 29 '16 at 3:35
  • Your first method is what I would expect to work. – PolyGeo Feb 29 '16 at 3:44
2

The simplest way would be to run the tool that needs to operate on the entire dataset on the feature class, not the layer.

fc_or_layer = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(fc_or_layer)

if hasattr(desc, "layer"): # .lyr file
    fc = desc.layer.catalogPath 
else:
    fc = desc.catalogPath

arcpy.SomeTool(fc)

If "SomeTool" requires a layer not a feature class as input:

arcpy.SomeTool(arcpy.mapping.Layer(fc)) # or use arcpy.MakeFeatureLayer_management

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.