Code:
# Dataframe To Feature Class tool.
# Pulls the extent of the active dataframe in an mxd, and fills out fields based on input values
# from the user.
# Author: LCpl Patrick Fischer
# Riff.Ibanezius@gmail.com
# 3D Topo Plt, 3D Intel BN, Camp Hansen Okinawa, Japan
import arcpy, os, sys
# Deletes feature class from previous time the script was ran.
arcpy.Delete_management(r"Z:\Training\Python\test_scripts\Extent_map.gdb\polygon")
# Collect Parameters from User input
mxdin = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument(mxdin)
df = arcpy.mapping.ListDataFrames(mxd, "La*") [0] # select main dataframe not location diagram
ext = df.extent
array = arcpy.Array()
array.add(ext.lowerLeft)
array.add(ext.lowerRight)
array.add(ext.upperRight)
array.add(ext.upperLeft)
array.add(ext.lowerLeft)
polygon = arcpy.Polygon(array, df.spatialReference)
arcpy.AddMessage("Extent has successfully been converted to a polygon")
#Checks spatial reference from the dataframe to see if it needs to be transformed into another projection.
if df.spatialReference == "WGS 1984":
arcpy.Project_management(polygon, r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon", "C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj")
else:
arcpy.CopyFeatures_management(polygon, r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon")
print 'Already projected into WGS 1984' #change to message box
arcpy.AddMessage("Successfuly projected into WGS 1984")
polypro = r"Z:\Training\Python\test_scripts\Extent_Map.gdb\polygon"
#add fields to new feature class
arcpy.AddField_management(polypro, "PRODUCT_ID", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "PRODUCT_DATE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "SCALE", "FLOAT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "EDITION", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "CLASSIFICATION", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "TYPE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "PRODUCER", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "COUNTRY_CODE", "TEXT", "", "", 50, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "NOTES", "TEXT", "", "", 250, "", "NULLABLE", "REQUIRED")
arcpy.AddField_management(polypro, "PDF_PATH", "TEXT", "", "", 250, "", "NULLABLE", "REQUIRED")
#Set parameters for user input
PROID = arcpy.GetParameterAsText(1)
PRODA = arcpy.GetParameterAsText(2)
ED = arcpy.GetParameterAsText(3)
CLA = arcpy.GetParameterAsText(4)
TYP = arcpy.GetParameterAsText(5)
PROD = arcpy.GetParameterAsText(6)
CC = arcpy.GetParameterAsText(7)
NOTES = arcpy.GetParameterAsText(8)
PDF = arcpy.GetParameterAsText(9)
arcpy.AddMessage("Calculating fields based on input values")
#Calculate Fields based on user input
for el in arcpy.mapping.ListLayoutElements(mxd, "DATAFRAME_ELEMENT"):
SCALE = el.scale
arcpy.CalculateField_management(polypro, "PRODUCT_ID", PROID, "", "")
arcpy.CalculateField_management(polypro, "PRODUCT_DATE", PRODA, "", "")
arcpy.CalculateField_management(polypro, "SCALE", SCALE, "", "")
arcpy.CalculateField_management(polypro, "EDITION", ED, "", "")
arcpy.CalculateField_management(polypro, "CLASSIFICATION", CLA, "", "")
arcpy.CalculateField_management(polypro, "TYPE", TYP, "", "")
arcpy.CalculateField_management(polypro, "PRODUCER", PROD, "", "")
arcpy.CalculateField_management(polypro, "COUNTRY_CODE", CC, "", "")
arcpy.CalculateField_management(polypro, "NOTES", NOTES, "", "")
arcpy.CalculateField_management(polypro, "PDF_PATH", PDF, "", "")
arcpy.AddMessage("Succesfully calculated fields, appending to Topo Products feature class.")
Feature = arcpy.GetParameterAsText(10)
arcpy.Append_management(polypro, Feature ", "NO_TEST", "", "")
Also sometimes i'll get an error about list is out of range, on one mxd it was due to what I think was to many data frames (around 50), but it happened again on another with just 2 data frames. I've ran this successfully with mxds with up to 5 data frames with no issues. Is this an anomaly or is something seriously wrong?
Bookmarks