Select to view content in your preferred language

Change scale bar properties with Python

2958
8
09-22-2010 07:53 AM
Status: Already Offered
Labels (1)
ShannonDeArmond
Occasional Contributor

I want to be able to access and alter scale bar properties such as division values, division numbers, and units via Python.  An ESRI staff member on the ArcObjects forum says it can't be done... yet.

8 Comments
TannerSemerad1
This would help me a lot. I currently have multiple scale bars in one Data Driven Pages MXD that are positioned according to the current scale. It would be nice to just have one that changed the division values and numbers dynamically.
ShannonDeArmond
Yep. Been waiting for this for years now.
TomSchuller
I did a custom PrintSOE which has this feature:
 http://www.arcgis.com/home/item.html?id=6809086326ea4c76bf026a32bb9dd698

M
ay be this can help you until Esri has implemented this feature
JoshJones1
I need this to be possible, make map automation much cleaner and make for much better and appropriate cartography.
TimBarnes

+100 The ability to dynamically set the properties of a scale bar is paramount in managing advanced webprinting and geoprocessing. Currently we have to setup (and manage!) multiple mxds at slightly different scale settings to ensure that the scale bars and grids/graticules* are actually useful.

*the ability to set the grid/graticule division through arcpy.mapping is also sorely needed.

SolanaFoo4

Just curious what the hold up is for ESRI to implement this?  Seems like a request that has been around for awhile now.

ShannonDeArmond

I spoke to a couple of folks on the ESRI arcpy.mapping team about this at UC a couple of years ago. The party line seemed to be that ESRI didn't want to reinvent everything for python that was already available in ArcObjects. I keep hoping if we bug them enough it might happen though. I have to maintain a handful of templates with about 20 named scale bars each to get around this problem, which is kind of ridiculous if you ask me.

JeffBarrette
Status changed to: Already Offered

Hello everyone and thank you for your feedback! I missed this thread because it wasn't labelled arcpy.mp.  We exposed Python CIM Access to be able to accommodate these finer grained requests.  Please review this topic: 

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

You should be able to accomplish now using arcpy.mp and CIM access. Also in the pipeline on the Layout team (post 3.1) that you might find useful is we plan on adding scale thresholds to layout map surrounds elements so, for example, you can have multiple, stacked scalebars with different settings, scale will control visibility and if you sent the thresholds up correctly, just one scale bar would display at a time.

As for arcpy, here is a script from a downloadable sample that has 30 CIM snippets.  I modified it slightly to also update divisions and subdivisions I saw mentioned in the thread.  Here is a before and after scale bar.

Capture.PNG

 

Capture.PNG

Code sample:

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts('GreatLakes')[0]

lyt_cim = lyt.getDefinition("V2") #Get the layout's CIM definition

#Iterate though all layout elements to find the ScaleBar element
for elm in lyt_cim.elements:
    if elm.name == "Scale Bar":
        #Change the number of divisions and subdivision
        elm.divisions = 3
        elm.subdivisions = 2
        #Flip the scale bar so markers and labels appear below the line
        elm.labelPosition = "Below"
        elm.markPosition = "Below"
        #Remove the first midpoint division label by labeling only divisions
        elm.labelFrequency = "Divisions"

lyt.setDefinition(lyt_cim) #Set the layout's CIM definition

The CIM samples can be located here:

https://esriurl.com/arcpy.mp/samples

Then scroll to the one called CIM Samples