Is it possible to copy any type of element from mxd at specific destination to current mxd ?

1756
10
Jump to solution
08-22-2016 04:15 AM
Yusuf_CelilKonak
New Contributor III

Is it possible to make script or add-in as below or is there any other way to get element to current one ? Thanks.

# to reach element @ specific mxd 

import arcpy
mxd = arcpy.mapping.MapDocument(r"D:\Reference\Reference.mxd")

element = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","Title")

# clone @ current

newElement = element.clone("2")

newElement.text= "Test !"

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

The Clone function will create the element in the same MXD. With arcpy you "cannot" add new elements (you can but you should use ArcObjects for that). What you can do is have default element outside your layout that you can clone and adapt to your needs. 

View solution in original post

10 Replies
by Anonymous User
Not applicable

Hello,

Please share more information , which tools you want create.

Thanks.

0 Kudos
Yusuf_CelilKonak
New Contributor III

Hello,

I've an reference mxd which has a lot of type of elements which are already formatted as expected. I want to copy a element (text, graphic etc.) from reference map document  to the mxd document that i'm working on. Normally, at current mxd i can clone any element at layout as much as desired as below;

import arcpy
mxd = arcpy.mapping.MapDocument("Current")

element = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","Title")

newElement = element.clone("2")

newElement.text= "Test !"

but that with that way i couldn't get element from mxd at specific folder. Maybe any other function for copy can help my situation.

Normally, elements copied to current mxd manually but if any function which can copy or clone element, we can solve the issue. I'm using script at the current mxd.

Thanks for the quick reply.

0 Kudos
XanderBakker
Esri Esteemed Contributor

You are making a small but important mistake when getting the text element. The ListLayoutElements will return a list. If you want to get the first element you need to append "[0]" to the line.

Once you have cloned the element you can move it to a new location as showed in the snippet below:

import arcpy
mxd = arcpy.mapping.MapDocument(r"D:\Reference\Reference.mxd")
element = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Title")[0]

# clone
new_element = element.clone()
new_element.text = "My New Text"
new_element.name = "TheNewElementName"

# change position of the new element
new_element.elementPositionX = new_element.elementPositionX -1
new_element.elementPositionY = new_element.elementPositionY -6

# refresh the active view
arcpy.RefreshActiveView()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
Yusuf_CelilKonak
New Contributor III

Hello Xander,

I'm normally trying as you corrected, at reference mxd if i use python window to check script and no error occurs and i can clone the element but when trying at another mxd to get element with same script gives "IndexError: list index out of range" error. What is the reason to get this error while using same function on another mxd  ?

Thanks in advance.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

If you try to get an element and get that error, then the index doesn't exist.  More importantly if you try to get the first element ie [0] then the list is empty 

>>> a = []
>>> a[0]
Traceback (most recent call last):
 File "<string>", line 1, in <module>
IndexError: list index out of range
XanderBakker
Esri Esteemed Contributor

Or in other words.. you searched for a text element with the name "Title". If there is no text element with that name, your list will be empty and your script will fail, when it tries to get the first element. You could change your code to:

import arcpy
mxd = arcpy.mapping.MapDocument(r"D:\Reference\Reference.mxd")
lst = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Title")
if len(lst) == 1:
    element = lst[0]

    # clone
    new_element = element.clone()
    new_element.text = "My New Text"
    new_element.name = "TheNewElementName"

    # change position of the new element
    new_element.elementPositionX = new_element.elementPositionX -1
    new_element.elementPositionY = new_element.elementPositionY -6

    # refresh the active view
    arcpy.RefreshActiveView()

If you have more than 1 text element with the name Title, it will not process them either. 

Yusuf_CelilKonak
New Contributor III

So what i'm getting from this issue is: listlayoutelements functions can not work for any other mxd then current. While same script works fine at reference.mxd,  fails at another map document. At reference mxd there is title element on layout but why same script can not reach this element again is the point i couldn't get. If you check issue with a simple sample, you'll find the reason i think. 

Thanks.

0 Kudos
Yusuf_CelilKonak
New Contributor III

Any idea for copy element (graphic, text etc.) from a.mxd to b.mxd while working on b.mxd would be helpful. It seems with clone function element couldn't move to other layout due to some reason. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

The Clone function will create the element in the same MXD. With arcpy you "cannot" add new elements (you can but you should use ArcObjects for that). What you can do is have default element outside your layout that you can clone and adapt to your needs.