Using ArcPy to move a scalebar

348
5
09-17-2017 04:08 AM
SimonWebster1
New Contributor

I'm looking to shift a scale bar with Python, and as such have a small proof of concept script happening in a python console window. Using the below code, no matter the location given with elementPositionY, the scale bar does not seem to change location at all. 

  • My current mxd has a single scale bar named "ScaleBar1"
  • My data frame has a geographic coordinate system set (not sure if this matters). 
  • I'm attempting to access the scale bar this way because ultimately I'll extend the for and if statements to work with multiple bars and some logic. 
  • I'm not a python coder, so please be gentle. 

My code: 

mxd = arcpy.mapping.MapDocument(r"C:\users\I5-Desktop\Documents\untitled.mxd") #current mxd
scaleBar = arcpy.mapping.ListLayoutElements(mxd,"MAPSURROUND_ELEMENT","ScaleBar1") #enforce working on "ScaleBar1". Can use wildcards when we have more.
for item in scaleBar:
   print item.name #confirm that we are working on the only scalebar that's currently in the document
   if item.name == "ScaleBar1":
      item.elementPositionY = 1
      print item.name #reconfirm we have entered the if statement with the name

What reason might there be for the scalebar not changing location? I can resolve its name, and enter a loop based on its name, etc. 

0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

everything points to saving the mxd since you aren't working with 'CURRENT', otherwise, there arcpy.RefreshActiveView

SimonWebster1
New Contributor

Thanks Dan

Neither saving nor using arcpy.RefreshActiveView after the posted code seems to make a difference. That said, there's no errors either. RefreshActiveView returns <function RefreshActiveView at 0x255B32B0> which seems ok. 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

you need... RefreshActiveView()  # the brackets

SimonWebster1
New Contributor

Thanks Dan.

Although this didn't work for me in this instance, I need to learn where I require brackets and where I don't. Rookie error. Thanks.

As it turns out, and for whatever reason, I needed to be using CURRENT and not a reference to the mxd. The brackets were similarly important.

So, the working code is:

mxd = arcpy.mapping.MapDocument("CURRENT") #current mxd
scaleBar = arcpy.mapping.ListLayoutElements(mxd,"MAPSURROUND_ELEMENT","ScaleBar1") #enforce working on "ScaleBar1". Can use wildcards when we have more.
for item in scaleBar:
   print item.name #confirm that we are working on the only scalebar that's currently in the document
   if item.name == "ScaleBar1":
      item.elementPositionY = 1
      arcpy.RefreshActiveView()

      print item.name #reconfirm we have entered the if statement with the name

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

As suspected... glad it worked out

0 Kudos