if int(arcpy.GetCount_management(layer).getOutput(0)) > 0: arcpy.DeleteFeatures_management(layer)
if int(arcpy.GetCount_management(layer).getOutput(0)) = 0: arcpy.DeleteFeatures_management(layer)
arcpy.DeleteFeatures_management(layer)
The selected features definition is part of the layer properties.
All you are doing is counting the number selected before you delete them.
Selected features from a featureclass can be defined by a definition expression and a selection set that is arbitrary
There is an arcpy.Describe() property FIDSet that will enable you to get a list of selected FID's that you can build into a set
then invert the set and make a new layer definition, but it is a bit geeky. You have to manipulate a list of FIDs and use an SQL query with a list.
# describe the feature layer to access the the selected set desc = arcpy.Describe(layer) # FIDSet will contain the selected features selectedFids = desc.FIDSet # If there are selectedFids (a selection set), write them to a new feature # class in the current workspace. if len(selectedFids) > 0: do something
Using the describe function will be something like this.# describe the feature layer to access the the selected set desc = arcpy.Describe(layer) # FIDSet will contain the selected features selectedFids = desc.FIDSet # If there are selectedFids (a selection set), write them to a new feature # class in the current workspace. if len(selectedFids) > 0: do something
Please let me know how you want to invert it now.
Thanks
oidField = desc.OIDFieldName queryList = selectedFids.replace(';', ',') oidDelim = arcpy.AddFieldDelimiters(layer, oidField) layerObject.definitionQuery = '{0} in ({1})'.format(oidDelim, queryList) arcpy.RefreshActiveView()
# describe the feature layer to access the the selected set desc = arcpy.Describe(layer) # FIDSet will contain the selected features selectedFids = desc.FIDSet # If there are selectedFids (a selection set), write them to a new feature # class in the current workspace. if len(selectedFids) > 0: # Here I create a new FC from my selected FIDS arcpy.CopyFeatures_management(layer,"AnewFC") queryList = selectedFids.replace(';', ',') oidDelim = arcpy.AddFieldDelimiters(layer, oidField) layerObject.definitionQuery = '{0} not in ({1})'.format(oidDelim, queryList) arcpy.RefreshActiveView()
You'll have to create a layer object to reference the definitionQuery parameter if you haven't already. Then something like this.oidField = desc.OIDFieldName queryList = selectedFids.replace(';', ',') oidDelim = arcpy.AddFieldDelimiters(layer, oidField) layerObject.definitionQuery = '{0} in ({1})'.format(oidDelim, queryList) arcpy.RefreshActiveView()
def button3Click(self, selection): index = self.listbox.curselection() label = self.listbox.get(index) # Local variables: layer = label # describe the feature layer to access the the selected set desc = arcpy.Describe(layer) # FIDSet will contain the selected features selectedFids = desc.FIDSet # If there are selectedFids (a selection set), write them to a new feature # class in the current workspace. if len(selectedFids) > 0: oidField = desc.OIDFieldName queryList = selectedFids.replace(';', ',') oidDelim = arcpy.AddFieldDelimiters(layer, oidField) layerObject.definitionQuery = '{0} in ({1})'.format(oidDelim, queryList) arcpy.RefreshActiveView()