Select to view content in your preferred language

Script Runs in interactive window in ArcMap but not when added to a tool box

647
2
Jump to solution
05-10-2012 06:06 AM
PatrickMoulden
Deactivated User
I am new to Python (been teaching myself the language only for the past 3 days and don't really have much of a programming background) and am working on a script that automates the process of creating a grid index for generating mapbooks, something I do quite often as a final deliverable at the engineering company I work for. The script uses the current environment settings to determine the output type and grabs the intersect features from the current "Layers" dataframe; the grid size is then determined from the dataframe size and position.  The idea is to create a 'one-click' process for developing an index grid.

The problem is that the code will run fine when run in the interactive python window in ArcMap but will not run when added to a script tool. Any Ideas?

It's still a bit messy and still needs to be cleaned up a good bit but here is what I have so far:

import arcpy from arcpy import env   environments = arcpy.ListEnvironments("workspace")  for environment in environments:     # As the environment is passed as a variable, use Python's getattr function      #   to evaluate the environment's value     #     envSetting = getattr(env, environment)      # Format and print each environment and its current setting     #     print "%-28s: %s" % (environment, envSetting)  desc = arcpy.Describe (envSetting) print desc.workspaceType wrkspc = desc.workspaceType print wrkspc  mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd, "Layers") [0] fcList = arcpy.mapping.ListLayers (mxd)  if (desc.workspaceType == 'FileSystem'):     if arcpy.Exists ("gridIndexFeatures.shp"):         arcpy.Delete_management ("gridIndexFeatures.shp") else:     if arcpy.Exists ("gridIndexFeatures"):         arcpy.Delete_management ("gridIndexFeatures")  dfAsScale = df.scale dfAsRotation = df.rotation dfAsmapUnits = df.mapUnits dfAsExtent = df.extent dfXMax = dfAsExtent.XMax dfXMin = dfAsExtent.XMin dfYMax = dfAsExtent.YMax dfYMin = dfAsExtent.YMin  dfAsWidth = abs(abs(dfXMax) - abs(dfXMin)) dfAsHeight = abs(abs(dfYMin) - abs(dfYMax))  print dfAsExtent print dfXMax print dfXMin print dfYMax print dfYMin print dfAsWidth print dfAsHeight print dfAsRotation print dfAsmapUnits print fcList  # Set local variables if (desc.workspaceType == 'FileSystem'):     outFeatureClass = "gridIndexFeatures.shp"     inFeatures = fc     #inFeatures2 = "gridIndexFeatures.shp"     noIntersect = "INTERSECTFEATURE"     usePageUnit = "NO_USEPAGEUNIT"     scale = dfAsScale     polygonWidth = dfAsWidth     polygonHeight= dfAsHeight else:     outFeatureClass = "gridIndexFeatures"     inFeatures = fc     #inFeatures2 = "gridIndexFeatures"     noIntersect = "INTERSECTFEATURE"     usePageUnit = "NO_USEPAGEUNIT"     scale = dfAsScale     polygonWidth = dfAsWidth     polygonHeight= dfAsHeight      # Execute GridIndexFeatures arcpy.GridIndexFeatures_cartography(outFeatureClass, inFeatures, noIntersect, usePageUnit, scale, polygonWidth, polygonHeight)  if (desc.workspaceType == 'FileSystem'):     inFeatures = "gridIndexFeatures.shp" else:     inFeatures = "gridIndexFeatures"  fieldName1 = "Rotation" fieldPrecision = 18 fieldAlias = "Rotation" fieldName2 = "status" fieldLength = 10 expression = dfAsRotation  arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", fieldPrecision, "", "8", fieldAlias, "NULLABLE") arcpy.CalculateField_management (inFeatures,"Rotation", expression,"PYTHON") # Set dataframe rotation to rotation of grid df.rotation = expression
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RuthBowers
Deactivated User
This is too much to explain (or that I have time for right now), but don't be discouraged, you are getting there.

There are some changes you have to make to run the script from a script tool.

This link would be a good place for you to start (continue) reading.  This is the top of the Getting Started With Writing Geoprocessing Scripts section in the ArcGIS online help.  If you read down through these sections, you will be off and running.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html?TopicName=About_getting_started_with_wr...

View solution in original post

0 Kudos
2 Replies
MikeMacRae
Frequent Contributor
I'm not really seeing how your tool is set up, but I don't see any parameters set. Does your toolbox prompt the user to enter parameters? if so, they aren't being picked up in your script. Try one of the 'arcpy.GetParameter' functions.

Are you running the tool from Arcmap or catalogue? I doubt it would run from catalogue as you've set the mxd to 'current' and catalogue would probably not understand which mxd is current.

Also, one final thought would be that in the ArcMap interactive python window, it'll pick up the current workspace and mxd, but the tool may not (not sure is that is true or not, but it came to mind) so you may have to set a hard path or parameter to the workspace and map.

Good luck,
Mike
0 Kudos
RuthBowers
Deactivated User
This is too much to explain (or that I have time for right now), but don't be discouraged, you are getting there.

There are some changes you have to make to run the script from a script tool.

This link would be a good place for you to start (continue) reading.  This is the top of the Getting Started With Writing Geoprocessing Scripts section in the ArcGIS online help.  If you read down through these sections, you will be off and running.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html?TopicName=About_getting_started_with_wr...
0 Kudos