Python Add-In problems

3460
5
Jump to solution
03-13-2013 10:17 AM
TomGeo
by
Occasional Contributor III
Hi everybody,

I was just watching the introduction to Desktop Add-Ins using Python and was trying to create the exact same add-in shown in the video. I can't find any flaws in the code I have written and the Add-In was created and installed without any problem. The toolbar is there and the button with the icon on it as well.
When I click the button the cursor is changing to the cursor type specified in the code and I can draw a rectangle on the map canvas. However, there is nothing shown that the system is busy creating the map package and there is non produced. Needless to say that Outlook does not open a new email...

The code to define the function you can find below and it would be nice if you can tell why it does not work.

On the account of not working... There is an import of module 'pythonaddins' and I get it marked as an unresolved import (that's in Eclipse/PyDev). Also when I use the Python shell, importing that particular module results in 'ImportError: No module named pythonaddins'

Cheers Thomas

import arcpy import pythonaddins  class CreateMPKExt(object):     """Implementation for CreateMapPackage_addin.mpkext (Extension)"""     def __init__(self):         # For performance considerations, please remove all unused methods in this class.         self.enabled = True     def activeViewChanged(self):         try:             if arcpy.mapping.MapDocument('Current').activeView == 'PAGE_LAYOUT':                 mpktool.enabled = False             else:                 mpktool.enabled = True         except NameError:             pass               class CreateMPKTool(object):     """Implementation for CreateMapPackage_addin.mpktool (Tool)"""     def __init__(self):         self.enabled = True         self.shape = "Rectangle" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.         self.cursor = 3          def onRectangle(self, rectangle_geometry):         import os, win32com.client                  #Get reference to current map document and set paths to Document and output package         mxd = arcpy.mapping.MapDocument('Current')         mxdPath = mxd.filePath         print "MXD Path: {0}".format(mxdPath)         mxdName = os.path.basename(mxdPath)[0:-4]         mxdDir = os.path.dirname(mxdPath)         pkgPath = os.path.join(mxdDir, mxdName + ".mpk")         print "Package Path {0}".format(pkgPath)                  #Check to see if package already exists and delete, then create map package         if os.path.exists(pkgPath):             os.remove(pkgPath)             print "Succesfully deleted existing package: {0}".format(pkgPath)         arcpy.PackageMap_management(mxdPath, pkgPath, extent=rectangle_geometry)                  #Open new email in Outlook and attach the Map Package         outlook = win32com.client.Dispatch("Outlook.Application")         email = outlook.CreateItem(0)         email.Subject = "Map Package Area of Interest"         email.Attachments.Add(pkgPath)         email.Display()
- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
Tags (2)
1 Solution

Accepted Solutions
ChrisFox3
Occasional Contributor III
Thomas,

I posted the add-in to arcgis.com if you want to download and compare:

http://www.arcgis.com/home/item.html?id=56df83424be54b5aa96aa564b88c10ed

The code looks alright after a quick look. To debug this what is being printed to the python window? There are a lot of print statements in the code, are any of them being reached? Also win32com is not available by default with a base install of python. You need to actually go out and install it to use it. The pythonaddins module should only be used within ArcGIS Desktop in an add-in.

View solution in original post

0 Kudos
5 Replies
LucasDanzinger
Esri Frequent Contributor
Have you ran this in ArcMap instead of Eclipse and the Python Shell? Also, are you at 10.1?
0 Kudos
TomGeo
by
Occasional Contributor III
Yes, I did 'compile' it into an extension, installed it and did run it in ArcMap 10.1. As I wrote earlier, I'm getting the extension to chose, the toolbar to activate and the button of the tool to click. Also the change of cursor works and drawing the rectangle. It just looks like handing over the extend of the drawn rectangle doesn't work. The method to create the map package as such is simple and I can't see where it should go wrong there.

Thomas
- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
ChrisFox3
Occasional Contributor III
Thomas,

I posted the add-in to arcgis.com if you want to download and compare:

http://www.arcgis.com/home/item.html?id=56df83424be54b5aa96aa564b88c10ed

The code looks alright after a quick look. To debug this what is being printed to the python window? There are a lot of print statements in the code, are any of them being reached? Also win32com is not available by default with a base install of python. You need to actually go out and install it to use it. The pythonaddins module should only be used within ArcGIS Desktop in an add-in.
0 Kudos
TomGeo
by
Occasional Contributor III
Hi Chris,

thanks for the download link and pointing out to look at the Python window! I was aware that win32com is an extra module to install and so I did. What I was not aware of, was the fact that the Python interpreter used is the 32bit and I am normally using arcpy in the 64bit version.
Installing the 32bit version helped a great deal and then it was only that the mxd didn't have a description that kept me from succeeding.

Works now and I'm looking forward to some nice Python tools! 🙂

Cheers Thomas
- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
NadeemQazi
New Contributor III
Hi there
I am also trying to use Pyton-addin. I have  created very simple application to create a toolbox and button under toolbox. it works i can see the my created toolbar in Add-in Manager. however when i tried to create an extension i cannot see my created extension in Customized---> Extension.  I have followed the link given in http://resources.arcgis.com/en/help/main/10.1/index.html#//014p00000018000000
but i cannot see any extension
please adviced.
0 Kudos