Solved! Go to Solution.
I think Joshua Bixby has an interesting thing there. I made some small adjustments to the python toolbox and it seems to be working now.
# Joseph Armbruster - 0Lat Inc - www.0lat.com
# Replace FULL_PATH_TO_A_FEATURE_CLASS_IN_A_GEODATABASE with a full path to a feature class
# Load pyt into arcmap 10.3.X or 10.5.X
# Run AddFeaturesToMap
# Select one feature from the target feature layer using the select tool
# Run GetCount
# Observe the returned feature count...
import arcpy, os
class Toolbox(object):
def __init__(self):
self.label = "TEST FOR ESRI"
self.alias = ""
self.tools = [
AddFeaturesToMap,
GetCount
]
class AddFeaturesToMap(object):
def __init__(self):
self.label = "For ESRI Add to Map"
self.description = ""
self.canRunInBackground = True
def getParameterInfo(self):
return
def isLicensed(self):
return True
def updateParameters(self, parameters):
return
def updateMessages(self, parameters):
return
def execute(self, parameters, messages):
arcpy.env.overwriteOutput = True
# dataset_to_process = r'FULL_PATH_TO_A_FEATURE_CLASS_IN_A_GEODATABASE'
dataset_to_process = r'C:\GeoNet\Selection\test.gdb\myLines'
mxd = arcpy.mapping.MapDocument('CURRENT')
tmp_new_layer_name = "tmp layer name"
arcpy.MakeFeatureLayer_management(dataset_to_process, tmp_new_layer_name)
new_layer = arcpy.mapping.Layer(tmp_new_layer_name)
new_layer.visible = True
new_layer.name = os.path.basename(dataset_to_process)
arcpy.mapping.AddLayer(mxd.activeDataFrame, new_layer, 'BOTTOM')
arcpy.Delete_management(tmp_new_layer_name)
del new_layer
del mxd
arcpy.RefreshTOC()
return
class GetCount(object):
def __init__(self):
self.label = "For ESRI Get Count"
self.description = ""
self.canRunInBackground = True
def getParameterInfo(self):
return [
arcpy.Parameter(
displayName="Input Geotiles or Grids",
name="input_geotiles_or_grids",
datatype="GPFeatureLayer",
parameterType="required",
direction="Input")
]
def isLicensed(self):
return True
def updateParameters(self, parameters):
return
def updateMessages(self, parameters):
return
def execute(self, parameters, messages):
input_feature_class = parameters[0].valueAsText
desc = arcpy.Describe(input_feature_class)
fid_set = desc.FIDSet
if fid_set == '':
lst_fid = []
else:
lst_fid = fid_set.split(';')
if len(lst_fid) > 0:
# messages.addMessage('selection made')
messages.addMessage('selection made: {}'.format(len(lst_fid)))
messages.addMessage(lst_fid)
else:
# messages.addMessage('no selection made')
messages.addMessage('no selection made: {}'.format(len(lst_fid)))
messages.addMessage(lst_fid)
feature_class_name = os.path.basename(input_feature_class)
feature_count = int(arcpy.GetCount_management(input_feature_class).getOutput(0))
messages.addMessage('number of features selected: %d' % (feature_count))
return
Although not documented, you can point Layer directly to a data set. Try the following for the execute block:
def execute(self, parameters, messages):
dataset_to_process = # path to feature class
mxd = arcpy.mapping.MapDocument('CURRENT')
lyr = arcpy.mapping.Layer(dataset_to_process)
arcpy.mapping.AddLayer(mxd.activeDataFrame, lyr, 'BOTTOM')
del lyr
del mxd
arcpy.RefreshTOC()
return
That's how I had it in my original script.
I just put the code provided by Joshua Bixby in the Python toolbox and it simply works. No double layers, selection returns the correct values.
So it doesn't work for you? I am running 10.6 and it works with the tests I have run.
My hands are itching to mark the Joshua's answer as the correct one, since it worked for me too (although we are both using 10.6).
go for it xander_bakker
Let's just wait a moment to see if the OP can confirm the provided solution is working for him...
Correction to my previous comment, this is Not how I had it Exactly... He removed the MakeFeatureLayer management call, which was important !