Select to view content in your preferred language

Code review please - script never ends

705
3
Jump to solution
12-19-2012 08:59 AM
JonPedder
Deactivated User
Not sure how else to say it, but after running the code below, the processing window just flashes continually and the script doesn't ever appear to end. I have to kill ArcMap process to get out of the script.

I'm posting my entire code snippet below, hopefully someone can point out my error.

Warning, new Python coder here, please don't laugh "too hard' at my brute force coding attempts 😛

# Jon Pedder # MapSAR - 12/15/12 # Make Briefing Map   import arcpy, os, glob  # Set enviroment # from arcpy import env    # Gather input parameters from user # 0. TargetFile mxd - string # 1. Folder to store pdf product - string # 2. Title of the Map - string # 3. Name of saved map - string # 4. Map Scale - string # 5. Center map on - list, string # 6. Use base data in current map - boolean  TargetFile = arcpy.GetParameterAsText(0) PDFlocation = arcpy.GetParameterAsText(1) aMapTitle = arcpy.GetParameterAsText(2) aMapName = arcpy.GetParameterAsText(3) aMapScale = arcpy.GetParameterAsText(4) aCenterMapOn = arcpy.GetParameterAsText(4) aBase_Data = arcpy.GetParameterAsText(6)  # Set Vars and overwrite option to true arcpy.env.overwriteOutput = True Targetmxd = None mxd = None  arcpy.AddMessage("Base data = " + aBase_Data)   ########################################  ###### BASE DATA COPY STARTS HERE      #  ########################################  # Gather information to copy over base_data  if aBase_Data == "true":      # Define vars    mxdlayer = "14 Base_Data_Group"   LayerFile = "C:\MapSAR\TempDir\Base_Layer"   LayerName = LayerFile + '.lyr'     # Save base_data layer file to disk in c:\MapSAR\TempDir from current mxd.   # If directory does not exist create it   if os.path.exists('c:\MapSAR\TempDir'):                arcpy.SaveToLayerFile_management(mxdlayer,LayerFile,"RELATIVE")       else:           os.makedirs('c:\MapSAR\TempDir')           arcpy.SaveToLayerFile_management(mxdlayer,LayerFile,"RELATIVE")    # Message to user           arcpy.AddMessage("Base Data Saved as "+ LayerFile)    Targetmxd = arcpy.mapping.MapDocument(TargetFile)    # Check for existing Base_Data layers in target. If present remove them   for df in arcpy.mapping.ListDataFrames(Targetmxd):          for lyr in arcpy.mapping.ListLayers(Targetmxd, "", df):              if 'Base_Data' in lyr.name:                   arcpy.mapping.RemoveLayer(df,lyr)                                      # Message to user that layers have been removed from target                   arcpy.AddMessage('removing '+ str(lyr) + ' from '+TargetFile)    # Message to user   arcpy.AddMessage('Loading Base_Data '+ LayerName +' from disk')    # Check if the layer exists on disk   if os.path.isfile(LayerName):           addLayer = arcpy.mapping.Layer(LayerName)           arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")           Targetmxd.save()   else:           # If not alert user of an error           arcpy.AddMessage(LayerName +' does not exist')           ######################################## ###### BASE DATA COPY ENDS HERE        # ########################################  ######################################## ###### EXPORT MAP STARTS HERE          # ########################################   MapTitle = aMapTitle MapName = aMapTitle + ".pdf" MapLocation = PDFlocation + "/" + aMapName + ".pdf"  arcpy.AddMessage("Generating Briefing Map " + MapName) mxd = arcpy.mapping.MapDocument(TargetFile)  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT","MapTitle"):  elm.text = "<BOL> " + MapTitle + "</BOL>"  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT","MapName"):  elm.text = MapName  fc = "PLS_Subject_Information" # iQuery = "Name =" + aCenterMapOn iQuery = ' "Name" = \'Jon Pedder\' '  df = arcpy.mapping.ListDataFrames(mxd, "MapSAR")[0] lyr = arcpy.mapping.ListLayers(mxd, "PLS_Subject_Information", df)[0]  arcpy.AddMessage("Panning to center on PLS for "+ aCenterMapOn) # Use the SelectLayerByAttribute tool to select PLS Subject and  #  zoom to the selection arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery) df.zoomToSelectedFeatures() df.scale = aMapScale  # Export the map to a .pdf arcpy.mapping.ExportToPDF(mxd,MapLocation)  ######################################## ###### EXPORT MAP ENDS HERE            # ########################################  # Clear vars del mxd, Targetmxd
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JonPedder
Deactivated User
Ok I think all this was a corrupt MXD file, interesting.

View solution in original post

0 Kudos
3 Replies
JonPedder
Deactivated User
And here's my parameters code in case the bug lies here

import arcpy
from arcpy import env

class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    
    
    subjects = []
    fc = "PLS_Subject_Information"
    field = "Name"
    cursor = arcpy.SearchCursor(fc)
    
    for i in cursor:
      sName = i.getValue(field)
      subjects.append(sName)

    self.params[6].filter.list = subjects
    self.params[6].value = subjects[0]

    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return


[ATTACH=CONFIG]20085[/ATTACH]
0 Kudos
JonPedder
Deactivated User
Update: This is happening with all my scripts so isn't code dependent. I've re installed SP1 for 10.1

Next is to see if the DB or mxd is corrupt
0 Kudos
JonPedder
Deactivated User
Ok I think all this was a corrupt MXD file, interesting.
0 Kudos