I have a folder that contain mxd's projects. Each mxd has 3 and more data frame. I want to insert layer file to a specific data frame with position in the TOC. I don't know how to do it. I'm trying this code:
import arcpy,os,sys
import arcpy.mapping
from arcpy import env
env.workspace = r"C:\Projects"
for mxdname in arcpy.ListFiles("*.mxd"):
print mxdname
mxd = arcpy.mapping.MapDocument(r"C:\Projects\\" + mxdname)
insertLayer = arcpy.mapping.Layer(r"C:\Projects\layers\ways.lyr")
dfList = arcpy.mapping.ListDataFrames(mxd, "*")
for df in dfList:
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.name =="residence":
arcpy.mapping.InsertLayer(df, lyr, insertLayer, "AFTER")
print 'InsertLayer'
mxd.save()
del mxd
Answer
That code are worked:
import arcpy,os,sys
from arcpy import env
env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
print mxdname
mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
LayerList = arcpy.mapping.ListLayers(mxd, "abc", df)
if len( LayerList) == 0:
continue
insertLayer = arcpy.mapping.Layer(r"C:\Project\layers\ways.lyr")
arcpy.mapping.InsertLayer(df, LayerList [0], insertLayer, "AFTER")
print 'InsertLayer'
mxd.save()
del mxd
No comments:
Post a Comment