help with script please

402
3
04-06-2011 07:48 AM
FrankVignati
Occasional Contributor II
i wrote a script that exports a pdf of an mxd based on a value in a polygon dataset, the script runs fine if run from python window in arcmap with all the values for GetParameterAsText hardcoded, AND it runs fine in model builder with an iterataion on a dbf file EXCEPT for one block of code
in the attached code the section that does not run is commented out starting with ## THIS IS WHERE IT FAILS IN MODEL BUILDER and ending with ## THIS IS WHERE IT PICKS UP, by running chunks of the script in the model the failure is the arcpy.MakeFeatureLayer_management command, if run alone from the python window the command works and two new layers are added to the active data frame, but when run in the model no new layers are added ## THIS COMMAND IS IGNORED??
, does anyone no why?

the error i get is
<type 'exceptions.IndexError'>: list index out of range
Failed to execute (Export Parcel Maps).

## Import standard modules
import sys, os

## Set the product code
import arcinfo

## Import arcpy module and environment module
import arcpy
import arcpy.mapping
from arcpy import env

## Overwrite pre-existing files
arcpy.env.overwriteOutput = True

## Set the workspace
IWork = arcpy.GetParameterAsText(1)
arcpy.env.workspace = IWork
# IWork = r"G:\AGIS\PDFs\TestParcelMap"

## Sets the MXD file to the current ArcMap MXD, if run out of ArcMap use the full path to the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")
# IMXD = arcpy.mapping.MapDocument("G:\\AGIS\\PDFs\\TestParcelMap\\Parcel_Map_Test.mxd")


## Set Data Frames for the layer to select from
MDF = arcpy.mapping.ListDataFrames(IMXD, "Main Map")[0]
SDF = arcpy.mapping.ListDataFrames(IMXD, "Subinset")[0]
LDF = arcpy.mapping.ListDataFrames(IMXD, "Local Indicator")[0]

## Set the Index layers
ILyr = arcpy.mapping.ListLayers(IMXD,"PA_Index",MDF)[0]
ILyrL = arcpy.mapping.ListLayers(IMXD,"PA_Index",LDF)[0]

## Set the Sheet Number
Sheet = arcpy.GetParameterAsText(0)
# Sheet = 7429

## Create the SQL Query for the sheet number
SheetQry = r"SHEETNUM2 = '%s'"%(Sheet)

## Select the Index Sheet
arcpy.SelectLayerByAttribute_management(ILyr, "NEW_SELECTION", SheetQry)
arcpy.SelectLayerByAttribute_management(ILyrL, "NEW_SELECTION", SheetQry)

## Set the Scale
# MScale = arcpy.GetParameterAsText(2)
# MScale = int(MScale)
MScale = 200

## Set the Data Frame scales
MDF.scale = MScale * 12
SDF.scale = MScale * 38
LDF.scale = MScale * 400

## Get Extent of Selected Sheet
IExt = ILyr.getSelectedExtent()

## Pan to selected extent
MDF.panToExtent(IExt)
SDF.panToExtent(IExt)
LDF.panToExtent(IExt)


## THIS IS WHERE IT FAILS IN MODEL BUILDER
## Capture the Section, Township, and Range
SLyr = arcpy.mapping.ListLayers(IMXD,"PLSS",LDF)[0]
## THIS COMMAND IS IGNORED??
arcpy.MakeFeatureLayer_management("PA_Index","SELSHEET")
arcpy.SelectLayerByLocation_management (SLyr,"INTERSECT", "SELSHEET", -10)
## THIS COMMAND IS IGNORED??
arcpy.MakeFeatureLayer_management("PLSS","STR")
arcpy.SelectLayerByLocation_management(SLyr,"INTERSECT", "SELSHEET", -10,"REMOVE_FROM_SELECTION")
## THIS IS WHERE IT WILL ERROR OUT
# SSheet = arcpy.mapping.ListLayers(IMXD, "SELSHEET", LDF)[0]
# arcpy.mapping.RemoveLayer(LDF,SSheet)
# SSTR = arcpy.SearchCursor("STR")
# for str in SSTR:
#    TWN = str.twn
#    RNG = str.rng
# del SSTR
# SSTR = arcpy.SearchCursor("STR")
# SEC1 = SSTR.next()
# SECLIST = []
# while SEC1:
#    SECVAL =SEC1.getValue("sec")
#    if SECVAL not in SECLIST:
#        SECLIST.append(SECVAL)
#    SEC1 = SSTR.next()
# del SSTR, SEC1
# SECLIST.sort()
# SEC = r"%s"%(SECLIST)
# SEC = SEC.lstrip('[')
# SEC = SEC.rstrip(']')
## THIS IS WHERE IT PICKS UP


# SSLyr = arcpy.mapping.ListLayers(IMXD, "STR", LDF)[0]
# arcpy.mapping.RemoveLayer(LDF,SSLyr)

## Set the Sheet Number
for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
   if SheetNum.text == "SHEET   0000":
       SheetNum.text = r"SHEET   %s"%(Sheet)

## COMMENTED OUT DUE TO FAIL
## Set the Sheet Number and SEC, TWN, RNG
# for SecNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
# if SecNum.text == "SECTION:  00":
#        SecNum.text = r"SECTION:  %s"%(SEC)

# for TRNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
#    if TRNum.text == "TWP 00 S  RNG 00 E":
#        TRNum.text = r"TWP %s S  RNG %s E"%(TWN, RNG)

## Refresh data farame
arcpy.RefreshActiveView

## Set the name for the PDF
Output = r"%s\P%s.pdf"%(IWork, Sheet)

## Export the map to PDF
arcpy.mapping.ExportToPDF(IMXD, Output)

## Set the Text Elements back
for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
   if SheetNum.text == r"SHEET   %s"%(Sheet):
       SheetNum.text = "SHEET   0000"

## COMMENTED OUT DUE TO FAIL
# for SecNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
#    if SecNum.text == r"SECTION:  %s"%(SEC):
#        SecNum.text = r"SECTION:  00"

# for TRNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
#    if TRNum.text == r"TWP %s S  RNG %s E"%(TWN, RNG):
#        TRNum.text = r"TWP 00 S  RNG 00 E"
Tags (2)
0 Kudos
3 Replies
FrankVignati
Occasional Contributor II
still not working
i added arcpy.RefreshTOC() hoping that would help and put the new layers into the script properties as derived output layers parameters and the new layers still do not get added to the mxd when run from model builder but will in the python window?? i'm trying to get a new layer of selected polygons to run a search cursor on, is there another way to doe this?

where the model bails i get <type 'exceptions.IndexError'>: list index out of range
Failed to execute

## Import arcpy module and environment module
import arcpy
import arcpy.mapping
from arcpy import env

## Overwrite pre-existing files
arcpy.env.overwriteOutput = True

## Set the workspace
# IWork = r"G:\AGIS\PDFs\TestParcelMap"
IWork = arcpy.GetParameterAsText(1)
arcpy.env.workspace = IWork

## Sets the MXD file to the current ArcMap MXD, if run out of ArcMap use the full path to the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")
# IMXD = arcpy.mapping.MapDocument("G:\\AGIS\\PDFs\\TestParcelMap\\Parcel_Map_Test.mxd")


## Set Data Frames for the layer to select from
MDF = arcpy.mapping.ListDataFrames(IMXD, "Main Map")[0]
SDF = arcpy.mapping.ListDataFrames(IMXD, "Subinset")[0]
LDF = arcpy.mapping.ListDataFrames(IMXD, "Local Indicator")[0]

## Set the Index layers
ILyr = arcpy.mapping.ListLayers(IMXD,"PA_Index",MDF)[0]
ILyrL = arcpy.mapping.ListLayers(IMXD,"PA_Index",LDF)[0]

## Set the Sheet Number
Sheet = arcpy.GetParameterAsText(0)
# Sheet = 7429

## Create the SQL Query for the sheet number
SheetQry = r"SHEETNUM2 = '%s'"%(Sheet)

## Select the Index Sheet
arcpy.SelectLayerByAttribute_management(ILyr, "NEW_SELECTION", SheetQry)
arcpy.SelectLayerByAttribute_management(ILyrL, "NEW_SELECTION", SheetQry)


## Get and set the Scale
try:
   ICur = arcpy.SearchCursor(ILyrL,SheetQry)
   for IRow in ICur:
       MScale = IRow.PARCELMAP
except:
   arcpy.GetMessages()
del ICur
del IRow

MScale = int(MScale)


## Set the Data Frame scales
MDF.scale = MScale * 12
SDF.scale = MScale * 38
LDF.scale = MScale * 300

## Get Extent of Selected Sheet
IExt = ILyr.getSelectedExtent()

## Pan to selected extent
MDF.panToExtent(IExt)
SDF.panToExtent(IExt)
LDF.panToExtent(IExt)


## THIS IS THE BLOCK THAT FAILS IN MODEL BUILDER

## Capture the Section, Township, and Range
SLyr = arcpy.mapping.ListLayers(IMXD,"PLSS",LDF)[0]
arcpy.MakeFeatureLayer_management("PA_Index","SIndex")
arcpy.SelectLayerByLocation_management (SLyr,"INTERSECT", "SIndex", -100)
arcpy.MakeFeatureLayer_management("PLSS","STR")
arcpy.RefreshTOC()
arcpy.SelectLayerByLocation_management(SLyr,"INTERSECT", "SIndex", -100,"REMOVE_FROM_SELECTION")

## THIS IS THE LINE WHERE IT FAILS

SIndex = arcpy.mapping.ListLayers(IMXD, "SIndex", LDF)[0]
# arcpy.mapping.RemoveLayer(LDF,SSheet)
# SSTR = arcpy.SearchCursor("STR")
# for str in SSTR:
#    TWN = str.twn
#    RNG = str.rng
# del SSTR
# SSTR = arcpy.SearchCursor("STR")
# SEC1 = SSTR.next()
# SECLIST = []
# while SEC1:
#    SECVAL =SEC1.getValue("sec")
#    if SECVAL not in SECLIST:
#        SECLIST.append(SECVAL)
#    SEC1 = SSTR.next()
# del SSTR, SEC1
# SECLIST.sort()
# SEC = r"%s"%(SECLIST)
# SEC = SEC.lstrip('[')
# SEC = SEC.rstrip(']')
# SSLyr = arcpy.mapping.ListLayers(IMXD, "STR", LDF)[0]
# arcpy.mapping.RemoveLayer(LDF,SSLyr)

## THIS IS WHERE IT PICKS UP
0 Kudos
RDHarles
Occasional Contributor
I haven't has a chance to take a good look at the code but "list index out of range" means that you have input arguments (e.g.  arcpy.GetParameterAsText) that are NOT getting assigned.  In other words, the script is not getting the arguments it's expecting.
0 Kudos
FrankVignati
Occasional Contributor II
fixed, had to add the parameters to the script:

## Capture the Section, Township, and Range
SLyr = arcpy.mapping.ListLayers(IMXD,"PLSS",LDF)[0]
arcpy.MakeFeatureLayer_management("PA_Index","SIndex")
arcpy.SelectLayerByLocation_management (SLyr,"INTERSECT", "SIndex", -100)
arcpy.MakeFeatureLayer_management("PLSS","STR")
arcpy.RefreshTOC()

## FIXED IT HERE
SIndex = arcpy.GetParameterAsText(2)
STR = arcpy.GetParameterAsText(3)

arcpy.SelectLayerByLocation_management(SLyr,"INTERSECT", "SIndex", -100,"REMOVE_FROM_SELECTION")
SSTR = arcpy.SearchCursor("STR")
for str in SSTR:
   TWN = str.twn
   RNG = str.rng
del SSTR, str
SSTR = arcpy.SearchCursor("STR")
SEC1 = SSTR.next()
SECLIST = []
while SEC1:
   SECVAL =SEC1.getValue("sec")
   if SECVAL not in SECLIST:
       SECLIST.append(SECVAL)
   SEC1 = SSTR.next()
del SSTR, SEC1
SECLIST.sort()
SEC = r"%s"%(SECLIST)
SEC = SEC.lstrip('[')
SEC = SEC.rstrip(']')
0 Kudos