Select to view content in your preferred language

I want to use arcpy to dynamically position a graphic element on my Layout

827
4
Jump to solution
09-29-2013 05:00 PM
OLANIYANOLAKUNLE
Frequent Contributor
I want to use arcpy to dynamically position a graphic element on my layout, I have parcels with different shape and size. The position of the parcel affects my graphic element when it is static. I want to use the number of lines that form the parcels to dynamically position  the graphic element on the layout sheet i.e. if the number of lines is 4 it should be at a particular position, if it is 6 then the position should also change dynamically? Any suggestions, Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
OLANIYANOLAKUNLE
Frequent Contributor
Yeah! I got it see my code below


import arcpy import pythonaddins import os import datetime import time arcpy.env.overwriteOutput = True  class GraphicElementClass(object):     """Implementation for GraphicElement_addin.button (Button)"""     def __init__(self):         self.enabled = True         self.checked = False     def onClick(self):         mxd = arcpy.mapping.MapDocument("Current")         df = arcpy.mapping.ListDataFrames(mxd)[0]                  lyr = arcpy.mapping.ListLayers(mxd, "LineTableLayoutz")[0]         tableText = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TableText")[0]                           numRows = int(arcpy.GetCount_management(lyr).getOutput(0))         rowHeight = 0.2         fieldNames = ["Sequence", "ParcelPlot_No", "FromBeaconNo", "ToBeaconNo", "Bearing", "Length"]         numColumns = len(fieldNames)         colWidth = 1.5                   if df.scale <= 250:            upperX = 2.5            upperY = 7.0          elif df.scale <= 500:            upperX = 2.5            upperY = 7.0         elif df.scale <= 750:            upperX = 2.5            upperY = 8.0          elif df.scale <= 1000:            upperX = 2.5            upperY = 8.0          elif df.scale <= 1500:            upperX = 2.5            upperY = 8.0          elif df.scale <= 2000:            upperX = 2.5            upperY = 8.0          elif df.scale <= 2500:            upperX = 2.5            upperY = 8.0          elif df.scale <= 5000:            upperX = 2.5            upperY = 8.0          else:             if df.scale <= 10000:                upperX = 2.5                upperY = 8.0                  tableText.elementPositionX = upperX + 0.05          tableText.elementPositionY = upperY         tableText.text = fieldNames[0]         accumWidth = colWidth         for field in range(1, numColumns):           newFieldTxt = tableText.clone("_clone")           newFieldTxt.text = fieldNames[field]           newFieldTxt.elementPositionX = newFieldTxt.elementPositionX + accumWidth           accumWidth = accumWidth + colWidth                   table = arcpy.SearchCursor(lyr.dataSource)         y = upperY - rowHeight         for row in table:           x = upperX + 0.05            try:                for field in fieldNames:               newCellTxt = tableText.clone("_clone")               newCellTxt.text = row.getValue(field)               newCellTxt.elementPositionX = x               newCellTxt.elementPositionY = y               accumWidth = accumWidth + colWidth               x = x + colWidth             y = y - rowHeight           except:             print"Invalid value assignment"

View solution in original post

0 Kudos
4 Replies
markdenil
Frequent Contributor
incorporate a "position_offset" variable in the calculation of the text y dimension location.

set a y shift to use for each additional line of text (say, 4 (mm))

before positioning each text element, test the string for the number of lines it contains
and subtract one (you don't want to shift the position of just one line).

now multiply that number (one less than the number of lines) by the the y shift you want to use.
then add that product (the offset position) to (or subtract it from)
the y position you would have used.

a one line text is placed at the default position
for two lines, the anchor point is shifted one line unit (4 mm in the example)
etcetera.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
I'm trying to get my footing with my python scripts, please how would my code for your suggestion look like? If I can get a starting point, it would help me a great deal. Thank you for your wonderful suggestion.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Yeah! I got it see my code below


import arcpy import pythonaddins import os import datetime import time arcpy.env.overwriteOutput = True  class GraphicElementClass(object):     """Implementation for GraphicElement_addin.button (Button)"""     def __init__(self):         self.enabled = True         self.checked = False     def onClick(self):         mxd = arcpy.mapping.MapDocument("Current")         df = arcpy.mapping.ListDataFrames(mxd)[0]                  lyr = arcpy.mapping.ListLayers(mxd, "LineTableLayoutz")[0]         tableText = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TableText")[0]                           numRows = int(arcpy.GetCount_management(lyr).getOutput(0))         rowHeight = 0.2         fieldNames = ["Sequence", "ParcelPlot_No", "FromBeaconNo", "ToBeaconNo", "Bearing", "Length"]         numColumns = len(fieldNames)         colWidth = 1.5                   if df.scale <= 250:            upperX = 2.5            upperY = 7.0          elif df.scale <= 500:            upperX = 2.5            upperY = 7.0         elif df.scale <= 750:            upperX = 2.5            upperY = 8.0          elif df.scale <= 1000:            upperX = 2.5            upperY = 8.0          elif df.scale <= 1500:            upperX = 2.5            upperY = 8.0          elif df.scale <= 2000:            upperX = 2.5            upperY = 8.0          elif df.scale <= 2500:            upperX = 2.5            upperY = 8.0          elif df.scale <= 5000:            upperX = 2.5            upperY = 8.0          else:             if df.scale <= 10000:                upperX = 2.5                upperY = 8.0                  tableText.elementPositionX = upperX + 0.05          tableText.elementPositionY = upperY         tableText.text = fieldNames[0]         accumWidth = colWidth         for field in range(1, numColumns):           newFieldTxt = tableText.clone("_clone")           newFieldTxt.text = fieldNames[field]           newFieldTxt.elementPositionX = newFieldTxt.elementPositionX + accumWidth           accumWidth = accumWidth + colWidth                   table = arcpy.SearchCursor(lyr.dataSource)         y = upperY - rowHeight         for row in table:           x = upperX + 0.05            try:                for field in fieldNames:               newCellTxt = tableText.clone("_clone")               newCellTxt.text = row.getValue(field)               newCellTxt.elementPositionX = x               newCellTxt.elementPositionY = y               accumWidth = accumWidth + colWidth               x = x + colWidth             y = y - rowHeight           except:             print"Invalid value assignment"
0 Kudos
markdenil
Frequent Contributor
Very good.
Sorry I was away, and not monitoring this forum, but you seem to have worked it out.
0 Kudos