Assuming that there are no curves in your line, this function accepts a featureclass containing your single polyline (you can use an in_memory feature class constructed on the fly) to rotate the data frame to show the line east/west.
Note that I am using 10.1 right now, so there is one function in this, RemoveTableview that might not work in 10.0. I could not find it in the 10.0 documentation. If so, you can take that out, but you might have to do some cleanup to remove the reference to the in memory table from your mxd.
This is not the fastest function out there, but the particular problem you have, finding the longest dimension across a polyline, is an O(n!) operation AFAIK, and so the PointDistance tool is going to be your best bet. FeatureVerticesToPoints is a pretty costly operation too, as well as freeing up the in_memory feature class at the end (but at least that is better than writing to disk).
def rotateToPolyline(fc):
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
vname = arcpy.CreateUniqueName("Vertices", "in_memory")
dname = arcpy.CreateUniqueName("Distances", "in_memory")
arcpy.FeatureVerticesToPoints_management(fc, vname)
vertices = arcpy.Describe(vname)
arcpy.PointDistance_analysis(vertices.name, vertices.name, dname)
distances = arcpy.Describe(dname)
rows = arcpy.SearchCursor(distances.name)
max = 0
from_id = -1
to_id = -1
for row in rows:
if row.getValue("DISTANCE") > max:
max = row.getValue("DISTANCE")
from_id = row.getValue("INPUT_FID")
to_id = row.getValue("NEAR_FID")
if max > 0:
oidfield = arcpy.AddFieldDelimiters(vertices.name, vertices.OIDFieldName)
pt_from = arcpy.SearchCursor(vertices.name, oidfield + " = " + str(from_id))
pt_to = arcpy.SearchCursor(vertices.name, oidfield + " = " + str(to_id))
pt_from = pt_from.next().getValue(vertices.ShapeFieldName)
pt_to = pt_to.next().getValue(vertices.ShapeFieldName)
rise = pt_to.firstPoint.Y - pt_from.firstPoint.Y
run = pt_to.firstPoint.X - pt_from.firstPoint.X
df.rotation = -math.degrees(math.atan2(rise, run))
else:
df.rotation = 0
df.zoomToSelectedFeatures()
arcpy.mapping.RemoveLayer(df, arcpy.mapping.Layer(vertices.name))
arcpy.Delete_management(vname)
#RemoveTableView might not work in 10.0
arcpy.mapping.RemoveTableView(df, arcpy.mapping.TableView(distances.name))
arcpy.Delete_management(dname)