I am confused as to why the script does not work. I believe something is wrong my CopyFeatures_management statement.import arcpy arcpy.env.workSpace = "C:\Working\MyData\Python.gdb" arcpy.env.overwriteOutput = 'True' mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd)[0] pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0] oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0] arcpy.AddMessage(pa.name) arcpy.AddMessage(oa.name) arcpy.CopyFeatures_management("pa", "oa")
Yet, if I write a basic script the CopyFeatures_management works. I only want to copy selected features & paste into an existing Feature Class, not create a new Feature Class.
import arcpy
try:
arcpy.env.workspace = "C:\Working\MyData\Python.gdb"
arcpy.CopyFeatures_management("HomeValue", "Test")
except:
print arcpy.GetMessages()
It doesn't work because you are telling the CopyFeatures_management to use the literal strings "pa" and "oa", which are not the names of any layers. Unquote those variables and possibly use the .name property as you did in the messages. So try either:
arcpy.CopyFeatures_management(pa, oa)
or try
arcpy.CopyFeatures_management(pa.name, oa.name)
Richard,
I have tested each of the suggestions multiple times and my results were unsuccessful.
arcpy.CopyFeatures_management(pa, oa) approach returns the Error 840: The value is not a Feature Class.
arcpy.CopyFeatures_management(pa.name, oa.name approach runs but does not work.
But, arcpy.env.workspace = "C:\Working\MyData\Python.gdb"
arcpy.CopyFeatures_management("PropertyAnno", "OwnerAnno") works. An annotation is selected and using the Select Features tool, the script runs, and copies and pastes the annotation from PropertyAnno into OwnerAnno.
The issue is with:
pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0]
oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0]
They are defined as Layers, but the output requires a Feature Class. Commenting those out, the script works outside of an edit session. But, when the script is run multiple times, the previous copy does not save. How can the copy be saved?
Mark, I am currently testing your suggestion.
Try:
arcpy.CopyFeatures_management(pa, oa.dataSource)
pa should be the layer, because it has a selection, but dataSource should get the feature class and path for the output.
Richard,
The result is the same. The script works, the selected annotation is copied from pa to oa. But, when the script is run the second time, the first copy is nullified, and the annotation(s) revert back from oa to pa. It works like editing and not saving edits. The tricky part seems to be copying & pasting into an existing Feature Class and having the Annotations saved into the Feature Class. I have also tried the Copy_management(input, output), but the statement does not like the existing oa Feature Class. Any further suggestions on how to get the copy & paste to "save"?
Mark,
Your suggestion of copying & pasting to a new Feature Class works. But, I want to use the two existing Annotation Feature Classes within ArcMap.
It sounds to me like you are expecting Copy Features to do something it won't do. It won't append features to an existing feature class, it creates a new feature class. To append to an existing feature class and keep any previously existing features you have to use Append.
If you actually do want to overwrite an existing feature class with a new feature class that uses the same name you cannot use the environment overwrite setting in a Python script to do that. That setting does not work in a Python script, it only works in Model Builder. You have to explicitly use the Delete tool to delete an existing feature class and then you can write to the same name the second time. This is how it always works. I have to run a script once without the Delete tool to just generate the outputs during the first run. Then before I can run it any more times I first have to add the Delete tool to the code and then I can run it again.
All,
I am writing a similiar script. I need the output to have parentheses added around an integer. For example, instead of 31, I need the output to be (31). Where can I locate information on how to add parentheses?
In python to make an integer variable into a string with parentheses added around it you would do something like:
parenInt = "(" + str(myInt) + ")"
where myInt is an integer value variable and parenInt is a string of the interger surrounded by parantheses.