Select to view content in your preferred language

Need a simple chunk of samplcode:  insert a geodbase feature class into a couple mxds

600
2
01-08-2013 05:10 PM
KenLucas
Frequent Contributor
I've searched the arcpy mapping site package for sample code and completed the 2 available online esri course for python.  Unsuccessfully.  I simply want to insert a feature class, "test", from my PGD, "Archive", into 2 mxds, Ken1 & Ken2. All of these elements are stored in my C/temp folder.
Ken
Tags (2)
0 Kudos
2 Replies
NathanHeick
Frequent Contributor
This might work, although I don't know if there is a better or more appropriate way to do it.  It looks like the key is to make a layer file first and then add it with arcpy.mapping.  It looks like you could make a layer file using the Make Feature Layer tool and the Save Layer to File tool.  Then, you can probably use the AddLayer function like in the 10.0 sample code I found below:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "New Data Frame")[0]
addLayer = arcpy.mapping.Layer(r"C:\Project\Data\Orthophoto.lyr")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, addLayer
0 Kudos
KenLucas
Frequent Contributor
Nathan,
  Thank you so much for the help; it worked! It still required a half hour of trial and error to make your code chunk work.  I couldn't find the "Make Feature Tool" you suggested. I created a lyr file, using the "Save as Layer file" feature.  The script would not run until I specified a DIFF folder pathway  in the SaveACopy line. A diff folder than the one holding the original mxd. So one folder must contain the original mxd and the script code and another folder must be specified to hold the output mxd. Plus, I don't the command works if the layer file is named the same as a feature class in the mxd; must be attempting to add a diff named lyr file.
I really appreciate the prompt help; I really struggled with this.
Ken
0 Kudos