Building Python Addin TOOLs on Rectangle

529
0
06-24-2019 06:52 PM
GenevieveSimard
New Contributor

I am a beginner with Python coding and would required assistance with building Python Addin with TOOL class and utilising

def onRectangle(self, rectangle_geometry)

I have created a toolbar with Python Addin where I have added several button and tools.

From the class SetINVISIBLE_TOOL(object):

I want to use the def onRectangle(self, rectangle_geometry):

so when the tool is used, the user must draw a rectangle to select a single point from a prescribed layer.

If the selection count == 1;

then the selected point will be set to invisible

I have tried a few things but can not find a solution.

One option the script run with no errors but nothing happen and no selection is made

Second option raise an error.

See below details of my code:

class SetINVISIBLE_TOOL(object):
    """Implementation for StructureEditing_V1_addin.tool_2 (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 3
        self.shape = "Rectangle" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.

    def onRectangle(self, rectangle_geometry):
        """Occurs when the rectangle is drawn and the mouse button is released.
        The rectangle is a extent object."""

        # Select points from the layer: Primary Structure Symbol (RED) which intersect with the extent rectangle


        mapdoc = arcpy.mapping.MapDocument("CURRENT")
        dfSTR = arcpy.mapping.ListDataFrames(mapdoc,"DEVELOPMENT (TEST) DATABASE")[0]

        userExtent = rectangle_geometry

        #ext = dfSTR.extent

        blackprimSTR = arcpy.mapping.ListLayers(mapdoc,"Primary Structure Symbol (BLACK)",dfSTR)[0]

        a = arcpy.Array()
        a.add(userExtent.lowerLeft)
        a.add(userExtent.lowerRight)
        a.add(userExtent.upperLeft)
        a.add(userExtent.upperRight)
        a.add(userExtent.lowerLeft)
        thepoly = arcpy.Polygon(a)
        thepolyLyr = arcpy.MakeFeatureLayer_management(thepoly)


        # boxAsFeature = arcpy.Polygon(arcpy.Array(rectangle_geometry))    not working
        # arcpy.SelectLayerByLocation_management(blackprimSTR, "Intersect", boxAsFeature, 0, "New_Selection")
        arcpy.SelectLayerByLocation_management(blackprimSTR,"INTERSECT",thepoly,"","NEW_SELECTION")        #Option 1 tool: run but nothing happen


        arcpy.SelectLayerByLocation_management(blackprimSTR,"INTERSECT",thepolyLyr,"","NEW_SELECTION")    #Option 2: Error is raise on this line
        arcpy.RefreshActiveView()

        # if selected point count == 1, run the rest of the script

        selCount = int(arcpy.GetCount_management(blackprimSTR)[0])

        if selCount == 1:
            # Set the the select point to be invisisble i.e. calc OUTPUT_SCALE = 10  (and SECONDARY_OUTPUT_SCALE = 10 if necessary)
            if TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_OUTPUT_SCALE == 25:
                arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.SECONDARY_OUTPUT_SCALE", expression="10", expression_type="VB", code_block="")

            arcpy.CalculateField_management(in_table="Symbols shown on map (BLACK)\Primary Structure Symbol (BLACK)", field="TIGER_PROD.ST_STRUCTURE_READING.OUTPUT_SCALE", expression="10", expression_type="VB", code_block="")

Your help is appreciated

Gen

0 Kudos
0 Replies