4

I am horribly new to coding with python. I made a toolbar using the wizard and old bits of VBA code. I created a button to search by map number, a tool to calculate area from UI and a second tool that would clear the area selected.

I get the following icons when I install the add in to test.enter image description here

The following is what I have for my code:

import arcpy
import sys
import os
import pythonaddins

class ZoomToMapNumber(object):
    """Implementation for Test Addin_addin.button (Button) Everything from Import to del mxd is external code / deleted pass after 2nd def"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mapno = sys.argv[1]
        mxd = arcpy.mapping.MapDocument("Current")
        lyr = arcpy.mapping.ListLayers(mxd,"SDEOWNER.MapIndex")[0]
        arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "\"MAPNO\" = " + str(mapno))
        result = int(arcpy.GetCount_management(lyr).getOutput(0))
        arcpy.AddMessage("Number of selected records: " + str(result))

        if result > 0
            df = arcpy.mapping.ListDataFrames(mxd)[0]
            df.zoomToSelectedFeatures()
            arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
            del df
        else:
            arcpy.AddError("No record selected for map number " + str(mapno))
        del lyr
        del mxd

class CalcDrainArea(object):
    """Implementation for Test Addin_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "Line" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
        inputs = []
    def onMouseDownMap(self, x, y, button, shift): # Inputs x,y coordinate pair to inputs for every mouse click
        for coordinate_pairs in (x, y):
            inputs.append(x, y)

    def onDblClick(self, x, y, button, shift): # Inputs the final x,y pair and the first x,y pair as last four values into the 'inputs' array
        final_x = inputs[0]
        final_y = inputs[1]
        inputs.append(x, y)
        inputs.appent(final_x, final_y)
        return inputs

    def calc_drainage_area(inputs) #takes (x1, y1, x2, y2, ... xn, yn)
        cArea = 0
        # Calculate the area based on the Surveyor's formula
        for i in range(0, len(x)-1):
            cArea = cArea + x[i]*y[i+1] - x[i+1]*y[i]
        cArea = abs(cArea / 2)

class ClearArea(object):
    """Implementation for Test Addin_addin.tool_1 (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
        mxd = arcpy.mapping.MapDocument("Current")
            for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
                elm.delete()
            arcpy.RefreshActiveView()
            del mxd

The following is the xml that results

<?xml version="1.0"?>

-<ESRI.Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.esri.com/Desktop/AddIns">

<Name>Drainage Test 2</Name>

<AddInID>{51a4d912-ff75-4245-a32c-242ea4bfa5dc}</AddInID>

<Description>Drainage Test 2</Description>

<Version>0.1</Version>

<Image/>

<Author>Drainage Test 2</Author>

<Company>Drainage Test 2</Company>

<Date>07/14/2015</Date>


-<Targets>

<Target version="10.1" name="Desktop"/>

</Targets>


-<AddIn language="PYTHON" namespace="Drainage Test Addin 2_addin" library="Drainage Test Addin 2_addin.py">


-<ArcMap>


-<Commands>


-<Button tip="ZoomToMapNumber" message="ZoomToMapNumber" image="" id="Drainage Test Addin 2_addin.button" class="ZoomToMapNumber" category="Drainage Test 2" caption="ZoomToMapNumber">

<Help heading="ZoomToMapNumber">ZoomToMapNumber</Help>

</Button>


-<Tool tip="CalcDrainArea" message="CalcDrainArea" image="" id="Drainage Test Addin 2_addin.tool" class="CalcDrainArea" category="Drainage Test 2" caption="CalcDrainArea">

<Help heading="CalcDrainArea">CalcDrainArea</Help>

</Tool>


-<Tool tip="ClearArea" message="ClearArea" image="" id="Drainage Test Addin 2_addin.tool_1" class="ClearArea" category="Drainage Test 2" caption="ClearArea">

<Help heading="ClearArea">ClearArea</Help>

</Tool>

</Commands>

<Extensions> </Extensions>


-<Toolbars>


-<Toolbar id="Drainage Test Addin 2_addin.toolbar" category="Drainage Test 2" caption="Toolbar" showInitially="true">


-<Items>

<Button refID="Drainage Test Addin 2_addin.button"/>

<Tool refID="Drainage Test Addin 2_addin.tool"/>

<Tool refID="Drainage Test Addin 2_addin.tool_1"/>

</Items>

</Toolbar>

</Toolbars>

<Menus> </Menus>

</ArcMap>

</AddIn>

</ESRI.Configuration>
  • The pythonaddin wizard is notorious for this. Check your config.xml to make sure things are where they should be. – Barbarossa Jul 13 '15 at 14:26
  • 3
    It's a good idea to have the Python window open inside ArcMap as errors should be piped there. – Paul Jul 13 '15 at 17:49
  • One problem I see is that in the CalcDrainArea class you're setting the shape attribute to an arcpy.Polygon. The pythonaddins module is looking for Circle, Line, or Rectangle. Also, as @Paul suggested, having the python window open will show bugs. – crmackey Jul 14 '15 at 0:43
  • I changed that to Line. I don't get anything coming up in ArcMap Python window its like it doesn't even access the code. – user54340 Jul 14 '15 at 13:42
  • I agree with Paul, I have had success trouble shooting this by having the Python window open and then try to use your toolbar to see if there are errors. I have also had success with this 'missing' issue by just starting the addin from scratch. – Tangnar Jul 14 '15 at 15:33
2

A broken record here but, having the Python window opening when adding in the toolbar to your map will often tell you where the problem is in your code.

But I think your problem might be with the spaces that you have in the name of your tools and buttons. Additionally there is a discrepancy between the name of your button in your Python code and the resulting XML. Your Python code is listing the name of the button as

"""Implementation for Test Addin_addin.button (Button) Everything from Import to del mxd is external code / deleted pass after 2nd def"""

but the xml has it as

id="Drainage Test Addin 2_addin.button" 

So 1) I think these should be the same if you haven't altered the name in the Python code itself. 2.) Try naming the tools and buttons without spaces in the Addin Wizard. For example "Drainage_Test_Addin".

Since you are new to coding I would suggest taking the functioning VBA code you have and try implementing a single button or tool on the toolbar. Once you get the hang of that you can add more buttons. This will of course require you to start over from scratch with the Addin Wizard as others have suggested.

  • Thanks for the advice F_Kellner - I always assumed the xml was regenerated anytime you run the makeaddin but I guess not. I will try from scratch again just creating my zoom button then work from there. Coding really isn't my strong suit, I was just doing a favor for a coworker getting their VBA transferred over, now I'm in over my head it seems. – user54340 Jul 15 '15 at 11:06
  • I believe that the xml is only regenerated when the Addin Wizard is used. But if you are only altering the Python code and then running makeaddin the xml will remain the same. Which is fine so long as you aren't adding new tools or buttons to the Python code or changing the names of the buttons and tools in the Python code. – F_Kellner Jul 15 '15 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.