I am a beginner at Python coding and would require some help with ArcGIS Python Add ins.
I have created a TOOL which is working fine. I want to modify my code so this TOOL is enable only when a condition is met (in my case when a single point is selected from a predefined layer). The TOOL become disable when selection count <> 1
I have try a few things but I cannot find the correct place where to put my code for testing of that condition.
Here is sample on my code:
class SetDLA_TOOL(object):
"""Implementation for StructureEditing_V1_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
mapdoc = arcpy.mapping.MapDocument("CURRENT")
dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0]
primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0]
# Get a count of black selected structure
primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE"]
rowCount1 = 0
cursor1 = None
with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1:
for row1 in cursor1:
if row1[0] is None:
rowCount1 = 0
else:
rowCount1 = rowCount1 + 1
del row1, cursor1
print ("RowCount1 is " + str(rowCount1))
if rowCount1 == 1:
self.enabled = True
else:
self.enabled = False
self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
#This works when the tool loads, but the enable/disabling of the tool is not updated afterward
def onUpdate(self): #Not sure if this def is legit for a tool class?
mapdoc = arcpy.mapping.MapDocument("CURRENT")
dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0]
primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0]
# Get a count of black selected structure
primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE"]
rowCount1 = 0
cursor1 = None
with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1:
for row1 in cursor1:
if row1[0] is None:
rowCount1 = 0
else:
rowCount1 = rowCount1 + 1
del row1, cursor1
print ("RowCount1 is " + str(rowCount1))
if rowCount1 == 1:
self.enabled = True
else:
self.enabled = False
def onMouseDownMap(self, X1, Y1, button, shift):
#This is the body of the TOOL works fine but itself
# Set variables
mapdoc = arcpy.mapping.MapDocument("CURRENT")
dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0]
primSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0]
secSTR = arcpy.mapping.ListLayers(mapdoc,"Secondary Structure Symbol - Dip < 90 (BLACK)",dfSTR)[0]
# Get a count of black selected structure
primSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_X","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_Y"]
rowCount1 = 0
cursor1 = None
with arcpy.da.SearchCursor(primSTR,primSTRfields) as cursor1:
for row1 in cursor1:
if row1[0] is None:
rowCount1 = 0
else:
rowCount1 = rowCount1 + 1
if rowCount1 == 1:
DLA = row1[0]
X0 = row1[1]
Y0 = row1[2]
del row1, cursor1
secSTRfields = ["TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_DIP_LABEL_ANGLE","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_X","DMB.%ST_STRUCTURE_READING_EXTRA.MAP_GDA_Y"]
rowCount2 = 0
cursor2 = None
with arcpy.da.SearchCursor(secSTR,secSTRfields) as cursor2:
for row2 in cursor2:
if row2[0] is None:
rowCount2 = 0
else:
rowCount2 = rowCount2 + 1
if rowCount2 == 1:
DLA = row2[0]
X0 = row2[1]
Y0 = row2[2]
del row2, cursor2
rowCountTotal = rowCount1 + rowCount2
print ("RowCount1 is " + str(rowCount1))
print ("RowCount2 is " + str(rowCount2))
print ("RowCountTotal is " + str(rowCountTotal))
if rowCountTotal <> 1:
pythonaddins.MessageBox("You must have a single structure point selected to run this tool!","Warning")
return
if rowCountTotal == 1:
self.enabled
print("Current Dip label Angle is: " + str(DLA))
# Coordinates of the mouse click are: X1,Y1
# print("Your mouse click is at: " + str(X1) + " , " + str(Y1))
# pythonaddins.MessageBox("Your mouse clicked at " + str(x) + " , " + str(y),"My Coordinates:")
# Calculate the angle between the structure symbol and the mouse click
angleRADIAN = math.atan2(Y1-Y0, X1-X0)
# Convert the RADIAN angle to DEGREES angle and then to GEOGRAPHIC angle
angleDEGREES = math.degrees(angleRADIAN)
angleGEO = round((450 - angleDEGREES )% 360)
# Set the Dip label Angle to GEOGRAPHIC angle derived from the mouse click
arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.DIP_LABEL_ANGLE", expression=angleGEO, expression_type="VB", code_block="")
print("The dip label angle has been changed to: " + str(angleGEO))
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mapdoc.save()
del angleRADIAN, angleDEGREES, angleGEO
del DLA,X0,Y0
del rowCount1, rowCount2, rowCountTotal
del dfSTR, primSTR, secSTR
del mapdoc
Thnaks for your help
Gen