2

I have 4 mxd's and I want to insert layer file to each mxd with position in the TOC. Each mxd has 3 and more data frame. I don't know why it does't work. I'm trying this code:

import arcpy,os,sys
from arcpy import env


env.workspace = r"F:\Projects\Guy\mikta3\gis"   
for mxdname in arcpy.ListFiles("*.mxd"):
    mxd = arcpy.mapping.MapDocument(r"F:\Projects\Guy\mikta3\gis\\" + mxdname)
    refLayer = arcpy.mapping.ListLayers(mxd, "roads", df)[0] # as display in TOC
    insertLayer = arcpy.mapping.Layer(r"F:\Projects\Guy\mikta3\gis\layers\residence.lyr")
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):                                              
            arcpy.mapping.InsertLayer(df, refLayer, insertLayer, "AFTER") # BEFORE\ AFTER


    mxd.save()
del mxd

3 Answers 3

4

In this line:

refLayer = arcpy.mapping.ListLayers(mxd, "roads", df)[0]

You reference df before you ever create it. If you remove it then it should return a list with just the roads layer in it.

Then the logic of the code becomes odd as you then go through ALL the dataframes attempting to insert a layer after roads. Your reflayer is coming from the first dataframe...

If every dataframe contains a road layer then it makes more sense to cycle through the dataframes getting a handle on the road layer in that dataframe and then do the insert.

0
1

this code solved my problem:

import arcpy,os,sys
import arcpy.mapping
from arcpy import env
import string


env.workspace = r"C:\Project"   
for mxdname in arcpy.ListFiles("*.mxd"):
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    insertLayer = arcpy.mapping.Layer(r"C:\Project\layers\ways.lyr")
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
    ex = 0 
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if insertLayer == lyr: 

            ex = 1   
    if ex == 0:     
        refLayer = arcpy.mapping.ListLayers(mxd, "atikot", df)[0] 
        arcpy.mapping.InsertLayer(df, refLayer, insertLayer, "AFTER")
        print mxdname
        print 'InsertLayer'               
    mxd.save()
del mxd
0

this is another code:

import arcpy,os,sys
import arcpy.mapping
from arcpy import env



env.workspace = r"F:\Projects\Guy\mikta3\gis"   
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"F:\Projects\Guy\mikta3\gis\\" + mxdname)
    insertLayer = arcpy.mapping.Layer(r"F:\Projects\Guy\mikta3\gis\layers\17.12.13\range400.lyr")
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
            if lyr.name =="SP-06-ROW.dwg Polyline":
                arcpy.mapping.InsertLayer(df, lyr, insertLayer, "AFTER") 
                print 'InsertLayer'

    mxd.save()
del mxd
1
  • If I read right, the question was: Why it doesn't work? I guess Hornbydd answers the question. You show the code that works but it is not the answer. IMO.
    – Tomek
    Commented Nov 5, 2014 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.