Select to view content in your preferred language

Getting hold of features selected in the map...

959
2
Jump to solution
07-02-2012 01:21 AM
AndrewJones
Deactivated User
Hello all...

I am coming from an ArcObjects background and seeing what I can do in Python to make my life easier!

I have been tasked with writing a tool that extracts all features in the map to a new set of shapefiles in a folder somewhere.  I thought I'd have a go in python so the users can run this from model builder but I have stumbled at the first hurdle.  There seems to be no way to get at features already selected in the map.

Can someone direct me at the best way of getting at already selected features?

Many Thanks
Andy
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MikeHunter
Frequent Contributor
Try this, which will loop through all the layers in a map, find ones that have selected features, and copy those features to a shapefile.  You will have to name them better than my example, but this should get you started.  Key thing to remember is that most Toolbox tools honor selection.

import os, sys import arcpy  def main():     mxd = arcpy.mapping.MapDocument("CURRENT")     df = mxd.activeDataFrame          counter = 1     for lyr in arcpy.mapping.ListLayers(mxd, "", df):         ext1 = lyr.getExtent()         ext2 = lyr.getSelectedExtent()         if not ext1 or ext1.equals(ext2):  #no selection             continue         outfc = ''.join(('c:/temp/shapefile', str(counter), '.shp'))         arcpy.CopyFeatures_management(lyr, outfc)         counter += 1  if __name__ == '__main__':     main()     


good luck,
Mike

View solution in original post

0 Kudos
2 Replies
MikeHunter
Frequent Contributor
Try this, which will loop through all the layers in a map, find ones that have selected features, and copy those features to a shapefile.  You will have to name them better than my example, but this should get you started.  Key thing to remember is that most Toolbox tools honor selection.

import os, sys import arcpy  def main():     mxd = arcpy.mapping.MapDocument("CURRENT")     df = mxd.activeDataFrame          counter = 1     for lyr in arcpy.mapping.ListLayers(mxd, "", df):         ext1 = lyr.getExtent()         ext2 = lyr.getSelectedExtent()         if not ext1 or ext1.equals(ext2):  #no selection             continue         outfc = ''.join(('c:/temp/shapefile', str(counter), '.shp'))         arcpy.CopyFeatures_management(lyr, outfc)         counter += 1  if __name__ == '__main__':     main()     


good luck,
Mike
0 Kudos
AndrewJones
Deactivated User
That's great.  I'll have a look and see how I go.
0 Kudos