cut & paste annotations

1787
28
09-06-2013 05:39 AM
MikeYoung5
New Contributor
Can Python be used to cut & paste Annotations?  I have read through arcpy.CopyFeatures_management and arcpy.Copy_management ESRI help docs.  I have been unable to find anything that mentions cut & paste Annotations.
Tags (2)
0 Kudos
28 Replies
RichardFairhurst
MVP Honored Contributor
The string is not working.  I suspect that is because I asked the wrong question.  I have an Annotation Feature Class that is displaying the information as integers within ArcMap, but the field type is a string.


Than the text should be calculated to a simple Python calculation of:

"(" + !AnnoText! + ")"

where you are calculating the annotation text field and that text field name is AnnoText.  Change AnnoText to match your actual annotation text field name.

The same simple calculation in VB Script is:

"(" & [AnnoText] & ")"
0 Kudos
JacobDrvar
New Contributor II
Than the text should be calculated to a simple Python calculation of:

"(" + !AnnoText! + ")"

where you are calculating the annotation text field and that text field name is AnnoText.  Change AnnoText to match your actual annotation text field name.

The same simple calculation in VB Script is:

"(" & [AnnoText] & ")"


I changed the text to match my field name.  My field name is TextString.  So, my line of script reads:

"(" + !TextString! + ")"

But, a Syntax Error is returned.  Changing the outer double quotes to single quotes eliminates the syntax error, yet does not add the parentheses.  I have a couple of Annotation Feature Classes with the field TextString.  Then, I attempted to specify which Feature Class to apply the parentheses. 

'(" + (History)!TextString! + ")'

The script does not have any errors.  Yet, does not add the parentheses.
0 Kudos
RichardFairhurst
MVP Honored Contributor
I changed the text to match my field name.  My field name is TextString.  So, my line of script reads:

"(" + !TextString! + ")"

But, a Syntax Error is returned.  Changing the outer double quotes to single quotes eliminates the syntax error, yet does not add the parentheses.  I have a couple of Annotation Feature Classes with the field TextString.  Then, I attempted to specify which Feature Class to apply the parentheses. 

'(" + (History)!TextString! + ")'

The script does not have any errors.  Yet, does not add the parentheses.


I have no idea what you think you were doing in the second calculation, but that version definitely should not work.  Aren't you just using a single feature class for the anno?  If two feature classes are involved you never mentioned that.

Screen shot the table and the field calculator dialog set up you used with my original expression.  I want to see exactly what you have and what you did.

Use the Field Calculator field list to build the expression and to insert the field name in the calculation.  Quotes should be fine, but so should single quotes (only for Python, not for VB Script).
0 Kudos
JacobDrvar
New Contributor II
I have no idea what you think you were doing in the second calculation, but that version definitely should not work.  Aren't you just using a single feature class for the anno?  If two feature classes are involved you never mentioned that.

Screen shot the table and the field calculator dialog set up you used with my original expression.  I want to see exactly what you have and what you did.

Use the Field Calculator field list to build the expression and to insert the field name in the calculation.  Quotes should be fine, but so should single quotes (only for Python, not for VB Script).


I am running a similiar script to copy & paste annotations, which is why I posted in this thread.  The copy & paste part of my script works fine.  I have two annotation Feature Classes, but only want the parentheses added to one Annotation Feature Class.  Which, is why I attempted to specify the Feature Class to add the parentheses.  I probably should have set up a conditional statement.

Your suggestion of "(" + !TextString! + ")" works using Field Calculator.  Parentheses are added to selected values within the TextString field.
0 Kudos
RichardFairhurst
MVP Honored Contributor
I am running a similiar script to copy & paste annotations, which is why I posted in this thread.  The copy & paste part of my script works fine.  I have two annotation Feature Classes, but only want the parentheses added to one Annotation Feature Class.  Which, is why I attempted to specify the Feature Class to add the parentheses.  I probably should have set up a conditional statement.

Your suggestion of "(" + !TextString! + ")" works using Field Calculator.  Parentheses are added to selected values within the TextString field.


Since your script is just a geoprocessing script, you could build the Selection and field calculation in Model Builder and export that as a Python Script.  If you had been using cursors to do this then it would be the same expression, just set to a specific field in a cursor operating on the correct layer.

A more sophisticated Field Calculator expression could add parentheses only where they did not already exist and otherwise leave the field as it is, so that even if you did not select the records in advance the output would be fine:

def AddParentheses(TextString):
  Output = TextString
  if Output[0] != "(":
    Output = "(" + Output + ")"
  return Output


Expression:  AddParentheses(!TextString!)
0 Kudos
JacobDrvar
New Contributor II
I plan to use a selection within the MXD.  So, I would only need to create the statement to add parentheses to the output.  Thanks for all of your assistance.
0 Kudos
JacobDrvar
New Contributor II
jdrvar;329266 wrote:
I changed the text to match my field name.  My field name is TextString.  So, my line of script reads:


"(" + !TextString! + ")"QUOTE]

addParentheses = "(" + !TextString! + ")" returns a Syntax Error.  I know the field name and type are correct, therefore, believed the expression to be correct.  That left the concatenation of parentheses.  But, the concatenation worked within Field Calculator.  Then, I went back reread the posts and tried adding a second variable; expression = (!TextString!).  Which also created a Syntax Error.  So, I removed the exclamation points from the addParentheses variable.  Removing the exclamation points, the script runs, and replaces existing text with parentheses.  But, I want to concatenate the existing text with parentheses.  Any suggestions?

TextString = arcpy.GetParameterAsText(0)
addParentheses = "(" + TextString + ")"

cursor = arcpy.da.UpdateCursor(History, ["TextString"])
for row in cursor:
 row[0] = addParentheses
 cursor.updateRow(row)
del row
del cursor 
0 Kudos
RichardFairhurst
MVP Honored Contributor
jdrvar;329266 wrote:
I changed the text to match my field name.  My field name is TextString.  So, my line of script reads:

"(" + !TextString! + ")"QUOTE]

addParentheses = "(" + !TextString! + ")" returns a Syntax Error.  I know the field name and type are correct, therefore, believed the expression to be correct.  That left the concatenation of parentheses.  But, the concatenation worked within Field Calculator.  Then, I went back reread the posts and tried adding a second variable; expression = (!TextString!).  Which also created a Syntax Error.  So, I removed the exclamation points from the addParentheses variable.  Removing the exclamation points, the script runs, and replaces existing text with parentheses.  But, I want to concatenate the existing text with parentheses.  Any suggestions?

TextString = arcpy.GetParameterAsText(0)
addParentheses = "(" + TextString + ")"

cursor = arcpy.da.UpdateCursor(History, ["TextString"])
for row in cursor:
 row[0] = addParentheses
 cursor.updateRow(row)
del row
del cursor 


Well this is nothing like a Field Calculation, so of course the !TextString! won't work.  I did not know you were going to this kind of script.  Just use the updatecursor itself to do this.

FieldName = arcpy.GetParameterAsText(0)
cursor = arcpy.da.UpdateCursor(History, [FieldName])
for row in cursor:
 row[0] = "(" + row[0] + ")"
 cursor.updateRow(row)
del row
del cursor


The input parameter would be any text field name.  So choose "TextString" in that parameter.  Or forget the parameter and hard code the field name.

cursor = arcpy.da.UpdateCursor(History, ["TextString"])
for row in cursor:
 row[0] = "(" + row[0] + ")"
 cursor.updateRow(row)
del row
del cursor
0 Kudos
JacobDrvar
New Contributor II
jdrvar;330609 wrote:


Well this is nothing like a Field Calculation, so of course the !TextString! won't work.  I did not know you were going to this kind of script.  Just use the updatecursor itself to do this.

FieldName = arcpy.GetParameterAsText(0)
cursor = arcpy.da.UpdateCursor(History, [FieldName])
for row in cursor:
 row[0] = "(" + row[0] + ")"
 cursor.updateRow(row)
del row
del cursor


The input parameter would be any text field name.  So choose "TextString" in that parameter.  Or forget the parameter and hard code the field name.

cursor = arcpy.da.UpdateCursor(History, ["TextString"])
for row in cursor:
 row[0] = "(" + row[0] + ")"
 cursor.updateRow(row)
del row
del cursor


I suppose I should have been more specific.  Using your suggestion works!  I just need to refresh to display the ().

arcpy.RefreshActiveView()
0 Kudos