Posts Tagged ‘automation’

Adding a button that will run custom code

Problem: You have written or found a script that you would like to be able to run by pressing a button on your ArcMap toolbar

There is a world of ways to extend to the basic functionality of ArcGIS, beyond just installing plugins. People have been writing custom code for years in various languages, and making it available in places like ArcScripts and the ESRI Developer Network.

Today I will show you how to take a script from a place like that and attach it to a button in your ArcMap toolbar so it will be available whenever you run the application. I have begun learning to program ArcObjects with Visual Basic for Applications, so I chose a VBA sample as my example code. The code I found will spell check label text using the Microsoft Word spell checker. It is available in the ESRI Developer Network code exchange here.

Note: The ESRI Developer Network is moving to the ArcGIS Resource Center for versions 9.3 and above. Newer sample VBA codes are available through the resource center here: http://resources.esri.com/help/9.3/arcgisdesktop/com/vba_start.htm

Before you worry about the code, you need to create your button. You can add a new button to any toolbar by right clicking on that bar and selecting Customize. If you go to the Commands tab and scroll all the way to the bottom, you will see a [ UI Controls ] option. If you highlight it you will be able to press a button to create a new UI (User Interface) Control.

If you save the control in Normal.mxt, it will will show up in every ArcMap session from now on. You can also save it only in the current .mxd, if you prefer. Select a UIButtonConrol, and click Create. Normal.UIButtonControl1 will now show up in your Commands list box. “Normal” indicates where you are saving the command and “UIButtonControl1″ is the name of the command. If you click on it you will be able to rename it to something more descriptive. I chose Normal.SpellCheck. Now, you can highlight it and drag it to any toolbar.

If you right click on the new button while the Customize dialog is still open, you will be able to change its appearance. You can chose from a selection of images, browse to a custom image, add text with the image, or chose text only. I did text only. Your command name will be used as the button text. At this point, if you pick View Source off the same menu, the Visual Basic Editor will open. A window will appear where you can paste the code for the spell check tool. You can get the code from here:
http://edndoc.esri.com/arcobjects/9.2/CPP_VB6_VBA_VCPP_Doc/COM_Samples_Docs/ArcMap/9be43e79-834e-42a2-96c8-59b0dba3180b.htm

Copy everything between…

Public Sub SpellCheck()

and

End Sub

…but don’t copy the opening or closing line itself. Visual basic has already added those lines for you, formatted correctly to work with the button you created. For instance, mine starts as:

Private Sub SpellCheck_Click()

Now, you will notice in Step 3 of the “How to Use” instructions that the developer tells you to add a reference to the Microsoft Word Object Library. You can add references by going to the Tools menu and selecting “References..”. The following list will come up. Scroll down and check off the appropriate library.

Be sure you save Normal.mxt (File menu or disk button) before exiting the Visual Basic Editor. Now, when you’re back in ArcMap, create some text labels. Then select one of them and press your new button. If you have any misspelled words in the selected label, a spell checker will pop up that should look quite familiar! Any corrections you make will be carried over to the label.

If you would like to learn more about ArcObjects and VBA, Getting to Know ArcObjects by Robert Burke seems great so far. It’s what I’m reading now.

Tags: , , ,

Removing Empty Feature Classes with Python

The Problem: You have a geodatabase with a lot of empty feature classes

This often happens if the geodatabase has been created from a template of standard themes, but this particular version doesn’t contain information to stick into all the available slots. It can be difficult to get a handle on what’s actually there with all the empty feature classes clogging up space. It can take up extra processing time or cause errors depending on what you’re doing with the data later. And, it’s just bad organization. The best practice is to get rid of them. You can do so by moving down the feature class list in ArcCatalog, checking for empties and deleting them one by one. But with large databases (the kind that need trimming down the most) this can take a lot of time.

The slow manual method:
Right click to delete feature class

The fast automated method: use this python code that an associate shared with me!

Copy and paste the following into any text editor and save it as a python .py file.

##############################################################
##  Delete Empty Feature Classes from Multiple Geodatabases
##
##  This script loops through all geodatabases within a directory
##  and removes empty featureclasses from each geodatabase.
##
##  User must hardcode directory.  Run this script in IDLE.
##
##  Python Version 2.4
##############################################################

# Create the Geoprocessor object
import arcgisscripting, os, sys, string
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:\\Program Files\\ArcGIS\\ArcToolbox\\Toolboxes\\Conversion Tools.tbx")

# Set workspace
gp.workspace = "C:\\Documents and Settings\\user\\Desktop\\gis\\"

# list personal geodatabases in the current workspace
listmdb = gp.listworkspaces("*", "access")
mdb = listmdb.next()

# loop through the personal geodatabases
while mdb:
    print mdb
    gp.workspace = mdb
    # list feature datasets in personal geodatabase
    listfdset=gp.listdatasets()
    fdset=listfdset.next()
    if not fdset == None:
        gp.workspace = mdb + "\\" + fdset
    # list feature classes in the personal geodatabase
    fcs = gp.listfeatureclasses()
    fcs.reset()
    fc = fcs.next()

    # loop through the featureclasses
    while fc:
       # print fc

        # if the feature class table has no records, delete the featureclass
        if gp.GetCount_management(fc) == 0:
            gp.Delete_management(fc)
            print fc + "deleted from" + mdb
        fc = fcs.next()

    mdb = listmdb.next()


The only thing you will need to change is the directory under # Set workspace. Put the path to your geodatabase(s) in between the gp.workspace = “” quotes, remembering to use two slashes \\ in between folder names.

This works with personal geodatabases. If you are using file geodatabases, go to the next line under # list personal geodatabases… and replace “access” with “FileGBD” between the gp.listworkspaces(“*”, “”) quotes.

Now, start Python IDLE, which ships with the ArcGIS suite. Open up your .py file. Then, select Run –> Run Module (or hit F5). This is what you will see:
Python module in IDLE
Python results in IDLE
It may take some time to run, but that is time you can spend doing other things. When it’s done, those empty feature classes are history.

You can edit, save and rerun the .py file as many times as you need if your geodatabases are spread between different directories. (Or, for even more automation, copy them all into the same directory before you start, and let the script loop through them).

Python is an extremely handy tool. If you are interested in learning more about it, see the resources below.

Tags: , , ,

Bad Behavior has blocked 83 access attempts in the last 7 days.