use a selection of features in arcmap in python script

352
2
06-17-2013 07:25 AM
KatharinaPalffy-Gelfand
New Contributor
Hi,
I am trying to write a python script and can't get over the first obstacle. I am on ArcMap 10.1.
I want for the user to select features in ArcMap and then use these selected features in a python script. Is there a way to do that?
Has somebody attempted that before and has some sample code?
The code that I tried does not use the selection, but the whole feature class instead. Please see code snippet below.

arcpy.AddMessage("Exporting selected features to working database...")
arcpy.MakeFeatureLayer_management(fc1,"fclyr")
arcpy.SetParameter(1,"fclyr")
fc2 = os.path.join(ws1,"Pipes")
arcpy.CopyFeatures_management("fclyr", fc2)

Your help would be appreciated.
Thanks!
Tags (2)
0 Kudos
2 Replies
MathewCoyle
Frequent Contributor
Make Feature Layer and most other GP tools honour selections. If you are running in the background that may cause issues.

Edit: Also, are you sure you are passing in the layer with the selection as the parameter and not the feature class the selection is based off of? Try posting your complete tool.
0 Kudos
KatharinaPalffy-Gelfand
New Contributor
Hi Mathew,
thanks for your answer. I got another answer that pushed me in the right direction. I have the user make the selection in ArcMap and input this feature class with the selected features as a feature layer in my toolbox tool. In the code, I just had to copy features and then create my feature layer to work with it. Please see the top part of my code attached.

import arcpy,os
from arcpy import env

ws = arcpy.GetParameterAsText(0)
pipes = arcpy.GetParameterAsText(1)

env.workspace = ws

pipesnew = os.path.join(ws,"Pipes")
arcpy.CopyFeatures_management(pipes,pipesnew)
arcpy.MakeFeatureLayer_management(pipesnew,"lyr")
0 Kudos