Python addin questions - edit existing addin, plus using existing code in addin.

565
2
02-03-2014 06:11 PM
AnthonyCheesman
New Contributor II
Hi - apologies for what is probably a n00b question.

I've just watched the free seminar on creating Python add-ins for 10.1, and am having a crack at building  a custom toolbar to deploy two existing (and working) Python scripts that I am using as part of a custom project.

Neither of the scripts require any user input - it should be just click and go - so they're probably not ideal for an addin, but the attraction is that I can package and deploy them to users relatively easily, plus I get to use custom graphics for the icons, rather than the standard toolbox scrool pic that gets used when you add a tool to a toolbar.

This (as far as I can see) should be as easy as defining the toolbar and tool classes (no need to define the extension, or is there?) and then pasting the code into the script.

Unfortunately, this doesn't seem to be working for me, as when I load the addin I get [missing] on the tool buttons. The seminar is a bit vague on this, but refers to it probably being a syntax error in the code. I'm pretty sure this isn't the case as both blocks of code work when run independently.

So - questions from me:

1/ pasting in existing code: I've deleted the pre-formed definitions and just left def _init_ - does the existing code get indented in line with this, or one step in?
2/ do I even need the def _init_ definition, or can I bin it?
3/ Is there a limit on the sizes of the icon graphics (16x16 pixels rings a bell)
4/ Final stupid question - how on earth do you go back and edit an existing add-in - or do you just start again from scratch?

Thanks all.

Code below:

import arcpy
import pythonaddins

class ExportGPSlog(object):
    """Implementation for AerialTrackingGPSlog_addin.gpstool (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.

    # Existing code block pasted in here

    # Export GPSlog to Dropbox
    # Written for Getac V200 Aerial Tracking Project
    # leftieant 4 Feb 2014

    # import libraries

    import arcpy
    from arcpy import env
    import socket
    import time
    from time import localtime, strftime
    import os

    # set environment parameters

    arcpy.env.workspace = r'D:\gis\aerialtracking'
    ws = arcpy.env.workspace
    computername = socket.gethostname()
    dropboxloc = "\\Dropbox\\output\\"

    # Check if gpslog exists

    print "Checking if GPSlog geodatabase exists."

    if arcpy.Exists("\\data\\GPSlog.gdb\\pointlog"):
        print "GPSlog geodatabase and pointlog GPS log both exist."
        print #blankline

    else:
        print "GPSlog geodatabase and/or pointlog GPS log do NOT exist."
        print "Exiting script."
        time.sleep(5)
        exit()

    # check if pointlog contains features or is empty

    print "Checking to see if pointlog contains any features to export."
    print #blankline

    if int(arcpy.GetCount_management("\\data\\GPSlog.gdb\\pointlog").getOutput(0)) == 0:
        print "GPS log is empty, therefore no features to export."
        print "Exiting script."
        exit()

    else:
        print "GPS log contains " + str(arcpy.GetCount_management("\\data\\GPSlog.gdb\\pointlog")) + " feature(s)."
        print "These features will now be exported to the Dropbox location."
        print #blankline

    # create datetime & PC-named stamped folder in \Dropbox\output

    print "Creating custom folder for GPS log export."
    print #blankline

    savetime = strftime("%Y%m%d_%H%M", localtime()) #this is the current time for the folder stamp
    foldername = computername + "_" + savetime + "_hr_GPSlogexport"
    exportfolder = ws + dropboxloc + foldername
    if not os.path.exists(exportfolder): os.makedirs(exportfolder)

    #export GPSlog.gdb/pointlog to exportfolder

    print "Exporting contents of GPS log to shapefile in custom folder."
    print #blankline

    try:
        arcpy.FeatureClassToShapefile_conversion("\\data\\GPSlog.gdb\\pointlog",exportfolder)
        print "Export complete."
        print #blankline

    except:
        print "Export failed."
        time.sleep(5)
        exit()

    #Clear out GPSlog

    print "Deleting all features from source GPSlog."

    try:
        arcpy.DeleteFeatures_management("\\data\\GPSlog.gdb\\pointlog")
        print "All features deleted from source log."
        print "Log is now clean and ready for more data collection."
        print #blankline
        print "Process complete."

    except:
        print "Unable to delete features from source log."
        print "Script failed."

    # delete variables

    del ws, computername, dropboxloc, savetime, foldername, exportfolder

class ExportPNGscreenshot(object):
    """Implementation for AerialTrackingPNG_addin.xstool (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.

    # existing code block pasted in here

    # export view to png file
    # leftieant 15 August 2013
    # This script exports the data view to a .png file (syncs via Dropbox account)

    import arcpy
    import os
    from arcpy import env
    from time import localtime, strftime

    # Set workspace

    arcpy.env.workspace = r'd:\gis\aerialtracking\output'
    ws = arcpy.env.workspace
    if str(os.path.exists(ws)) == "False":
        os.makedirs(ws)

    # Set time variable and print output variable

    printtime = strftime("%Y%m%d_%H%M", localtime())
    print printtime
    pngname = ws +"\\mapexport_" + printtime + "hr.png"
    print pngname

    # Set mxd

    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd)[0] #[0] pulls the first item out of the list

    #Export PNG

    arcpy.mapping.ExportToPNG(mxd, pngname, df, df_export_width = 1600, df_export_height = 1200, world_file = False)

    #delete variables

    del ws, printtime, pngname, mxd
Tags (2)
0 Kudos
2 Replies
AnthonyCheesman
New Contributor II
Lesson #1 learnt - use 'button' not 'tool'.

Still not working though...
0 Kudos
AnthonyCheesman
New Contributor II
Lesson #2 - try with something much less complicated first.

Jigged together a quick 'add layer' script which seems to be working ok - which seems to indicate issues with my coding. Will investigate further tomorrow.
0 Kudos